comparison src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncProtocol.java @ 331:42fcc9419498

add error message, add lostParent case by fail to connectTo.
author oc
date Tue, 03 Feb 2015 12:31:48 +0900
parents 9d1ed0099350
children dff2f92ae3ff
comparison
equal deleted inserted replaced
330:9d1ed0099350 331:42fcc9419498
30 // echoSocket.setReuseAddress(true); 30 // echoSocket.setReuseAddress(true);
31 os = new DataOutputStream(echoSocket.getOutputStream()); 31 os = new DataOutputStream(echoSocket.getOutputStream());
32 is = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); 32 is = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
33 } 33 }
34 34
35 public void findRootReply(int port) throws IOException { 35 public void findRootReply(int port) {
36 sendWithHostAndPort(TreeCommand.FIND_ROOT_REPLY, null, port, (short) 0); 36 try {
37 sendWithHostAndPort(TreeCommand.FIND_ROOT_REPLY, null, port, (short) 0);
38 } catch (IOException e) {
39 e.printStackTrace();
40 System.out.println("cannot send findRootReplay");
41 }
37 } 42 }
38 43
39 public void whereToConnect(String hostname, int port) throws IOException { 44 public void whereToConnect(String hostname, int port) {
40 sendWithHostAndPort(TreeCommand.WHERE_TO_CONNECT, hostname, port, (short) 0); 45 try {
46 sendWithHostAndPort(TreeCommand.WHERE_TO_CONNECT, hostname, port, (short) 0);
47 } catch (IOException e) {
48 e.printStackTrace();
49 System.out.println("cannot send whereToConnect");
50 }
41 } 51 }
42 52
43 public void connectTo(String hostname, int port,int leaderFlag, short yourId) throws IOException{ 53 public void connectTo(String hostname, int port,int leaderFlag, short yourId) {
44 TreeCommand command = leaderFlag == 1 ? TreeCommand.CONNECT_TO_AS_LEADER : TreeCommand.CONNECT_TO; 54 TreeCommand command = leaderFlag == 1 ? TreeCommand.CONNECT_TO_AS_LEADER : TreeCommand.CONNECT_TO;
45 sendWithHostAndPort(command, hostname, port, yourId); 55 try {
56 sendWithHostAndPort(command, hostname, port, yourId);
57 } catch (IOException e) {
58 e.printStackTrace();
59 System.out.println("cannot send connectTo");
60 }
46 } 61 }
47 62
48 public void lostParent(String hostname, int port) throws IOException { 63 public void lostParent(String hostname, int port) {
49 sendWithHostAndPort(TreeCommand.LOST_PARENT, hostname, port, (short) 0); 64 try {
65 sendWithHostAndPort(TreeCommand.LOST_PARENT, hostname, port, (short) 0);
66 } catch (IOException e) {
67 e.printStackTrace();
68 System.out.println("cannot send lostParent");
69 }
50 } 70 }
51 71
52 public void lostLeftChild(String hostname, int port) { 72 public void lostLeftChild(String hostname, int port) {
53 sendWithHostAndPort(TreeCommand.LOST_LEFT_CHILD, hostname, port, (short) 0); 73 try {
74 sendWithHostAndPort(TreeCommand.LOST_LEFT_CHILD, hostname, port, (short) 0);
75 } catch (IOException e) {
76 e.printStackTrace();
77 System.out.println("cannot send lostLeftChild");
78 }
54 } 79 }
55 80
56 public void lostRightChild(String hostname, int port) { 81 public void lostRightChild(String hostname, int port) {
57 sendWithHostAndPort(TreeCommand.LOST_RIGHT_CHILD, hostname, port, (short) 0); 82 try {
83 sendWithHostAndPort(TreeCommand.LOST_RIGHT_CHILD, hostname, port, (short) 0);
84 } catch (IOException e) {
85 e.printStackTrace();
86 System.out.println("cannot send lostRightChild");
87 }
58 } 88 }
59 89
60 public void sendWithHostAndPort(TreeCommand command, String hostname, int port, short value) { 90 public void sendWithHostAndPort(TreeCommand command, String hostname, int port, short value) throws IOException {
61 try {
62 openport(); 91 openport();
63 if (hostname == null) { 92 if (hostname == null) {
64 // in case of root finder, we can't get localaddress from datagram packet. 93 // in case of root finder, we can't get localaddress from datagram packet.
65 // so use local part of TCP socket. 94 // so use local part of TCP socket.
66 hostname = echoSocket.getLocalAddress().getHostAddress(); 95 hostname = echoSocket.getLocalAddress().getHostAddress();
76 buf.putInt(port); 105 buf.putInt(port);
77 buf.put(hostname.getBytes(), 0, hostname.length()); 106 buf.put(hostname.getBytes(), 0, hostname.length());
78 while (buf.hasRemaining()) buf.put((byte) 0); 107 while (buf.hasRemaining()) buf.put((byte) 0);
79 buf.flip(); 108 buf.flip();
80 sendCommandToTheRoot(buf); 109 sendCommandToTheRoot(buf);
81 } catch (IOException e) {
82 System.out.println("cannot send command to the root.");
83 streamClose();
84 }
85 } 110 }
86 111
87 public void sendCommandToTheRoot(ByteBuffer buf) throws IOException { 112 public void sendCommandToTheRoot(ByteBuffer buf) throws IOException {
88 char[] charBuf = new char[12]; 113 char[] charBuf = new char[12];
89 is.read(charBuf, 0, 12); // skip root's version header 114 is.read(charBuf, 0, 12); // skip root's version header