summaryrefslogtreecommitdiff
path: root/client/Piztor/src/com/macaroon/piztor/SocketClient.java
blob: fe926d91f3a2bbbf1551fc8a1428521e696184d4 (plain) (blame)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.macaroon.piztor;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Locale;
import java.util.Vector;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;

public class SocketClient {
	static Socket client;
	
	static final int ByteLength = 1;
	static final int IntLength = 4;
	static final int DoubleLength = 8;
	static final int TokenLength = 32;

	public SocketClient(String site, int port, int retime) throws UnknownHostException,
			IOException {
		try {
			client = new Socket();
			client.connect(new InetSocketAddress(site,port), 5000);
			client.setSoTimeout(retime);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			throw e;
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} 
	}

	public int sendMsg(Req req,Handler recall) throws IOException,SocketTimeoutException {
		try {
			DataOutputStream out = new DataOutputStream(
					client.getOutputStream());
			int tmp = req.type;
			int len;
			switch (tmp) {
			case 0:
				ReqLogin rau = (ReqLogin) req;
				String id = rau.user;
				String pa = rau.pass;
				len = IntLength+ByteLength+id.length()+ByteLength+pa.length()+ByteLength;				
				out.writeInt(len);
				out.writeByte(tmp);				
				out.writeBytes(id);
				out.writeByte(0);
				out.writeBytes(pa);
				out.writeByte(0);
				break;
			case 1:
				ReqUpdate rup = (ReqUpdate) req;
				String tk1 = rup.token;
				String name1 = rup.uname;
				len = IntLength+ByteLength+TokenLength+name1.length()+ByteLength+DoubleLength+DoubleLength;				
				out.writeInt(len);				
				out.writeByte(tmp);				
				double slat = rup.lat;
				double slot = rup.lot;
				byte[] b = hexStringToBytes(tk1);						
				out.write(b);
				out.writeBytes(name1);
				out.writeByte(0);
				out.writeDouble(slat);
				out.writeDouble(slot);
				break;
			case 2:
				ReqLocation ras = (ReqLocation) req;
				String tk2 = ras.token;
				String name2 = ras.uname;
				len = IntLength+ByteLength+TokenLength+name2.length()+ByteLength+IntLength;				
				out.writeInt(len);
				out.writeByte(tmp);				
				int gid = ras.gid;
				byte[] b2 = hexStringToBytes(tk2);						
				out.write(b2);
				out.writeBytes(name2);
				out.writeByte(0);
				out.writeInt(gid);
				break;
			case 3:
				ReqUserinfo rus = (ReqUserinfo) req;
				String tk3 = rus.token;
				String name3 = rus.uname;
				len = IntLength+ByteLength+TokenLength+name3.length()+ByteLength+IntLength;				
				out.writeInt(len);
				out.writeByte(tmp);				
				int usid = rus.uid;
				byte[] b3 = hexStringToBytes(tk3);						
				out.write(b3);
				out.writeBytes(name3);
				out.writeByte(0);
				out.writeInt(usid);
				break;
			}
			out.flush();
			DataInputStream in = new DataInputStream(client.getInputStream());
			Message msg = new Message();
			int outlen = in.readInt();
			int type = in.readUnsignedByte();
			switch (type) {
			case 0:
				int status = in.readUnsignedByte();
				int id = in.readInt();
				byte[] buffer = new byte[32];
				in.read(buffer);
				String tk = "";
				for (int i = 0; i < buffer.length; i++) {
				   String hex = Integer.toHexString(buffer[i] & 0xFF);
				   if (hex.length() == 1) {
				    hex = '0' + hex;
				   }
				   tk += hex;
				}
				ResLogin rchklogin = new ResLogin(id,tk,status);
				msg.obj = rchklogin;
				msg.what = 0;
				recall.sendMessage(msg);
				break;
			case 1:
				int status1 = in.readUnsignedByte();
				ResUpdate rchkupd = new ResUpdate(status1);
				msg.obj = rchkupd;
				msg.what = 1;
				recall.sendMessage(msg);
				break;
			case 2:
				int status2 = in.readUnsignedByte();
				int n = 0;
				outlen-=(IntLength+ByteLength+ByteLength);
				Vector<Rlocation> tmpv = new Vector<Rlocation>();
				while(outlen > 0) {
					int tid = in.readInt();
					double lat = in.readDouble();
					double lot = in.readDouble();
					tmpv.add(new Rlocation(tid,lat,lot));
					outlen -= (IntLength+DoubleLength+DoubleLength);
					n++;
				}
				ResLocation rlocin = new ResLocation(n,status2,tmpv);
				msg.obj = rlocin;
				msg.what = 2;
				recall.sendMessage(msg);
				break;
			case 3:
				int status3 = in.readUnsignedByte();
				outlen-=(IntLength+ByteLength+ByteLength);
				ReqUserinfo rus = (ReqUserinfo) req;
				int u = rus.uid;
				int g = 0,s = 0;
				while(outlen > 0) {
					int typ = in.readUnsignedByte();
					outlen-=ByteLength;
					switch(typ){
					case 0:
						g = in.readInt();
						outlen-=IntLength;
						break;
					case 1:
						s = in.readByte();
						outlen-=ByteLength;
						break;
					}
				}
				ResUserinfo resus = new ResUserinfo(status3,u,g,s);
				msg.obj = resus;
				msg.what = 3;
				recall.sendMessage(msg);
				break;
			}
			return 0;

		} catch (SocketTimeoutException e){
			System.out.println("Time out!");
			return 1;			
		} catch (IOException e) {
			throw e;
		} 
	}

	public void closeSocket() throws IOException{
		try {
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	@SuppressLint("DefaultLocale")
	private static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
				int pos = i * 2;
				d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}
	
	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

}