-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathZeroConf.java
More file actions
248 lines (220 loc) · 8.69 KB
/
Copy pathZeroConf.java
File metadata and controls
248 lines (220 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* ZeroConf plugin for Cordova/Phonegap
*
* @author Matt Kane
* Copyright (c) Triggertrap Ltd. 2012. All Rights Reserved.
* Available under the terms of the MIT License.
*
*/
package com.triggertrap;
import java.io.IOException;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.wifi.WifiManager;
import android.util.Log;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceInfo;
import javax.jmdns.ServiceListener;
public class ZeroConf extends CordovaPlugin {
WifiManager.MulticastLock lock;
private JmDNS jmdns = null;
private ServiceListener listener;
private static final String LOG_TAG = "ZeroConf";
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
Log.d(LOG_TAG,"action:" + action + " callbackId:" + callbackContext.getCallbackId() );
if (action.equals("watch")) {
String type = args.optString(0);
if (type != null) {
try {
watch(type, callbackContext);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
} else {
callbackContext.error("Service type not specified.");
}
} else if (action.equals("unwatch")) {
String type = args.optString(0);
if (type != null) {
unwatch(type);
} else {
callbackContext.error("Service type not specified.");
}
} else if (action.equals("register")) {
JSONObject obj = args.optJSONObject(0);
if (obj != null) {
String type = obj.optString("type");
String name = obj.optString("name");
int port = obj.optInt("port");
String text = obj.optString("text");
if(type == null) {
callbackContext.error("Missing required service info.");
}
register(type, name, port, text);
} else {
callbackContext.error("Missing required service info.");
}
} else if (action.equals("close")) {
if(jmdns != null) {
try {
jmdns.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else if (action.equals("unregister")) {
if(jmdns != null) {
jmdns.unregisterAllServices();
}
} else if (action.equals("list")) {
String type = args.optString(0);
int timeout = args.optInt(1);
if (type != null) {
try {
list(type, timeout, callbackContext);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
callbackContext.error("Service type not specified.");
}
} else {
Log.e(LOG_TAG, "Invalid action: " + action);
return false;
}
PluginResult result = new PluginResult(Status.NO_RESULT);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
return true;
}
private void watch(String type, final CallbackContext callbackContext) throws IOException {
WifiManager wifi = (WifiManager) this.cordova.getActivity().getSystemService(android.content.Context.WIFI_SERVICE);
lock = wifi.createMulticastLock("ZeroConfPluginLock");
lock.setReferenceCounted(true);
lock.acquire();
try {
if(jmdns == null) {
jmdns = JmDNS.create();
}
listener = new ServiceListener() {
public void serviceResolved(ServiceEvent ev) {
Log.d(LOG_TAG, "Resolved");
sendCallback("added", ev.getInfo(), callbackContext);
}
public void serviceRemoved(ServiceEvent ev) {
Log.d(LOG_TAG, "Removed");
sendCallback("removed", ev.getInfo(), callbackContext);
}
public void serviceAdded(ServiceEvent event) {
Log.d(LOG_TAG, "Added");
// Force serviceResolved to be called again
jmdns.requestServiceInfo(event.getType(), event.getName(), 1);
}
};
} catch (IOException e) {
e.printStackTrace();
return;
}
Log.d(LOG_TAG, "Watch " + type);
Log.d(LOG_TAG, "Name: " + jmdns.getName() + " host: " + jmdns.getHostName());
jmdns.addServiceListener(type, listener);
}
private void unwatch(String type) {
if(jmdns == null) {
return;
}
jmdns.removeServiceListener(type, listener);
}
private void register (String type, String name, int port, String text) {
if(name == null) {
name = "";
}
if(text == null) {
text = "";
}
try {
ServiceInfo service = ServiceInfo.create(type, name, port, text);
jmdns.registerService(service);
} catch (IOException e) {
e.printStackTrace();
}
}
private void list(final String type, final int timeout, final CallbackContext callbackContext) throws IOException {
if(jmdns == null) {
jmdns = JmDNS.create();
}
cordova.getThreadPool().execute(new Runnable() {
public void run() {
ServiceInfo[] serviceInfoList;
serviceInfoList = jmdns.list(type, timeout);
JSONArray services = new JSONArray();
if (serviceInfoList != null) {
for (int index = 0; index < serviceInfoList.length; index++) {
services.put(jsonifyService(serviceInfoList[index]));
}
}
Log.d(LOG_TAG, "List " + type);
Log.d(LOG_TAG, "Name: " + jmdns.getName() + " host: " + jmdns.getHostName());
//PluginResult result = new PluginResult(PluginResult.Status.OK, services);
//result.setKeepCallback(true);
//callbackContext.sendPluginResult(result);
callbackContext.success(services); // Thread-safe.
}
});
}
private void sendCallback(String action, ServiceInfo info, CallbackContext callbackContext) {
JSONObject status = new JSONObject();
try {
status.put("action", action);
status.put("service", jsonifyService(info));
Log.d(LOG_TAG, "Sending result: " + status.toString());
PluginResult result = new PluginResult(PluginResult.Status.OK, status);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
public static JSONObject jsonifyService(ServiceInfo info) {
JSONObject obj = new JSONObject();
try {
obj.put("application", info.getApplication());
obj.put("domain", info.getDomain());
obj.put("port", info.getPort());
obj.put("name", info.getName());
obj.put("server", info.getServer());
obj.put("description", info.getNiceTextString());
obj.put("protocol", info.getProtocol());
obj.put("qualifiedname", info.getQualifiedName());
obj.put("type", info.getType());
JSONArray addresses = new JSONArray();
String[] add = info.getHostAddresses();
for(int i = 0; i < add.length; i++) {
addresses.put(add[i]);
}
obj.put("addresses", addresses);
JSONArray urls = new JSONArray();
String[] url = info.getURLs();
for(int i = 0; i < url.length; i++) {
urls.put(url[i]);
}
obj.put("urls", urls);
} catch (JSONException e) {
e.printStackTrace();
return null;
}
return obj;
}
}