-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathServer.java
162 lines (128 loc) · 4.26 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
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
/* Server.java
* ~~~~~~~~~~~
* Please do not remove the following notices.
* Copyright (c) 2010 by Geekscape Pty. Ltd.
* License: GPLv3. http://geekscape.org/static/parrot_license.html
*
* To Do
* ~~~~~
* - Refactor duplicate "forward" and "backward" command code.
* - Provide feedback to client.
* - Change "rate" command.
* - Implement all Parrot commands.
* - Transparently pass AT commands.
* - Adjust logging level (filtering), especially navigation output.
*/
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
public class Server {
private static final int DEFAULT_SERVER_PORT = 5600;
private ParrotCommunication parrotCommunication = null;
public Server(
ParrotCommunication parrotCommunication) {
this.parrotCommunication = parrotCommunication;
try {
ServerSocket serverSocket = new ServerSocket(DEFAULT_SERVER_PORT);
System.out.println("New ServerSocket: " + serverSocket);
while(true) {
try {
Socket socket = serverSocket.accept();
new SocketHandler(socket);
}
catch (IOException ioException) {
System.err.println("Socket ioException: " + ioException.getMessage());
}
}
}
catch (Exception exception) {
System.err.println("Server exception: " + exception.getMessage());
System.exit(-1);
}
}
private class SocketHandler extends Thread {
Socket socket = null;
public SocketHandler(
Socket socket) {
this.socket = socket;
start();
}
public void run() {
// System.out.println("Socket: " + socket);
try {
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
String buffer;
while ((buffer = bufferedReader.readLine()) != null) {
// System.out.println("Command received: " + buffer);
executeCommand(buffer);
}
}
catch (IOException ioException) {
// System.out.println("Socket disconnected");
}
finally {
try {
socket.close();
}
catch (IOException ioException) {}
// System.out.println("Closed socket connection");
}
}
private void executeCommand(
String buffer) {
StringTokenizer stringTokenizer = new StringTokenizer(buffer);
String command = stringTokenizer.nextToken();
String parameter = null;
int repeat = 1;
if (stringTokenizer.hasMoreTokens()) {
parameter = stringTokenizer.nextToken();
try {
repeat = Integer.parseInt(parameter);
}
catch (NumberFormatException numberFormatException) {}
}
if (command.equalsIgnoreCase("takeoff")) {
System.out.println("Command: takeoff");
parrotCommunication.transmitRefCommand(
ParrotCommunication.BEHAVIOUR_TAKEOFF
);
}
else if (command.equalsIgnoreCase("land")) {
System.out.println("Command: land");
parrotCommunication.transmitRefCommand(
ParrotCommunication.BEHAVIOUR_LAND
);
}
else if (command.equalsIgnoreCase("forward")) {
System.out.println("Command: forward: " + repeat);
parrotCommunication.emergencyAbort = false;
for (int index = 0; index < repeat; index ++) {
if (parrotCommunication.emergencyAbort == true) break;
parrotCommunication.transmitProgressiveCommand(
ParrotCommunication.MODE_PROGRESSIVE, 0f, -0.30f, 0f, 0f
);
}
parrotCommunication.transmitProgressiveCommand(
ParrotCommunication.MODE_HOVER, 0f, 0f, 0f, 0f
);
}
else if (command.equalsIgnoreCase("backward")) {
System.out.println("Command: backward");
parrotCommunication.emergencyAbort = false;
for (int index = 0; index < repeat; index ++) {
if (parrotCommunication.emergencyAbort == true) break;
parrotCommunication.transmitProgressiveCommand(
ParrotCommunication.MODE_PROGRESSIVE, 0f, 0.30f, 0f, 0f
);
}
parrotCommunication.transmitProgressiveCommand(
ParrotCommunication.MODE_HOVER, 0f, 0f, 0f, 0f
);
}
else {
System.out.println("Unknown server command: " + command);
}
}
}
}