ConcertChecker.java
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Willow Garage, Inc.
00005  * Copyright (c) 2013, OSRF.
00006  * Copyright (c) 2013, Yujin Robot.
00007  *
00008  * All rights reserved.
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  *
00013  *  * Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  *  * Redistributions in binary form must reproduce the above
00016  *    copyright notice, this list of conditions and the following
00017  *    disclaimer in the documentation and/or other materials provided
00018  *    with the distribution.
00019  *  * Neither the name of Willow Garage, Inc. nor the names of its
00020  *    contributors may be used to endorse or promote products derived
00021  *    from this software without specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  * POSSIBILITY OF SUCH DAMAGE.
00035  */
00036 
00037 package com.github.rosjava.android_remocons.common_tools;
00038 
00039 import android.util.Log;
00040 
00041 import com.github.rosjava.android_apps.application_management.ConcertDescription;
00042 import com.github.rosjava.android_apps.application_management.MasterId;
00043 
00044 import org.ros.address.InetAddressFactory;
00045 import org.ros.android.NodeMainExecutorService;
00046 import org.ros.internal.node.client.ParameterClient;
00047 import org.ros.internal.node.server.NodeIdentifier;
00048 import org.ros.namespace.GraphName;
00049 import org.ros.node.NodeConfiguration;
00050 
00051 import java.net.URI;
00052 import java.net.URISyntaxException;
00053 import java.util.Date;
00054 
00055 import static com.github.rosjava.android_remocons.common_tools.RoconConstants.*;
00056 
00066 public class ConcertChecker {
00067     public interface ConcertDescriptionReceiver {
00071         void receive(ConcertDescription concertDescription);
00072     }
00073 
00074     public interface FailureHandler {
00079         void handleFailure(String reason);
00080     }
00081 
00082     private CheckerThread checkerThread;
00083     private ConcertDescriptionReceiver foundConcertCallback;
00084     private FailureHandler failureCallback;
00085 
00089     public ConcertChecker(ConcertDescriptionReceiver foundConcertCallback, FailureHandler failureCallback) {
00090         this.foundConcertCallback = foundConcertCallback;
00091         this.failureCallback = failureCallback;
00092     }
00093 
00098     public void beginChecking(MasterId masterId) {
00099         stopChecking();
00100         if (masterId.getMasterUri() == null) {
00101             failureCallback.handleFailure("empty concert URI");
00102             return;
00103         }
00104         URI uri;
00105         try {
00106             uri = new URI(masterId.getMasterUri());
00107         } catch (URISyntaxException e) {
00108             failureCallback.handleFailure("invalid concert URI");
00109             return;
00110         }
00111         checkerThread = new CheckerThread(masterId, uri);
00112         checkerThread.start();
00113     }
00114 
00118     public void stopChecking() {
00119         if (checkerThread != null && checkerThread.isAlive()) {
00120             checkerThread.interrupt();
00121         }
00122     }
00123 
00124     private class CheckerThread extends Thread {
00125         private URI concertUri;
00126         private MasterId masterId;
00127 
00128         public CheckerThread(MasterId masterId, URI concertUri) {
00129             this.concertUri = concertUri;
00130             this.masterId = masterId;
00131             setDaemon(true);
00132             // don't require callers to explicitly kill all the old checker threads.
00133             setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
00134                 @Override
00135                 public void uncaughtException(Thread thread, Throwable ex) {
00136                     failureCallback.handleFailure("exception: " + ex.getMessage());
00137                 }
00138             });
00139         }
00140 
00141         @Override
00142         public void run() {
00143             try {
00144                 // Check if the concert exists by looking for concert name parameter
00145                 // getParam throws when it can't find the parameter.
00146                 ParameterClient paramClient = new ParameterClient(
00147                         NodeIdentifier.forNameAndUri("/concert_checker", concertUri.toString()), concertUri);
00148                 String name = (String) paramClient.getParam(GraphName.of(CONCERT_NAME_PARAM)).getResult();
00149                 Log.i("ConcertRemocon", "Concert " + name + " found; retrieve additional information");
00150 
00151                 NodeMainExecutorService nodeMainExecutorService = new NodeMainExecutorService();
00152                 NodeConfiguration nodeConfiguration = NodeConfiguration.newPublic(
00153                         InetAddressFactory.newNonLoopback().getHostAddress(), concertUri);
00154 
00155                 // Check for the concert information topic
00156                 ListenerNode<concert_msgs.ConcertInfo> readInfoTopic =
00157                         new ListenerNode(CONCERT_INFO_TOPIC, concert_msgs.ConcertInfo._TYPE);
00158                 nodeMainExecutorService.execute(readInfoTopic, nodeConfiguration.setNodeName("read_info_node"));
00159                 readInfoTopic.waitForResponse();
00160 
00161 
00162                 concert_msgs.ConcertInfo concertInfo = readInfoTopic.getLastMessage();
00163 
00164                 String              concertName = concertInfo.getName();
00165                 String              concertDesc = concertInfo.getDescription();
00166                 rocon_std_msgs.Icon concertIcon = concertInfo.getIcon();
00167 
00168                 if (name.equals(concertName) == false)
00169                     Log.w("ConcertRemocon", "Concert names from parameter and topic differs; we use the later");
00170 
00171                 // Check for the concert roles topic
00172                 ListenerNode<concert_msgs.Roles> readRolesTopic =
00173                         new ListenerNode(CONCERT_ROLES_TOPIC, concert_msgs.Roles._TYPE);
00174                 nodeMainExecutorService.execute(readRolesTopic, nodeConfiguration.setNodeName("concert_roles_node"));
00175                 readRolesTopic.waitForResponse();
00176 
00177                 nodeMainExecutorService.shutdownNodeMain(readInfoTopic);
00178                 nodeMainExecutorService.shutdownNodeMain(readRolesTopic);
00179 
00180                 // configure concert description
00181                 Date timeLastSeen = new Date();
00182                 ConcertDescription description = new ConcertDescription(masterId, concertName, concertDesc, concertIcon, timeLastSeen);
00183                 Log.i("ConcertRemocon", "Concert is available");
00184                 description.setConnectionStatus(ConcertDescription.OK);
00185                 description.setUserRoles(readRolesTopic.getLastMessage());
00186                 foundConcertCallback.receive(description);
00187                 return;
00188             } catch (RuntimeException e) {
00189                 // thrown if concert could not be found in the getParam call (from java.net.ConnectException)
00190                 Log.w("ConcertRemocon", "could not find concert [" + concertUri + "][" + e.toString() + "]");
00191                 failureCallback.handleFailure(e.toString());
00192             } catch (Throwable e) {
00193                 Log.w("ConcertRemocon", "exception while creating node in concert checker for URI " + concertUri, e);
00194                 failureCallback.handleFailure(e.toString());
00195             }
00196         }
00197     }
00198 }


android_remocons
Author(s): Daniel Stonier, Kazuto Murase
autogenerated on Sat Jun 8 2019 19:32:24