From 686cf083c79fce3a032d229bb1b7ea8ed49046c0 Mon Sep 17 00:00:00 2001 From: Goblin911 Date: Mon, 26 Aug 2013 11:16:39 +0800 Subject: add new network, but userinfo need to rewrite. --- client/Piztor/src/com/macaroon/piztor/InitAct.java | 2 +- client/Piztor/src/com/macaroon/piztor/Login.java | 1 + client/Piztor/src/com/macaroon/piztor/Main.java | 51 +++- client/Piztor/src/com/macaroon/piztor/Req.java | 28 +- .../src/com/macaroon/piztor/ReqLocation.java | 26 +- .../Piztor/src/com/macaroon/piztor/ReqLogin.java | 30 +- .../Piztor/src/com/macaroon/piztor/ReqUpdate.java | 30 +- .../src/com/macaroon/piztor/ReqUserinfo.java | 14 + client/Piztor/src/com/macaroon/piztor/Res.java | 22 +- .../src/com/macaroon/piztor/ResLocation.java | 34 +-- .../Piztor/src/com/macaroon/piztor/ResLogin.java | 30 +- .../Piztor/src/com/macaroon/piztor/ResUpdate.java | 24 +- .../src/com/macaroon/piztor/ResUserinfo.java | 21 ++ .../Piztor/src/com/macaroon/piztor/Rlocation.java | 24 +- .../src/com/macaroon/piztor/SocketClient.java | 309 ++++++++++++------- client/Piztor/src/com/macaroon/piztor/Transam.java | 332 ++++++++++++--------- .../Piztor/src/com/macaroon/piztor/UserInfo.java | 3 +- 17 files changed, 597 insertions(+), 384 deletions(-) create mode 100644 client/Piztor/src/com/macaroon/piztor/ReqUserinfo.java create mode 100644 client/Piztor/src/com/macaroon/piztor/ResUserinfo.java diff --git a/client/Piztor/src/com/macaroon/piztor/InitAct.java b/client/Piztor/src/com/macaroon/piztor/InitAct.java index d5a6059..bd4f5f4 100644 --- a/client/Piztor/src/com/macaroon/piztor/InitAct.java +++ b/client/Piztor/src/com/macaroon/piztor/InitAct.java @@ -18,7 +18,7 @@ public class InitAct extends PiztorAct { @Override protected void onStart() { super.onStart(); - if (UserInfo.token == -1) + if (UserInfo.token == null) AppMgr.trigger(AppMgr.noToken); else { //TODO jump to main diff --git a/client/Piztor/src/com/macaroon/piztor/Login.java b/client/Piztor/src/com/macaroon/piztor/Login.java index a931dae..d095d1c 100644 --- a/client/Piztor/src/com/macaroon/piztor/Login.java +++ b/client/Piztor/src/com/macaroon/piztor/Login.java @@ -24,6 +24,7 @@ public class Login extends PiztorAct { if (m.what == 0) { ResLogin res = (ResLogin) m.obj; UserInfo.token = res.t; + UserInfo.username = edtUser.getText().toString(); actMgr.trigger(AppMgr.loginSuccess); } else if (m.what == 101) { actMgr.trigger(loginFailed); diff --git a/client/Piztor/src/com/macaroon/piztor/Main.java b/client/Piztor/src/com/macaroon/piztor/Main.java index 3df5d19..17a8b1e 100644 --- a/client/Piztor/src/com/macaroon/piztor/Main.java +++ b/client/Piztor/src/com/macaroon/piztor/Main.java @@ -33,8 +33,8 @@ public class Main extends PiztorAct { System.out.println("fuck!!!"); else { ReqUpdate r = new ReqUpdate(UserInfo.token, - l.getLatitude(), l.getLongitude(), - System.currentTimeMillis(), 1000); + UserInfo.username, l.getLatitude(), + l.getLongitude(), System.currentTimeMillis(), 1000); AppMgr.transam.send(r); } } @@ -45,21 +45,35 @@ public class Main extends PiztorAct { @Override public void handleMessage(Message m) { switch (m.what) { - case 3: - ResLocation location = (ResLocation) m.obj; - for (int i = 0; i < location.n; i++) { - System.out.println(location.l.get(i).lat + " " - + location.l.get(i).lot); - } - actMgr.trigger(SuccessFetch); - break; - case 2: + case 1: ResUpdate update = (ResUpdate) m.obj; if (update.t == 0) System.out.println("update success"); else System.out.println("update failed"); break; + case 2: + ResLocation location = (ResLocation) m.obj; + if (location.s == 0) { + for (int i = 0; i < location.n; i++) { + System.out.println(location.l.get(i).i + " : " + + location.l.get(i).lat + " " + + location.l.get(i).lot); + } + actMgr.trigger(SuccessFetch); + } else { + System.out + .println("resquest for location must be wrong!!!"); + } + break; + case 3: + ResUserinfo r = (ResUserinfo) m.obj; + if (r.s == 0) { + System.out.println(r.id + " " + r.sex + " " + r.groupId); + } else { + System.out.println("reqest for userInfo must be wrong!!!"); + } + break; default: break; } @@ -86,16 +100,23 @@ public class Main extends PiztorAct { } } + // TODO flush map view + void flushMap() { + + } + class StartStatus extends ActStatus { @Override void enter(int e) { System.out.println("enter start status!!!!"); if (e == TimerFlush) { - ReqLocation r = new ReqLocation(UserInfo.token, 1, - System.currentTimeMillis(), 1000); + ReqLocation r = new ReqLocation(UserInfo.token, + UserInfo.username, 1, System.currentTimeMillis(), 1000); AppMgr.transam.send(r); } + if (e == SuccessFetch) + flushMap(); } @Override @@ -111,8 +132,8 @@ public class Main extends PiztorAct { void enter(int e) { System.out.println("enter Fetch status!!!!"); if (e == FetchButtonPress) { - ReqLocation r = new ReqLocation(UserInfo.token, 1, - System.currentTimeMillis(), 1000); + ReqLocation r = new ReqLocation(UserInfo.token, + UserInfo.username, 1, System.currentTimeMillis(), 1000); AppMgr.transam.send(r); } } diff --git a/client/Piztor/src/com/macaroon/piztor/Req.java b/client/Piztor/src/com/macaroon/piztor/Req.java index 32988b8..ad84922 100644 --- a/client/Piztor/src/com/macaroon/piztor/Req.java +++ b/client/Piztor/src/com/macaroon/piztor/Req.java @@ -1,14 +1,16 @@ -package com.macaroon.piztor; - -public class Req{ - int type; //request type - int token; //authentciation - long time; //current time - long alive; //alive time - Req(int t,int k,long tm,long av){ - type = t; - token = k; - time = tm; - alive = av; - } +package com.macaroon.piztor; + +public class Req{ + int type; //request type + String token; //authentciation + String uname; //username + long time; //current time + long alive; //alive time + Req(int t,String k,String name,long tm,long av){ + type = t; + token = k; + uname = name; + time = tm; + alive = av; + } } \ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ReqLocation.java b/client/Piztor/src/com/macaroon/piztor/ReqLocation.java index 000f373..ee4824c 100644 --- a/client/Piztor/src/com/macaroon/piztor/ReqLocation.java +++ b/client/Piztor/src/com/macaroon/piztor/ReqLocation.java @@ -1,14 +1,14 @@ -package com.macaroon.piztor; - -//--------------------------------------// -// Ask Location // -//--------------------------------------// - -public class ReqLocation extends Req{ - int gid; //group id; - - ReqLocation(int token,int groupid,long time,long alive){ - super(3,token,time,alive); //for type 3 - gid = groupid; - } +package com.macaroon.piztor; + +//--------------------------------------// +// Ask Location // +//--------------------------------------// + +public class ReqLocation extends Req{ + int gid; //group id; + + ReqLocation(String token,String name,int groupid,long time,long alive){ + super(2,token,name,time,alive); //for type 2 + gid = groupid; + } } \ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ReqLogin.java b/client/Piztor/src/com/macaroon/piztor/ReqLogin.java index 294cae3..23aba93 100644 --- a/client/Piztor/src/com/macaroon/piztor/ReqLogin.java +++ b/client/Piztor/src/com/macaroon/piztor/ReqLogin.java @@ -1,16 +1,16 @@ -package com.macaroon.piztor; - -//--------------------------------------// -// authentication // -//--------------------------------------// - -public class ReqLogin extends Req{ - String user; //username - String pass; //password - - ReqLogin(String u,String p,long time,long alive){ - super(0,0,time,alive); //for type 0 - user = u; - pass = p; - } +package com.macaroon.piztor; + +//--------------------------------------// +// authentication // +//--------------------------------------// + +public class ReqLogin extends Req{ + String user; //username + String pass; //password + + ReqLogin(String u,String p,long time,long alive){ + super(0,"","",time,alive); //for type 0 + user = u; + pass = p; + } } \ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ReqUpdate.java b/client/Piztor/src/com/macaroon/piztor/ReqUpdate.java index e4b1497..b7b8fba 100644 --- a/client/Piztor/src/com/macaroon/piztor/ReqUpdate.java +++ b/client/Piztor/src/com/macaroon/piztor/ReqUpdate.java @@ -1,16 +1,16 @@ -package com.macaroon.piztor; - -//--------------------------------------// -// Update Location // -//--------------------------------------// - -public class ReqUpdate extends Req{ - double lat; //latitude - double lot; //longitude - - ReqUpdate(int token,double latitude,double longitude,long time,long alive){ - super(2,token,time,alive); //for type 2 - lat = latitude; - lot = longitude; - } +package com.macaroon.piztor; + +//--------------------------------------// +// Update Location // +//--------------------------------------// + +public class ReqUpdate extends Req{ + double lat; //latitude + double lot; //longitude + + ReqUpdate(String token,String name,double latitude,double longitude,long time,long alive){ + super(1,token,name,time,alive); //for type 1 + lat = latitude; + lot = longitude; + } } \ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ReqUserinfo.java b/client/Piztor/src/com/macaroon/piztor/ReqUserinfo.java new file mode 100644 index 0000000..9ae0cb5 --- /dev/null +++ b/client/Piztor/src/com/macaroon/piztor/ReqUserinfo.java @@ -0,0 +1,14 @@ +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 + uid = id; + } +} \ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/Res.java b/client/Piztor/src/com/macaroon/piztor/Res.java index 98af108..4cc98a9 100644 --- a/client/Piztor/src/com/macaroon/piztor/Res.java +++ b/client/Piztor/src/com/macaroon/piztor/Res.java @@ -1,11 +1,11 @@ -package com.macaroon.piztor; - -public class Res{ - int t; - int s; - Res(int type,int status){ - t = type; - s = status; - } -} - +package com.macaroon.piztor; + +public class Res{ + int t; + int s; + Res(int type,int status){ + t = type; + s = status; + } +} + diff --git a/client/Piztor/src/com/macaroon/piztor/ResLocation.java b/client/Piztor/src/com/macaroon/piztor/ResLocation.java index eb90840..f6622d5 100644 --- a/client/Piztor/src/com/macaroon/piztor/ResLocation.java +++ b/client/Piztor/src/com/macaroon/piztor/ResLocation.java @@ -1,18 +1,18 @@ -package com.macaroon.piztor; - -import java.util.Vector; - -//--------------------------------------// -// Location Info // -//--------------------------------------// - -public class ResLocation extends Res{ - Vector l; //vector for location info - int n; //number of location info - - ResLocation(int num,Vector locationvec){ - super(3,255); //for type 3 - l = locationvec; - n = num; - } +package com.macaroon.piztor; + +import java.util.Vector; + +//--------------------------------------// +// Location Info // +//--------------------------------------// + +public class ResLocation extends Res{ + Vector l; //vector for location info + int n; //number of location info + + ResLocation(int num,int status,Vector locationvec){ + super(2,status); //for type 2 + l = locationvec; + n = num; + } } \ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ResLogin.java b/client/Piztor/src/com/macaroon/piztor/ResLogin.java index 5b597a4..e2f138c 100644 --- a/client/Piztor/src/com/macaroon/piztor/ResLogin.java +++ b/client/Piztor/src/com/macaroon/piztor/ResLogin.java @@ -1,15 +1,17 @@ -package com.macaroon.piztor; - - -//--------------------------------------// -// Respond to login // -//--------------------------------------// - -public class ResLogin extends Res{ - int t; //user token - - ResLogin(int token,int status){ - super(0,status); //for type 0 - t = token; - } +package com.macaroon.piztor; + + +//--------------------------------------// +// Respond to login // +//--------------------------------------// + +public class ResLogin extends Res{ + String t; //user token + int uid; //userid + + ResLogin(int id,String token,int status){ + super(0,status); //for type 0 + t = token; + uid = id; + } } \ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ResUpdate.java b/client/Piztor/src/com/macaroon/piztor/ResUpdate.java index 727eaa3..dd0d29b 100644 --- a/client/Piztor/src/com/macaroon/piztor/ResUpdate.java +++ b/client/Piztor/src/com/macaroon/piztor/ResUpdate.java @@ -1,13 +1,13 @@ -package com.macaroon.piztor; - - -//--------------------------------------// -// Respond to update location // -//--------------------------------------// - -public class ResUpdate extends Res{ - - ResUpdate(int status){ - super(2,status); //for type 2 - } +package com.macaroon.piztor; + + +//--------------------------------------// +// Respond to update location // +//--------------------------------------// + +public class ResUpdate extends Res{ + + ResUpdate(int status){ + super(1,status); //for type 1 + } } \ 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 new file mode 100644 index 0000000..abd5e62 --- /dev/null +++ b/client/Piztor/src/com/macaroon/piztor/ResUserinfo.java @@ -0,0 +1,21 @@ +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; + } +} diff --git a/client/Piztor/src/com/macaroon/piztor/Rlocation.java b/client/Piztor/src/com/macaroon/piztor/Rlocation.java index 142cc9b..6ac9154 100644 --- a/client/Piztor/src/com/macaroon/piztor/Rlocation.java +++ b/client/Piztor/src/com/macaroon/piztor/Rlocation.java @@ -1,13 +1,13 @@ -package com.macaroon.piztor; - -class Rlocation{ - int i; //id - double lat; //latitude - double lot; //longitude - - Rlocation(int id,double latitude,double longitude){ - i = id; - lat = latitude; - lot = longitude; - } +package com.macaroon.piztor; + +class Rlocation{ + int i; //id + double lat; //latitude + double lot; //longitude + + Rlocation(int id,double latitude,double longitude){ + i = id; + lat = latitude; + lot = longitude; + } } \ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/SocketClient.java b/client/Piztor/src/com/macaroon/piztor/SocketClient.java index 18c19da..3bc7ef3 100644 --- a/client/Piztor/src/com/macaroon/piztor/SocketClient.java +++ b/client/Piztor/src/com/macaroon/piztor/SocketClient.java @@ -1,107 +1,202 @@ -package com.macaroon.piztor; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.net.Socket; -import java.net.UnknownHostException; -import java.util.Vector; - -import android.os.Handler; -import android.os.Message; - -public class SocketClient { - static Socket client; - - public SocketClient(String site, int port) throws UnknownHostException, - IOException { - try { - client = new Socket(site, port); - } catch (UnknownHostException e) { - throw e; - } catch (IOException e) { - throw e; - } - } - - public void sendMsg(Req req,Handler recall) throws IOException { - try { - DataOutputStream out = new DataOutputStream( - client.getOutputStream()); - int tmp = req.type; - out.writeByte(tmp); - switch (tmp) { - case 0: - ReqLogin rau = (ReqLogin) req; - String id = rau.user; - String pa = rau.pass; - out.writeBytes(id + "\0" + pa); - break; - case 2: - ReqUpdate rup = (ReqUpdate) req; - int tk2 = rup.token; - double slat = rup.lat; - double slot = rup.lot; - out.writeInt(tk2); - out.writeDouble(slat); - out.writeDouble(slot); - break; - case 3: - ReqLocation ras = (ReqLocation) req; - int tk3 = ras.token; - int gid = ras.gid; - out.writeInt(tk3); - out.writeInt(gid); - break; - } - out.flush(); - client.shutdownOutput(); - DataInputStream in = new DataInputStream(client.getInputStream()); - Message msg = new Message(); - int type = in.readUnsignedByte(); - switch (type) { - case 0: - int id = in.readInt(); - int status = in.readUnsignedByte(); - ResLogin rchklogin = new ResLogin(id,status); - msg.obj = rchklogin; - msg.what = 0; - recall.sendMessage(msg); - break; - case 2: - int status1 = in.readUnsignedByte(); - ResUpdate rchkupd = new ResUpdate(status1); - msg.obj = rchkupd; - msg.what = 1; - recall.sendMessage(msg); - break; - case 3: - int n = in.readInt(); - Vector tmpv = new Vector(); - for (int i = 1; i <= n; i++) { - int tid = in.readInt(); - double lat = in.readDouble(); - double lot = in.readDouble(); - tmpv.add(new Rlocation(tid,lat,lot)); - } - ResLocation rlocin = new ResLocation(n,tmpv); - msg.obj = rlocin; - msg.what = 3; - recall.sendMessage(msg); - break; - } - - } catch (IOException e) { - throw e; - } - } - - public void closeSocket() { - try { - client.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - -} +package com.macaroon.piztor; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.net.UnknownHostException; +import java.util.Vector; + +import android.annotation.SuppressLint; +import android.os.Handler; +import android.os.Message; + +public class SocketClient { + static Socket client; + + public SocketClient(String site, int port) throws UnknownHostException, + IOException { + try { + client = new Socket(site, port); + } catch (UnknownHostException e) { + throw e; + } catch (IOException e) { + throw e; + } + } + + public void sendMsg(Req req,Handler recall) throws IOException { + 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 = 4+1+id.length()+1+pa.length(); + out.writeInt(len); + out.writeByte(tmp); + out.writeBytes(id); + out.writeByte(0); + out.writeBytes(pa); + break; + case 1: + ReqUpdate rup = (ReqUpdate) req; + String tk1 = rup.token; + String name1 = rup.uname; + len = 4+1+32+name1.length()+1+8+8; + 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 = 4+1+32+name2.length()+1+4; + 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 = 4+1+32+name3.length()+1+4; + 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 = in.readInt(); + Vector tmpv = new Vector(); + for (int i = 1; i <= n; i++) { + int tid = in.readInt(); + double lat = in.readDouble(); + double lot = in.readDouble(); + tmpv.add(new Rlocation(tid,lat,lot)); + } + ResLocation rlocin = new ResLocation(n,status2,tmpv); + msg.obj = rlocin; + msg.what = 2; + recall.sendMessage(msg); + break; + case 3: + int status3 = in.readUnsignedByte(); + Vector tmpu = new Vector(); + 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); + break; + case 1: + boolean s = in.readBoolean(); + in.readByte(); + r = new RKeyGender(s); + outlen-=2; + tmpu.add(r); + break; + } + } + ResUserinfo resus = new ResUserinfo(status3,tmpu); + msg.obj = resus; + msg.what = 3; + recall.sendMessage(msg); + break; + } + + } 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); + } + +} diff --git a/client/Piztor/src/com/macaroon/piztor/Transam.java b/client/Piztor/src/com/macaroon/piztor/Transam.java index 3410502..481a676 100644 --- a/client/Piztor/src/com/macaroon/piztor/Transam.java +++ b/client/Piztor/src/com/macaroon/piztor/Transam.java @@ -1,139 +1,195 @@ -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 java.util.TimerTask; - -import android.annotation.SuppressLint; -import android.os.Handler; -import android.os.Message; - -public class Transam implements Runnable { - - public Timer timer; - public Timer mtimer; - public boolean running = false; - public boolean flag = true; - public int cnt = 4; - Res res; - Req req; - public int p; // port - public String i; // ip - Thread thread; - Handler core; - Handler recall; // recall - Queue reqtask; // request task - - Transam(String ip, int port, Handler Recall) { - p = port; - i = ip; - recall = Recall; - reqtask = new LinkedList(); - } - - public void send(Req r) { - reqtask.offer(r); - - } - - public void setHandler(Handler Recall) { - recall = Recall; - reqtask.clear(); - } - - public void run() { // start the main timer - // TimerTask tmain = new Timertk(); - // mtimer = new Timer(); - // mtimer.schedule(tmain, 100, 100); //check the queue for every 100 - // msec - - 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(); - ret.obj = "Time out!"; - ret.what = 100; - recall.sendMessage(ret); - } else { // run the request - final thd t = new thd(); - flag = false; - thread = new Thread(t); - cnt = 4; - running = true; - thread.start(); - timer = new Timer(); - TimerTask task = new Timertk(); - timer.schedule(task, 2000, 2000); - } - } - } - } - } - - class tmain extends TimerTask { - public void run() { - - } - }; - - class thd implements Runnable { - public void run() { - try { - SocketClient client = new SocketClient(i, p); - client.sendMsg(req, recall); - Message msg = new Message(); - msg.what = 1; - handler.sendMessage(msg); - client.closeSocket(); - } catch (UnknownHostException e) { - e.printStackTrace(); - System.out.println("UnknownHostException"); - } catch (IOException e) { - e.printStackTrace(); - System.out.println("IOException"); - } - - } - } - - @SuppressLint("HandlerLeak") - Handler handler = new Handler() { - public void handleMessage(Message msg) { - switch (msg.what) { - case 1: - flag = true; - break; - case 2: - final thd t = new thd(); - thread = new Thread(t); - thread.start(); - break; - } - super.handleMessage(msg); - } - }; - - class Timertk extends TimerTask { - public void run() { - if (flag == false && cnt > 0) { - cnt--; - } else if (cnt == 0) { - Message msg = new Message(); - msg.obj = "connecting failed"; - msg.what = 101; - recall.sendMessage(msg); - timer.cancel(); - } else if (flag == true) { - timer.cancel(); - running = false; - } - } - }; +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 java.util.TimerTask; + +import android.annotation.SuppressLint; +import android.os.Handler; +import android.os.Message; + +// Piztor Transmission Protocol v0.3a // + +//------------------------------------------------// +// // +// *return msg type* // +// 0 for login // +// 1 for updateLocation // +// 2 for locationRequest // +// 3 for userinfo // +// // +// ----------I'm the division line-------- // +// // +// 100 for timeOut // +// 101 for conection failed // +// 102 for reconnect // +// 103 for IOexception // +// 104 for UnknownHostException // +// // +// ----------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 & RUserinfo(base class) // +// 0 RKeyGroupID -- groupid // +// 1 RKeyGender -- gender // +// // +// status -- 0 for success // +// 1 for failed/invalid // +// // +//------------------------------------------------// + +public class Transam implements Runnable { + public Timer timer; + public Timer mtimer; + public boolean running = false; + public boolean flag = true; + public int cnt = 4; + Res res; + Req req; + public int p; //port + public String i; //ip + Thread thread; + Handler core; + Handler recall; //recall + Queue reqtask ; //request task + + Transam(String ip, int port,Handler Recall) { + p = port; + i = ip; + recall = Recall; + reqtask = new LinkedList(); + } + + + public void send(Req r){ + reqtask.offer(r); + + } + + public void setHandler(Handler Recall){ + recall = Recall; + reqtask.clear(); + } + + public void run() { //start the main timer + //TimerTask tmain = new Timertk(); + //mtimer = new Timer(); + //mtimer.schedule(tmain, 100, 100); //check the queue for every 100 msec + + 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(); + ret.obj = "Time out!"; + ret.what = 100; + recall.sendMessage(ret); + } + else{ //run the request + final thd t = new thd(); + flag = false; + thread = new Thread(t); + cnt = 4; + running = true; + thread.start(); + timer = new Timer(); + TimerTask task = new Timertk(); + timer.schedule(task, 2000, 2000); + } + } + } + } + } + + class tmain extends TimerTask { + public void run() { + + } + }; + + class thd implements Runnable { + public void run() { + try { + SocketClient client = new SocketClient(i,p); + client.sendMsg(req,recall); + Message msg = new Message(); + msg.what = 1; + handler.sendMessage(msg); + client.closeSocket(); + } catch (UnknownHostException e) { + e.printStackTrace(); + Message msg = new Message(); + msg.obj = "UnknownHostException"; + msg.what = 104; + recall.sendMessage(msg); + } catch (IOException e) { + e.printStackTrace(); + Message msg = new Message(); + msg.obj = "IOException"; + msg.what = 103; + recall.sendMessage(msg); + } + + } + } + + @SuppressLint("HandlerLeak") + Handler handler = new Handler() { + public void handleMessage(Message msg) { + switch (msg.what) { + case 1: + flag = true; + break; + case 2: + final thd t = new thd(); + thread = new Thread(t); + thread.start(); + break; + } + super.handleMessage(msg); + } + }; + + class Timertk extends TimerTask { + 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; + handler.sendMessage(m); + } else if (cnt == 0) { + Message msg = new Message(); + msg.obj = "connecting failed"; + msg.what = 101; + recall.sendMessage(msg); + timer.cancel(); + } else if (flag == true) { + timer.cancel(); + running = false; + } + } + }; } \ 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 04be578..3d163d5 100644 --- a/client/Piztor/src/com/macaroon/piztor/UserInfo.java +++ b/client/Piztor/src/com/macaroon/piztor/UserInfo.java @@ -3,5 +3,6 @@ package com.macaroon.piztor; public class UserInfo { static String ip = "69.85.86.42"; static int port = 9990; - static int token = -1; + static String token = null; + static String username = null; } -- cgit v1.2.3-70-g09d2