Ask Your Question
0

how to reconnect master on PC

asked 2012-04-16 00:06:20 -0500

rosdai gravatar image

updated 2012-04-16 00:17:05 -0500

Hi all,

I have an application for android which sends data from a nodemain to master on PC. onCreate I used connectmaster() to run a new NodeMain called Sender. now I want to add a reconnect function, which will kill this Sender and connect it to Master again.

I tried the following code:

   private void reconnectMaster()
   {
       disconnectMaster();
       try {
           Thread.sleep(3000);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       Log.v(TAG, "shut down node sender");
       connectmaster();
   }

   private void disconnectMaster(){

       nodeMainExecutor.shutdownNodeMain(sender);

   }

   public void connectmaster(){
       try{
           String Uri = ((EditText)findViewById(R.id.hostvalue)).getText().toString();
           Integer port = Integer.parseInt(((EditText)findViewById(R.id.portvalue)).getText().toString());
           URI masterUri = URI.create("http://"+ Uri + ":" + port);
           Log.v("Sender", "setMasterURI");
           String localhost = InetAddressFactory.newNonLoopback().getHostAddress();
//           String localhost = "192.168.13.111";
           Log.v("Sender", "setlocalhost");

           nodeConfiguration = NodeConfiguration.newPublic(localhost, masterUri);
           Log.v("Sender", "nodeConfiguration");

           nodeConfiguration.setNodeName("Ant_Sender");
           Log.v("Sender", "nodeName");     

           }catch (Exception e) {
           e.printStackTrace();
           Log.e("Sender", "nodeConfiguration error");
       }

       Log.i("SDM", "create new Sender"); 
       sender = new Sender();
       nodeMainExecutor.execute(sender, nodeConfiguration);

   }

and my Sender has the following code:

public class Sender implements NodeMain {

   public Publisher<ant_msgs.sdm> publisherSDM;
   public Publisher<ant_msgs.wgt> publisherWGT;
   public Publisher<ant_msgs.hrm> publisherHRM;
   private Node node;

public Sender() {
//     TODO Auto-generated constructor stub

}

  @Override
  public GraphName getDefaultNodeName() {
    return new GraphName("Sender");
  }

  @Override
  public void onStart(Node node) {
      Log.i("Sender", "enter onStart");
      this.node = node;
      publisherSDM = node.newPublisher("ANT", ant_msgs.sdm._TYPE);

  }

  public void publishSDM(float speed, float cadence, float distance, long stride, String data)
      {
      Log.i("Sender", "publishSDM enter");

      ant_msgs.sdm msg = publisherSDM.newMessage();
      msg.speed(speed);
      msg.cadence(cadence);
      msg.distance(distance);
      msg.stride(stride);
      msg.data(data);
      publisherSDM.publish(msg);
  }

  @Override
  public void onShutdown(Node node) {
      Log.i("sender", "onShutdown");
      node.shutdown();
      node = null;
  }

  @Override
  public void onShutdownComplete(Node node) {
  }
}

but every time i click on the button reconnect, hundreds of "onShutDown" is shown in my logcat (which is in my Sender's shutdown method log.v), and then the application broke down

04-16 11:54:17.437: I/Registrar(7170): Unregistering publisher: Publisher<PublisherDefinition<PublisherIdentifier<NodeSlaveIdentifier</Ant_Sender, http://192.168.13.111:42290>, TopicIdentifier</rosout>>, Topic<TopicIdentifier</rosout>, TopicDescription<rosgraph_msgs/Log,acffd30cd6b6de30f120938c17c593fb>>>>
04-16 11:54:17.437: W/Registrar(7170): Registrar no longer running, request ignored.
04-16 11:54:17.757: I/DefaultPublisher(7170): Publisher unregistration failed: Publisher<PublisherDefinition<PublisherIdentifier<NodeSlaveIdentifier</Ant_Sender, http://192.168.13.111:42290>, TopicIdentifier</rosout>>, Topic<TopicIdentifier</rosout>, TopicDescription<rosgraph_msgs/Log,acffd30cd6b6de30f120938c17c593fb>>>>

04-16 11:53:28.430: E/dalvikvm(5334): Thread creation failed (err=No such file or directory)
04-16 11:53:28.430: D/AndroidRuntime(5334): Shutting down VM
04-16 11:53:28.430: W/dalvikvm(5334): threadid=1: thread exiting with uncaught exception (group=0x40018560)
04-16 11:53:28.437: E/AndroidRuntime(5334): FATAL EXCEPTION: main
04-16 11:53:28.437: E/AndroidRuntime(5334): java.lang.OutOfMemoryError: thread creation failed
04-16 11:53:28.437: E/AndroidRuntime(5334):     at java.lang.VMThread.create(Native Method)
04-16 11:53:28.437: E/AndroidRuntime(5334):     at java.lang.Thread ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2012-04-16 01:16:46 -0500

rosdai gravatar image

I found a solution: just delete the content in onShutDown method in Sender

I didn't really understand what onShutDown does. instead of shutting down the node, onShutDown is a user specified function that is called when the node is shut down by the system.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-04-16 00:06:20 -0500

Seen: 418 times

Last updated: Apr 16 '12