DiscoveryHandler.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Yujin Robot.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 package com.github.rosjava.android_extras.ros_master_browser;
00017 
00018 import java.net.Inet4Address;
00019 import java.net.Inet6Address;
00020 import java.util.ArrayList;
00021 import java.util.Arrays;
00022 import java.util.Iterator;
00023 
00024 import android.os.AsyncTask;
00025 import android.widget.TextView;
00026 import com.github.rosjava.zeroconf_jmdns_suite.jmdns.DiscoveredService;
00027 import com.github.rosjava.zeroconf_jmdns_suite.jmdns.ZeroconfDiscoveryHandler;
00028 
00038 public class DiscoveryHandler implements ZeroconfDiscoveryHandler {
00039 
00040         /*********************
00041          * Tasks
00042          ********************/
00043         private class ServiceAddedTask extends AsyncTask<DiscoveredService, String, Void> {
00044                 
00045             protected Void doInBackground(DiscoveredService... services) {
00046             android.util.Log.i("zeroconf", "Service added ");
00047                 if ( services.length == 1 ) {
00048                         DiscoveredService service = services[0];
00049                                 String result = "[+] Service added: " + service.name + "." + service.type + "." + service.domain + ".";
00050                                 publishProgress(result);
00051                 } else {
00052                     publishProgress("Error - ServiceAddedTask::doInBackground received #services != 1");
00053                 }
00054                 return null;
00055             }
00056 
00057             protected void onProgressUpdate(String... progress) {
00058                 uiLog(progress);
00059                 }
00060         }
00061 
00062         private class ServiceResolvedTask extends AsyncTask<DiscoveredService, String, DiscoveredService> {
00063                 
00064             protected DiscoveredService doInBackground(DiscoveredService... services) {
00065             android.util.Log.i("zeroconf", " Resolved");
00066                 if ( services.length == 1 ) {
00067                         DiscoveredService discovered_service = services[0];
00068                         String result = "[=] Service resolved: " + discovered_service.name + "." + discovered_service.type + "." + discovered_service.domain + ".\n";
00069                         result += "    Port: " + discovered_service.port;
00070                         for ( String address : discovered_service.ipv4_addresses ) {
00071                                 result += "\n    Address: " + address;
00072                         }
00073                         for ( String address : discovered_service.ipv6_addresses ) {
00074                                 result += "\n    Address: " + address;
00075                         }
00076                         publishProgress(result);
00077                         return discovered_service;
00078                 } else {
00079                     publishProgress("Error - ServiceAddedTask::doInBackground received #services != 1");
00080                 }
00081                 return null;
00082             }
00083 
00084             protected void onProgressUpdate(String... progress) {
00085                 uiLog(progress);
00086                 }
00087             
00088             protected void onPostExecute(DiscoveredService discovered_service) {
00089                 // add to the content and notify the list view if its a new service
00090                 // this is a bit horrible - ServiceInfo/DiscoveredService objects can't add 
00091                 // extra addresses, so we just discard one if it doesn't have a complete list of ip's
00092                 // (quite often we get just an ipv4 address come in, or just an ipv6 address
00093                 // but also a service info with both).
00094                 if ( discovered_service != null ) {
00095                                 Iterator<DiscoveredService> it = discovered_services.iterator();
00096                                 Boolean exists = false;
00097                                 Boolean modified_address_list = false;
00098                                 while ( it.hasNext() )
00099                                 {
00100                                         DiscoveredService s = it.next();
00101                                         if ( s.name.equals(discovered_service.name) ) {
00102                                         for ( String address : discovered_service.ipv4_addresses ) {
00103                                                 if ( !s.ipv4_addresses.contains(address) ) {
00104                                 s.ipv4_addresses.add(address);
00105                                 modified_address_list = true;
00106                                                         break;
00107                                                 }
00108                                         }
00109                                         for ( String address : discovered_service.ipv6_addresses ) {
00110                                                 if ( !s.ipv6_addresses.contains(address) ) {
00111                                 s.ipv6_addresses.add(address);
00112                                                         modified_address_list = true;
00113                                                         break;
00114                                                 }
00115                                         }
00116                                         exists = true;
00117                                                 break;
00118                                         }
00119                                 }
00120                                 if ( exists ) {
00121                                         if ( modified_address_list ) {
00122                         discovery_adapter.notifyDataSetChanged();
00123                                         }
00124                                 } else {
00125                                         discovered_services.add(discovered_service);
00126                                         discovery_adapter.notifyDataSetChanged();
00127                                 }
00128                 }
00129             }
00130         }
00131         
00132         private class ServiceRemovedTask extends AsyncTask<DiscoveredService, String, DiscoveredService> {
00133                 
00134             protected DiscoveredService doInBackground(DiscoveredService... services) {
00135                 if ( services.length == 1 ) {
00136                         DiscoveredService discovered_service = services[0];
00137                     String result = "[-] Service removed: " + discovered_service.name + "." + discovered_service.type + "." + discovered_service.domain + ".";
00138                     // Address and port information is not usually transferred here, you'd have
00139                     // to look it up on stored objects here in this program if you wanted to work out
00140                     // which was removed.
00141                         publishProgress(result);
00142                         return discovered_service;
00143                 } else {
00144                     publishProgress("Error - ServiceAddedTask::doInBackground received #services != 1");
00145                 }
00146                 return null;
00147             }
00148 
00149             protected void onProgressUpdate(String... progress) {
00150                 uiLog(progress);
00151                 }
00152             
00153             protected void onPostExecute(DiscoveredService discovered_service) {
00154                 // remove service from storage and notify list view
00155                 if ( discovered_service != null ) {
00156                                 int index = 0;
00157                                 for ( DiscoveredService s : discovered_services ) {
00158                                         if ( s.name.equals(discovered_service.name) ) {
00159                                                 break;
00160                                         } else {
00161                                                 ++index;
00162                                         }
00163                                 }
00164                                 if ( index != discovered_services.size() ) {
00165                                         discovered_services.remove(index);
00166                                         discovery_adapter.notifyDataSetChanged();
00167                                 } else {
00168                                         android.util.Log.i("zeroconf", "Tried to remove a non-existant service");
00169                                 }
00170                 }
00171             }
00172         }
00173 
00174         /*********************
00175          * Variables
00176          ********************/
00177         private ArrayList<DiscoveredService> discovered_services;
00178     private DiscoveryAdapter discovery_adapter;
00179         private TextView text_view;
00180 
00181         /*********************
00182          * Constructors
00183          ********************/
00184         public DiscoveryHandler(TextView tv, DiscoveryAdapter discovery_adapter, ArrayList<DiscoveredService> discovered_services) {
00185                 this.text_view = tv;
00186                 this.discovery_adapter = discovery_adapter;
00187                 this.discovered_services = discovered_services;
00188         }
00189 
00190         /*********************
00191          * Callbacks
00192          ********************/
00193         public void serviceAdded(DiscoveredService service) {
00194                 new ServiceAddedTask().execute(service);
00195         }
00196         
00197         public void serviceRemoved(DiscoveredService service) {
00198                 new ServiceRemovedTask().execute(service);
00199         }
00200         
00201         public void serviceResolved(DiscoveredService service) {
00202                 new ServiceResolvedTask().execute(service);
00203         }
00204 
00205         /*********************
00206          * Utility
00207          ********************/
00208         private void uiLog(String... messages) {
00209         for (String msg : messages ) {
00210             android.util.Log.i("zeroconf", msg);
00211             text_view.append(msg + "\n");
00212         }
00213         int line_count = text_view.getLineCount(); 
00214         int view_height = text_view.getHeight();
00215         int pixels_per_line = text_view.getLineHeight();
00216         int pixels_difference = line_count*pixels_per_line - view_height;
00217         if ( pixels_difference > 0 ) {
00218                 text_view.scrollTo(0, pixels_difference);
00219         }
00220         }
00221 }


android_extras
Author(s): Daniel Stonier
autogenerated on Thu Aug 27 2015 12:12:07