1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package triptracker.client.net;
21
22 import static triptracker.core.ConnectionState.*;
23 import static triptracker.core.Protocol.*;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
29
30 import triptracker.core.Coordinate;
31 import triptracker.core.Route;
32 import triptracker.core.Utils;
33
34 public class GPSSocket extends SocketConnection<GPSSocketListener> {
35 /***
36 * Logon a user of the type {@link triptracker.core.Protocol#SENDCLIENT}.
37 *
38 * @param user username
39 * @param pass password
40 * @throws IOException on connection failure
41 */
42 public void logon(String user, String pass) throws IOException {
43 logon(SENDCLIENT, user, pass);
44 }
45
46 /***
47 * Sends coordinates to server.
48 *
49 * @param coord coordinate to send to server
50 */
51 public void sendCoord(Coordinate coord){
52 sendMessage(COORD_ADD, coord.getY(), coord.getX(),
53 coord.getDateString());
54 }
55
56 /***
57 * Sends buffered coordinates to server.
58 *
59 * @param coordBuffer string buffer of all coordinates
60 * @return true if send successful, false if send failed
61 */
62 public boolean sendTmpCoords(StringBuilder coordBuffer){
63
64
65
66
67
68
69 if(isConnected()) {
70
71 int size = coordBuffer.toString().getBytes().length;
72 sendMessage(COORD_BUFFER_ADD, size);
73
74
75
76 sendMessage(coordBuffer);
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 return true;
111 }
112
113 return false;
114 }
115
116 /***
117 * Generates new route
118 * @param description
119 */
120 public void makeRoute(String description){
121 sendMessage(MAKE_ROUTE, description);
122 }
123
124 /*** For locking route when it is finnished. */
125 public void lockRoute(int route){
126 sendMessage(ROUTE_LOCK, route);
127 }
128
129 protected void routesReceived(List<Route> routes) {
130 for (GPSSocketListener listener : listeners) {
131 try {
132 listener.routeList(routes);
133 } catch (RuntimeException e) {
134 brokenListener(listener, e);
135 }
136 }
137 }
138
139 /***
140 * {@inheritDoc}
141 */
142 @Override
143 public void addListener(GPSSocketListener listener) {
144 listeners.add(listener);
145 }
146
147 /***
148 * {@inheritDoc}
149 */
150 @Override
151 public void removeListener(GPSSocketListener listener) {
152 listeners.remove(listener);
153 }
154
155 /***
156 * {@inheritDoc}
157 */
158 @Override
159 protected boolean messageHandler(String message){
160 String[] msg = message.split(DELIMITER, -1);
161
162
163 try {
164
165 switch (Integer.parseInt(msg[0])) {
166 case AUTH_FAIL:
167 connectionUpdate(AUTH_FAILED);
168 break;
169 case AUTH_OK:
170 connectionUpdate(AUTH_SUCCESS);
171 break;
172 case VIEW_ROUTES:
173 List<Route> routes = new ArrayList<Route>();
174
175 for (int i = 1; i < msg.length; i += 6) {
176 int routeId = Integer.parseInt(msg[i]);
177 int userId = Integer.parseInt(msg[i + 1]);
178 String description = msg[i + 2];
179 boolean active = Utils.strToBool(msg[i + 3]);
180 Date date = Utils.parseDate(msg[i + 4]);
181 boolean visible = Utils.strToBool(msg[i + 5]);
182 Route route = new Route(routeId, userId, description,
183 active, visible, date);
184 routes.add(route);
185 }
186 routesReceived(routes);
187 break;
188 case MAKE_ROUTE:
189 routeCreated(Integer.parseInt(msg[1]));
190 break;
191 default:
192 invalidMessage(message);
193 break;
194 }
195 } catch (NumberFormatException num) {
196 System.out.println("NumberFormatException: " + num);
197 num.printStackTrace();
198 invalidMessage(message);
199 }
200
201 return true;
202 }
203
204 /***
205 * Publish a route created message event to all listeners. This signals that
206 * a previous request to create a route has been accepted.
207 *
208 * @param routeId route ID of the new route
209 */
210 protected void routeCreated(int routeId) {
211 for (GPSSocketListener listener : listeners) {
212 try {
213 listener.routeCreated(routeId);
214 } catch (RuntimeException e) {
215 brokenListener(listener, e);
216 }
217 }
218 }
219 }