-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatServerimplement.java
64 lines (60 loc) · 2.02 KB
/
ChatServerimplement.java
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
// Import Java RMI Library
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.*;
public class ChatServerimplement extends UnicastRemoteObject implements ChatServer
{
// Create a Hash Map with Name and Client Object
private Map<String,ChatClient> client_map=new HashMap<>();
// Empty Constructor
public ChatServerimplement() throws RemoteException { }
// Login method to add a Client to the Map
public String[] login(ChatClient client) throws RemoteException
{
String name_cl = client.getName();
// Check if the Client name Already Exists
if(client_map.containsKey(name_cl))
{
throw new RuntimeException("Name already Exists");
}
// Adding each client to the Map
String[] cl_names = list();
client_map.put(name_cl,client);
for(String clientname: cl_names)
{
client_map.get(clientname).joined(name_cl);
}
return cl_names;
}
// To return Client Name List
public String[] list()
{
return client_map.keySet().toArray(new String[client_map.size()]);
}
// To show messages that are communicated from one Client to Another
public void send_Msg(String from, String to, String msg) throws RemoteException
{
client_map.get(to).show_msg(from, msg);
}
// To Broadcast a Message to all Client
public void send_Msg(String from, String msg) throws RemoteException
{
String[] cl_names=list();
for(String clientname: cl_names)
{
send_Msg(from,clientname,msg);
}
}
// To keep track of Clients who are not active
public void logout(String name) throws RemoteException
{
// To remove a Client from Map
client_map.remove(name);
String[] cl_names=list();
for(String clientname: cl_names)
{
client_map.get(clientname).left(name);
}
}
}