Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1111
- //RemObject interface
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- public interface RemObject extends Remote {
- public double calculate(double a, double b)
- throws RemoteException;
- }
- //----------------------------------------
- //klasa klienta -> public static...
- public class MyClient {
- public static void main(String[] args) {
- //deklaracja zmiennych + referencji obiektu zdalnego
- double wynik;
- RemObject zObiekt;
- //pobranie danych o adresie uslugi(adresu zdalnego obiektu)
- if (args.length==0) {
- System.out.println("You have to enter RMI object address in this form: //host_address/service_name ");
- return;
- }
- String adres = args[0];
- //utworzenie systemowego menadzera bezpieczenstwa -->na zajeciach dzialalo bez tego
- // //use this if needed
- // if (System.getSecurityManager()==null)
- // System.setSecurityManager(new SecurityManager());
- //pobieranie referencji do zdalnego obiektu za pomoca klasy i metody Naming.lookup
- try {
- zObiekt = (RemObject) java.rmi.Naming.lookup(adres);
- } catch (Exception e) {
- System.out.println("Nie mozna pobrac referencji do "+adres);
- e.printStackTrace();
- return;
- }
- System.out.println("Referencja do "+adres+" jest pobrana.");
- // wywolanie uslugi i wyswietlenie wyniku
- try {
- wynik = zObiekt.calculate(1.1, 2.2);
- } catch (Exception e) {
- System.out.println("blad zdalnego wywolania.");
- e.printStackTrace();
- return;
- }
- System.out.println("Wynik = "+wynik);
- return;
- }
- }
- /*
- w odrebnym cmd niz serwer
- wejsc w sciezke klienta rmi -> bin
- wpisac w konsole:
- java MyClient //adresserwera/nazwauslugi
- */
- 22222222222222222222222222222222
- //klasa klienta -> public static...
- public class MyClient {
- public static void main(String[] args) {
- //deklaracja zmiennych + referencji obiektu zdalnego
- double wynik;
- RemObject zObiekt;
- // 2 - obsluga drugiego parametru
- RemObject2 zObiekt2;
- ResultType wynik2;
- InputType inObj;
- inObj = new InputType();
- inObj.x1 = 1.5;
- inObj.x2 = 2.5;
- inObj.operation = "add"; //lub "sub"
- //pobranie danych o adresie uslugi(adresu zdalnego obiektu)
- if (args.length==0) {
- System.out.println("You have to enter RMI object address in this form: //host_address/service_name ");
- return;
- }
- String adres = args[0];
- String adres2 = args[1];
- //utworzenie systemowego menadzera bezpieczenstwa -->na zajeciach dzialalo bez tego
- // //use this if needed
- // if (System.getSecurityManager()==null)
- // System.setSecurityManager(new SecurityManager());
- //pobieranie referencji do zdalnego obiektu za pomoca klasy i metody Naming.lookup
- try {
- zObiekt = (RemObject) java.rmi.Naming.lookup(adres);
- zObiekt2 = (RemObject2) java.rmi.Naming.lookup(adres2);
- } catch (Exception e) {
- System.out.println("Nie mozna pobrac referencji do "+adres);
- e.printStackTrace();
- return;
- }
- System.out.println("Referencja do "+adres+" jest pobrana.");
- // wywolanie uslugi i wyswietlenie wyniku
- try {
- wynik = zObiekt.calculate(1.1, 2.2);
- wynik2 = zObiekt2.calculate(inObj);
- } catch (Exception e) {
- System.out.println("blad zdalnego wywolania.");
- e.printStackTrace();
- return;
- }
- System.out.println("Wynik = "+wynik);
- System.out.println("Wynik2 = "+wynik2.result);
- return;
- }
- }
- /*
- w odrebnym cmd niz serwer
- wejsc w sciezke klienta rmi -> bin
- wpisac w konsole:
- java MyClient //adresserwera/nazwauslugi nazwauslugi2
- */
- //----------------------------------------------------
- //interfejs RemObject2 do klienta
- //do klasy drugiego obiektu zdalnego
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- public interface RemObject2 extends Remote {
- public ResultType calculate(InputType inputParam)
- throws RemoteException;
- }
- //----------------------------------------------------
- //klasa InputType do klienta
- //bedzie typem parametru zdalnie wywolywanej metody klasy obiektu zdalnego (klasy RemObject2)
- import java.io.Serializable;
- public class InputType implements Serializable {
- private static final long serialVersionUID = 101L;
- String operation;
- public double x1;
- public double x2;
- public double getx1() {
- return x1;
- }
- public double getx2() {
- return x2;
- }
- }
- //----------------------------------------------------
- //klasa ResultType do klienta
- //typ wyniku obliczen zwracanego przez zmetode zdalnego obiektu
- import java.io.Serializable;
- public class ResultType implements Serializable {
- private static final long serialVersionUID = 102L;
- String result_description;
- public double result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement