# HG changeset patch
# User e085711
# Date 1309921000 -32400
# Node ID 5953ac30cb208c29235af3a2d46926f82c8d5631
# Parent 1ad24c0af09e5b546b1207ef427053c6a3c65e5b
add acceptClient.java
diff -r 1ad24c0af09e -r 5953ac30cb20 hades.cr.ie.u-ryukyu.ac.jp.html
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hades.cr.ie.u-ryukyu.ac.jp.html Wed Jul 06 11:56:40 2011 +0900
@@ -0,0 +1,10 @@
+
hbpVNC desktop
+
+
+University of the Ryukyu
+
+
diff -r 1ad24c0af09e -r 5953ac30cb20 nw0811.st.ie.u-ryukyu.ac.jp.html
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nw0811.st.ie.u-ryukyu.ac.jp.html Wed Jul 06 11:56:40 2011 +0900
@@ -0,0 +1,10 @@
+hbpVNC desktop
+
+
+University of the Ryukyu
+
+
diff -r 1ad24c0af09e -r 5953ac30cb20 src/myVncProxy/acceptClient.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/myVncProxy/acceptClient.java Wed Jul 06 11:56:40 2011 +0900
@@ -0,0 +1,229 @@
+package myVncProxy;
+
+import java.net.Socket;
+import java.io.*;
+import java.net.*;
+import java.util.*;
+
+public class acceptClient extends Thread {
+ ServerSocket echoServer=null;
+ // ソケットや入出力用のストリームの宣言
+ String line,port,handover;
+ BufferedReader is;
+ PrintStream os;
+ Socket clientSocket = null;
+ int intv_time = 100;
+ int i = 0 , p = 0,t = 0;
+ //String[] sub = new String[100];
+ LinkedList ls = new LinkedList();
+ String request,name;
+ int treebranch = 2;//treeの子ノードの数
+ String leaderflag="0",sendleaderflag="0";
+ boolean runflag =false,addrRegistor=true;
+ BufferedReader temp = null;//一時的にisを保存する変数
+
+
+ public acceptClient(String _name) {
+ // TODO Auto-generated constructor stub
+ name = _name;
+ }
+
+
+ public void run() {
+
+ while(true){
+ // ポート9999番を開く
+ try {
+ echoServer = new ServerSocket(9999);
+ }
+ catch (IOException e) {
+ System.out.println(e);
+ }
+
+ // クライアントからの要求を受けるソケットを開く
+ try {
+ clientSocket = echoServer.accept();
+ is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
+ os = new PrintStream(clientSocket.getOutputStream());
+
+ // クライアントからのメッセージを待ち、受け取ったメッセージをそのまま返す
+ while (true){
+ /*
+ if(runflag==false){
+ temp = is;
+ line = is.readLine();
+ port = is.readLine();
+ }else{
+ if(temp!=null){
+ is = temp;
+ }
+ */
+ line = is.readLine();
+ port = is.readLine();
+ //}
+ System.out.println("データーを受信しましたlin="+line+" port="+port);
+ //自分の IPADRESSを取得する
+ InetAddress addr = InetAddress.getLocalHost();
+ String add = new String(addr.getHostAddress());
+
+ if("1".equals(line)){
+ System.out.println("親が落ちましたmessage" + port);
+ os.println(ls.getLast());
+ handover= String.valueOf((Integer.parseInt(port) - 1) / treebranch);
+ String hidenchild=ls.getLast();
+ String newparent=ls.get(Integer.parseInt(handover));
+ ls.remove(Integer.parseInt(port));
+ ls.add(Integer.parseInt(port),ls.getLast());
+ ls.remove(Integer.parseInt(port));
+ i--;
+ t++;
+ runflag = true;
+ if(t % treebranch == 0){
+ p--;
+ }
+ if((i-1)%treebranch==1){
+ sendleaderflag = "1";
+ }else{
+ sendleaderflag = "0";
+ }
+ /*
+ if(temp!=null){
+ System.out.println("leaderに返信します");
+ is =temp;
+ line = is.readLine();
+ port = is.readLine();
+ os.println(ls.get(Integer.parseInt(port)));
+ os.println(port);
+ runflag=false;
+ temp=null;
+ }
+ */
+
+ child child = new child();
+ child.clost(hidenchild,newparent,os,port,handover,sendleaderflag,i);
+ os.println(port);
+ Thread.sleep(intv_time);
+ is.close();
+ os.close();
+
+ }else if("2".equals(line)){
+ System.out.println("子供が落ちましたmessage" + port);
+ //exit シグナルを送る
+ Thread.sleep(intv_time);
+ }else if("3".equals(line)){
+ System.out.println("落ちたのを確認しました");
+
+ os.println(ls.get(Integer.parseInt(port)));
+ os.println(port);
+ line=null;
+ runflag = false;
+ is.close();
+ os.close();
+ }else{
+ if(addrRegistor==true){
+ ls.add(add);
+ addrRegistor = false;
+ }
+ System.out.println(p);
+ if(line != null){
+ sock conf = new sock();
+ conf.arg(line,ls);
+ i++;
+ }else{
+ break;
+ }
+
+ if(i>=treebranch+1){
+ if((i-1)%treebranch==0){
+ leaderflag = "1";
+ }else{
+ leaderflag = "0";
+ }
+ if((i-1)%treebranch==0){
+ p++;
+ }
+ // request = [p-1];
+ request = ls.get(p);
+ os.println(request);
+ System.out.println("pの値="+p);
+ os.println(String.valueOf(p));
+ System.out.println("iの値="+i);
+ os.println(String.valueOf(i));
+ System.out.println("leaderflag="+leaderflag + "\n");
+ os.println(leaderflag);
+ }else{
+ //treeの親ノードに接続する人に接続する人を教える
+
+ os.println(add);//あとで渡されたmainArgs[1](現在はname)を渡す予定
+ os.println("0");
+ os.println(String.valueOf(i));
+ os.println(leaderflag + "\n");
+ }
+ Thread.sleep(intv_time);
+ }
+ }
+ }catch (IOException e){
+ System.out.println(e);
+ }
+ catch(InterruptedException e){
+ e.printStackTrace();
+ }
+ try{
+ echoServer.close();
+ }
+ catch (IOException e){
+ System.out.println(e);
+ }
+ }
+ }
+}
+
+
+class sock{
+ void arg(String line,LinkedList ls){
+ if(line != null){
+ ls.add(line);
+ }
+ int g=0;
+
+ for(String bs: ls){
+ System.out.println(g+"番目"+bs);
+ g++;
+ }
+ }
+}
+
+class child{
+ Socket echoSocket;
+ DataOutputStream os = null;
+ BufferedReader is = null;
+ void clost(String hiddenchild,String newparent,PrintStream rep,String newtreenum,String newpnum,String newleaderflag,int i) throws IOException{
+ try {
+
+ System.out.println(hiddenchild + "に接続します");
+ echoSocket = new Socket(hiddenchild, 10001 + (i + 1));//i+1は実験中に同じマシーンを使っていたのでportを変えて対応、本番時には取り除く予定。
+
+ os = new DataOutputStream(echoSocket.getOutputStream());
+ is = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
+
+ os.writeBytes(newparent+"\n");
+ os.writeBytes(newpnum+"\n");
+ os.writeBytes(newtreenum+"\n");
+ os.writeBytes(newleaderflag+"\n");
+
+ /*
+ rep.println(newparent);
+ rep.println(newpnum);
+ rep.println(newtreenum);
+ rep.println(newleaderflag+"\n");
+
+ rep.close();
+ */
+ } catch (UnknownHostException e) {
+ System.err.println("Don't know about host: localhost");
+ } catch (IOException e) {
+ System.err.println("Couldn't get I/O for the connection to: localhost");
+ }
+
+ }
+}
\ No newline at end of file