This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
/
WeinreClientCommands.java
102 lines (77 loc) · 3.84 KB
/
WeinreClientCommands.java
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
/*
* weinre is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2010, 2011 IBM Corporation
*/
package weinre.server.service;
import java.io.IOException;
import java.util.List;
import org.apache.wink.json4j.JSONArray;
import org.apache.wink.json4j.JSONException;
import org.apache.wink.json4j.JSONObject;
import weinre.server.Channel;
import weinre.server.Client;
import weinre.server.ConnectionManager;
import weinre.server.Main;
import weinre.server.Target;
//-------------------------------------------------------------------
public class WeinreClientCommands {
//---------------------------------------------------------------
public void registerClient(Channel channel, String callbackId) throws IOException {
Client client = new Client(channel);
JSONObject description = client.getDescription();
channel.sendCallback("WeinreClientEvents", callbackId, description);
channel.sendEvent("WeinreClientEvents", "serverProperties", Main.getSettings().asProperties());
}
//---------------------------------------------------------------
public void getTargets(Channel channel, String callbackId) throws IOException {
List<Target> targets = ConnectionManager.$.getTargets(channel.getId());
JSONArray targetResults = new JSONArray();
for (Target target: targets) {
targetResults.add(target.getDescription());
}
channel.sendCallback("WeinreClientEvents", callbackId, targetResults);
}
//---------------------------------------------------------------
public void getClients(Channel channel, String callbackId) throws IOException {
List<Client> clients = ConnectionManager.$.getClients(channel.getId());
JSONArray clientResults = new JSONArray();
for (Client client: clients) {
clientResults.add(client.getDescription());
}
channel.sendCallback("WeinreClientEvents", callbackId, clientResults);
}
//---------------------------------------------------------------
public void connectTarget(Channel channel, String clientId, String targetId, String callbackId) {
Client client = ConnectionManager.$.getClient(clientId);
if (client == null) return;
Target target = ConnectionManager.$.getTarget(targetId);
if (target == null) return;
ConnectionManager.$.connect(client, target);
}
//---------------------------------------------------------------
public void disconnectTarget(Channel channel, String clientId, String callbackId) {
Client client = ConnectionManager.$.getClient(clientId);
if (client == null) return;
Target target = client.getConnectedTarget();
if (target == null) return;
ConnectionManager.$.disconnect(client, target);
}
//---------------------------------------------------------------
public void logDebug(Channel channel, String message, String callbackId) {
Main.debug("client " + channel.getName() + ": " + message);
}
//---------------------------------------------------------------
public void logInfo(Channel channel, String message, String callbackId) {
Main.info("client " + channel.getName() + ": " + message);
}
//---------------------------------------------------------------
public void logWarning(Channel channel, String message, String callbackId) {
Main.warn("client " + channel.getName() + ": " + message);
}
//---------------------------------------------------------------
public void logError(Channel channel, String message, String callbackId) {
Main.error("client " + channel.getName() + ": " + message);
}
}