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