Advertisement
Guest User

Untitled

a guest
May 28th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import java.rmi.*;
  2.  
  3. import java.rmi.server.*;
  4.  
  5. import java.util.*;
  6.  
  7. import java.security.*;
  8.  
  9.  
  10.  
  11.  
  12.  
  13. public class Serwer extends UnicastRemoteObject implements SerwerInt
  14.  
  15. {
  16.  
  17. static HashMap<String,Klient> klienci = new HashMap<String,Klient>();
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. public Serwer() throws RemoteException
  26.  
  27. {
  28.  
  29. super();
  30.  
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37. public String dajHost(String nazwa) throws RemoteException
  38.  
  39. {
  40.  
  41. Klient K = klienci.get(nazwa);
  42.  
  43. if (K!=null)
  44.  
  45. {
  46.  
  47. try
  48.  
  49. {
  50.  
  51. if (K.g.F())
  52.  
  53. {
  54.  
  55. return K.Host;
  56.  
  57. }
  58.  
  59. else return null;
  60.  
  61. }
  62.  
  63. catch (Exception e)
  64.  
  65. {
  66.  
  67. System.out.println(e.toString());
  68.  
  69. return null;
  70.  
  71. }
  72.  
  73. }
  74.  
  75. else return null;
  76.  
  77. }
  78.  
  79.  
  80.  
  81. public int dajPort(String nazwa) throws RemoteException
  82.  
  83. {
  84.  
  85. Klient K = klienci.get(nazwa);
  86.  
  87. if (K!=null)
  88.  
  89. {
  90.  
  91. try
  92.  
  93. {
  94.  
  95. if (K.g.F())
  96.  
  97. {
  98.  
  99. return K.PORT;
  100.  
  101. }
  102.  
  103. else return -1;
  104.  
  105. }
  106.  
  107. catch (Exception e)
  108.  
  109. {
  110.  
  111. System.out.println(e.toString());
  112.  
  113. return -1;
  114.  
  115. }
  116.  
  117. }
  118.  
  119. else return -1;
  120.  
  121. }
  122.  
  123.  
  124.  
  125. public Key dajKlucz(String nazwa) throws RemoteException
  126.  
  127. {
  128.  
  129. Klient K = klienci.get(nazwa);
  130.  
  131. if (K!=null)
  132.  
  133. {
  134.  
  135. try
  136.  
  137. {
  138.  
  139. if (K.g.F())
  140.  
  141. {
  142.  
  143. return K.klucz;
  144.  
  145. }
  146.  
  147. else return null;
  148.  
  149. }
  150.  
  151. catch (Exception e)
  152.  
  153. {
  154.  
  155. System.out.println(e.toString());
  156.  
  157. return null;
  158.  
  159. }
  160.  
  161. }
  162.  
  163. else return null;
  164.  
  165. }
  166.  
  167.  
  168.  
  169. public synchronized boolean rejestruj(CebulaInt g, String login, Key K, String host, int port) throws RemoteException
  170.  
  171. {
  172.  
  173. for (Klient i : klienci.values())
  174.  
  175. {
  176.  
  177. try
  178.  
  179. {
  180.  
  181. i.g.F();
  182.  
  183. }
  184.  
  185. catch (Exception e)
  186.  
  187. {
  188.  
  189. klienci.remove(i.Nazwa);
  190.  
  191. }
  192.  
  193. }
  194.  
  195. if (!klienci.containsKey(login))
  196.  
  197. {
  198.  
  199. System.out.println("zarejestrowal się uzytkownik: " + login);
  200.  
  201. Klient w = new Klient(g,login,K, host, port);
  202.  
  203. try
  204.  
  205. {
  206.  
  207. klienci.put(login,w);
  208.  
  209. }
  210.  
  211. catch (NullPointerException e)
  212.  
  213. {
  214.  
  215. System.out.println(e);
  216.  
  217. return false;
  218.  
  219. }
  220.  
  221. return true;
  222.  
  223. }
  224.  
  225. else return false;
  226.  
  227. }
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235. public void wyrejestruj(String login) throws RemoteException
  236.  
  237. {
  238.  
  239. klienci.remove(login);
  240.  
  241. }
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249. public static void main(String[] args)
  250.  
  251. {
  252.  
  253. if (System.getSecurityManager() == null)
  254.  
  255. {
  256.  
  257. System.setSecurityManager(new RMISecurityManager());
  258.  
  259. }
  260.  
  261.  
  262.  
  263. String sciezkaSerwera = "//"+args[0]+"/Serwer";
  264.  
  265.  
  266.  
  267. try
  268.  
  269. {
  270.  
  271. Serwer serwer = new Serwer();
  272.  
  273. Naming.rebind(sciezkaSerwera, serwer);
  274.  
  275. System.out.println("Zacząłem działać. Narazie wszystko ok");
  276.  
  277. }
  278.  
  279. catch (Exception e)
  280.  
  281. {
  282.  
  283. System.out.println("Wyjatek serwera: " + e.toString());
  284.  
  285. }
  286.  
  287. }
  288.  
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement