Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Edit Configurations:
- VM Options: -Djava.security.manager -Djava.security.policy=bin/srv.policy
- Program Arguments: //localhost/usg //localhost/usg2
- java -Djava.security.policy=srv.policy MyServer //localhost/usg //localhost/usg2
- MySerwer -main class
- --------------------
- import java.rmi.RemoteException;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.registry.Registry;
- public class MySerwer {
- public static void main(String[] args)
- {
- if (args.length == 0) {
- System.out.println("You have to enter RMI object address in the form: //host_address/service_name");
- return;
- }
- if (System.getSecurityManager() == null)
- System.setSecurityManager(new SecurityManager());
- // zamiast start rmiregistry
- try {
- Registry registry = LocateRegistry.createRegistry(1099);
- }
- catch (RemoteException e) {
- e.printStackTrace();
- }
- try {
- RemObjImpl implObiektu = new RemObjImpl();
- RemObjImpl2 implObiektu2 = new RemObjImpl2();
- java.rmi.Naming.rebind(args[0], implObiektu);
- java.rmi.Naming.rebind(args[1], implObiektu2);
- System.out.println("Server is registered now :-)");
- System.out.println("Press Crl+C to stop...");
- } catch (Exception e) {
- System.out.println("SERVER CAN'T BE REGISTERED!");
- e.printStackTrace();
- return;
- }
- }
- }
- -----------------------------------------------------------------------------------------------------------
- RemObject - interface
- ------------------------
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- public interface RemObject extends Remote {
- public double calculate(double a, double b)
- throws RemoteException;
- }
- -----------------------------------------------------------------------------------------------------------
- RemObjImpl - class
- ------------------------
- import java.rmi.RemoteException;
- import java.rmi.server.UnicastRemoteObject;
- public class RemObjImpl extends UnicastRemoteObject
- implements RemObject
- {
- private static final long serialVersionUID = 101L;
- public RemObjImpl() throws RemoteException {
- super();
- }
- public double calculate(double a, double b)
- throws RemoteException {
- return a + b;
- }
- }
- -----------------------------------------------------------------------------------------------------------
- RemObject2 - interface
- ------------------------
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- public interface RemObject2 extends Remote {
- public ResultType calculate(InputType inputParam)
- throws RemoteException;
- }
- -----------------------------------------------------------------------------------------------------------
- RemObjImpl2 - class
- ------------------------
- import java.rmi.RemoteException;
- import java.rmi.server.UnicastRemoteObject;
- public class RemObjImpl2 extends UnicastRemoteObject
- implements RemObject2
- {
- public RemObjImpl2() throws RemoteException {
- super();
- }
- public ResultType calculate(InputType inParam)
- throws RemoteException {
- double zm1, zm2;
- ResultType wynik = new ResultType();
- zm1 = inParam.getx1(); zm2 = inParam.getx2();
- wynik.result_description = "Operacja "+inParam.operation;
- switch (inParam.operation) {
- case "add" :
- wynik.result = zm1 + zm2;
- break;
- case "sub" :
- wynik.result = zm1 - zm2;
- break;
- default:
- wynik.result = 0;
- wynik.result_description = "Podano zla operacje";
- return wynik;
- }
- return wynik;
- }
- }
- -----------------------------------------------------------------------------------------------------------
- InputType - class
- ------------------------
- 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;
- }
- }
- -----------------------------------------------------------------------------------------------------------
- ResultType - class
- ------------------------
- import java.io.Serializable;
- public class ResultType implements Serializable { private static final long serialVersionUID = 102L; String result_description;
- public double result;
- }
- srv.policy - txt w bin
- ------------------------
- grant {
- permission java.security.AllPermission;
- };
- ===========================================================================================================
- MyClient - main class
- ------------------------
- public class MyClient {
- public static void main(String[] args) {
- double wynik;
- RemObject zObiekt;
- RemObject2 zObiekt2;
- ResultType wynik2;
- InputType inObj;
- if (args.length == 0) {
- System.out.println("You have to enter RMI object address in the form: // host_address/service_name ");
- return;
- }
- String adres = args[0];
- String adres2 = args[1];
- // //use this if needed
- // if (System.getSecurityManager() == null)
- // System.setSecurityManager(new SecurityManager());
- 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);
- System.out.println("Nie mozna pobrac referencji do "+adres2);
- e.printStackTrace();
- return;
- }
- System.out.println("Referencja do "+adres+" jest pobrana.");
- try {
- wynik = zObiekt.calculate(1.1, 2.2);
- inObj = new InputType();
- inObj.x1= 42.0;
- inObj.x2= 1.3;
- inObj.operation="add"; //lub "sub"
- wynik2 = zObiekt2.calculate(inObj);
- } catch (Exception e) {
- System.out.println("Blad zdalnego wywolania.");
- e.printStackTrace();
- return;
- }
- System.out.println("Wynik = "+wynik);
- System.out.println("Wynik = "+wynik2.result);
- System.out.println("Komentarz = "+wynik2.result_description);
- return;
- }
- }
- -----------------------------------------------------------------------------------------------------------
- RemObject - interface
- ------------------------
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- public interface RemObject extends Remote {
- public double calculate(double a, double b)
- throws RemoteException;
- }
- -----------------------------------------------------------------------------------------------------------
- RemObject2 - interface
- ------------------------
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- public interface RemObject2 extends Remote {
- public ResultType calculate(InputType inputParam)
- throws RemoteException;
- }
- -----------------------------------------------------------------------------------------------------------
- ResultType - class
- ------------------------
- import java.io.Serializable;
- public class ResultType implements Serializable { private static final long serialVersionUID = 102L; String result_description;
- public double result;
- }
- -----------------------------------------------------------------------------------------------------------
- InputType - class
- ------------------------
- 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;
- }
- }
- -----------------------------------------------------------------------------------------------------------
- srv.policy - txt w bin
- ------------------------
- grant {
- permission java.security.AllPermission;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement