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.common_tools.zeroconf;
00018
00019 import android.annotation.SuppressLint;
00020 import android.os.AsyncTask;
00021
00022 import com.github.rosjava.zeroconf_jmdns_suite.jmdns.DiscoveredService;
00023 import com.github.rosjava.zeroconf_jmdns_suite.jmdns.ZeroconfDiscoveryHandler;
00024
00025 import java.util.ArrayList;
00026
00036 public class DiscoveryHandler implements ZeroconfDiscoveryHandler {
00037
00043 @SuppressLint("NewApi")
00044 private class ServiceAddedTask extends AsyncTask<DiscoveredService, String, Void> {
00045
00046 @SuppressLint("NewApi")
00047 protected Void doInBackground(DiscoveredService... services) {
00048 if (services.length == 1) {
00049 DiscoveredService service = services[0];
00050 String result = "[+] Service added: " + service.name + "." + service.type + "." + service.domain + ".";
00051 publishProgress(result);
00052 } else {
00053 publishProgress("Error - ServiceAddedTask::doInBackground received #services != 1");
00054 }
00055 return null;
00056 }
00057
00058 }
00059
00060 @SuppressLint("NewApi")
00061 private class ServiceResolvedTask extends AsyncTask<DiscoveredService, String, DiscoveredService> {
00062
00063 @SuppressLint("NewApi")
00064 protected DiscoveredService doInBackground(DiscoveredService... services) {
00065 if (services.length == 1) {
00066 DiscoveredService discovered_service = services[0];
00067 String result = "[=] Service resolved: " + discovered_service.name + "." + discovered_service.type + "." + discovered_service.domain + ".\n";
00068 result += " Port: " + discovered_service.port;
00069 for (String address : discovered_service.ipv4_addresses) {
00070 result += "\n Address: " + address;
00071 }
00072 for (String address : discovered_service.ipv6_addresses) {
00073 result += "\n Address: " + address;
00074 }
00075 publishProgress(result);
00076 return discovered_service;
00077 } else {
00078 publishProgress("Error - ServiceAddedTask::doInBackground received #services != 1");
00079 }
00080 return null;
00081 }
00082
00083 @SuppressLint("NewApi")
00084 protected void onPostExecute(DiscoveredService discovered_service) {
00085
00086 if (discovered_service != null) {
00087 int index = 0;
00088 for (DiscoveredService s : discovered_services) {
00089 if (s.name.equals(discovered_service.name)) {
00090 break;
00091 } else {
00092 ++index;
00093 }
00094 }
00095 if (index == discovered_services.size()) {
00096 discovered_services.add(discovered_service);
00097 discovery_adapter.notifyDataSetChanged();
00098 } else {
00099 android.util.Log.i("zeroconf", "Tried to add an existing service (fix this)");
00100 }
00101 }
00102 }
00103 }
00104
00105 @SuppressLint("NewApi")
00106 private class ServiceRemovedTask extends AsyncTask<DiscoveredService, String, DiscoveredService> {
00107
00108 @SuppressLint("NewApi")
00109 protected DiscoveredService doInBackground(DiscoveredService... services) {
00110 if (services.length == 1) {
00111 DiscoveredService discovered_service = services[0];
00112 String result = "[-] Service removed: " + discovered_service.name + "." + discovered_service.type + "." + discovered_service.domain + ".\n";
00113 result += " Port: " + discovered_service.port;
00114 publishProgress(result);
00115 return discovered_service;
00116 } else {
00117 publishProgress("Error - ServiceAddedTask::doInBackground received #services != 1");
00118 }
00119 return null;
00120 }
00121
00122
00123 protected void onPostExecute(DiscoveredService discovered_service) {
00124
00125 if (discovered_service != null) {
00126 int index = 0;
00127 for (DiscoveredService s : discovered_services) {
00128 if (s.name.equals(discovered_service.name)) {
00129 break;
00130 } else {
00131 ++index;
00132 }
00133 }
00134 if (index != discovered_services.size()) {
00135 discovered_services.remove(index);
00136 discovery_adapter.notifyDataSetChanged();
00137 } else {
00138 android.util.Log.i("zeroconf", "Tried to remove a non-existant service");
00139 }
00140 }
00141 }
00142 }
00143
00149 private ArrayList<DiscoveredService> discovered_services;
00150 private DiscoveryAdapter discovery_adapter;
00151
00157 public DiscoveryHandler(DiscoveryAdapter discovery_adapter, ArrayList<DiscoveredService> discovered_services) {
00158 this.discovery_adapter = discovery_adapter;
00159 this.discovered_services = discovered_services;
00160 }
00161
00167 public void serviceAdded(DiscoveredService service) {
00168 new ServiceAddedTask().execute(service);
00169 }
00170
00171 public void serviceRemoved(DiscoveredService service) {
00172 new ServiceRemovedTask().execute(service);
00173 }
00174
00175 public void serviceResolved(DiscoveredService service) {
00176 new ServiceResolvedTask().execute(service);
00177 }
00178 }