Blame view

JChatServer/src/pad/prac2/Connection.java 2.66 KB
1
2
package pad.prac2;
3
4
import java.io.IOException;
5
public class Connection extends Thread
6
{
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
	private MySocket socket;
	private Server serv;
	private boolean sleep, kill;

	public Connection(Server s)
	{
		serv = s;
		kill = false;
		sleep = true;
	}

	public void awake()
	{
		synchronized(this)
		{
			sleep = false;
			this.notify();
		}
	}

	public void finish()
	{
		kill = true;
30
31
32
33
34
		if(sleep)
		{
			sleep = false;
			notify();
		}
35
36
37
38
39
40
41
42
		socket.close();
	}

	public void setSock(MySocket s)
	{
		socket = s;
	}
43
44
45
46
47
	public void sendMessage(String message) throws IOException
	{
		socket.sendMsg(message);
	}
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
	public void run()
	{
		while(!kill)
		{
			while(sleep)
			{
				try
				{
					synchronized(this)
					{
						wait();
					}
				} 
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
			if(!kill)
			{
68
69
				String str;
				try
70
71
				{
					str = socket.recvMsg();
72
					while(str.contains(" "))
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
						socket.sendMsg("CHATNICKINVALID");
						str = socket.recvMsg();
					}
					while(serv.isOnline(str))
					{
						socket.sendMsg("CHATNICKEXIST");
						str = socket.recvMsg();
					}
					socket.sendMsg("CHATOK");
					serv.addToChatroom(this, str);
				} 
				catch (IOException e)
				{
					if(!kill)
					{
						System.out.println("TCP: Communication with client failed while entering chatroom");
						sleep = true;
						serv.finishWorker(this);
					}
					continue;
				}
				while(true)
				{
					try
					{
						str = "";
						str = socket.recvMsg();
						if(str.equals("/disconnect"))
102
						{
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
							socket.sendMsg("DISC_OK");
							System.out.println(serv.getNickname(this) + " disconnected");
							break;
						}
						else if(str.equals("/exit"))
						{
							socket.sendMsg("EXIT_OK");
							System.out.println(serv.getNickname(this) + " disconnected");
							break;
						}
						else if(str.equals("/who"))
						{
							socket.sendMsg(serv.listOnline());
						}
						else if(str.startsWith("@"))
						{
							try
120
							{
121
122
123
124
125
126
127
128
								if(!str.contains(" "))
								{
									serv.sendTo(this,str.substring(1),"");
								}
								else
								{
									serv.sendTo(this,str.substring(1,str.indexOf(' ')),str.substring(str.indexOf(' ')+1));
								}
129
							}
130
							catch(ChatException cE)
131
							{
132
								socket.sendMsg(cE.getMessage());
133
							}
134
						}
135
						else if(str.equals(""))
136
						{
137
							break;
138
						}
139
140
141
142
143
						else
						{
							System.out.println("FROM " + serv.getNickname(this) + ": " + str);
							serv.sendToChat(this,str);
						}
144
					}
145
					catch(IOException ioExc)
146
					{
147
						System.out.println("TCP: Error writing to socket");
148
149
150
151
152
153
154
155
						break;
					}
				}
				sleep = true;
			}
			serv.finishWorker(this);
		}
	}
156
}