diff --git a/rmi_counter/CounterInterface.java b/rmi_counter/CounterInterface.java new file mode 100644 index 0000000..d7a6bc9 --- /dev/null +++ b/rmi_counter/CounterInterface.java @@ -0,0 +1,15 @@ +package rmi_counter; + +import java.rmi.Remote; +import java.rmi.RemoteException; + +/** + * Created by Giovanni on 16/11/2014. + */ +public interface CounterInterface extends Remote{ + + int getvalue (String from) throws RemoteException; + int sum (String from, int value) throws RemoteException; + + +} diff --git a/rmi_counter/LaunchClient.java b/rmi_counter/LaunchClient.java new file mode 100644 index 0000000..6028e00 --- /dev/null +++ b/rmi_counter/LaunchClient.java @@ -0,0 +1,64 @@ +package rmi_counter; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.rmi.Naming; +import java.rmi.NotBoundException; +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.util.logging.Logger; + +/** + * Created by Giovanni on 16/11/2014. + */ +public class LaunchClient { + private static Logger l =Logger.getLogger("counterclientlogger"); + + public static void main (String args[]) { + + + try { + + CounterInterface rc = (CounterInterface) Naming.lookup("rmi://localhost/Counter"); + BufferedReader i = new BufferedReader(new InputStreamReader(System.in)); + + while (true) { + l.info("Commands:current,sum ,any other to quit"); + String command = LaunchClient.cmd(i); + l.info(command); + if (command.equals("current")) { + l.info(rc.getvalue("Client1") + ""); + } else if ( command.length()>=3 && command.substring(0,3).equals("sum")) { + int value = Integer.parseInt(command.substring(4)); + l.info(String.valueOf(rc.sum( "Client1",value ))); + } else { + l.info("quitting"); + break; + } + } + } catch (RemoteException e) { + e.printStackTrace(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (NotBoundException e) { + e.printStackTrace(); + } + } + + + public static String cmd (BufferedReader i ){ + try { + String input = i.readLine(); + return input; + + } catch (IOException e) { + } + return ""; + } + + + +} diff --git a/rmi_counter/LaunchServer.java b/rmi_counter/LaunchServer.java new file mode 100644 index 0000000..8cd9673 --- /dev/null +++ b/rmi_counter/LaunchServer.java @@ -0,0 +1,63 @@ +package rmi_counter; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.util.logging.Logger; + +/** + * Created by Giovanni on 16/11/2014. + */ +public class LaunchServer { + private static Logger l =Logger.getLogger("counterlogger"); + + public static void main (String args[]) { + + + + try { + + RemoteCounter rc = new RemoteCounter(0); + Registry r = LocateRegistry.getRegistry(); + l.info("Trying to bind"); + r.rebind("Counter",rc); + l.info("Binded"); + + BufferedReader i = new BufferedReader(new InputStreamReader(System.in)); + + while (true) { + l.info("Commands:status,accesses,any other to quit"); + String command = LaunchServer.cmd(i); + l.info(command); + if (command.equals("status")) { + l.info(rc.getlocalvalue() + ""); + } else if (command.equals("accesses")) { + l.info(rc.getaccesses()); + } else { + l.info("quitting"); + break; + } + } + } catch (RemoteException e) { + e.printStackTrace(); + } + } + + + public static String cmd (BufferedReader i ){ + try { + String input = i.readLine(); + return input; + + } catch (IOException e) { + } + return ""; + } + + + +} diff --git a/rmi_counter/LocalCounter.java b/rmi_counter/LocalCounter.java new file mode 100644 index 0000000..129031a --- /dev/null +++ b/rmi_counter/LocalCounter.java @@ -0,0 +1,26 @@ +package rmi_counter; + +import java.rmi.registry.LocateRegistry; + +/** + * Created by Giovanni on 16/11/2014. + */ +public class LocalCounter { + + public LocalCounter(int in){ + value=in; + } + + public int increment(){ + return (value++); + } + + + public int getlocalvalue(){ + return value; + } + + + private int value; + +} diff --git a/rmi_counter/RemoteCounter.java b/rmi_counter/RemoteCounter.java new file mode 100644 index 0000000..f262916 --- /dev/null +++ b/rmi_counter/RemoteCounter.java @@ -0,0 +1,45 @@ +package rmi_counter; + +import java.rmi.RemoteException; +import java.rmi.server.UnicastRemoteObject; +import java.util.Enumeration; +import java.util.Vector; +import java.util.logging.Logger; + +/** + * Created by Giovanni on 16/11/2014. + */ +public class RemoteCounter extends LocalCounter implements CounterInterface { + + private static Logger l = Logger.getLogger("RemoteCounter"); + + public RemoteCounter( int in) throws RemoteException{ + super(in); + UnicastRemoteObject.exportObject(this,5000); + accesses = new Vector(); + } + + @Override + public synchronized int getvalue(String from) throws RemoteException { + accesses.add("Access from " + from + " value " + getlocalvalue()); + return getlocalvalue(); + } + + @Override + public synchronized int sum(String from, int value) throws RemoteException { + accesses.add("Access from " + from + " value to add " + value); + for (int i = 0;i < value ;i++) + increment(); + return getlocalvalue(); + } + + public String getaccesses (){ + String returned = ""; + for (Enumeration e = accesses.elements(); e.hasMoreElements() ;) + returned+="\n" + e.nextElement(); + return returned; + } + + Vector accesses; + +} diff --git a/rmi_hello_world/HelloWorldImpl.java b/rmi_hello_world/HelloWorldImpl.java index ad02e96..d06d816 100644 --- a/rmi_hello_world/HelloWorldImpl.java +++ b/rmi_hello_world/HelloWorldImpl.java @@ -15,8 +15,7 @@ public class HelloWorldImpl extends UnicastRemoteObject implements HelloWorldInt } @Override - public String stampa(String in) throws RemoteException { - System.out.print(in); - return in; + public String stampa() throws RemoteException { + return "HelloWorld"; } } diff --git a/rmi_hello_world/HelloWorldInterface.java b/rmi_hello_world/HelloWorldInterface.java index de9faab..000296d 100644 --- a/rmi_hello_world/HelloWorldInterface.java +++ b/rmi_hello_world/HelloWorldInterface.java @@ -9,6 +9,6 @@ import java.rmi.RemoteException; public interface HelloWorldInterface extends Remote { - String stampa (String in) throws RemoteException; + String stampa () throws RemoteException; } diff --git a/rmi_hello_world/LaunchClient.java b/rmi_hello_world/LaunchClient.java new file mode 100644 index 0000000..d35a631 --- /dev/null +++ b/rmi_hello_world/LaunchClient.java @@ -0,0 +1,39 @@ +package rmi_hello_world; + +import java.net.MalformedURLException; +import java.rmi.Naming; +import java.rmi.NotBoundException; +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.util.logging.Logger; + +/** + * Created by Giovanni on 16/11/2014. + */ +public class LaunchClient { + + private static final Logger l = Logger.getLogger("Client"); + + public static void main (String[] arg) { + + + try { + l.info("Cercando l'oggetto remoto"); + HelloWorldInterface o = (HelloWorldInterface) Naming.lookup("rmi://localhost/HW"); + l.info("Found"); + l.info(o.stampa()); + + } catch (RemoteException e) { + e.printStackTrace(); + } catch (NotBoundException e) { + e.printStackTrace(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + + + } + + +} diff --git a/rmi_hello_world/Launch.java b/rmi_hello_world/LaunchServer.java similarity index 76% rename from rmi_hello_world/Launch.java rename to rmi_hello_world/LaunchServer.java index a0b30ed..141cd0b 100644 --- a/rmi_hello_world/Launch.java +++ b/rmi_hello_world/LaunchServer.java @@ -10,19 +10,19 @@ import java.util.logging.Logger; /** * Created by Giovanni on 09/11/2014. */ -public class Launch { +public class LaunchServer { public static void main (String[] arg){ Logger l =Logger.getLogger("hellowordlogger"); - System.setSecurityManager(new RMISecurityManager()); + System.setSecurityManager(new SecurityManager()); try { - Registry registry = LocateRegistry.createRegistry(10000); + Registry registry = LocateRegistry.getRegistry(); HelloWorldImpl obj = new HelloWorldImpl(); l.info("Creato oggetto"); - registry.bind("HW",obj); + registry.rebind("HW", obj); l.info("Oggetto registrato"); diff --git a/rmi_hello_world/jli.dll b/rmi_hello_world/jli.dll deleted file mode 100644 index e198aae..0000000 Binary files a/rmi_hello_world/jli.dll and /dev/null differ diff --git a/rmi_hello_world/rmiregistry.exe b/rmi_hello_world/rmiregistry.exe deleted file mode 100644 index 8591bac..0000000 Binary files a/rmi_hello_world/rmiregistry.exe and /dev/null differ