summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeddy <ted.sybil@gmail.com>2013-08-23 11:11:23 +0800
committerTeddy <ted.sybil@gmail.com>2013-08-23 11:11:23 +0800
commit8d5489095e4b0526e4152f7e3a1e9bc3a758e9bd (patch)
treeb6b2ebca38aad5c63fa92846d6793a9e7ba817ff
parenta44601d57a7b96d69848d5bdd2fa6f3e442d0c85 (diff)
parent0b0d350bfe97c0101f31d767d54892f7cef05b96 (diff)
Merge branch 'master' of github.com:Determinant/piztor
-rw-r--r--client/Socket-demo/MainActivity.java44
-rw-r--r--client/Socket-demo/Myrequest.java10
-rw-r--r--client/Socket-demo/Myrespond.java12
-rw-r--r--client/Socket-demo/SocketClient.java107
-rw-r--r--client/Socket-demo/Transam.java98
5 files changed, 271 insertions, 0 deletions
diff --git a/client/Socket-demo/MainActivity.java b/client/Socket-demo/MainActivity.java
new file mode 100644
index 0000000..ab2ef0c
--- /dev/null
+++ b/client/Socket-demo/MainActivity.java
@@ -0,0 +1,44 @@
+package com.example.test;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.view.Menu;
+import android.view.View;
+import android.widget.TextView;
+
+public class MainActivity extends Activity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_main);
+ TextView textview = (TextView)findViewById(R.id.textView1);
+ textview.setText("Server Created!");
+ }
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ // Inflate the menu; this adds items to the action bar if it is present.
+ getMenuInflater().inflate(R.menu.main, menu);
+ return true;
+ }
+
+
+ public void sendMessage(View view) {
+
+ }
+
+
+
+
+
+
+
+
+}
+
+
+
+
+
+
diff --git a/client/Socket-demo/Myrequest.java b/client/Socket-demo/Myrequest.java
new file mode 100644
index 0000000..e5bfce2
--- /dev/null
+++ b/client/Socket-demo/Myrequest.java
@@ -0,0 +1,10 @@
+package com.example.test;
+
+import java.util.Vector;
+
+public class Myrequest{
+ public Vector<Object> contain;
+ Myrequest(){
+ contain = new Vector<Object>();
+ }
+} \ No newline at end of file
diff --git a/client/Socket-demo/Myrespond.java b/client/Socket-demo/Myrespond.java
new file mode 100644
index 0000000..ea6ef3b
--- /dev/null
+++ b/client/Socket-demo/Myrespond.java
@@ -0,0 +1,12 @@
+package com.example.test;
+
+import java.util.Vector;
+
+public class Myrespond{
+ public Vector<Object> contain;
+ public String wrong;
+ Myrespond(){
+ wrong = null;
+ contain = new Vector<Object>();
+ }
+} \ No newline at end of file
diff --git a/client/Socket-demo/SocketClient.java b/client/Socket-demo/SocketClient.java
new file mode 100644
index 0000000..df42484
--- /dev/null
+++ b/client/Socket-demo/SocketClient.java
@@ -0,0 +1,107 @@
+package com.example.test;
+
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+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 Myrespond sendMsg(Myrequest req)throws IOException{
+ try{
+ DataOutputStream out = new DataOutputStream(client.getOutputStream());
+ int tmp = (Integer) req.contain.get(0);
+ switch(tmp){
+ case 0:
+ String id = (String) req.contain.get(1);
+ String pass = (String) req.contain.get(2);
+ out.writeUTF(id);
+ out.writeUTF(pass);
+ break;
+ case 1:
+ int tk1 = (Integer) req.contain.get(1);
+ int acc = (Integer) req.contain.get(2);
+ String mess = (String) req.contain.get(3);
+ out.writeInt(tk1);
+ out.writeInt(acc);
+ out.writeUTF(mess);
+ break;
+ case 2:
+ int tk2 = (Integer) req.contain.get(1);
+ double slot = (Double) req.contain.get(2);
+ double slat = (Double) req.contain.get(3);
+ out.writeInt(tk2);
+ out.writeDouble(slot);
+ out.writeDouble(slat);
+ break;
+ case 3:
+ int tk3 = (Integer) req.contain.get(1);
+ int gid = (Integer) req.contain.get(2);
+ out.writeInt(tk3);
+ out.writeInt(gid);
+ break;
+ }
+ out.flush();
+ client.shutdownOutput();
+ DataInputStream in = new DataInputStream(client.getInputStream());
+ int type = in.readUnsignedByte();
+ Myrespond r = new Myrespond();
+ switch(type){
+ case 0:
+ int id = in.readInt();
+ int status = in.readUnsignedByte();
+ r.contain.add(0);
+ r.contain.add(id);
+ r.contain.add(status);
+ break;
+ case 1:
+ r.contain.add(1);
+ //reserved
+ break;
+ case 2:
+ r.contain.add(2);
+ //reserved
+ break;
+ case 3:
+ int n = in.readInt();
+ r.contain.add(3);
+ r.contain.add(n);
+ for(int i=1;i<=n;i++)
+ {
+ int tid = in.readInt();
+ double lot = in.readDouble();
+ double lat = in.readDouble();
+ r.contain.add(tid);
+ r.contain.add(lot);
+ r.contain.add(lat);
+ }
+ break;
+ }
+ return r;
+ }catch(IOException e){
+ throw e;
+ }
+ }
+ public void closeSocket(){
+ try{
+ client.close();
+ }catch(IOException e){
+ e.printStackTrace();
+ }
+ }
+
+}
diff --git a/client/Socket-demo/Transam.java b/client/Socket-demo/Transam.java
new file mode 100644
index 0000000..6f0fb44
--- /dev/null
+++ b/client/Socket-demo/Transam.java
@@ -0,0 +1,98 @@
+package com.example.test;
+
+import java.io.IOException;
+import java.net.UnknownHostException;
+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 boolean flag = true;
+ public int cnt = 4;
+ public int port;
+ public String ip;
+ Thread thread;
+ Myrequest req;
+ Myrespond res;
+ Controller core;
+
+ Transam(String i,int p,Myrequest r,Controller c){
+ port = p;
+ ip = i;
+ req = r;
+ core = c;
+ }
+
+ public void run() {
+ final thd t = new thd();
+ flag = false;
+ thread = new Thread(t);
+ cnt = 4;
+ thread.start();
+ timer = new Timer();
+ TimerTask task = new Timertk();
+ timer.schedule(task,2000,2000);
+ }
+
+ class thd implements Runnable {
+ @Override
+ public void run() {
+ try{
+ SocketClient client = new SocketClient(ip,port);
+ res = client.sendMsg(req);
+ core.recieveInfo(res);
+ Message msg = new Message();
+ msg.what = 1;
+ handler.sendMessage(msg);
+ client.closeSocket();
+ }catch (UnknownHostException e){
+ }catch (IOException e){
+ }
+
+ }
+ }
+
+ @SuppressLint("HandlerLeak")
+ Handler handler = new Handler(){
+ public void handleMessage(Message msg) {
+ switch (msg.what) {
+ case 1:
+ flag = true;
+ break;
+ case 2:
+ res = new Myrespond();
+ res.wrong = msg.obj.toString();
+ core.recieveInfo(res);
+ break;
+ case 3:
+ 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 = 2;
+ handler.sendMessage(msg);
+ timer.cancel();
+ }
+ else if(flag==true){
+ timer.cancel();
+ }
+ }
+ };
+} \ No newline at end of file