|
1 | | -package com.tlantic.plugins.socket; |
2 | | - |
3 | | -import java.io.BufferedReader; |
4 | | -import java.io.IOException; |
5 | | -import java.io.InputStreamReader; |
6 | | -import java.io.PrintWriter; |
7 | | -import java.net.Socket; |
8 | | -import java.net.UnknownHostException; |
9 | | - |
10 | | - |
11 | | -/** |
12 | | - * @author viniciusl |
13 | | - * |
14 | | - * This class represents a socket connection, behaving like a thread to listen |
15 | | - * a TCP port and receive data |
16 | | - */ |
17 | | -public class Connection extends Thread { |
18 | | - private SocketPlugin hook; |
19 | | - |
20 | | - private Socket callbackSocket; |
21 | | - private PrintWriter writer; |
22 | | - private BufferedReader reader; |
23 | | - |
24 | | - private Boolean mustClose; |
25 | | - private String host; |
26 | | - private int port; |
27 | | - |
28 | | - |
29 | | - /** |
30 | | - * Creates a TCP socket connection object. |
31 | | - * |
32 | | - * @param pool Object containing "sendMessage" method to be called as a callback for data receive. |
33 | | - * @param host Target host for socket connection. |
34 | | - * @param port Target port for socket connection |
35 | | - */ |
36 | | - public Connection(SocketPlugin pool, String host, int port) { |
37 | | - super(); |
38 | | - setDaemon(true); |
39 | | - |
40 | | - this.mustClose = false; |
41 | | - this.host = host; |
42 | | - this.port = port; |
43 | | - this.hook = pool; |
44 | | - } |
45 | | - |
46 | | - |
47 | | - /** |
48 | | - * Returns socket connection state. |
49 | | - * |
50 | | - * @return true if socket connection is established or false case else. |
51 | | - */ |
52 | | - public boolean isConnected() { |
53 | | - return this.callbackSocket.isConnected(); |
54 | | - } |
55 | | - |
56 | | - /** |
57 | | - * Closes socket connection. |
58 | | - */ |
59 | | - public void close() { |
60 | | - // closing connection |
61 | | - try { |
62 | | - this.mustClose = true; |
63 | | - this.writer.close(); |
64 | | - this.reader.close(); |
65 | | - callbackSocket.close(); |
66 | | - } catch (IOException e) { |
67 | | - e.printStackTrace(); |
68 | | - } |
69 | | - } |
70 | | - |
71 | | - |
72 | | - /** |
73 | | - * Writes on socket output stream to send data to target host. |
74 | | - * |
75 | | - * @param data information to be sent |
76 | | - */ |
77 | | - public void write(String data) { |
78 | | - this.writer.println(data); |
79 | | - } |
80 | | - |
81 | | - |
82 | | - |
83 | | - /* (non-Javadoc) |
84 | | - * @see java.lang.Thread#run() |
85 | | - */ |
86 | | - public void run() { |
87 | | - String chunk = null; |
88 | | - |
89 | | - // creating connection |
90 | | - try { |
91 | | - this.callbackSocket = new Socket(this.host, this.port); |
92 | | - this.writer = new PrintWriter(this.callbackSocket.getOutputStream(), true); |
93 | | - this.reader = new BufferedReader(new InputStreamReader(callbackSocket.getInputStream())); |
94 | | - |
95 | | - // receiving data chunk |
96 | | - while(!this.mustClose){ |
97 | | - |
98 | | - try { |
99 | | - chunk = reader.readLine().replaceAll("\"\"", "null"); |
100 | | - System.out.print("## RECEIVED DATA: " + chunk); |
101 | | - hook.sendMessage(this.host, this.port, chunk); |
102 | | - } catch (IOException e) { |
103 | | - e.printStackTrace(); |
104 | | - } |
105 | | - } |
106 | | - |
107 | | - } catch (UnknownHostException e1) { |
108 | | - // TODO Auto-generated catch block |
109 | | - e1.printStackTrace(); |
110 | | - } catch (IOException e1) { |
111 | | - // TODO Auto-generated catch block |
112 | | - e1.printStackTrace(); |
113 | | - } |
114 | | - |
115 | | - } |
116 | | - |
117 | | -} |
| 1 | +package com.tlantic.plugins.socket; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.io.PrintWriter; |
| 7 | +import java.net.Socket; |
| 8 | +import java.net.UnknownHostException; |
| 9 | + |
| 10 | + |
| 11 | +/** |
| 12 | + * @author viniciusl |
| 13 | + * |
| 14 | + * This class represents a socket connection, behaving like a thread to listen |
| 15 | + * a TCP port and receive data |
| 16 | + */ |
| 17 | +public class Connection extends Thread { |
| 18 | + private SocketPlugin hook; |
| 19 | + |
| 20 | + private Socket callbackSocket; |
| 21 | + private PrintWriter writer; |
| 22 | + private BufferedReader reader; |
| 23 | + |
| 24 | + private Boolean mustClose; |
| 25 | + private String host; |
| 26 | + private int port; |
| 27 | + |
| 28 | + |
| 29 | + /** |
| 30 | + * Creates a TCP socket connection object. |
| 31 | + * |
| 32 | + * @param pool Object containing "sendMessage" method to be called as a callback for data receive. |
| 33 | + * @param host Target host for socket connection. |
| 34 | + * @param port Target port for socket connection |
| 35 | + */ |
| 36 | + public Connection(SocketPlugin pool, String host, int port) { |
| 37 | + super(); |
| 38 | + setDaemon(true); |
| 39 | + |
| 40 | + this.mustClose = false; |
| 41 | + this.host = host; |
| 42 | + this.port = port; |
| 43 | + this.hook = pool; |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns socket connection state. |
| 49 | + * |
| 50 | + * @return true if socket connection is established or false case else. |
| 51 | + */ |
| 52 | + public boolean isConnected() { |
| 53 | + |
| 54 | + boolean result = ( |
| 55 | + this.callbackSocket == null ? false : |
| 56 | + this.callbackSocket.isConnected() && |
| 57 | + this.callbackSocket.isBound() && |
| 58 | + !this.callbackSocket.isClosed() && |
| 59 | + !this.callbackSocket.isInputShutdown() && |
| 60 | + !this.callbackSocket.isOutputShutdown()); |
| 61 | + |
| 62 | + // if everything apparently is fine, time to test the streams |
| 63 | + if (result) { |
| 64 | + try { |
| 65 | + this.callbackSocket.getInputStream().available(); |
| 66 | + } catch (IOException e) { |
| 67 | + // connection lost |
| 68 | + result = false; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return result; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Closes socket connection. |
| 77 | + */ |
| 78 | + public void close() { |
| 79 | + // closing connection |
| 80 | + try { |
| 81 | + //this.writer.close(); |
| 82 | + //this.reader.close(); |
| 83 | + callbackSocket.shutdownInput(); |
| 84 | + callbackSocket.shutdownOutput(); |
| 85 | + callbackSocket.close(); |
| 86 | + this.mustClose = true; |
| 87 | + } catch (IOException e) { |
| 88 | + e.printStackTrace(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + /** |
| 94 | + * Writes on socket output stream to send data to target host. |
| 95 | + * |
| 96 | + * @param data information to be sent |
| 97 | + */ |
| 98 | + public void write(String data) { |
| 99 | + this.writer.println(data); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + /* (non-Javadoc) |
| 105 | + * @see java.lang.Thread#run() |
| 106 | + */ |
| 107 | + public void run() { |
| 108 | + String chunk = null; |
| 109 | + |
| 110 | + // creating connection |
| 111 | + try { |
| 112 | + this.callbackSocket = new Socket(this.host, this.port); |
| 113 | + this.writer = new PrintWriter(this.callbackSocket.getOutputStream(), true); |
| 114 | + this.reader = new BufferedReader(new InputStreamReader(callbackSocket.getInputStream())); |
| 115 | + |
| 116 | + // receiving data chunk |
| 117 | + while(!this.mustClose){ |
| 118 | + |
| 119 | + try { |
| 120 | + |
| 121 | + if (this.isConnected()) { |
| 122 | + chunk = reader.readLine(); |
| 123 | + |
| 124 | + if (chunk != null) { |
| 125 | + chunk = chunk.replaceAll("\"\"", "null"); |
| 126 | + System.out.print("## RECEIVED DATA: " + chunk); |
| 127 | + hook.sendMessage(this.host, this.port, chunk); |
| 128 | + } |
| 129 | + } |
| 130 | + } catch (Exception e) { |
| 131 | + e.printStackTrace(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + } catch (UnknownHostException e1) { |
| 136 | + // TODO Auto-generated catch block |
| 137 | + e1.printStackTrace(); |
| 138 | + } catch (IOException e1) { |
| 139 | + // TODO Auto-generated catch block |
| 140 | + e1.printStackTrace(); |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments