From f4595bc3d9afaac5d3c91bb95dff0217cf5b728c Mon Sep 17 00:00:00 2001 From: Goblin911 Date: Tue, 27 Aug 2013 11:03:38 +0800 Subject: flush network, add login failed toast --- client/Piztor/src/com/macaroon/piztor/InitAct.java | 2 +- client/Piztor/src/com/macaroon/piztor/Login.java | 19 ++++++---- client/Piztor/src/com/macaroon/piztor/Main.java | 1 - .../src/com/macaroon/piztor/SocketClient.java | 40 ++++++++++++++-------- client/Piztor/src/com/macaroon/piztor/Transam.java | 4 +-- .../Piztor/src/com/macaroon/piztor/UserInfo.java | 4 +-- 6 files changed, 44 insertions(+), 26 deletions(-) (limited to 'client') diff --git a/client/Piztor/src/com/macaroon/piztor/InitAct.java b/client/Piztor/src/com/macaroon/piztor/InitAct.java index fb31346..4d2b656 100644 --- a/client/Piztor/src/com/macaroon/piztor/InitAct.java +++ b/client/Piztor/src/com/macaroon/piztor/InitAct.java @@ -10,7 +10,7 @@ public class InitAct extends PiztorAct { id = "initAct"; super.onCreate(savedInstanceState); AppMgr.init(); - AppMgr.transam.setTimeOutTime(10000); + AppMgr.transam.setTimeOutTime(5000); setContentView(R.layout.activity_init); } diff --git a/client/Piztor/src/com/macaroon/piztor/Login.java b/client/Piztor/src/com/macaroon/piztor/Login.java index 52df2c9..3ad6cfb 100644 --- a/client/Piztor/src/com/macaroon/piztor/Login.java +++ b/client/Piztor/src/com/macaroon/piztor/Login.java @@ -9,6 +9,7 @@ import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; +import android.widget.Toast; public class Login extends PiztorAct { @@ -30,6 +31,10 @@ public class Login extends PiztorAct { } if (m.what == 0) { ResLogin res = (ResLogin) m.obj; + if (res.s == 1) { + actMgr.trigger(loginFailed); + return; + } UserInfo.token = res.t; UserInfo.id = res.uid; UserInfo.username = edtUser.getText().toString(); @@ -65,7 +70,9 @@ public class Login extends PiztorAct { @Override void leave(int e) { - + Toast toast = Toast.makeText(getApplicationContext(), + "login failed", Toast.LENGTH_LONG); + toast.show(); } } @@ -106,11 +113,11 @@ public class Login extends PiztorAct { @Override public boolean onKeyDown(int keyCode, KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_BACK) { - AppMgr.exit(); - return true; - } - return super.onKeyDown(keyCode, event); + if (keyCode == KeyEvent.KEYCODE_BACK) { + AppMgr.exit(); + return true; + } + return super.onKeyDown(keyCode, event); } @Override diff --git a/client/Piztor/src/com/macaroon/piztor/Main.java b/client/Piztor/src/com/macaroon/piztor/Main.java index 5873afe..f9e8b00 100644 --- a/client/Piztor/src/com/macaroon/piztor/Main.java +++ b/client/Piztor/src/com/macaroon/piztor/Main.java @@ -82,7 +82,6 @@ public class Main extends PiztorAct { default: break; } - } }; diff --git a/client/Piztor/src/com/macaroon/piztor/SocketClient.java b/client/Piztor/src/com/macaroon/piztor/SocketClient.java index f97e397..fe926d9 100644 --- a/client/Piztor/src/com/macaroon/piztor/SocketClient.java +++ b/client/Piztor/src/com/macaroon/piztor/SocketClient.java @@ -3,22 +3,30 @@ 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.Locale; import java.util.Vector; +import android.annotation.SuppressLint; import android.os.Handler; import android.os.Message; public class SocketClient { static Socket client; + + static final int ByteLength = 1; + static final int IntLength = 4; + static final int DoubleLength = 8; + static final int TokenLength = 32; public SocketClient(String site, int port, int retime) throws UnknownHostException, IOException { try { - client = new Socket(site, port); + client = new Socket(); + client.connect(new InetSocketAddress(site,port), 5000); client.setSoTimeout(retime); } catch (UnknownHostException e) { e.printStackTrace(); @@ -26,7 +34,7 @@ public class SocketClient { } catch (IOException e) { e.printStackTrace(); throw e; - } + } } public int sendMsg(Req req,Handler recall) throws IOException,SocketTimeoutException { @@ -40,7 +48,7 @@ public class SocketClient { ReqLogin rau = (ReqLogin) req; String id = rau.user; String pa = rau.pass; - len = 4+1+id.length()+1+pa.length()+1; + len = IntLength+ByteLength+id.length()+ByteLength+pa.length()+ByteLength; out.writeInt(len); out.writeByte(tmp); out.writeBytes(id); @@ -52,7 +60,7 @@ public class SocketClient { ReqUpdate rup = (ReqUpdate) req; String tk1 = rup.token; String name1 = rup.uname; - len = 4+1+32+name1.length()+1+8+8; + len = IntLength+ByteLength+TokenLength+name1.length()+ByteLength+DoubleLength+DoubleLength; out.writeInt(len); out.writeByte(tmp); double slat = rup.lat; @@ -68,7 +76,7 @@ public class SocketClient { ReqLocation ras = (ReqLocation) req; String tk2 = ras.token; String name2 = ras.uname; - len = 4+1+32+name2.length()+1+4; + len = IntLength+ByteLength+TokenLength+name2.length()+ByteLength+IntLength; out.writeInt(len); out.writeByte(tmp); int gid = ras.gid; @@ -82,7 +90,7 @@ public class SocketClient { ReqUserinfo rus = (ReqUserinfo) req; String tk3 = rus.token; String name3 = rus.uname; - len = 4+1+32+name3.length()+1+4; + len = IntLength+ByteLength+TokenLength+name3.length()+ByteLength+IntLength; out.writeInt(len); out.writeByte(tmp); int usid = rus.uid; @@ -126,13 +134,16 @@ public class SocketClient { break; case 2: int status2 = in.readUnsignedByte(); - int n = in.readInt(); + int n = 0; + outlen-=(IntLength+ByteLength+ByteLength); Vector tmpv = new Vector(); - for (int i = 1; i <= n; i++) { + while(outlen > 0) { int tid = in.readInt(); double lat = in.readDouble(); double lot = in.readDouble(); tmpv.add(new Rlocation(tid,lat,lot)); + outlen -= (IntLength+DoubleLength+DoubleLength); + n++; } ResLocation rlocin = new ResLocation(n,status2,tmpv); msg.obj = rlocin; @@ -141,21 +152,21 @@ public class SocketClient { break; case 3: int status3 = in.readUnsignedByte(); - outlen-=6; + outlen-=(IntLength+ByteLength+ByteLength); ReqUserinfo rus = (ReqUserinfo) req; int u = rus.uid; int g = 0,s = 0; - while(outlen>0){ + while(outlen > 0) { int typ = in.readUnsignedByte(); - outlen-=1; + outlen-=ByteLength; switch(typ){ case 0: g = in.readInt(); - outlen-=4; + outlen-=IntLength; break; case 1: s = in.readByte(); - outlen-=1; + outlen-=ByteLength; break; } } @@ -184,11 +195,12 @@ public class SocketClient { } } + @SuppressLint("DefaultLocale") private static byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } - hexString = hexString.toUpperCase(Locale.CHINA); + hexString = hexString.toUpperCase(); 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 53750d5..e946387 100644 --- a/client/Piztor/src/com/macaroon/piztor/Transam.java +++ b/client/Piztor/src/com/macaroon/piztor/Transam.java @@ -53,7 +53,7 @@ public class Transam implements Runnable { public boolean flag = true; public int cnt = 4; //retry times public int tcnt; //current remain retry times - public int retime = 1000; //timeout time + public int retime = 10000; //timeout time Res res; Req req; public int p; //port @@ -123,7 +123,7 @@ public class Transam implements Runnable { try { SocketClient client = new SocketClient(i,p,retime); int out = client.sendMsg(req,recall); - if(out == 0){ + if(out == 0){ client.closeSocket(); running = false; } diff --git a/client/Piztor/src/com/macaroon/piztor/UserInfo.java b/client/Piztor/src/com/macaroon/piztor/UserInfo.java index 9c78fc3..d404c7e 100644 --- a/client/Piztor/src/com/macaroon/piztor/UserInfo.java +++ b/client/Piztor/src/com/macaroon/piztor/UserInfo.java @@ -1,8 +1,8 @@ package com.macaroon.piztor; public class UserInfo { - static String ip = "69.85.86.42"; -// static String ip = "192.168.1.101"; +// static String ip = "69.85.86.42"; + static String ip = "192.168.1.101"; static int port = 2222; static String token = null; static String username = null; -- cgit v1.2.3-70-g09d2