summaryrefslogtreecommitdiff
path: root/client/Piztor/src/com/macaroon/piztor/PushClient.java
blob: ea32ad6004d20b4dd597c0ec20041d3a4579bae2 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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.Vector;

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



public class PushClient {
	static Socket client;
	static Handler recall;
	
	static final int ByteLength = 1;
	static final int IntLength = 4;
	static final int DoubleLength = 8;
	static final int TokenLength = 32;
	static final int FingerPrintLength = 32;

	public final static int StartPush =5;
	
	public final static int Message = 0;
	public final static int Location = 1;
	public final static int PushMessage =100;
	public final static int PushLocation =101;
	
	public final static int Reconnect =-2;
	
	public final static int Failed = 2;
	public final static int TimeOut = 1;
	public final static int Success = 0;
	
	private String LastPrint = "";

	
	public PushClient(String site, int port,int retime) throws UnknownHostException,
			IOException {
		try {
			client = new Socket();
			client.connect(new InetSocketAddress(site,port), 5000);
			client.setSoTimeout(5000);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			throw e;
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} 
	}
	
	public void setPushHandler(Handler handler) {
		recall = handler;
	}
	
	public int start(ReqStartPush r) throws IOException,SocketTimeoutException {
		try {			
			DataOutputStream out = new DataOutputStream(client.getOutputStream());
			DataInputStream in = new DataInputStream(client.getInputStream());
			int len;
			len = IntLength+ByteLength+TokenLength+(r.uname).length()+ByteLength;	
			byte[] b = new byte[len];
			int pos = 0;
			write(b,intToBytes(len),pos);
			pos+=IntLength;
			b[pos] = (byte) 5;
			pos+=ByteLength;
			write(b,hexStringToBytes(r.token),pos);
			pos+=TokenLength;
			write(b,r.uname.getBytes(),pos);
			pos+=r.uname.length();
			b[pos] = 0;
			pos+=ByteLength;
			out.write(b);
			out.flush();
			Message msg = new Message();
			in.readInt();
			in.readUnsignedByte();
		    int status = in.readUnsignedByte();
		    ResStartPush rchk = new ResStartPush(status);
			msg.obj = rchk;
			msg.what = StartPush;
			recall.sendMessage(msg);
			if(status == 1) {
				return Failed;
			}
			return Success;
		} catch (SocketTimeoutException e){
			e.printStackTrace();
			return TimeOut;			
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} 
	}
	
	public boolean isClosed() {
		return client.isClosed();
	}

	public void listen(Handler recall,Handler h) throws IOException{
		client.setSoTimeout(0);
		DataInputStream in = new DataInputStream(client.getInputStream());
		DataOutputStream out = new DataOutputStream(client.getOutputStream());
		
		while(true){
			try {
				int len = in.readInt();
				System.out.println(len);
				int tmp = in.readUnsignedByte();
				byte[] buffer = new byte[32];
				in.read(buffer);
				String p = byteToHexString(buffer);
				int outlen;
				int pos=0;
				byte[] o = new byte[IntLength+ByteLength+FingerPrintLength];;
				outlen = IntLength+ByteLength+FingerPrintLength;
				switch(tmp) {
				case Message:				
					byte[] b = new byte[len-IntLength-ByteLength-FingerPrintLength-ByteLength];
					in.read(b);
					String m = new String(b);
					in.readUnsignedByte();
					if(LastPrint != p) {
					Message msg = new Message();
					msg.what = PushMessage;
					msg.obj = new ResPushMessage(m);
					recall.sendMessage(msg);
					LastPrint = p;
					}			
					write(o,intToBytes(outlen),pos);				//can be folded!
					pos+=IntLength;
					o[pos]=(byte) Message;
					pos+=ByteLength;
					write(o,hexStringToBytes(p),pos);
					pos+=FingerPrintLength;
					out.write(o);
					out.flush();
					break;
				case Location:
					len-=(IntLength+ByteLength+FingerPrintLength);
					int n=0;
					Vector<RLocation> tmpv = new Vector<RLocation>();
					while(len > 0) {
						int tid = in.readInt();
						double lat = in.readDouble();
						double lot = in.readDouble();
						tmpv.add(new RLocation(tid,lat,lot));
						len -= (IntLength+DoubleLength+DoubleLength);
						n++;
					}
					if(LastPrint != p) {
						Message msg = new Message();
						msg.obj = new ResPushLocation(n,tmpv);
						msg.what = PushLocation;
						recall.sendMessage(msg);
						LastPrint = p;
					}				
					write(o,intToBytes(outlen),pos);
					pos+=IntLength;
					o[pos]=(byte) Location;
					pos+=ByteLength;
					write(o,hexStringToBytes(p),pos);
					pos+=FingerPrintLength;
					out.write(o);
					out.flush();
					break;
				}		
			
			} catch (IOException e) {
			e.printStackTrace();
			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);
	}
	
	private static byte[] intToBytes(int i) {   
		  byte[] d = new byte[4];   
		  d[0] = (byte)((i >> 24) & 0xFF);
		  d[1] = (byte)((i >> 16) & 0xFF);
		  d[2] = (byte)((i >> 8) & 0xFF); 
		  d[3] = (byte)(i & 0xFF);
		  return d;
	}
	
	@SuppressLint("UseValueOf")
	public static byte[] doubleToBytes(double d){
		  byte[] b=new byte[8];
		  long l=Double.doubleToLongBits(d);
		  for(int i=0;i < 8;i++){
		   b[i]=new Long(l).byteValue();
		   l=l>>8;
		  }
		  return b;
	}
	
	private static void write(byte[] s,byte[] w,int l) {