summaryrefslogtreecommitdiff
path: root/src/Client/ChatClient.java
blob: 7872c948798688acae68e7e0335821df80b28bf7 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package Client;
import java.io.*;
import java.net.Socket;

public class ChatClient
{
	private String nick;
	private String realname;
	private	ChatClientWriter writer;
	private	UserInterface ui;
	private Socket soc;
	private boolean isConnected = false;
	
	private static final String CHANNEL = "#Dor-chat";
	
	ChatClient() {
		this.soc = null;
	}
	
	/** returning the nick name */
	public String getNick ()
	{
		return this.nick;
	}
	/** returning the real name */
	public String getRealname ()
	{
		return this.realname;
	}
	/** changing the nick name *
	 * @param nick name the name to set*/
	public void setNick (String nick)
	{
		this.nick = nick;
	}
	/** changing the real name
	 * @param realname the name to set*/
	public void setRealname (String realname)
	{
		this.realname = realname;
	}
	
	/** mark client as connected */
	public void setConnected()
	{
		this.isConnected = true;
	}
	
	/** returning connection */
	public boolean getConnected()
	{
		return this.isConnected;
	}
	
	/** send a line to server */
	public void runSendline (String line)
	{
		if (!this.isConnected)
			return;
		writer.sendLine(line);
	}
	
	/** send line to server */
	private void earlySendline(String line)
	{
		writer.sendLine(line);
	}
	
	/** if the line is special command privmsg will handle it, else it will send the line as a privmsg.  
	 * @param line the line.*/
	public void PRIVMSG (String line)
	{
		String[] words = line.split("[ \t]", 2);
		if (words[0].equalsIgnoreCase("/QUOTE"))
		{
			runSendline(line.substring(7, line.length()));
		}
		
		else if(words[0].equalsIgnoreCase("/CLEAR"))
		{
			ui.Clear();
		}
		
		else
		{
			runSendline("PRIVMSG " + ChatClient.CHANNEL + " :" + line);
		}
	}
	
	/** checking for nick Collisions and handling them. */
	public void fixNickCollision()
	{
		String newNick = getNick() + new Integer((int)(Math.random()*1000)).toString();
		earlySendline("NICK " + newNick);
	}
	
	/** sending nick and user to the writer.*/
	public void startup() {
		earlySendline("NICK " + this.getNick());
		earlySendline("USER " + this.getNick() + " 0 * :" + this.getRealname());		
	}
	
	/** Initial setup with the server once the nick was set up*/
	public void joinChannel()
	{
		// 0: mode (nothing special). *: unused in RFC 2812, server name in RFC 1459
		this.setConnected(); // FIXME: Not sure yet if client is connected.
		runSendline("JOIN " + ChatClient.CHANNEL);
	
	}
	
	/** closing the socket*/
	public void shutdown()
	{
		if (soc != null)
		{
			try
			{
				soc.close();
			} 
			catch (IOException e) {}
		}
	}
	
	/** opening new connection
	 * @param host name.
	 * @param port name. */
	public boolean connect (String host, int port)
	{
		try
		{
			soc = new Socket(host, port);
			ChatClientReader reader = new ChatClientReader(this, soc, ui);
			
			this.writer = new ChatClientWriter(soc,this);

			reader.start();
			writer.start();
			
			return true;
			
		} catch (IOException e)
		{
			System.err.println("Failed connecting to server: " + host + ":" + port + " (" + e + ")");
			return false;
		}
	}
	
	public static void main(String[] args) throws IOException
	{
 		ChatClient client = new ChatClient();
		client.setNick("Dor");
		client.setRealname("Dor bivas");
		client.ui = new UserInterface(client);
	}
}