Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. import java.rmi.Naming;
  2. import java.rmi.Remote;
  3. import java.rmi.RemoteException;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7.  
  8. public class ChatRegistryImpl implements ChatRegistryInterface
  9. {
  10.     private Map<String,Remote> db;
  11.    
  12.     public ChatRegistryImpl()
  13.     {
  14.         db = new HashMap<String,Remote>();
  15.     }
  16.    
  17.     @Override
  18.     public Remote connectRoom(String roomName, Remote clientListener) throws RemoteException
  19.     {
  20.         try
  21.         {
  22.             RoomServer rs = (RoomServer) db.get(roomName);
  23.            
  24.             if( rs.addClient(clientListener) )
  25.                 return (Remote)rs;
  26.             else
  27.                 return null;
  28.         }
  29.         catch( ClassCastException | NullPointerException e)
  30.         {
  31.             return null;
  32.         }
  33.     }
  34.  
  35.     @Override
  36.     public Remote createRoom(String roomName, Remote clientListener) throws RemoteException
  37.     {
  38.         try
  39.         {
  40.             RoomServer roomServerRemote = new RoomServer();
  41.            
  42.             if( roomServerRemote.addClient(clientListener) )
  43.             {
  44.                 db.put(roomName, roomServerRemote);
  45.                 return roomServerRemote;
  46.             }
  47.             else
  48.                 return null;
  49.            
  50.         }
  51.         catch( ClassCastException | NullPointerException e)
  52.         {
  53.             return null;
  54.         }
  55.     }
  56.  
  57.     @Override
  58.     public String showRooms() throws RemoteException
  59.     {
  60.         // TODO migliorare
  61.         return db.keySet().toString();
  62.     }
  63.    
  64.     public static void main( String args[] )
  65.     {
  66.         final int REGISTRYPORT = 1099;
  67.         String registryHost = "localhost";
  68.         String serviceName = "ChatRegistry";
  69.        
  70.         try
  71.         {
  72.             String completeName = "//"+ registryHost + ":" + REGISTRYPORT + "/" + serviceName;
  73.             ChatRegistryImpl serverRMI = new ChatRegistryImpl();
  74.             Naming.rebind(completeName, serverRMI);
  75.         }
  76.         catch (Exception e)
  77.         {
  78.             e.printStackTrace();
  79.             //TODO
  80.         }
  81.        
  82.     }
  83.  
  84.  
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement