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