1 | import java.net.*;
|
---|
2 | import java.io.*;
|
---|
3 |
|
---|
4 | /*
|
---|
5 | * This class serves as a basis for the threads that the other projects use to listen to incoming messages over a network. The most important
|
---|
6 | * method, run, initializes a connection based on the constructor arguments and calls different methods depending on the result, which could
|
---|
7 | * range from a successful connection, to an unsuccessful one, to one that gets interrupted later on. Any class inheriting this one simply
|
---|
8 | * has to extend the methods called in run and the processMessage method to customize the behavior in different situations.
|
---|
9 | */
|
---|
10 |
|
---|
11 | public class Connection extends Thread {
|
---|
12 | private boolean connected;
|
---|
13 | private boolean interrupted;
|
---|
14 | private String ip;
|
---|
15 | private int port;
|
---|
16 | private Socket socket;
|
---|
17 | private PrintWriter out;
|
---|
18 | private BufferedReader in;
|
---|
19 | private String name;
|
---|
20 |
|
---|
21 |
|
---|
22 | public Connection(String ip, int port, String threadName) {
|
---|
23 | super(threadName);
|
---|
24 |
|
---|
25 | connected = false;
|
---|
26 | interrupted = false;
|
---|
27 | socket = null;
|
---|
28 | this.ip = ip;
|
---|
29 | this.port = port;
|
---|
30 | }
|
---|
31 |
|
---|
32 | public Connection(Socket socket, String threadName) {
|
---|
33 | super(threadName);
|
---|
34 |
|
---|
35 | connected = false;
|
---|
36 | interrupted = false;
|
---|
37 | this.socket = socket;
|
---|
38 | }
|
---|
39 |
|
---|
40 | public static boolean isConnected(Connection obj) {
|
---|
41 | return obj != null && obj.connected;
|
---|
42 | }
|
---|
43 |
|
---|
44 | protected PrintWriter getOut() {
|
---|
45 | return out;
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected BufferedReader getIn() {
|
---|
49 | return in;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void interrupt() {
|
---|
53 | interrupted = true;
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void closeConnection() {
|
---|
57 | try {
|
---|
58 | if(connected) {
|
---|
59 | connected = false;
|
---|
60 | socket.close();
|
---|
61 | out.close();
|
---|
62 | in.close();
|
---|
63 | }
|
---|
64 | } catch(IOException ioe) {
|
---|
65 | ioe.printStackTrace();
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected void sendMessage(MessageType type, String input) {
|
---|
70 | out.println(type);
|
---|
71 | out.println(input);
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected void processMessage(MessageType type, String input) {
|
---|
75 | System.out.println(input);
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected void connectionStart() {
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected void connectionSuccess() {
|
---|
83 |
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected void connectionFailure() {
|
---|
87 |
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected void connectionBreak() {
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected void peerDisconnect() {
|
---|
95 |
|
---|
96 | }
|
---|
97 |
|
---|
98 | protected void connectionEnd() {
|
---|
99 |
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void run() {
|
---|
103 | String strType, str;
|
---|
104 |
|
---|
105 | try {
|
---|
106 | connectionStart();
|
---|
107 | if(socket == null)
|
---|
108 | socket = new Socket(ip, port);
|
---|
109 | if(!interrupted) {
|
---|
110 | out = new PrintWriter(socket.getOutputStream(), true);
|
---|
111 | in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
---|
112 | connected = true;
|
---|
113 | connectionSuccess();
|
---|
114 | }
|
---|
115 | }catch (UnknownHostException uhe) {
|
---|
116 | if(!interrupted)
|
---|
117 | connectionFailure();
|
---|
118 | }catch (ConnectException ce) {
|
---|
119 | if(!interrupted)
|
---|
120 | connectionFailure();
|
---|
121 | }catch(IOException ioe) {
|
---|
122 | if(!interrupted)
|
---|
123 | ioe.printStackTrace();
|
---|
124 | }
|
---|
125 |
|
---|
126 | if(interrupted)
|
---|
127 | closeConnection();
|
---|
128 |
|
---|
129 | try{
|
---|
130 | while(connected && (strType = in.readLine()) != null && (str = in.readLine()) != null) {
|
---|
131 | processMessage(MessageType.valueOf(strType), str);
|
---|
132 | }
|
---|
133 | if(connected)
|
---|
134 | peerDisconnect();
|
---|
135 | }catch(SocketException se) {
|
---|
136 | if(connected)
|
---|
137 | connectionBreak();
|
---|
138 | }catch(IOException ioe) {
|
---|
139 | ioe.printStackTrace();
|
---|
140 | }
|
---|
141 |
|
---|
142 | closeConnection();
|
---|
143 | connectionEnd();
|
---|
144 | }
|
---|
145 | }
|
---|