[04e7260] | 1 | import java.net.*;
|
---|
| 2 | import java.io.*;
|
---|
| 3 |
|
---|
[d6c8eb6] | 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 |
|
---|
[04e7260] | 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 |
|
---|
| 20 | public Connection(String ip, int port, String threadName) {
|
---|
| 21 | super(threadName);
|
---|
| 22 |
|
---|
| 23 | connected = false;
|
---|
| 24 | interrupted = false;
|
---|
| 25 | socket = null;
|
---|
| 26 | this.ip = ip;
|
---|
| 27 | this.port = port;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | public Connection(Socket socket, String threadName) {
|
---|
| 31 | super(threadName);
|
---|
| 32 |
|
---|
| 33 | connected = false;
|
---|
| 34 | interrupted = false;
|
---|
| 35 | this.socket = socket;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public static boolean isConnected(Connection obj) {
|
---|
| 39 | return obj != null && obj.connected;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | protected PrintWriter getOut() {
|
---|
| 43 | return out;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | protected BufferedReader getIn() {
|
---|
| 47 | return in;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public void interrupt() {
|
---|
| 51 | interrupted = true;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public void closeConnection() {
|
---|
| 55 | try {
|
---|
| 56 | if(connected) {
|
---|
| 57 | connected = false;
|
---|
| 58 | socket.close();
|
---|
| 59 | out.close();
|
---|
| 60 | in.close();
|
---|
| 61 | }
|
---|
| 62 | } catch(IOException ioe) {
|
---|
| 63 | ioe.printStackTrace();
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | protected void sendMessage(MessageType type, String input) {
|
---|
| 68 | out.println(type);
|
---|
| 69 | out.println(input);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | protected void processMessage(MessageType type, String input) {
|
---|
| 73 |
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | protected void connectionStart() {
|
---|
| 77 |
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | protected void connectionSuccess() {
|
---|
| 81 |
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | protected void connectionFailure() {
|
---|
| 85 |
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | protected void connectionBreak() {
|
---|
| 89 |
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | protected void peerDisconnect() {
|
---|
| 93 |
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | protected void connectionEnd() {
|
---|
| 97 |
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public void run() {
|
---|
| 101 | String strType, str;
|
---|
| 102 |
|
---|
| 103 | try {
|
---|
| 104 | connectionStart();
|
---|
| 105 | if(socket == null)
|
---|
| 106 | socket = new Socket(ip, port);
|
---|
| 107 | if(!interrupted) {
|
---|
| 108 | out = new PrintWriter(socket.getOutputStream(), true);
|
---|
| 109 | in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
---|
| 110 | connected = true;
|
---|
| 111 | connectionSuccess();
|
---|
| 112 | }
|
---|
| 113 | }catch (UnknownHostException uhe) {
|
---|
| 114 | if(!interrupted)
|
---|
| 115 | connectionFailure();
|
---|
| 116 | }catch (ConnectException ce) {
|
---|
| 117 | if(!interrupted)
|
---|
| 118 | connectionFailure();
|
---|
| 119 | }catch(IOException ioe) {
|
---|
| 120 | if(!interrupted)
|
---|
| 121 | ioe.printStackTrace();
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | if(interrupted)
|
---|
| 125 | closeConnection();
|
---|
| 126 |
|
---|
| 127 | try{
|
---|
| 128 | while(connected && (strType = in.readLine()) != null && (str = in.readLine()) != null) {
|
---|
| 129 | processMessage(MessageType.valueOf(strType), str);
|
---|
| 130 | }
|
---|
| 131 | if(connected)
|
---|
| 132 | peerDisconnect();
|
---|
| 133 | }catch(SocketException se) {
|
---|
| 134 | if(connected)
|
---|
| 135 | connectionBreak();
|
---|
| 136 | }catch(IOException ioe) {
|
---|
| 137 | ioe.printStackTrace();
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | closeConnection();
|
---|
| 141 | connectionEnd();
|
---|
| 142 | }
|
---|
| 143 | }
|
---|