summaryrefslogtreecommitdiff
path: root/src/com/encrox/twitchbot/client/Client.java
blob: 557517ebbe5fa2610a79ad37e691d8f4e1fce63f (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.encrox.twitchbot.client;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;

import com.encrox.twitchbot.client.messages.Message;

public class Client implements Runnable {
	
	private volatile Socket socket, iSocket;
	private volatile BufferedInputStream bis = null;
	private volatile DataOutputStream dos = null;
	private volatile DataOutputStream iDos = null;
	
	public int connect() {
		try {
			socket = new Socket("encrox.com", 5424);
			bis = new BufferedInputStream(socket.getInputStream());
			dos = new DataOutputStream(socket.getOutputStream());
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return MessageTypes.ERROR;
		} catch (IOException e) {
			e.printStackTrace();
			return MessageTypes.ERROR;
		}
		Runtime.getRuntime().addShutdownHook(new Thread(this));
		return MessageTypes.OK;
	}
	
	public void login() {
		try {
			iSocket = new Socket("encrox.com", 5425);
			iDos = new DataOutputStream(iSocket.getOutputStream());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void post(String message) {
		try {
			iDos.writeInt(message.length());
			iDos.write(message.getBytes("UTF-8"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void write(byte[] data) {
		try {
			dos.write(data);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public int send(Message message) {
		try {
			dos.write(message.getID());
			dos.write(message.getData());
			byte[] res = new byte[4];
			bis.read(res);
			System.out.println(Utils.ba_to_int(res));
			return Utils.ba_to_int(res);
		} catch (IOException e) {
			e.printStackTrace();
			return MessageTypes.ERROR;
		}
	}
	
	public IRCMessage getMessage() {
		byte[] user = new byte[32];
		byte level;
		byte[] message;
		byte[] buffer;
		try {
			bis.read(user);
			buffer = new byte[1];
			bis.read(buffer);
			level = buffer[0];
			buffer = new byte[4];
			bis.read(buffer);
			message = new byte [Utils.ba_to_int(buffer)];
			bis.read(message);
			return new IRCMessage(new String(Utils.crop(user), "UTF-8"), level, new String(message, "UTF-8"));
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void run() {
		try {
			bis.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			dos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			iDos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			iSocket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}