Go to the documentation of this file.00001 package ros.zeroconf.android.jmdns.demos;
00002
00003 import java.lang.Thread;
00004 import java.util.List;
00005 import javax.jmdns.ServiceInfo;
00006 import android.app.Activity;
00007 import android.os.AsyncTask;
00008 import android.os.Bundle;
00009 import android.widget.TextView;
00010 import android.text.method.ScrollingMovementMethod;
00011 import ros.zeroconf.jmdns.Zeroconf;
00012 import ros.zeroconf.android.jmdns.Logger;
00013 import org.ros.message.zeroconf_comms.DiscoveredService;
00014
00015
00016
00017
00037 public class ZeroconfActivity extends Activity {
00038
00039
00040
00041
00042 private class PublisherTask extends AsyncTask<Zeroconf, String, Void> {
00043
00044 protected Void doInBackground(Zeroconf... zeroconfs) {
00045 if ( zeroconfs.length == 1 ) {
00046 Zeroconf zconf = zeroconfs[0];
00047 String msg = "*********** Publishing a Ros Master **************\n"
00048 + " + DudeMaster [_ros-master._tcp][local][8888]\n";
00049 publishProgress(msg);
00050 zconf.addService("DudeMaster", "_ros-master._tcp", "local", 8888, "Dude's test master");
00051 } else {
00052 publishProgress("Error - PublisherTask::doInBackground received #zeroconfs != 1");
00053 }
00054 return null;
00055 }
00056
00057 protected void onProgressUpdate(String... progress) {
00058 TextView tv = (TextView) findViewById(R.id.mytextview);
00059 for (String msg : progress ) {
00060 android.util.Log.i("zeroconf", msg);
00061 tv.append(msg + "\n");
00062 }
00063 }
00064 }
00065
00066 private class DiscoveryTask extends AsyncTask<Zeroconf, String, Void> {
00067
00068 protected Void doInBackground(Zeroconf... zeroconfs) {
00069 if ( zeroconfs.length == 1 ) {
00070 Zeroconf zconf = zeroconfs[0];
00071 zconf.addListener("_ros-master._tcp","local");
00072 publishProgress("*********** Discovering Ros Masters **************");
00073 int i = 0;
00074 while( i < 20 ) {
00075 try {
00076 List<DiscoveredService> discovered_services = zconf.listDiscoveredServices();
00077 publishProgress("------------------------------------------\n");
00078 if ( discovered_services.size() > 0 ) {
00079 for ( DiscoveredService discovered_service : discovered_services ) {
00080 publishProgress(zconf.toString(discovered_service));
00081 }
00082 } else {
00083 publishProgress("...");
00084 }
00085 Thread.sleep(2000L);
00086 } catch (InterruptedException e) {
00087 e.printStackTrace();
00088 }
00089 ++i;
00090 }
00091 zconf.removeListener("_ros-master._tcp","local");
00092 publishProgress("*********** Done **************");
00093 } else {
00094 publishProgress("Error - DiscoveryTask::doInBackground received #zeroconfs != 1");
00095 }
00096 return null;
00097 }
00098
00099 protected void onProgressUpdate(String... progress) {
00100 TextView tv = (TextView) findViewById(R.id.mytextview);
00101 for (String msg : progress ) {
00102 android.util.Log.i("zeroconf", msg);
00103 tv.append(msg + "\n");
00104 }
00105 scrollToBottom();
00106 }
00107 }
00108
00109 private void scrollToBottom() {
00110 TextView tv = (TextView) findViewById(R.id.mytextview);
00111 int line_count = tv.getLineCount();
00112 int view_height = tv.getHeight();
00113 int pixels_per_line = tv.getLineHeight();
00114 int pixels_difference = line_count*pixels_per_line - view_height;
00115 if ( pixels_difference > 0 ) {
00116 tv.scrollTo(0, pixels_difference);
00117 }
00118 }
00119
00120
00121
00122 private Zeroconf zeroconf;
00123 private Logger logger;
00124
00126 @Override
00127 public void onCreate(Bundle savedInstanceState)
00128 {
00129 super.onCreate(savedInstanceState);
00130 setContentView(R.layout.main);
00131 TextView tv = (TextView)findViewById(R.id.mytextview);
00132 tv.setMovementMethod(new ScrollingMovementMethod());
00133 tv.setText("");
00134 logger = new Logger();
00135 zeroconf = new Zeroconf(logger);
00136
00137 new PublisherTask().execute(zeroconf);
00138 try {
00139 Thread.sleep(1000L);
00140 } catch (InterruptedException e) {
00141 e.printStackTrace();
00142 }
00143 new DiscoveryTask().execute(zeroconf);
00144 }
00145
00146 @Override
00147 public void onDestroy() {
00148 logger.println("*********** Zeroconf Destroy **************");
00149 zeroconf.removeAllServices();
00150 super.onDestroy();
00151 }
00152 }