[eb9300b] | 1 | package common;
|
---|
| 2 |
|
---|
| 3 | import java.net.*;
|
---|
| 4 | import java.io.*;
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
| 7 | * 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
|
---|
| 8 | * method, run, initializes a connection based on the constructor arguments and calls different methods depending on the result, which could
|
---|
| 9 | * range from a successful connection, to an unsuccessful one, to one that gets interrupted later on. Any class inheriting this one simply
|
---|
| 10 | * has to extend the methods called in run and the processMessage method to customize the behavior in different situations.
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | public class Connection extends Thread {
|
---|
| 14 | private boolean connected;
|
---|
| 15 | private boolean interrupted;
|
---|
| 16 | private String ip;
|
---|
| 17 | private int port;
|
---|
| 18 | private Socket socket;
|
---|
| 19 | private PrintWriter out;
|
---|
| 20 | private BufferedReader in;
|
---|
| 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 |
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | protected void connectionStart() {
|
---|
| 79 |
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | protected void connectionSuccess() {
|
---|
| 83 |
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | protected void connectionFailure(String str) {
|
---|
| 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 | connectionFailure("about to make new socket");
|
---|
| 109 | socket = new Socket(ip, port);
|
---|
| 110 | }
|
---|
| 111 | connectionFailure("made new socket");
|
---|
| 112 | if(!interrupted) {
|
---|
| 113 | out = new PrintWriter(socket.getOutputStream(), true);
|
---|
| 114 | in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
---|
| 115 | connected = true;
|
---|
| 116 | connectionSuccess();
|
---|
| 117 | }
|
---|
| 118 | connectionFailure("end of connection");
|
---|
| 119 | }catch(UnknownHostException uhe) {
|
---|
| 120 | if(!interrupted)
|
---|
| 121 | connectionFailure("UnknownHostException");
|
---|
| 122 | }catch(ConnectException ce) {
|
---|
| 123 | if(!interrupted)
|
---|
| 124 | connectionFailure("ConnectException");
|
---|
| 125 | }catch(IOException ioe) {
|
---|
| 126 | if(!interrupted)
|
---|
| 127 | ioe.printStackTrace();
|
---|
| 128 | connectionFailure("IOException");
|
---|
| 129 | }catch(Exception e) {
|
---|
| 130 | connectionFailure("Exception");
|
---|
| 131 | }catch(Throwable e) {
|
---|
| 132 | connectionFailure("Throwable");
|
---|
| 133 | }
|
---|
| 134 | connectionFailure("finished connection");
|
---|
| 135 |
|
---|
| 136 | if(interrupted)
|
---|
| 137 | closeConnection();
|
---|
| 138 |
|
---|
| 139 | try{
|
---|
| 140 | while(connected && (strType = in.readLine()) != null && (str = in.readLine()) != null) {
|
---|
| 141 | processMessage(MessageType.valueOf(strType), str);
|
---|
| 142 | }
|
---|
| 143 | if(connected)
|
---|
| 144 | peerDisconnect();
|
---|
| 145 | }catch(SocketException se) {
|
---|
| 146 | if(connected)
|
---|
| 147 | connectionBreak();
|
---|
| 148 | }catch(IOException ioe) {
|
---|
| 149 | ioe.printStackTrace();
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | closeConnection();
|
---|
| 153 | connectionEnd();
|
---|
| 154 | }
|
---|
| 155 | }
|
---|