summaryrefslogtreecommitdiff
path: root/client/Piztor/src/com/macaroon/piztor/Transam.java
blob: 53750d5ed474fcf506e4fd251b3adce29699e559 (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
package com.macaroon.piztor;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Timer;

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

//       Piztor Transmission Protocol v0.4        //

//------------------------------------------------//
//												  //
//				*return msg type*			      //
//				0   for   login					  //
//              1   for   updateLocation		  //
//              2   for   locationRequest		  //
//			    3   for   userinfo				  //
//											      //
//     ----------I'm the division line--------    //
//                                                //
//             -1   for   Exceptions			  //
//                                                //
//    ----------I'm the division line--------     //
//                                                //
//				 *Request form*					  //
//        login -- username & password			  //
//update -- token & username & latitude & longitude//
//	  getlocation -- token & username & groupid   //
//    getuserinfo -- token & userinfo & userid    //
//												  //
//    ----------I'm the division line--------     //
//                                                //
//               *Respond form*					  //
//        login -- status & userid & token        //
//			  update -- status                    //
//     getlocation -- status & entrynumber & data //
//       entry  -- userid & latitude & longitude  //
//                                                //
//  getuserinfo -- status & uid & gid & gender    //
//												  //
//          status -- 0 for success               //
//					  1 for failed/invalid        //				
//												  //
//------------------------------------------------//

public class Transam implements Runnable {
	public Timer timer;
	public boolean running = false; 
	public boolean flag = true;
	public int cnt = 4;				//retry times
	public int tcnt;				//current remain retry times
	public int retime = 1000;		//timeout time
	Res res;
	Req req;
	public int p;					//port
	public String i;				//ip
	Thread thread;
	Handler core;
	Handler recall;					//recall
	Queue<Req> reqtask ;			//request task

	Transam(String ip, int port,Handler Recall) {
		p = port;
		i = ip;
		recall = Recall;
		reqtask = new LinkedList<Req>();
	}
	
	public void send(Req r){
		reqtask.offer(r);
		
	}
	
	public void setTimeOutTime(int msec){
		retime = msec;
	}
	
	public void setRetryTimes(int times){
		cnt = times;
	}
	
	
	public void setHandler(Handler Recall){
		recall = Recall;
		reqtask.clear();
	}

	public void run() {								//start the main thread		
		while(true){
			if(running == false){
				
				if(!reqtask.isEmpty()){				//poll the head request
					req = reqtask.poll();	
					if(req.time + req.alive < System.currentTimeMillis()){		//time out!
						Message ret = new Message();
						TimeOutException t = new TimeOutException();
						ret.obj = t;
						ret.what = -1;
						recall.sendMessage(ret);
					}
					else{	                        //run the request
						running = true;
						tcnt = cnt;
						connect();
					}
				}				
			}
		}
	}
	
	private void connect(){
		final thd t = new thd();
		thread = new Thread(t);
		thread.start();
	}

	class thd implements Runnable {
		public void run() {
			try {
				SocketClient client = new SocketClient(i,p,retime);
				int out = client.sendMsg(req,recall);
				if(out == 0){
					client.closeSocket();
					running = false;
				}
				else {
					client.closeSocket();
					Message msg = new Message();
					msg.what = 0;
					handler.sendMessage(msg);
				}				
			} catch (UnknownHostException e) {
				e.printStackTrace();
				Message msg = new Message();
				msg.what = -1;
				msg.obj = e;
				handler.sendMessage(msg);
			} catch (IOException e) {
				e.printStackTrace();
				Message msg = new Message();
				msg.what = -1;
				msg.obj = e;
				handler.sendMessage(msg);
			}

		}
	}

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case -1:
				if (tcnt > 0) {
					tcnt--;
					System.out.println(tcnt);
					connect();
				} else if (tcnt == 0) {
					Message m = new Message();
					m.obj = msg.obj;
					m.what = -1;
					recall.sendMessage(m);
					running = false;
				}
				break;
			case 0:
				if (tcnt > 0) {
					tcnt--;
					connect();
				} else if (tcnt == 0) {
					Message m = new Message();
					ConnectFailedException c = new ConnectFailedException();
					m.obj = c;
					m.what = -1;
					recall.sendMessage(m);
					running = false;
				}
				break;
			}
			super.handleMessage(msg);
		}
	};
	
	class ConnectFailedException extends Exception{
		private static final long serialVersionUID = 101L;
		public ConnectFailedException() {  
			super();  
			}		
	}
	
	class TimeOutException extends Exception{
		private static final long serialVersionUID = 102L;
		public TimeOutException() {  
			super();  
			}	
		
	}
	
	class JavaHostException extends Exception{
		private static final long serialVersionUID = 103L;
		public JavaHostException() {  
			super();  
			}	
		
	}
	
}