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.java198
1 files changed, 102 insertions, 96 deletions
diff --git a/client/Piztor/src/com/macaroon/piztor/Transam.java b/client/Piztor/src/com/macaroon/piztor/Transam.java
index 9ac6bea..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,98 +49,102 @@ 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;
+ 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
- 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 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
-
- 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 thread
+ 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
- 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, 2000, 2000);
+ 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);
}
}
@@ -152,54 +154,58 @@ 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 = 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 {
+
+ 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();
+ }
+
}
-
+
+ class JavaHostException extends Exception{
+ private static final long serialVersionUID = 103L;
+ public JavaHostException() {
+ super();
+ }
+
+ }
+
} \ No newline at end of file