summaryrefslogtreecommitdiff
path: root/client/Piztor/src/com/macaroon/piztor/Transam.java
diff options
context:
space:
mode:
Diffstat (limited to 'client/Piztor/src/com/macaroon/piztor/Transam.java')
-rw-r--r--client/Piztor/src/com/macaroon/piztor/Transam.java224
1 files changed, 193 insertions, 31 deletions
diff --git a/client/Piztor/src/com/macaroon/piztor/Transam.java b/client/Piztor/src/com/macaroon/piztor/Transam.java
index f77df39..7902fde 100644
--- a/client/Piztor/src/com/macaroon/piztor/Transam.java
+++ b/client/Piztor/src/com/macaroon/piztor/Transam.java
@@ -20,19 +20,25 @@ import android.os.Message;
// 2 for locationRequest //
// 3 for userinfo //
// 4 for logout //
+// 5 for requestpush //
+// 6 for sendmessage //
+// //
+// 100 for pushmessage //
// //
// ----------I'm the division line-------- //
// //
// -1 for Exceptions //
+// Exception (req type , exception type) //
// //
// ----------I'm the division line-------- //
// //
// *Request form* //
// login -- username & password //
//update -- token & username & latitude & longitude//
-// getlocation -- token & username & groupid //
+//getlocation -- token & username & company & section//
// getuserinfo -- token & userinfo & userid //
// logout -- token & username //
+// send message -- token & username & message //
// //
// ----------I'm the division line-------- //
// //
@@ -42,21 +48,50 @@ import android.os.Message;
// getlocation -- status & entrynumber & data //
// entry -- userid & latitude & longitude //
// //
-// getuserinfo -- status & uid & gid & gender //
-// logout -- status //
+//getuserinfo -- status & uid & company & section & gender//
+// logout -- status //
+// request push -- status //
+// send message -- status //
// //
// status -- 0 for success //
// 1 for failed/invalid //
// //
+// push message -- message //
+// //
//------------------------------------------------//
+
+
public class Transam implements Runnable {
+
+ public final static int Login =0;
+ public final static int Update =1;
+ public final static int Location =2;
+ public final static int UserInfo =3;
+ public final static int Logout =4;
+ public final static int StartPush =5;
+ public final static int SendMessage =6;
+
+ public final static int PushMessage =100;
+ public final static int PushLocation =101;
+
+ public final static int GroupID =0;
+ public final static int Gender =1;
+
+ public final static int EConnectedFailedException =101;
+ public final static int ETimeOutException =102;
+ public final static int EJavaHostException =103;
+ public final static int EPushFailedException =104;
+ public final static int EIOException =105;
+ public final static int EUnknownHostException =106;
+
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 = 10000; //timeout time
+ public int rcnt; //current remain retry times (push)
+ public int retime = 2000; //timeout time
Res res;
Req req;
public int p; //port
@@ -65,6 +100,17 @@ public class Transam implements Runnable {
Handler core;
Handler recall; //recall
Queue<Req> reqtask ; //request task
+
+ public String itoken;
+ public String iname;
+
+ Thread Pushthread;
+ PushClient push;
+
+
+ public final static int Reconnect =-2;
+ public final static int Exception =-1;
+ public final static int TimeOut =0;
Transam(String ip, int port,Handler Recall) {
p = port;
@@ -78,6 +124,29 @@ public class Transam implements Runnable {
}
+ public void startPush(String token,String name) {
+ itoken = token;
+ iname = name;
+ rcnt = cnt;
+ connectpush();
+ }
+
+ public void stopPush() {
+ try{
+ if(push.isClosed() == false) {
+ push.closeSocket();
+ itoken = null;
+ iname = null;
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ Message msg = new Message();
+ msg.what = Exception;
+ msg.obj = new EIOException(5,0);
+ recall.sendMessage(msg);
+ }
+ }
+
public void setTimeOutTime(int msec){
retime = msec;
}
@@ -89,8 +158,13 @@ public class Transam implements Runnable {
public void setHandler(Handler Recall){
recall = Recall;
+ if(push != null) {
+ push.setPushHandler(Recall);
+ }
reqtask.clear();
}
+
+
public void run() { //start the main thread
while(true){
@@ -100,9 +174,9 @@ public class Transam implements Runnable {
req = reqtask.poll();
if(req.time + req.alive < System.currentTimeMillis()){ //time out!
Message ret = new Message();
- TimeOutException t = new TimeOutException();
+ ETimeOutException t = new ETimeOutException(req.type,req.time);
ret.obj = t;
- ret.what = -1;
+ ret.what = Exception;
recall.sendMessage(ret);
}
else{ //run the request
@@ -120,6 +194,49 @@ public class Transam implements Runnable {
thread = new Thread(t);
thread.start();
}
+
+ private void connectpush() {
+ reqpush r = new reqpush();
+ Pushthread = new Thread(r);
+ Pushthread.start();
+ }
+
+ class reqpush implements Runnable {
+ public void run() {
+ try {
+ if(itoken == null || iname == null) return;
+ push = new PushClient(i,p,retime);
+ push.setPushHandler(recall);
+ int out = push.start(new ReqStartPush(itoken,iname));
+ if(out == 1) {
+ push.closeSocket();
+ Message msg = new Message();
+ msg.what = Reconnect;
+ msg.obj = new ETimeOutException(5,0);
+ handler.sendMessage(msg);
+ }
+ else if (out == 2){
+ stopPush();
+ }
+ else {
+ push.listen(recall,handler);
+ }
+ } catch (UnknownHostException e) {
+ e.printStackTrace();
+ Message msg = new Message();
+ msg.what = Reconnect;
+ msg.obj = new EUnknownHostException(5,0);
+ handler.sendMessage(msg);
+ } catch (IOException e) {
+ e.printStackTrace();
+ Message msg = new Message();
+ msg.what = Reconnect;
+ msg.obj = new EIOException(5,0);
+ handler.sendMessage(msg);
+ }
+
+ }
+ }
class thd implements Runnable {
public void run() {
@@ -132,21 +249,23 @@ public class Transam implements Runnable {
}
else {
client.closeSocket();
- Message msg = new Message();
- msg.what = 0;
- handler.sendMessage(msg);
+ Message m = new Message();
+ EConnectFailedException c = new EConnectFailedException(req.type,req.time);
+ m.obj = c;
+ m.what = Exception;
+ handler.sendMessage(m);
}
} catch (UnknownHostException e) {
e.printStackTrace();
Message msg = new Message();
- msg.what = -1;
- msg.obj = e;
+ msg.what = Exception;
+ msg.obj = new EUnknownHostException(req.type,req.time);
handler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
Message msg = new Message();
- msg.what = -1;
- msg.obj = e;
+ msg.what = Exception;
+ msg.obj = new EIOException(req.type,req.time);
handler.sendMessage(msg);
}
@@ -157,7 +276,7 @@ public class Transam implements Runnable {
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
- case -1:
+ case Exception:
if (tcnt > 0) {
tcnt--;
System.out.println(tcnt);
@@ -165,50 +284,93 @@ public class Transam implements Runnable {
} else if (tcnt == 0) {
Message m = new Message();
m.obj = msg.obj;
- m.what = -1;
+ m.what = Exception;
recall.sendMessage(m);
running = false;
}
break;
- case 0:
+ case TimeOut:
if (tcnt > 0) {
tcnt--;
connect();
} else if (tcnt == 0) {
Message m = new Message();
- ConnectFailedException c = new ConnectFailedException();
+ EConnectFailedException c = new EConnectFailedException(req.type,req.time);
m.obj = c;
- m.what = -1;
+ m.what = Exception;
recall.sendMessage(m);
running = false;
}
break;
+ case Reconnect:
+ if (rcnt > 0) {
+ rcnt--;
+ connectpush();
+ } else if (rcnt == 0) {
+ Message m = new Message();
+ //EPushFailedException c = new EPushFailedException(req.type);
+ m.obj = msg.obj;
+ m.what = Exception;
+ recall.sendMessage(m);
+ }
+ break;
}
super.handleMessage(msg);
}
};
- class ConnectFailedException extends Exception{
+ class EException extends Exception {
+ private static final long serialVersionUID = 100L;
+ int Rtype;
+ int Etype;
+ long time;
+ public EException(int e,int r,long timep) {
+ super();
+ Rtype = r;
+ Etype = e;
+ time = timep;
+ }
+ }
+
+ class EConnectFailedException extends EException{
private static final long serialVersionUID = 101L;
- public ConnectFailedException() {
- super();
+ public EConnectFailedException(int t,long timep) {
+ super(101,t,timep);
}
}
- class TimeOutException extends Exception{
+ class ETimeOutException extends EException{
private static final long serialVersionUID = 102L;
- public TimeOutException() {
- super();
- }
-
+ public ETimeOutException(int t,long timep) {
+ super(102,t,timep);
+ }
}
- class JavaHostException extends Exception{
+ class EJavaHostException extends EException{
private static final long serialVersionUID = 103L;
- public JavaHostException() {
- super();
- }
-
+ public EJavaHostException(int t,long timep) {
+ super(103,t,timep);
+ }
}
+ class EPushFailedException extends EException{
+ private static final long serialVersionUID = 104L;
+ public EPushFailedException(int t,long timep) {
+ super(104,t,timep);
+ }
+ }
+
+ class EIOException extends EException{
+ private static final long serialVersionUID = 105L;
+ public EIOException(int t,long timep) {
+ super(105,t,timep);
+ }
+ }
+
+ class EUnknownHostException extends EException{
+ private static final long serialVersionUID = 106L;
+ public EUnknownHostException(int t,long timep) {
+ super(106,t,timep);
+ }
+ }
} \ No newline at end of file