Advertisement
asia11

Klient 1 i 2

Nov 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.81 KB | None | 0 0
  1. 1111
  2. //RemObject interface
  3. import java.rmi.Remote;
  4. import java.rmi.RemoteException;
  5.  
  6. public interface RemObject extends Remote {
  7.     public double calculate(double a, double b)
  8.     throws RemoteException;
  9. }
  10.  
  11. //----------------------------------------
  12. //klasa klienta -> public static...
  13.  
  14. public class MyClient {
  15.  
  16.     public static void main(String[] args) {
  17. //deklaracja zmiennych + referencji obiektu zdalnego
  18.         double wynik;
  19.         RemObject zObiekt;
  20.        
  21. //pobranie danych o adresie uslugi(adresu zdalnego obiektu)
  22.         if (args.length==0) {
  23.             System.out.println("You have to enter RMI object address in this form: //host_address/service_name ");
  24.             return;
  25.         }
  26.        
  27.         String adres = args[0];
  28.        
  29. //utworzenie systemowego menadzera bezpieczenstwa -->na zajeciach dzialalo bez tego    
  30. //      //use this if needed
  31. //      if (System.getSecurityManager()==null)
  32. //          System.setSecurityManager(new SecurityManager());
  33.        
  34. //pobieranie referencji do zdalnego obiektu za pomoca klasy i metody Naming.lookup
  35.         try {
  36.             zObiekt = (RemObject) java.rmi.Naming.lookup(adres);
  37.         } catch (Exception e) {
  38.             System.out.println("Nie mozna pobrac referencji do "+adres);
  39.             e.printStackTrace();
  40.             return;
  41.         }
  42.         System.out.println("Referencja do "+adres+" jest pobrana.");
  43.        
  44. // wywolanie uslugi i wyswietlenie wyniku
  45.         try {
  46.             wynik = zObiekt.calculate(1.1, 2.2);
  47.         } catch (Exception e) {
  48.             System.out.println("blad zdalnego wywolania.");
  49.             e.printStackTrace();
  50.             return;
  51.         }
  52.         System.out.println("Wynik = "+wynik);
  53.         return;
  54.     }
  55. }
  56.  
  57. /*
  58. w odrebnym cmd niz serwer
  59. wejsc w sciezke klienta rmi -> bin
  60. wpisac w konsole:
  61. java MyClient //adresserwera/nazwauslugi
  62. */
  63. 22222222222222222222222222222222
  64. //klasa klienta -> public static...
  65.  
  66. public class MyClient {
  67.  
  68.     public static void main(String[] args) {
  69. //deklaracja zmiennych + referencji obiektu zdalnego
  70.         double wynik;
  71.         RemObject zObiekt;
  72. // 2 - obsluga drugiego parametru
  73.         RemObject2 zObiekt2;
  74.         ResultType wynik2;
  75.         InputType inObj;
  76.         inObj = new InputType();
  77.         inObj.x1 = 1.5;
  78.         inObj.x2 = 2.5;
  79.         inObj.operation = "add";  //lub "sub"
  80.        
  81.        
  82. //pobranie danych o adresie uslugi(adresu zdalnego obiektu)
  83.         if (args.length==0) {
  84.             System.out.println("You have to enter RMI object address in this form: //host_address/service_name ");
  85.             return;
  86.         }
  87.        
  88.         String adres = args[0];
  89.         String adres2 = args[1];
  90.  
  91. //utworzenie systemowego menadzera bezpieczenstwa -->na zajeciach dzialalo bez tego    
  92. //      //use this if needed
  93. //      if (System.getSecurityManager()==null)
  94. //          System.setSecurityManager(new SecurityManager());
  95.        
  96. //pobieranie referencji do zdalnego obiektu za pomoca klasy i metody Naming.lookup
  97.         try {
  98.             zObiekt = (RemObject) java.rmi.Naming.lookup(adres);
  99.             zObiekt2 = (RemObject2) java.rmi.Naming.lookup(adres2);
  100.         } catch (Exception e) {
  101.             System.out.println("Nie mozna pobrac referencji do "+adres);
  102.             e.printStackTrace();
  103.             return;
  104.         }
  105.         System.out.println("Referencja do "+adres+" jest pobrana.");
  106.        
  107.        
  108. // wywolanie uslugi i wyswietlenie wyniku
  109.         try {
  110.             wynik = zObiekt.calculate(1.1, 2.2);
  111.             wynik2 = zObiekt2.calculate(inObj);
  112.         } catch (Exception e) {
  113.             System.out.println("blad zdalnego wywolania.");
  114.             e.printStackTrace();
  115.             return;
  116.         }
  117.         System.out.println("Wynik = "+wynik);
  118.         System.out.println("Wynik2 = "+wynik2.result);
  119.         return;
  120.     }
  121. }
  122.  
  123.  
  124. /*
  125. w odrebnym cmd niz serwer
  126. wejsc w sciezke klienta rmi -> bin
  127. wpisac w konsole:
  128. java MyClient //adresserwera/nazwauslugi nazwauslugi2
  129. */
  130.  
  131.  
  132. //----------------------------------------------------
  133. //interfejs RemObject2 do klienta
  134. //do klasy drugiego obiektu zdalnego
  135.  
  136. import java.rmi.Remote;
  137. import java.rmi.RemoteException;
  138.  
  139. public interface RemObject2 extends Remote {
  140.     public ResultType calculate(InputType inputParam)
  141.         throws RemoteException;
  142. }
  143.  
  144. //----------------------------------------------------
  145. //klasa InputType do klienta
  146. //bedzie typem parametru zdalnie wywolywanej metody klasy obiektu zdalnego (klasy RemObject2)
  147. import java.io.Serializable;
  148.  
  149. public class InputType implements Serializable {
  150.     private static final long serialVersionUID = 101L;
  151.     String operation;
  152.     public double x1;
  153.     public double x2;
  154.    
  155.     public double getx1() {
  156.         return x1;
  157.     }
  158.     public double getx2() {
  159.         return x2;
  160.     }
  161. }
  162.  
  163. //----------------------------------------------------
  164. //klasa ResultType do klienta
  165. //typ wyniku obliczen zwracanego przez zmetode zdalnego obiektu
  166. import java.io.Serializable;
  167.  
  168. public class ResultType implements Serializable {
  169.     private static final long serialVersionUID = 102L;
  170.     String result_description;
  171.     public double result;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement