1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package triptracker.core;
21
22 import java.io.IOException;
23 import java.io.OutputStream;
24
25 /***
26 * The Protocol class works by being a common place where both the server and
27 * client can obtain information in order to conform to the protocol.
28 */
29 public class Protocol {
30 /***
31 * The newline character separating each message. This is defined to keep
32 * the protocol portable and consistent between platforms and languages
33 * because the newline used by <code>println()</code> in Java is different
34 * on both Windows, Linux/Unix and Mac. Although <code>readLine()</code> in
35 * Java automatically handles all newline types, other languages probably
36 * won't.
37 */
38 public static final String NEWLINE = "\r\n";
39
40 /*** Message field delimiter. */
41 public static final String DELIMITER = ";";
42
43 /*** Default protocol port number. */
44 public static final int PORTNR = 5455;
45
46 /*** Default host address. */
47 public static final String HOST = "triptracker.dyndns.org";
48
49 /*** Default compressed buffersize. */
50 public static final int ZIP_BUFFERSIZE = 1024;
51
52 /*** Client is a coordinate sender/source. */
53 public static final int SENDCLIENT = 1;
54
55 /*** Client is a coordinate reciever/sink. */
56 public static final int MAPCLIENT = 2;
57
58 /*** Client wants to terminate the connection. */
59 public static final int QUIT = 0;
60
61 /*** Authentication failed. */
62 public static final int AUTH_FAIL = -1;
63
64 /*** Authentication successful. */
65 public static final int AUTH_OK = 0;
66
67 /***
68 * New coordinate added.
69 * <p>
70 * Syntax: {@code <latitude>, <longitude>, <dateTime>}
71 * <p>
72 * The format of dateTime is "yyyy-MM-dd hh:mm:ss".
73 */
74 public static final int COORD_ADD = 1;
75
76 /***
77 * Add a bundle of coordinates.
78 * <p>
79 * Syntax: {@code <zipped string of coordinates>}
80 */
81 public static final int COORD_BUFFER_ADD = 2;
82
83 /***
84 * Make a new route. Also used when returning the route ID to a GPS client
85 * after the route is successfully created.
86 * <p>
87 * Syntax: {@code <description>}
88 */
89 public static final int MAKE_ROUTE = 3;
90
91 /***
92 * Set active route.
93 * <p>
94 * Syntax: {@code <routeId>}
95 */
96 public static final int SET_ROUTE = 4;
97
98 /***
99 * Request for a list of all routes for a spesific user.
100 * <p>
101 * Syntax: {@code <username>}
102 */
103 public static final int VIEW_ROUTES = 5;
104
105 /*** Get list of all users. */
106 public static final int USERS_GET = 6;
107
108 /***
109 * Get all coodinates for spesific route.
110 * <p>
111 * Syntax: {@code <routeId>}
112 * */
113 public static final int ROUTE_GET = 7;
114
115 /***
116 * Lock/unlock a route.
117 * <p>
118 * Syntax: {@code <routeId>}
119 */
120 public static final int ROUTE_LOCK = 8;
121
122 /***
123 * Realtime state has changed. Also used to request a list of realtime
124 * routes from the server.
125 * <p>
126 * Syntax: {@code <enabled>}
127 * <p>
128 * enabled: true if realtime is starting, and false if realtime stopped
129 */
130 public static final int REALTIME = 9;
131
132 /*** Gets spesific user*/
133 public static final int USER_GET = 10;
134
135 /***
136 // * Set the route's visibility to enable other users to see it or not.
137 // * <p>
138 // * Syntax: {@code <routeId>, <public>}
139 // * <p>
140 // * public: zero for not public and 1 for public
141 // */
142
143
144 /***
145 * Send a message with the specified stream. The sent message will be
146 * composed of the message parts (elements in the message parameter array)
147 * separated by the {@link #DELIMITER} character and the whole message
148 * will be terminated by the {@link #NEWLINE} string.
149 *
150 * @param out output stream to sent message to
151 * @param flush if <code>true</code> flush the output stream and force any
152 * buffered output bytes to be written out after the message has
153 * been written to the stream
154 * @param message message to send
155 * @throws IOException on failed send
156 */
157 public static void send(OutputStream out, boolean flush, Object... message)
158 throws IOException {
159
160
161 out.write(makeMsg(message).getBytes());
162
163 if (flush) {
164 out.flush();
165 }
166 }
167
168 /***
169 * Sends a message with the specified stream. This method has the same
170 * effect as <code>send(stream, false, message)</code>.
171 *
172 * @param out output stream to sent message to
173 * @param message message to send
174 * @throws IOException on failed send
175 * @see #send(OutputStream, boolean, Object[])
176 */
177 public static void send(OutputStream out, Object... message)
178 throws IOException {
179 send(out, false, message);
180 }
181
182 /***
183 * Builds a message by adding {@link #DELIMITER} between each message part
184 * and putting {@link #NEWLINE} at the end.
185 *
186 * @param message parts to delimit
187 * @return properly delimited message
188 * @see #makeMsg(String, StringBuilder, Object[])
189 */
190 public static String makeMsg(Object... message) {
191 StringBuilder builder = new StringBuilder();
192 makeMsg(builder, message).toString();
193 builder.append(NEWLINE);
194 return builder.toString();
195 }
196
197 /***
198 * Builds a message by adding {@link #DELIMITER} between each message part
199 * and putting <code>msgDelim</code> at the end. All messages will be
200 * appended to the existing <code>builder</code>, or if <code>builder</code>
201 * is <code>null</code> then a new {@link StringBuilder} will be created.
202 *
203 * @param msgDelim string to put at the end of the message
204 * @param builder StringBuilder to use for adding data, if null then a new
205 * string builder will be created
206 * @param message parts to delimit
207 * @return StringBuilder containing a properly delimited message including
208 * the trailing {@link #NEWLINE}.
209 */
210 public static StringBuilder makeMsg(String msgDelim, StringBuilder builder,
211 Object... message) {
212 StringBuilder str = null;
213
214 if (builder == null) {
215 builder = new StringBuilder();
216 }
217
218 if (builder != null && builder.length() > 0) {
219 builder.append(msgDelim);
220 }
221
222 str = makeMsg(builder, message);
223
224 return str;
225 }
226
227 /***
228 * Builds a message by adding {@link #DELIMITER} between each message part
229 * and putting {@link #NEWLINE} at the end. All messages will be appended to
230 * the existing <code>builder</code>, or if <code>builder</code> is
231 * <code>null</code> then a new {@link StringBuilder} will be created.
232 *
233 * @param builder StringBuilder to use for adding data, if null then a new
234 * string builder will be created
235 * @param message parts to delimit
236 * @return StringBuilder containing a properly delimited message including
237 * the trailing {@link #NEWLINE}.
238 */
239 public static StringBuilder makeMsg(StringBuilder builder,
240 Object... message) {
241 if (builder == null) {
242 builder = new StringBuilder();
243 }
244
245 builder.append(message[0]);
246
247
248 for (int i = 1; i < message.length; i++) {
249 builder.append(DELIMITER);
250 if (message[i] instanceof Boolean) {
251 builder.append(((Boolean) message[i]) ? 1 : 0);
252 } else {
253 builder.append(message[i].toString());
254 }
255 }
256
257 return builder;
258 }
259
260 }