Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.rmi.Naming;
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- import java.util.HashMap;
- import java.util.Map;
- public class ChatRegistryImpl implements ChatRegistryInterface
- {
- private Map<String,Remote> db;
- public ChatRegistryImpl()
- {
- db = new HashMap<String,Remote>();
- }
- @Override
- public Remote connectRoom(String roomName, Remote clientListener) throws RemoteException
- {
- try
- {
- RoomServer rs = (RoomServer) db.get(roomName);
- if( rs.addClient(clientListener) )
- return (Remote)rs;
- else
- return null;
- }
- catch( ClassCastException | NullPointerException e)
- {
- return null;
- }
- }
- @Override
- public Remote createRoom(String roomName, Remote clientListener) throws RemoteException
- {
- try
- {
- RoomServer roomServerRemote = new RoomServer();
- if( roomServerRemote.addClient(clientListener) )
- {
- db.put(roomName, roomServerRemote);
- return roomServerRemote;
- }
- else
- return null;
- }
- catch( ClassCastException | NullPointerException e)
- {
- return null;
- }
- }
- @Override
- public String showRooms() throws RemoteException
- {
- // TODO migliorare
- return db.keySet().toString();
- }
- public static void main( String args[] )
- {
- final int REGISTRYPORT = 1099;
- String registryHost = "localhost";
- String serviceName = "ChatRegistry";
- try
- {
- String completeName = "//"+ registryHost + ":" + REGISTRYPORT + "/" + serviceName;
- ChatRegistryImpl serverRMI = new ChatRegistryImpl();
- Naming.rebind(completeName, serverRMI);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- //TODO
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement