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.java132
1 files changed, 70 insertions, 62 deletions
diff --git a/client/Piztor/src/com/macaroon/piztor/Transam.java b/client/Piztor/src/com/macaroon/piztor/Transam.java
index fae9252..53750d5 100644
--- a/client/Piztor/src/com/macaroon/piztor/Transam.java
+++ b/client/Piztor/src/com/macaroon/piztor/Transam.java
@@ -1,18 +1,16 @@
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 //
+// Piztor Transmission Protocol v0.4 //
//------------------------------------------------//
// //
@@ -51,10 +49,11 @@ 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;
+ public int cnt = 4; //retry times
+ public int tcnt; //current remain retry times
+ public int retime = 1000; //timeout time
Res res;
Req req;
public int p; //port
@@ -76,16 +75,21 @@ public class Transam implements Runnable {
}
+ public void setTimeOutTime(int msec){
+ retime = msec;
+ }
+
+ public void setRetryTimes(int times){
+ cnt = times;
+ }
+
+
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
-
+ public void run() { //start the main thread
while(true){
if(running == false){
@@ -98,49 +102,49 @@ public class Transam implements Runnable {
ret.what = -1;
recall.sendMessage(ret);
}
- else{ //run the request
- final thd t = new thd();
- flag = false;
- thread = new Thread(t);
- cnt = 4;
+ else{ //run the request
running = true;
- thread.start();
- timer = new Timer();
- TimerTask task = new Timertk();
- timer.schedule(task, 20000, 20000);
+ tcnt = cnt;
+ connect();
}
}
}
}
}
- class tmain extends TimerTask {
- public void run() {
-
- }
- };
+ private void connect(){
+ final thd t = new thd();
+ thread = new Thread(t);
+ thread.start();
+ }
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();
+ SocketClient client = new SocketClient(i,p,retime);
+ int out = client.sendMsg(req,recall);
+ if(out == 0){
+ client.closeSocket();
+ running = false;
+ }
+ else {
+ client.closeSocket();
+ Message msg = new Message();
+ msg.what = 0;
+ handler.sendMessage(msg);
+ }
} catch (UnknownHostException e) {
e.printStackTrace();
Message msg = new Message();
- msg.obj = e;
msg.what = -1;
- recall.sendMessage(msg);
+ msg.obj = e;
+ handler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
Message msg = new Message();
- msg.obj = e;
msg.what = -1;
- recall.sendMessage(msg);
+ msg.obj = e;
+ handler.sendMessage(msg);
}
}
@@ -150,40 +154,36 @@ public class Transam implements Runnable {
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
- case 1:
- flag = true;
+ case -1:
+ if (tcnt > 0) {
+ tcnt--;
+ System.out.println(tcnt);
+ connect();
+ } else if (tcnt == 0) {
+ Message m = new Message();
+ m.obj = msg.obj;
+ m.what = -1;
+ recall.sendMessage(m);
+ running = false;
+ }
break;
- case 2:
- final thd t = new thd();
- thread.interrupt();
- thread = new Thread(t);
- thread.start();
+ case 0:
+ if (tcnt > 0) {
+ tcnt--;
+ connect();
+ } else if (tcnt == 0) {
+ Message m = new Message();
+ ConnectFailedException c = new ConnectFailedException();
+ m.obj = c;
+ m.what = -1;
+ recall.sendMessage(m);
+ running = false;
+ }
break;
}
super.handleMessage(msg);
}
};
-
- class Timertk extends TimerTask {
- public void run() {
- if (flag == false && cnt > 0) {
- cnt--;
- Message m = new Message();
- m.what = 2;
- handler.sendMessage(m);
- } else if (cnt == 0) {
- Message msg = new Message();
- ConnectFailedException c = new ConnectFailedException();
- msg.obj = c;
- msg.what = -1;
- recall.sendMessage(msg);
- timer.cancel();
- } else if (flag == true) {
- timer.cancel();
- running = false;
- }
- }
- };
class ConnectFailedException extends Exception{
private static final long serialVersionUID = 101L;
@@ -200,4 +200,12 @@ public class Transam implements Runnable {
}
+ class JavaHostException extends Exception{
+ private static final long serialVersionUID = 103L;
+ public JavaHostException() {
+ super();
+ }
+
+ }
+
} \ No newline at end of file