From 772e758cf0b992517dd7435d6ba7a32a9901cf0b Mon Sep 17 00:00:00 2001 From: Goblin911 Date: Mon, 26 Aug 2013 15:17:52 +0800 Subject: add error token jump! --- client/Piztor/src/com/macaroon/piztor/AppMgr.java | 21 ++++++--- client/Piztor/src/com/macaroon/piztor/InitAct.java | 1 + client/Piztor/src/com/macaroon/piztor/Login.java | 8 ++-- client/Piztor/src/com/macaroon/piztor/Main.java | 15 +++++- .../src/com/macaroon/piztor/ReqUserinfo.java | 11 +++-- .../src/com/macaroon/piztor/ResUserinfo.java | 41 ++++++++-------- .../Piztor/src/com/macaroon/piztor/Settings.java | 2 +- .../src/com/macaroon/piztor/SocketClient.java | 30 ++++++------ client/Piztor/src/com/macaroon/piztor/Transam.java | 54 +++++++++++++--------- .../Piztor/src/com/macaroon/piztor/UserInfo.java | 3 +- 10 files changed, 106 insertions(+), 80 deletions(-) diff --git a/client/Piztor/src/com/macaroon/piztor/AppMgr.java b/client/Piztor/src/com/macaroon/piztor/AppMgr.java index 2e5e2b3..e86bf3e 100644 --- a/client/Piztor/src/com/macaroon/piztor/AppMgr.java +++ b/client/Piztor/src/com/macaroon/piztor/AppMgr.java @@ -9,22 +9,23 @@ import android.os.Handler; @SuppressLint("UseSparseArrays") public class AppMgr { // Status - public enum ActivityStatus{ + public enum ActivityStatus { create, start, resume, restart, stop, pause, destroy - } + } + static ActivityStatus status; static PiztorAct nowAct; - //TODO fix + // TODO fix static Handler fromTransam, fromGPS; static Transam transam = null; static Tracker tracker = null; static Thread tTransam, tGPS; - //Event - + // Event + final static int noToken = 101; final static int loginSuccess = 102; - - + final static int errorToken = 103; + final static int hasToken = 104; static HashMap, HashMap>> mp; static void setStatus(ActivityStatus st) { @@ -39,6 +40,8 @@ public class AppMgr { else if (mp.get(nowAct.getClass()) == null) System.out.println("second"); i.setClass(nowAct, mp.get(nowAct.getClass()).get(event)); + if (event == errorToken) + UserInfo.token = null; nowAct.startActivity(i); } @@ -84,6 +87,10 @@ public class AppMgr { addStatus(Main.class); addTransition(InitAct.class, noToken, Login.class); addTransition(Login.class, loginSuccess, Main.class); + addTransition(Main.class, errorToken, Login.class); + addTransition(Settings.class, errorToken, Login.class); + addTransition(InitAct.class, hasToken, Main.class); + addTransition(InitAct.class, errorToken, Login.class); } } diff --git a/client/Piztor/src/com/macaroon/piztor/InitAct.java b/client/Piztor/src/com/macaroon/piztor/InitAct.java index bd4f5f4..7eba1e3 100644 --- a/client/Piztor/src/com/macaroon/piztor/InitAct.java +++ b/client/Piztor/src/com/macaroon/piztor/InitAct.java @@ -22,6 +22,7 @@ public class InitAct extends PiztorAct { AppMgr.trigger(AppMgr.noToken); else { //TODO jump to main + AppMgr.trigger(AppMgr.hasToken); } } diff --git a/client/Piztor/src/com/macaroon/piztor/Login.java b/client/Piztor/src/com/macaroon/piztor/Login.java index d095d1c..4ddd603 100644 --- a/client/Piztor/src/com/macaroon/piztor/Login.java +++ b/client/Piztor/src/com/macaroon/piztor/Login.java @@ -20,13 +20,15 @@ public class Login extends PiztorAct { Handler hand = new Handler() { @Override public void handleMessage(Message m) { - System.out.println(m.what); + System.out.println("receive what : " + m.what); if (m.what == 0) { ResLogin res = (ResLogin) m.obj; UserInfo.token = res.t; + UserInfo.id = res.uid; UserInfo.username = edtUser.getText().toString(); + System.out.println(res.s + " :!!! " + res.t); actMgr.trigger(AppMgr.loginSuccess); - } else if (m.what == 101) { + } else { actMgr.trigger(loginFailed); } } @@ -51,7 +53,7 @@ public class Login extends PiztorAct { String pass = edtPass.getText().toString(); long nowtime = System.currentTimeMillis(); System.out.println(user + " : " + pass + "\n"); - AppMgr.transam.send(new ReqLogin(user, pass, nowtime, 1000)); + AppMgr.transam.send(new ReqLogin(user, pass, nowtime, 10000)); } @Override diff --git a/client/Piztor/src/com/macaroon/piztor/Main.java b/client/Piztor/src/com/macaroon/piztor/Main.java index 17a8b1e..573c753 100644 --- a/client/Piztor/src/com/macaroon/piztor/Main.java +++ b/client/Piztor/src/com/macaroon/piztor/Main.java @@ -49,8 +49,10 @@ public class Main extends PiztorAct { ResUpdate update = (ResUpdate) m.obj; if (update.t == 0) System.out.println("update success"); - else + else { System.out.println("update failed"); + actMgr.trigger(AppMgr.errorToken); + } break; case 2: ResLocation location = (ResLocation) m.obj; @@ -64,14 +66,17 @@ public class Main extends PiztorAct { } else { System.out .println("resquest for location must be wrong!!!"); + actMgr.trigger(AppMgr.errorToken); } break; case 3: ResUserinfo r = (ResUserinfo) m.obj; if (r.s == 0) { - System.out.println(r.id + " " + r.sex + " " + r.groupId); + System.out.println("id : " + r.uid + " sex : " + r.sex + + " group : " + r.gid); } else { System.out.println("reqest for userInfo must be wrong!!!"); + actMgr.trigger(AppMgr.errorToken); } break; default: @@ -110,6 +115,12 @@ public class Main extends PiztorAct { @Override void enter(int e) { System.out.println("enter start status!!!!"); + if (e == ActMgr.Create) { + AppMgr.transam.send(new ReqUserinfo(UserInfo.token, + UserInfo.username, UserInfo.id, System + .currentTimeMillis(), 5000)); + } + if (e == TimerFlush) { ReqLocation r = new ReqLocation(UserInfo.token, UserInfo.username, 1, System.currentTimeMillis(), 1000); diff --git a/client/Piztor/src/com/macaroon/piztor/ReqUserinfo.java b/client/Piztor/src/com/macaroon/piztor/ReqUserinfo.java index 9ae0cb5..77bcf84 100644 --- a/client/Piztor/src/com/macaroon/piztor/ReqUserinfo.java +++ b/client/Piztor/src/com/macaroon/piztor/ReqUserinfo.java @@ -4,11 +4,12 @@ package com.macaroon.piztor; // Ask user info // //--------------------------------------// -public class ReqUserinfo extends Req { - int uid; // user id - - ReqUserinfo(String token, String name, int id, long time, long alive) { - super(3, token, name, time, alive); // for type 3 +public class ReqUserinfo extends Req{ + int uid; //user id + + + ReqUserinfo(String token,String name,int id,long time,long alive){ + super(3,token,name,time,alive); //for type 3 uid = id; } } \ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ResUserinfo.java b/client/Piztor/src/com/macaroon/piztor/ResUserinfo.java index abd5e62..ed981b4 100644 --- a/client/Piztor/src/com/macaroon/piztor/ResUserinfo.java +++ b/client/Piztor/src/com/macaroon/piztor/ResUserinfo.java @@ -1,21 +1,20 @@ -package com.macaroon.piztor; - -import java.util.Vector; - -//--------------------------------------// -// Respond to User Info // -//--------------------------------------// - -public class ResUserinfo extends Res{ - int sex; //1 for male, 0 for female - int groupId; - int id; - - - Vector l; //user info - - ResUserinfo(int status,Vector userinfo){ - super(3,status); //for type 3 - l = userinfo; - } -} +package com.macaroon.piztor; + + +//--------------------------------------// +// Respond to User Info // +//--------------------------------------// + +public class ResUserinfo extends Res{ + int uid; //userid + int gid; //groupid + int sex; //type 0 for female,type 1 for male + + + ResUserinfo(int status,int u,int g,int s){ + super(3,status); //for type 3 + uid = u; + gid = g; + sex =s; + } +} \ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/Settings.java b/client/Piztor/src/com/macaroon/piztor/Settings.java index 292a112..94c6a94 100644 --- a/client/Piztor/src/com/macaroon/piztor/Settings.java +++ b/client/Piztor/src/com/macaroon/piztor/Settings.java @@ -4,7 +4,7 @@ import android.os.Bundle; import android.app.Activity; import android.view.Menu; -public class Settings extends Activity { +public class Settings extends PiztorAct { @Override protected void onCreate(Bundle savedInstanceState) { diff --git a/client/Piztor/src/com/macaroon/piztor/SocketClient.java b/client/Piztor/src/com/macaroon/piztor/SocketClient.java index 3bc7ef3..812de05 100644 --- a/client/Piztor/src/com/macaroon/piztor/SocketClient.java +++ b/client/Piztor/src/com/macaroon/piztor/SocketClient.java @@ -5,9 +5,9 @@ import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; +import java.util.Locale; import java.util.Vector; -import android.annotation.SuppressLint; import android.os.Handler; import android.os.Message; @@ -36,12 +36,13 @@ public class SocketClient { ReqLogin rau = (ReqLogin) req; String id = rau.user; String pa = rau.pass; - len = 4+1+id.length()+1+pa.length(); + len = 4+1+id.length()+1+pa.length()+1; 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; @@ -136,29 +137,25 @@ public class SocketClient { break; case 3: int status3 = in.readUnsignedByte(); - Vector tmpu = new Vector(); + outlen-=6; + ReqUserinfo rus = (ReqUserinfo) req; + int u = rus.uid; + int g = 0,s = 0; while(outlen>0){ - RUserinfo r; int typ = in.readUnsignedByte(); outlen-=1; switch(typ){ case 0: - int gid = in.readInt(); - in.readByte(); - r = new RKeyGroupID(gid); - outlen-=5; - tmpu.add(r); + g = in.readInt(); + outlen-=4; break; case 1: - boolean s = in.readBoolean(); - in.readByte(); - r = new RKeyGender(s); - outlen-=2; - tmpu.add(r); + s = in.readByte(); + outlen-=1; break; } } - ResUserinfo resus = new ResUserinfo(status3,tmpu); + ResUserinfo resus = new ResUserinfo(status3,u,g,s); msg.obj = resus; msg.what = 3; recall.sendMessage(msg); @@ -179,12 +176,11 @@ public class SocketClient { } } - @SuppressLint("DefaultLocale") private static byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } - hexString = hexString.toUpperCase(); + hexString = hexString.toUpperCase(Locale.CHINA); int length = hexString.length() / 2; char[] hexChars = hexString.toCharArray(); byte[] d = new byte[length]; diff --git a/client/Piztor/src/com/macaroon/piztor/Transam.java b/client/Piztor/src/com/macaroon/piztor/Transam.java index 481a676..fae9252 100644 --- a/client/Piztor/src/com/macaroon/piztor/Transam.java +++ b/client/Piztor/src/com/macaroon/piztor/Transam.java @@ -24,11 +24,7 @@ import android.os.Message; // // // ----------I'm the division line-------- // // // -// 100 for timeOut // -// 101 for conection failed // -// 102 for reconnect // -// 103 for IOexception // -// 104 for UnknownHostException // +// -1 for Exceptions // // // // ----------I'm the division line-------- // // // @@ -46,9 +42,7 @@ import android.os.Message; // getlocation -- status & entrynumber & data // // entry -- userid & latitude & longitude // // // -// getuserinfo -- status & RUserinfo(base class) // -// 0 RKeyGroupID -- groupid // -// 1 RKeyGender -- gender // +// getuserinfo -- status & uid & gid & gender // // // // status -- 0 for success // // 1 for failed/invalid // @@ -77,7 +71,6 @@ public class Transam implements Runnable { reqtask = new LinkedList(); } - public void send(Req r){ reqtask.offer(r); @@ -100,8 +93,9 @@ public class Transam implements Runnable { req = reqtask.poll(); if(req.time + req.alive < System.currentTimeMillis()){ //time out! Message ret = new Message(); - ret.obj = "Time out!"; - ret.what = 100; + TimeOutException t = new TimeOutException(); + ret.obj = t; + ret.what = -1; recall.sendMessage(ret); } else{ //run the request @@ -113,7 +107,7 @@ public class Transam implements Runnable { thread.start(); timer = new Timer(); TimerTask task = new Timertk(); - timer.schedule(task, 2000, 2000); + timer.schedule(task, 20000, 20000); } } } @@ -138,14 +132,14 @@ public class Transam implements Runnable { } catch (UnknownHostException e) { e.printStackTrace(); Message msg = new Message(); - msg.obj = "UnknownHostException"; - msg.what = 104; + msg.obj = e; + msg.what = -1; recall.sendMessage(msg); } catch (IOException e) { e.printStackTrace(); Message msg = new Message(); - msg.obj = "IOException"; - msg.what = 103; + msg.obj = e; + msg.what = -1; recall.sendMessage(msg); } @@ -161,6 +155,7 @@ public class Transam implements Runnable { break; case 2: final thd t = new thd(); + thread.interrupt(); thread = new Thread(t); thread.start(); break; @@ -173,17 +168,14 @@ public class Transam implements Runnable { public void run() { if (flag == false && cnt > 0) { cnt--; - Message msg = new Message(); - msg.obj = "Reconnect for rest " + cnt + " times"; - msg.what = 102; - recall.sendMessage(msg); Message m = new Message(); - msg.what = 2; + m.what = 2; handler.sendMessage(m); } else if (cnt == 0) { Message msg = new Message(); - msg.obj = "connecting failed"; - msg.what = 101; + ConnectFailedException c = new ConnectFailedException(); + msg.obj = c; + msg.what = -1; recall.sendMessage(msg); timer.cancel(); } else if (flag == true) { @@ -192,4 +184,20 @@ public class Transam implements Runnable { } } }; + + 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(); + } + + } + } \ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/UserInfo.java b/client/Piztor/src/com/macaroon/piztor/UserInfo.java index 3d163d5..63f413e 100644 --- a/client/Piztor/src/com/macaroon/piztor/UserInfo.java +++ b/client/Piztor/src/com/macaroon/piztor/UserInfo.java @@ -2,7 +2,8 @@ package com.macaroon.piztor; public class UserInfo { static String ip = "69.85.86.42"; - static int port = 9990; + static int port = 2222; static String token = null; static String username = null; + static int id = -1; } -- cgit v1.2.3