diff options
author | Goblin911 <[email protected]> | 2013-08-26 17:23:04 +0800 |
---|---|---|
committer | Goblin911 <[email protected]> | 2013-08-26 17:23:04 +0800 |
commit | 9b17e1808a93840c265123925c2b60e55c2ec930 (patch) | |
tree | db8886235b797cbcc877101b0d785a04cbb8f000 /client/Piztor/src/com | |
parent | 92c5473740f45883c719002d4bc47236c669f51e (diff) |
fix some bugs of socket thread
Diffstat (limited to 'client/Piztor/src/com')
5 files changed, 54 insertions, 46 deletions
diff --git a/client/Piztor/src/com/macaroon/piztor/Login.java b/client/Piztor/src/com/macaroon/piztor/Login.java index 4ddd603..cb3b182 100644 --- a/client/Piztor/src/com/macaroon/piztor/Login.java +++ b/client/Piztor/src/com/macaroon/piztor/Login.java @@ -21,6 +21,10 @@ public class Login extends PiztorAct { @Override public void handleMessage(Message m) { System.out.println("receive what : " + m.what); + if (m.what == -1) { + ((Exception)m.obj).printStackTrace(); + return; + } if (m.what == 0) { ResLogin res = (ResLogin) m.obj; UserInfo.token = res.t; @@ -53,7 +57,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, 10000)); + AppMgr.transam.send(new ReqLogin(user, pass, nowtime, 5000)); } @Override diff --git a/client/Piztor/src/com/macaroon/piztor/Main.java b/client/Piztor/src/com/macaroon/piztor/Main.java index 573c753..d5d260b 100644 --- a/client/Piztor/src/com/macaroon/piztor/Main.java +++ b/client/Piztor/src/com/macaroon/piztor/Main.java @@ -47,7 +47,7 @@ public class Main extends PiztorAct { switch (m.what) { case 1: ResUpdate update = (ResUpdate) m.obj; - if (update.t == 0) + if (update.s == 0) System.out.println("update success"); else { System.out.println("update failed"); diff --git a/client/Piztor/src/com/macaroon/piztor/SocketClient.java b/client/Piztor/src/com/macaroon/piztor/SocketClient.java index 812de05..703b974 100644 --- a/client/Piztor/src/com/macaroon/piztor/SocketClient.java +++ b/client/Piztor/src/com/macaroon/piztor/SocketClient.java @@ -18,6 +18,7 @@ public class SocketClient { IOException {
try {
client = new Socket(site, port);
+ client.setSoTimeout(1000);
} catch (UnknownHostException e) {
throw e;
} catch (IOException e) {
@@ -27,6 +28,7 @@ public class SocketClient { public void sendMsg(Req req,Handler recall) throws IOException {
try {
+
DataOutputStream out = new DataOutputStream(
client.getOutputStream());
int tmp = req.type;
diff --git a/client/Piztor/src/com/macaroon/piztor/Transam.java b/client/Piztor/src/com/macaroon/piztor/Transam.java index fae9252..9ac6bea 100644 --- a/client/Piztor/src/com/macaroon/piztor/Transam.java +++ b/client/Piztor/src/com/macaroon/piztor/Transam.java @@ -52,53 +52,55 @@ import android.os.Message; public class Transam implements Runnable {
public Timer timer;
public Timer mtimer;
- public boolean running = false;
+ public boolean running = false;
public boolean flag = true;
public int cnt = 4;
Res res;
Req req;
- public int p; //port
- public String i; //ip
+ public int p; // port
+ public String i; // ip
Thread thread;
Handler core;
- Handler recall; //recall
- Queue<Req> reqtask ; //request task
+ Handler recall; // recall
+ Queue<Req> reqtask; // request task
- Transam(String ip, int port,Handler Recall) {
+ Transam(String ip, int port, Handler Recall) {
p = port;
i = ip;
recall = Recall;
reqtask = new LinkedList<Req>();
}
-
- public void send(Req r){
+
+ public void send(Req r) {
reqtask.offer(r);
-
+
}
-
- public void setHandler(Handler Recall){
+
+ 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!
+ 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();
TimeOutException t = new TimeOutException();
ret.obj = t;
ret.what = -1;
recall.sendMessage(ret);
- }
- else{ //run the request
+ } else { // run the request
final thd t = new thd();
flag = false;
thread = new Thread(t);
@@ -107,24 +109,24 @@ public class Transam implements Runnable { thread.start();
timer = new Timer();
TimerTask task = new Timertk();
- timer.schedule(task, 20000, 20000);
+ 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);
+ SocketClient client = new SocketClient(i, p);
+ client.sendMsg(req, recall);
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
@@ -155,7 +157,6 @@ public class Transam implements Runnable { break;
case 2:
final thd t = new thd();
- thread.interrupt();
thread = new Thread(t);
thread.start();
break;
@@ -184,20 +185,21 @@ public class Transam implements Runnable { }
}
};
-
- class ConnectFailedException extends Exception{
+
+ class ConnectFailedException extends Exception {
private static final long serialVersionUID = 101L;
- public ConnectFailedException() {
- super();
- }
+
+ public ConnectFailedException() {
+ super();
+ }
}
-
- class TimeOutException extends Exception{
+
+ class TimeOutException extends Exception {
private static final long serialVersionUID = 102L;
- public TimeOutException() {
- super();
- }
-
+ 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 63f413e..86a7a4c 100644 --- a/client/Piztor/src/com/macaroon/piztor/UserInfo.java +++ b/client/Piztor/src/com/macaroon/piztor/UserInfo.java @@ -1,7 +1,7 @@ package com.macaroon.piztor; public class UserInfo { - 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; |