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
|
package com.orbekk.discovery;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.orbekk.same.ClientApp;
import com.orbekk.same.DiscoveryListener;
import com.orbekk.same.MasterApp;
import com.orbekk.same.NetworkNotificationListener;
import com.orbekk.same.SameController;
public class SameService extends Service {
final static int PORT = 15066;
private Logger logger = LoggerFactory.getLogger(getClass());
private Thread discoveryThread = null;
private SameController sameController = null;
public final class DiscoveryThread extends Thread {
Broadcaster broadcast;
DiscoveryListener listener;
public DiscoveryThread(DiscoveryListener listener) {
broadcast = new Broadcaster(SameService.this);
this.listener = listener;
}
@Override public void run() {
while (!Thread.interrupted()) {
DatagramPacket packet = broadcast.receiveBroadcast(PORT);
String content = new String(packet.getData(), 0, packet.getLength());
String[] words = content.split(" ");
if (!content.startsWith("Discover") || content.length() < 2) {
logger.warn("Invalid discovery message: {}", content);
continue;
}
String port = words[1];
String url = "http://" + packet.getAddress().getHostAddress() +
":" + port + "/ClientService.json";
listener.discover(url);
Message message = Message.obtain();
message.obj = "New client: " + url;
toastHandler.sendMessage(message);
}
}
@Override public void interrupt() {
super.interrupt();
broadcast.interrupt();
}
}
private Handler toastHandler = new Handler() {
@Override public void handleMessage(Message message) {
Toast.makeText(SameService.this,
(String)message.obj, Toast.LENGTH_SHORT)
.show();
logger.info("Display toast: {}", (String)message.obj);
}
};
private void createNetwork() {
if (discoveryThread == null) {
synchronized (this) {
discoveryThread = new DiscoveryThread(sameController.getClient());
discoveryThread.start();
}
}
}
private void sendBroadcastDiscovery(InetAddress ip) {
Broadcaster broadcaster = new Broadcaster(this);
String message = "Discover " + (PORT + 2);
byte[] data = message.getBytes();
if (ip.equals(broadcaster.getBroadcastAddress())) {
broadcaster.sendUdpData(data, ip, PORT);
} else {
String remoteAddress =
String.format("http://%s:%s/ClientService.json",
ip.getHostAddress(), PORT + 2);
sameController.getClient().sendDiscoveryRequest(
remoteAddress);
}
}
private void joinNetwork(InetAddress ip) {
sameController.getClient().setNetworkListener(
new NetworkNotificationListener() {
@Override
public void notifyNetwork(String networkName, String masterUrl) {
Message message = Message.obtain();
message.obj = "notifyNetwork(" + networkName + ", " +
masterUrl;
toastHandler.sendMessage(message);
}
});
sendBroadcastDiscovery(ip);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.setProperty("http.keepAlive", "false");
Toast.makeText(this, "service start: " + intent.getAction(),
Toast.LENGTH_SHORT).show();
if (sameController == null) {
sameController = SameController.create(PORT + 2);
try {
sameController.start();
String myIp = new Broadcaster(this).getWlanAddress()
.getHostAddress();
String myUrl = "http://" + myIp + ":" + (PORT + 2) + "/";
sameController.setUrl(myUrl);
} catch (Exception e) {
logger.error("Failed to start server", e);
return START_STICKY;
}
}
if (intent.getAction().equals("create")) {
createNetwork();
} else if (intent.getAction().equals("join")) {
try {
InetAddress ip = InetAddress.getByName(intent.getExtras().getString("ip"));
joinNetwork(ip);
} catch (UnknownHostException e) {
logger.error("Unknown host.", e);
}
}
return START_STICKY;
}
@Override
public void onDestroy() {
Toast.makeText(this, "service stopped", Toast.LENGTH_SHORT).show();
if (discoveryThread != null) {
discoveryThread.interrupt();
}
if (sameController != null) {
sameController.stop();
}
}
}
|