-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteServer.java
More file actions
59 lines (51 loc) · 1.95 KB
/
RemoteServer.java
File metadata and controls
59 lines (51 loc) · 1.95 KB
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
import java.io.ObjectStreamClass;
import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;
public class RemoteServer extends UnicastRemoteObject implements ServerIn {
private List<String> clientsForBroadcast;
public int id = 1;
public RemoteServer() throws RemoteException {
clientsForBroadcast = new ArrayList<>();
}
@Override
public int getId() throws RemoteException {
return id++;
}
@Override
public void welcome(String name) throws RemoteException{
clientsForBroadcast.add(name);
}
@Override
public void boardCastToAll(String message, String name) throws RemoteException {
System.out.println(name + ": " + message);
}
@Override
public void clientToAll(String name, String s) throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry(Server.hostname, Server.port);
for(int i = 0; i < clientsForBroadcast.size(); i++){
if(clientsForBroadcast.equals(name)) continue;
ClientIn c = (ClientIn)registry.lookup(clientsForBroadcast.get(i));
c.receiveMessage(s);
}
}
@Override
public void removeClient(String name) throws RemoteException, NotBoundException {
for(int i = 0; i < clientsForBroadcast.size(); i++){
if(clientsForBroadcast.get(i).equals((name))){
clientsForBroadcast.remove(i);
System.out.println(name + " left the chatRoom");
clientToAll(name, name + " left the chatRoom");
}
}
}
public void setNameAndId(String name, int ID) throws RemoteException {
System.out.println(name+" has joined the ChatRoom");
System.out.println(name+"'s ID is ="+ID);
}
}