๐Ÿ“ฆ ShaneK / IRC-Bot

๐Ÿ“„ GenericClient.java ยท 105 lines
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
105package Main;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

import Interface.ServerCallbackClass;
import Misc.Constants;

/**
 * @author Shane
 *
 * Generic client for use with generic server. Automagically determines if server is encrypted and responds appropriately.
 */

public class GenericClient implements Runnable {
	private String IP = null;
	private int port = 0;
	private boolean encrypted = false;
	private Socket connection = null;
	private BufferedReader serverIn;
	private DataOutputStream serverOut;
	private ServerCallbackClass callbackMachine = null;
	private int messagesSent = 0;
	private int messagesReceived = 0;
	private BufferedInputStream serverInputForByteReading;
	
	//Has to do with reading bytes instead of strings
	private boolean readingBytes = false;
	private int numOfBytes = 0;
	
	public GenericClient(String serverIP, int port, ServerCallbackClass scc) {
		this.IP = serverIP;
		this.port = port;
		try{
			connection = new Socket(serverIP, port);
			serverIn = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			serverOut = new DataOutputStream(connection.getOutputStream());
			serverInputForByteReading = new BufferedInputStream(connection.getInputStream());
			this.callbackMachine = scc;
			if(scc != null)
				scc.connectedToServer();
			(new Thread(this)).start();
		}catch(Exception e){
			e.printStackTrace();
			if(this.callbackMachine != null)
				this.callbackMachine.couldNotConnectToServer();
		}
	}
	
	public void sendMessage(String message){
		try{
			this.serverOut.writeBytes(message+"\n");
			this.callbackMachine.sentMessageToServer(message, ++messagesSent);
		}catch(Exception e){
			e.printStackTrace();
			if(this.callbackMachine != null){
				this.callbackMachine.serverConnectionDied(messagesSent, messagesReceived);
			}
		}
	}

	@Override
	public void run() {
		System.out.println("Being awesome.");
		try{
			while(true){
				if(readingBytes){
					System.out.println("Reading bytes now1");
					byte[] message = new byte[numOfBytes];
					System.out.println("Declared variable, reading into byte[]");
					serverInputForByteReading.read(message);
					System.out.println("Read Bytes, saying OKAY");
					serverOut.writeBytes(Constants.OKAY+'\n');
					System.out.println("Said OKAY; Bytes read: "+new String(message));
				}else{
					System.out.println("Waiting for message");
					String message = serverIn.readLine();
					if(this.callbackMachine != null){
						this.callbackMachine.receivedMessageFromServer(message, ++messagesReceived);
					}
				}
			}
		}catch(Exception e){
			if(this.callbackMachine != null){
				this.callbackMachine.serverConnectionDied(messagesSent, ++messagesReceived);
			}
		}
	}
	
	public int getPort(){
		return this.port;
	}
	
	public String getIP(){
		return this.IP;
	}
	
	public boolean isEncrypted(){
		return this.encrypted;
	}
}