Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.rmi.*;
- import java.rmi.server.*;
- import java.util.*;
- import java.security.*;
- public class Serwer extends UnicastRemoteObject implements SerwerInt
- {
- static HashMap<String,Klient> klienci = new HashMap<String,Klient>();
- public Serwer() throws RemoteException
- {
- super();
- }
- public String dajHost(String nazwa) throws RemoteException
- {
- Klient K = klienci.get(nazwa);
- if (K!=null)
- {
- try
- {
- if (K.g.F())
- {
- return K.Host;
- }
- else return null;
- }
- catch (Exception e)
- {
- System.out.println(e.toString());
- return null;
- }
- }
- else return null;
- }
- public int dajPort(String nazwa) throws RemoteException
- {
- Klient K = klienci.get(nazwa);
- if (K!=null)
- {
- try
- {
- if (K.g.F())
- {
- return K.PORT;
- }
- else return -1;
- }
- catch (Exception e)
- {
- System.out.println(e.toString());
- return -1;
- }
- }
- else return -1;
- }
- public Key dajKlucz(String nazwa) throws RemoteException
- {
- Klient K = klienci.get(nazwa);
- if (K!=null)
- {
- try
- {
- if (K.g.F())
- {
- return K.klucz;
- }
- else return null;
- }
- catch (Exception e)
- {
- System.out.println(e.toString());
- return null;
- }
- }
- else return null;
- }
- public synchronized boolean rejestruj(CebulaInt g, String login, Key K, String host, int port) throws RemoteException
- {
- for (Klient i : klienci.values())
- {
- try
- {
- i.g.F();
- }
- catch (Exception e)
- {
- klienci.remove(i.Nazwa);
- }
- }
- if (!klienci.containsKey(login))
- {
- System.out.println("zarejestrowal się uzytkownik: " + login);
- Klient w = new Klient(g,login,K, host, port);
- try
- {
- klienci.put(login,w);
- }
- catch (NullPointerException e)
- {
- System.out.println(e);
- return false;
- }
- return true;
- }
- else return false;
- }
- public void wyrejestruj(String login) throws RemoteException
- {
- klienci.remove(login);
- }
- public static void main(String[] args)
- {
- if (System.getSecurityManager() == null)
- {
- System.setSecurityManager(new RMISecurityManager());
- }
- String sciezkaSerwera = "//"+args[0]+"/Serwer";
- try
- {
- Serwer serwer = new Serwer();
- Naming.rebind(sciezkaSerwera, serwer);
- System.out.println("Zacząłem działać. Narazie wszystko ok");
- }
- catch (Exception e)
- {
- System.out.println("Wyjatek serwera: " + e.toString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement