Advertisement
Guest User

Cz2

a guest
Nov 20th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.54 KB | None | 0 0
  1. Edit Configurations:
  2. VM Options: -Djava.security.manager -Djava.security.policy=bin/srv.policy
  3. Program Arguments: //localhost/usg //localhost/usg2
  4.  
  5. java -Djava.security.policy=srv.policy MyServer //localhost/usg //localhost/usg2
  6.  
  7. MySerwer -main class
  8. --------------------
  9. import java.rmi.RemoteException;
  10. import java.rmi.registry.LocateRegistry;
  11. import java.rmi.registry.Registry;
  12.  
  13. public  class  MySerwer {
  14.     public  static  void  main(String[]  args)
  15.     {
  16.         if  (args.length  ==  0)  {
  17.             System.out.println("You  have  to  enter  RMI  object  address  in the  form:  //host_address/service_name");
  18.             return;
  19.         }
  20.  
  21.         if  (System.getSecurityManager()  ==  null)
  22.             System.setSecurityManager(new  SecurityManager());
  23.  
  24.         // zamiast start rmiregistry
  25.         try {
  26.             Registry registry = LocateRegistry.createRegistry(1099);
  27.         }
  28.         catch (RemoteException e) {
  29.             e.printStackTrace();
  30.         }
  31.  
  32.         try {
  33.             RemObjImpl  implObiektu  =  new  RemObjImpl();
  34.             RemObjImpl2  implObiektu2  =  new  RemObjImpl2();
  35.             java.rmi.Naming.rebind(args[0],  implObiektu);
  36.             java.rmi.Naming.rebind(args[1],  implObiektu2);
  37.             System.out.println("Server  is  registered  now  :-)");
  38.             System.out.println("Press  Crl+C  to  stop...");
  39.         }  catch  (Exception  e)  {
  40.             System.out.println("SERVER  CAN'T  BE  REGISTERED!");
  41.             e.printStackTrace();
  42.             return;
  43.         }
  44.     }
  45. }
  46.  
  47. -----------------------------------------------------------------------------------------------------------
  48. RemObject - interface
  49. ------------------------
  50. import java.rmi.Remote;
  51.         import java.rmi.RemoteException;
  52.  
  53. public  interface  RemObject  extends Remote {
  54.     public  double  calculate(double  a,  double  b)
  55.             throws RemoteException;
  56. }
  57. -----------------------------------------------------------------------------------------------------------
  58. RemObjImpl - class
  59. ------------------------
  60. import java.rmi.RemoteException;
  61. import java.rmi.server.UnicastRemoteObject;
  62.  
  63. public  class  RemObjImpl  extends  UnicastRemoteObject
  64.         implements  RemObject
  65. {
  66.     private  static  final  long  serialVersionUID  =  101L;
  67.  
  68.     public  RemObjImpl()  throws  RemoteException  {
  69.         super();
  70.     }
  71.  
  72.     public  double  calculate(double  a,  double  b)
  73.             throws  RemoteException  {
  74.         return  a  +  b;
  75.     }
  76. }
  77. -----------------------------------------------------------------------------------------------------------
  78. RemObject2 - interface
  79. ------------------------
  80. import java.rmi.Remote;
  81. import java.rmi.RemoteException;
  82.  
  83. public  interface  RemObject2  extends Remote {
  84.     public ResultType calculate(InputType inputParam)
  85.             throws RemoteException;
  86. }
  87. -----------------------------------------------------------------------------------------------------------
  88. RemObjImpl2 - class
  89. ------------------------
  90. import java.rmi.RemoteException;
  91. import java.rmi.server.UnicastRemoteObject;
  92.  
  93. public  class  RemObjImpl2  extends UnicastRemoteObject
  94.         implements  RemObject2
  95. {
  96.     public  RemObjImpl2()  throws RemoteException {
  97.         super();
  98.     }
  99.  
  100.     public  ResultType  calculate(InputType  inParam)
  101.             throws  RemoteException  {
  102.         double  zm1,  zm2;
  103.         ResultType  wynik  =  new  ResultType();
  104.  
  105.         zm1  =  inParam.getx1(); zm2  =  inParam.getx2();
  106.         wynik.result_description  =  "Operacja  "+inParam.operation;
  107.  
  108.         switch  (inParam.operation)  {
  109.             case "add" :
  110.                 wynik.result  =  zm1  +  zm2;
  111.                 break;
  112.             case "sub" :
  113.                 wynik.result  =  zm1  -  zm2;
  114.                 break;
  115.             default:
  116.                 wynik.result  =  0;
  117.                 wynik.result_description  =  "Podano  zla  operacje";
  118.                 return  wynik;
  119.         }
  120.  
  121.         return  wynik;
  122.     }
  123. }
  124. -----------------------------------------------------------------------------------------------------------
  125. InputType - class
  126. ------------------------
  127. import java.io.Serializable;
  128.  
  129. public  class  InputType  implements Serializable { private  static  final  long  serialVersionUID  =  101L; String  operation;
  130.     public  double  x1;
  131.     public  double  x2;
  132.  
  133.     public  double  getx1() {
  134.         return  x1;
  135.     }
  136.     public  double  getx2() {
  137.         return  x2;
  138.     }
  139. }
  140. -----------------------------------------------------------------------------------------------------------
  141. ResultType - class
  142. ------------------------
  143. import java.io.Serializable;
  144.  
  145. public  class  ResultType  implements Serializable { private  static  final  long  serialVersionUID  =  102L; String  result_description;
  146.     public  double  result;
  147. }
  148. srv.policy - txt w bin
  149. ------------------------
  150. grant  {
  151. permission  java.security.AllPermission;
  152. };
  153.  
  154. ===========================================================================================================
  155.  
  156.  
  157.  
  158.  
  159. MyClient - main class
  160. ------------------------
  161. public  class  MyClient  {
  162.     public  static  void  main(String[]  args)  {
  163.         double  wynik;
  164.         RemObject  zObiekt;
  165.         RemObject2  zObiekt2;
  166.         ResultType  wynik2;
  167.         InputType   inObj;
  168.         if  (args.length  ==  0)  {
  169.             System.out.println("You  have  to  enter  RMI  object  address  in the  form:  //  host_address/service_name  ");
  170.             return;
  171.         }
  172.  
  173.         String  adres  =  args[0];
  174.         String  adres2  =  args[1];
  175.  
  176.  
  177. //  //use  this  if  needed
  178. //  if  (System.getSecurityManager()  ==  null)
  179. //  System.setSecurityManager(new  SecurityManager());
  180.  
  181.         try {
  182.             zObiekt  =  (RemObject)  java.rmi.Naming.lookup(adres);
  183.             zObiekt2  =  (RemObject2)  java.rmi.Naming.lookup(adres2);
  184.         }  catch  (Exception  e)  {
  185.             System.out.println("Nie  mozna  pobrac  referencji  do  "+adres);
  186.             System.out.println("Nie  mozna  pobrac  referencji  do  "+adres2);
  187.             e.printStackTrace();
  188.             return;
  189.         }
  190.         System.out.println("Referencja  do  "+adres+"  jest  pobrana.");
  191.  
  192.         try {
  193.             wynik  =  zObiekt.calculate(1.1,  2.2);
  194.  
  195.             inObj  =  new  InputType();
  196.             inObj.x1=  42.0;
  197.             inObj.x2=  1.3;
  198.             inObj.operation="add";  //lub  "sub"
  199.             wynik2  =  zObiekt2.calculate(inObj);
  200.         }  catch  (Exception  e)  {
  201.             System.out.println("Blad  zdalnego  wywolania.");
  202.             e.printStackTrace();
  203.             return;
  204.         }
  205.         System.out.println("Wynik  =  "+wynik);
  206.         System.out.println("Wynik  =  "+wynik2.result);
  207.         System.out.println("Komentarz  =  "+wynik2.result_description);
  208.         return;
  209.     }
  210. }
  211. -----------------------------------------------------------------------------------------------------------
  212. RemObject - interface
  213. ------------------------
  214. import java.rmi.Remote;
  215. import java.rmi.RemoteException;
  216.  
  217. public  interface  RemObject  extends Remote {
  218.     public  double  calculate(double  a,  double  b)
  219.             throws RemoteException;
  220. }
  221. -----------------------------------------------------------------------------------------------------------
  222. RemObject2 - interface
  223. ------------------------
  224. import java.rmi.Remote;
  225. import java.rmi.RemoteException;
  226.  
  227. public  interface  RemObject2  extends Remote {
  228.     public ResultType calculate(InputType inputParam)
  229.             throws RemoteException;
  230. }
  231. -----------------------------------------------------------------------------------------------------------
  232. ResultType - class
  233. ------------------------
  234. import java.io.Serializable;
  235.  
  236. public  class  ResultType  implements Serializable { private  static  final  long  serialVersionUID  =  102L; String  result_description;
  237.     public  double  result;
  238. }
  239. -----------------------------------------------------------------------------------------------------------
  240. InputType - class
  241. ------------------------
  242. import java.io.Serializable;
  243.  
  244. public  class  InputType  implements Serializable { private  static  final  long  serialVersionUID  =  101L; String  operation;
  245.     public  double  x1;
  246.     public  double  x2;
  247.  
  248.     public  double  getx1() {
  249.         return  x1;
  250.     }
  251.     public  double  getx2() {
  252.         return  x2;
  253.     }
  254. }
  255. -----------------------------------------------------------------------------------------------------------
  256. srv.policy - txt w bin
  257. ------------------------
  258. grant  {
  259. permission  java.security.AllPermission;
  260. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement