comparison src/main/java/alice/daemon/Connection.java @ 417:aadea6a59376 dispose

create MetaCodeSegment use reflection
author sugi
date Tue, 15 Jul 2014 00:06:10 +0900
parents 8f71c3e6f11d
children aefbe41fcf12
comparison
equal deleted inserted replaced
416:6508ed6ea6a6 417:aadea6a59376
1 package alice.daemon; 1 package alice.daemon;
2 2
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.net.Socket; 4 import java.net.Socket;
5 import java.nio.ByteBuffer; 5 import java.nio.ByteBuffer;
6 import java.nio.channels.ClosedChannelException;
6 import java.util.concurrent.LinkedBlockingQueue; 7 import java.util.concurrent.LinkedBlockingQueue;
7 8
8 import alice.codesegment.SingletonMessage; 9 import alice.codesegment.SingletonMessage;
9 import alice.datasegment.Command; 10 import alice.datasegment.Command;
11 import alice.datasegment.DataSegment;
10 12
11 public class Connection { 13 public class Connection {
12 14
13 public Socket socket; 15 public Socket socket;
14 public LinkedBlockingQueue<Command> sendQueue = new LinkedBlockingQueue<Command>(); 16 public LinkedBlockingQueue<Command> sendQueue = new LinkedBlockingQueue<Command>();
15 17
16 public Connection(Socket socket) { 18 public Connection(Socket socket) {
17 this.socket = socket; 19 this.socket = socket;
18 } 20 }
19 21
20 public Connection() {} 22 public Connection() {}
21 23
22 public void sendCommand(Command cmd) { 24 public void sendCommand(Command cmd) {
23 try { 25 try {
24 sendQueue.put(cmd); 26 sendQueue.put(cmd);
25 } catch (InterruptedException e) { 27 } catch (InterruptedException e) {
26 e.printStackTrace(); 28 e.printStackTrace();
27 } 29 }
28 } 30 }
29 31
30 public String getInfoString() { 32 public String getInfoString() {
31 return socket.getInetAddress().getHostName() 33 return socket.getInetAddress().getHostName()
32 + ":" + socket.getPort(); 34 + ":" + socket.getPort();
33 } 35 }
34 36
35 public synchronized void write(Command cmd) { 37 public synchronized void write(Command cmd) {
36 CommandMessage cmdMsg = cmd.convert(); 38 CommandMessage cmdMsg = cmd.convert();
37 ByteBuffer buffer; 39 ByteBuffer buffer;
38 try { 40 try {
39 buffer = ByteBuffer.wrap(SingletonMessage.getInstance().write(cmdMsg)); 41 buffer = ByteBuffer.wrap(SingletonMessage.getInstance().write(cmdMsg));
40 while (buffer.hasRemaining()) { 42 while (buffer.hasRemaining()) {
41 socket.getChannel().write(buffer); 43 socket.getChannel().write(buffer);
42 } 44 }
45 } catch (ClosedChannelException e) {
46 // when put dataSegment to remote if connection close this dataSemgent put.
47 putConnectionInfo();
48
43 } catch (IOException e) { 49 } catch (IOException e) {
44 e.printStackTrace(); 50 e.printStackTrace();
45 } 51 }
46 } 52 }
47 53
48 public void close(){ 54 public void close(){
49 try { 55 try {
50 socket.shutdownOutput(); 56 socket.shutdownOutput();
51 socket.shutdownInput(); 57 socket.shutdownInput();
52 socket.close(); 58 socket.close();
59 } catch (ClosedChannelException e) {
60 putConnectionInfo();
53 } catch (IOException e) { 61 } catch (IOException e) {
54 e.printStackTrace(); 62 e.printStackTrace();
55 } 63 }
64
65 }
66
67 public void putConnectionInfo() {
68 ConnectionInfo c = new ConnectionInfo(socket.getInetAddress().toString() ,socket.getPort());
69 DataSegment.getLocal().put("disconnect", c);
56 70
57 } 71 }
58 } 72 }