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 
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          * Tasks
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                 // add to the content and notify the list view if its a new service
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                 // remove service from storage and notify list view
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          * Variables
00144          ********************/
00145         private ArrayList<DiscoveredService> discovered_services;
00146     private DiscoveryAdapter discovery_adapter;
00147 
00148         /*********************
00149          * Constructors
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          * Callbacks
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 }


android_remocons
Author(s): Daniel Stonier , Kazuto Murase
autogenerated on Wed Aug 26 2015 10:40:28