Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package com.github.rosjava.android_remocons.robot_remocon.zeroconf;
00018
00019 import java.util.ArrayList;
00020
00021 import android.annotation.SuppressLint;
00022 import android.os.AsyncTask;
00023
00024 import com.github.rosjava.zeroconf_jmdns_suite.jmdns.DiscoveredService;
00025 import com.github.rosjava.zeroconf_jmdns_suite.jmdns.ZeroconfDiscoveryHandler;
00026
00036 public class DiscoveryHandler implements ZeroconfDiscoveryHandler {
00037
00038
00039
00040
00041 @SuppressLint("NewApi")
00042 private class ServiceAddedTask extends AsyncTask<DiscoveredService, String, Void> {
00043
00044 @SuppressLint("NewApi")
00045 protected Void doInBackground(DiscoveredService... services) {
00046 if ( services.length == 1 ) {
00047 DiscoveredService service = services[0];
00048 String result = "[+] Service added: " + service.name + "." + service.type + "." + service.domain + ".";
00049 publishProgress(result);
00050 } else {
00051 publishProgress("Error - ServiceAddedTask::doInBackground received #services != 1");
00052 }
00053 return null;
00054 }
00055
00056 }
00057
00058 @SuppressLint("NewApi")
00059 private class ServiceResolvedTask extends AsyncTask<DiscoveredService, String, DiscoveredService> {
00060
00061 @SuppressLint("NewApi")
00062 protected DiscoveredService doInBackground(DiscoveredService... services) {
00063 if ( services.length == 1 ) {
00064 DiscoveredService discovered_service = services[0];
00065 String result = "[=] Service resolved: " + discovered_service.name + "." + discovered_service.type + "." + discovered_service.domain + ".\n";
00066 result += " Port: " + discovered_service.port;
00067 for ( String address : discovered_service.ipv4_addresses ) {
00068 result += "\n Address: " + address;
00069 }
00070 for ( String address : discovered_service.ipv6_addresses ) {
00071 result += "\n Address: " + address;
00072 }
00073 publishProgress(result);
00074 return discovered_service;
00075 } else {
00076 publishProgress("Error - ServiceAddedTask::doInBackground received #services != 1");
00077 }
00078 return null;
00079 }
00080
00081 @SuppressLint("NewApi")
00082 protected void onPostExecute(DiscoveredService discovered_service) {
00083
00084 if ( discovered_service != null ) {
00085 int index = 0;
00086 for ( DiscoveredService s : discovered_services ) {
00087 if ( s.name.equals(discovered_service.name) ) {
00088 break;
00089 } else {
00090 ++index;
00091 }
00092 }
00093 if ( index == discovered_services.size() ) {
00094 discovered_services.add(discovered_service);
00095 discovery_adapter.notifyDataSetChanged();
00096 } else {
00097 android.util.Log.i("zeroconf", "Tried to add an existing service (fix this)");
00098 }
00099 }
00100 }
00101 }
00102
00103 @SuppressLint("NewApi")
00104 private class ServiceRemovedTask extends AsyncTask<DiscoveredService, String, DiscoveredService> {
00105
00106 @SuppressLint("NewApi")
00107 protected DiscoveredService doInBackground(DiscoveredService... services) {
00108 if ( services.length == 1 ) {
00109 DiscoveredService discovered_service = services[0];
00110 String result = "[-] Service removed: " + discovered_service.name + "." + discovered_service.type + "." + discovered_service.domain + ".\n";
00111 result += " Port: " + discovered_service.port;
00112 publishProgress(result);
00113 return discovered_service;
00114 } else {
00115 publishProgress("Error - ServiceAddedTask::doInBackground received #services != 1");
00116 }
00117 return null;
00118 }
00119
00120
00121 protected void onPostExecute(DiscoveredService discovered_service) {
00122
00123 if ( discovered_service != null ) {
00124 int index = 0;
00125 for ( DiscoveredService s : discovered_services ) {
00126 if ( s.name.equals(discovered_service.name) ) {
00127 break;
00128 } else {
00129 ++index;
00130 }
00131 }
00132 if ( index != discovered_services.size() ) {
00133 discovered_services.remove(index);
00134 discovery_adapter.notifyDataSetChanged();
00135 } else {
00136 android.util.Log.i("zeroconf", "Tried to remove a non-existant service");
00137 }
00138 }
00139 }
00140 }
00141
00142
00143
00144
00145 private ArrayList<DiscoveredService> discovered_services;
00146 private DiscoveryAdapter discovery_adapter;
00147
00148
00149
00150
00151 public DiscoveryHandler(DiscoveryAdapter discovery_adapter, ArrayList<DiscoveredService> discovered_services) {
00152 this.discovery_adapter = discovery_adapter;
00153 this.discovered_services = discovered_services;
00154 }
00155
00156
00157
00158
00159 public void serviceAdded(DiscoveredService service) {
00160 new ServiceAddedTask().execute(service);
00161 }
00162
00163 public void serviceRemoved(DiscoveredService service) {
00164 new ServiceRemovedTask().execute(service);
00165 }
00166
00167 public void serviceResolved(DiscoveredService service) {
00168 new ServiceResolvedTask().execute(service);
00169 }
00170 }