-
Notifications
You must be signed in to change notification settings - Fork 5
/
Server.java
118 lines (108 loc) · 3.06 KB
/
Server.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package dk.itu.btemu;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static String ADB_CMD_PATH = "";//"/Applications/code/android-sdk/tools/";
public static String ADB_EXE = "adb";
public static String ADB_CMD(){ return ADB_CMD_PATH + ADB_EXE; }
static int StartPort = 8123;
static int _n = 0;
public static int next() {
_n++;
return StartPort+_n;
}
int port;
boolean running;
CommandParser cmdParser;
Thread serverThread;
ServerSocket server;
public Server(int port) {
running = true;
this.port = port;
this.cmdParser = new CommandParser();
}
public void start() {
try {
server = new ServerSocket(port);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return;
}
serverThread = new Thread(new Runnable(){
@Override
public void run() {
while(running) {
try {
Socket spawned = server.accept();
System.out.println("accepting a new connection from "
+ spawned.getInetAddress());
spawnWorker(spawned);
} catch (Exception e) {
e.printStackTrace();
System.out.println("error in server main loop!");
}
}
}
});
serverThread.start();
}
public void stop() {
if(serverThread != null) {
running = false;
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected void spawnWorker(final Socket socket) throws IOException {
new Thread(cmdParser.resolve(socket)).start();
}
public static void main(String args[]) throws Exception {
int port = 8199;
if(args.length>0) {
if(args[0].equals("--help")) {
PrintHelp();
return;
} else {
for(String s : args) {
if(s.startsWith("--port=")) {
String sport = s.substring("--port=".length());
port = Integer.parseInt(sport);
} else if(s.startsWith("--adb.path=")) {
ADB_CMD_PATH = s.substring("--adb.path".length());
}
}
}
}
if( System.getProperty("os.name").toLowerCase().indexOf("win") >= 0 ) {
//this is windows, so the ADB_EXE is adb.exe
ADB_EXE = "adb.exe";
}
Server s = new Server(port);
s.start();
System.out.println("Server started on port " + s.port);
System.out.println("press any key to exit");
System.in.read();
System.out.println("exiting...");
s.stop();
}
private static void PrintHelp() {
String out = "Start the Bluetooth Android Emulator server.\n";
out += "Sample usage:\n";
out += "java dk.itu.btemu.Server --adb.path=/usr/local/share/android.sk/tools/\n\n";
out += "Parameters:\n";
out += "--port=<port> specify the port on which run the server\n";
out += "\tdefault: 8199. Note that currently is hard-coded in\n";
out += "\tthe android library.\n";
out += "--adb.path=<path> specify where to find the adb command\n";
out += "\tdefault: /Applications/code/android.sdk/tools/\n\n";
out += "If you want to check which devices are connected, \n";
out += "telnet localhost <port>, followd by the discovery command\n";
out += "which is 2]](return)";
System.out.println(out);
}
}