DiscoveryHandler.java
Go to the documentation of this file.
00001 package ros.zeroconf.android.jmdns.master_browser;
00002 
00003 import java.util.ArrayList;
00004 
00005 import android.os.AsyncTask;
00006 import android.widget.TextView;
00007 import ros.zeroconf.jmdns.ZeroconfDiscoveryHandler;
00008 import org.ros.message.zeroconf_comms.DiscoveredService;
00009 
00019 public class DiscoveryHandler implements ZeroconfDiscoveryHandler {
00020 
00021         /*********************
00022          * Tasks
00023          ********************/
00024         private class ServiceAddedTask extends AsyncTask<DiscoveredService, String, Void> {
00025                 
00026             protected Void doInBackground(DiscoveredService... services) {
00027                 if ( services.length == 1 ) {
00028                     DiscoveredService service = services[0];
00029                                 String result = "[+] Service added: " + service.name + "." + service.type + "." + service.domain + ".";
00030                                 publishProgress(result);
00031                 } else {
00032                     publishProgress("Error - ServiceAddedTask::doInBackground received #services != 1");
00033                 }
00034                 return null;
00035             }
00036 
00037             protected void onProgressUpdate(String... progress) {
00038                 uiLog(progress);
00039                 }
00040         }
00041 
00042         private class ServiceResolvedTask extends AsyncTask<DiscoveredService, String, DiscoveredService> {
00043                 
00044             protected DiscoveredService doInBackground(DiscoveredService... services) {
00045                 if ( services.length == 1 ) {
00046                     DiscoveredService discovered_service = services[0];
00047                         String result = "[=] Service resolved: " + discovered_service.name + "." + discovered_service.type + "." + discovered_service.domain + ".\n";
00048                         result += "    Port: " + discovered_service.port;
00049                         for ( String address : discovered_service.ipv4_addresses ) {
00050                                 result += "\n    Address: " + address;
00051                         }
00052                         for ( String address : discovered_service.ipv6_addresses ) {
00053                                 result += "\n    Address: " + address;
00054                         }
00055                         publishProgress(result);
00056                         return discovered_service;
00057                 } else {
00058                     publishProgress("Error - ServiceAddedTask::doInBackground received #services != 1");
00059                 }
00060                 return null;
00061             }
00062 
00063             protected void onProgressUpdate(String... progress) {
00064                 uiLog(progress);
00065                 }
00066             
00067             protected void onPostExecute(DiscoveredService discovered_service) {
00068                 // add to the content and notify the list view if its a new service
00069                 if ( discovered_service != null ) {
00070                                 int index = 0;
00071                                 for ( DiscoveredService s : discovered_services ) {
00072                                         if ( s.name.equals(discovered_service.name) ) {
00073                                                 break;
00074                                         } else {
00075                                                 ++index;
00076                                         }
00077                                 }
00078                                 if ( index == discovered_services.size() ) {
00079                                         discovered_services.add(discovered_service);
00080                                         discovery_adapter.notifyDataSetChanged();
00081                                 } else {
00082                                         android.util.Log.i("zeroconf", "Tried to add an existing service (fix this)");
00083                                 }
00084                 }
00085             }
00086         }
00087         
00088         private class ServiceRemovedTask extends AsyncTask<DiscoveredService, String, DiscoveredService> {
00089                 
00090             protected DiscoveredService doInBackground(DiscoveredService... services) {
00091                 if ( services.length == 1 ) {
00092                     DiscoveredService discovered_service = services[0];
00093                     String result = "[-] Service removed: " + discovered_service.name + "." + discovered_service.type + "." + discovered_service.domain + ".\n";
00094                     result += "    Port: " + discovered_service.port;
00095                         for ( String address : discovered_service.ipv4_addresses ) {
00096                                 result += "\n    Address: " + address;
00097                         }
00098                         for ( String address : discovered_service.ipv6_addresses ) {
00099                                 result += "\n    Address: " + address;
00100                         }
00101                         publishProgress(result);
00102                         return discovered_service;
00103                 } else {
00104                     publishProgress("Error - ServiceAddedTask::doInBackground received #services != 1");
00105                 }
00106                 return null;
00107             }
00108 
00109             protected void onProgressUpdate(String... progress) {
00110                 uiLog(progress);
00111                 }
00112             
00113             protected void onPostExecute(DiscoveredService discovered_service) {
00114                 // remove service from storage and notify list view
00115                 if ( discovered_service != null ) {
00116                                 int index = 0;
00117                                 for ( DiscoveredService s : discovered_services ) {
00118                                         if ( s.name.equals(discovered_service.name) ) {
00119                                                 break;
00120                                         } else {
00121                                                 ++index;
00122                                         }
00123                                 }
00124                                 if ( index != discovered_services.size() ) {
00125                                         discovered_services.remove(index);
00126                                         discovery_adapter.notifyDataSetChanged();
00127                                 } else {
00128                                         android.util.Log.i("zeroconf", "Tried to remove a non-existant service");
00129                                 }
00130                 }
00131             }
00132         }
00133 
00134         /*********************
00135          * Variables
00136          ********************/
00137         private ArrayList<DiscoveredService> discovered_services;
00138     private DiscoveryAdapter discovery_adapter;
00139         private TextView text_view;
00140 
00141         /*********************
00142          * Constructors
00143          ********************/
00144         public DiscoveryHandler(TextView tv, DiscoveryAdapter discovery_adapter, ArrayList<DiscoveredService> discovered_services) {
00145                 this.text_view = tv;
00146                 this.discovery_adapter = discovery_adapter;
00147                 this.discovered_services = discovered_services;
00148         }
00149 
00150         /*********************
00151          * Callbacks
00152          ********************/
00153         public void serviceAdded(DiscoveredService service) {
00154                 new ServiceAddedTask().execute(service);
00155         }
00156         
00157         public void serviceRemoved(DiscoveredService service) {
00158                 new ServiceRemovedTask().execute(service);
00159         }
00160         
00161         public void serviceResolved(DiscoveredService service) {
00162                 new ServiceResolvedTask().execute(service);
00163         }
00164 
00165         /*********************
00166          * Utility
00167          ********************/
00168         private void uiLog(String... messages) {
00169         for (String msg : messages ) {
00170             android.util.Log.i("zeroconf", msg);
00171             text_view.append(msg + "\n");
00172         }
00173         int line_count = text_view.getLineCount(); 
00174         int view_height = text_view.getHeight();
00175         int pixels_per_line = text_view.getLineHeight();
00176         int pixels_difference = line_count*pixels_per_line - view_height;
00177         if ( pixels_difference > 0 ) {
00178                 text_view.scrollTo(0, pixels_difference);
00179         }
00180         }
00181 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations


zeroconf_android_master_browser
Author(s): Daniel Stonier
autogenerated on Fri Apr 26 2013 20:45:18