diff --git a/rmi_exam/Client.java b/rmi_exam/Client.java new file mode 100644 index 0000000..80de92a --- /dev/null +++ b/rmi_exam/Client.java @@ -0,0 +1,24 @@ +package rmi_exam; + +import java.rmi.RemoteException; +import java.rmi.server.UnicastRemoteObject; + +/** + * Created by Giovanni on 16/01/2015. + */ +public class Client extends UnicastRemoteObject implements ClientInterface { + + private final String nome; + + public Client (String in_name) throws RemoteException { + nome = in_name; + } + + public void detto (String s) throws RemoteException{ + System.out.println(s); + } + + public String getname(){return nome;} + + +} diff --git a/rmi_exam/ClientData.java b/rmi_exam/ClientData.java new file mode 100644 index 0000000..ea1375a --- /dev/null +++ b/rmi_exam/ClientData.java @@ -0,0 +1,25 @@ +package rmi_exam; + +/** + * Created by Giovanni on 16/01/2015. + */ +public class ClientData { + public ClientData (String inname){ + name = inname; + } + private boolean voto = false; + private boolean havotato = false; + private boolean blocked = false; + private String name; + + public void setVoto(boolean invoto){voto=invoto;} + public void setHavotato(boolean invotato){havotato = invotato;} + public void setBlocked (boolean value) {blocked=value;} + public String getName(){return name;} + public boolean getvoto() {return voto;}; + public boolean gethavotato(){return havotato;} + public boolean getblocked(){return blocked;} + +} + + diff --git a/rmi_exam/ClientInterface.java b/rmi_exam/ClientInterface.java new file mode 100644 index 0000000..463d995 --- /dev/null +++ b/rmi_exam/ClientInterface.java @@ -0,0 +1,11 @@ +package rmi_exam; + +import java.rmi.Remote; +import java.rmi.RemoteException; + +/** + * Created by Giovanni on 16/01/2015. + */ +public interface ClientInterface extends Remote { + void detto (String s) throws RemoteException; +} diff --git a/rmi_exam/LaunchClient.java b/rmi_exam/LaunchClient.java new file mode 100644 index 0000000..68234b3 --- /dev/null +++ b/rmi_exam/LaunchClient.java @@ -0,0 +1,80 @@ +package rmi_exam; + + +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; + +/** + * Created by Giovanni on 16/01/2015. + */ +public class LaunchClient { + + public static void main (String[] args ){ + + if (args.length <2 || args.length > 2) + System.exit(-1); + System.setSecurityManager(new SecurityManager()); + ClientInterface c = null; + ServerInterface s = null; + try{ + c = new Client(args[0]); + s = (ServerInterface) Naming.lookup("rmi://" + args[1] + "/server"); + s.iscriviti(c,( (Client)c ).getname() ); + BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); + String comando = command(b); + + while (!comando.equals("!quit")){ + if (comando.startsWith("!")){ + boolean vote_result; + if (comando.substring(1).equals("si")) + vote_result = s.vota(c,true); + else if (comando.substring(1).equals("no")) + vote_result = s.vota(c,false); + else vote_result = false; + + if (vote_result) + System.out.println("votato"); + else + System.out.println("Non puoi votare poichè non c'è una votazione in corso, hai gia votato, sei stato bloccato o il tuo input è scorretto"); + } else { + boolean dici_result; + dici_result = s.dici(c,comando); + if (!dici_result) + System.out.println ("Non puoi parlare poichè non c'è il numero minimo di partecipanti o sei stato bloccato"); + } + comando = command(b); + } + + } + catch (RemoteException e){ + e.printStackTrace(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (NotBoundException e) { + e.printStackTrace(); + } + finally { + try { + s.disiscriviti(c); + } catch (RemoteException e) { + e.printStackTrace(); + } + System.exit(0); + + } + + } + + public static String command (BufferedReader b){ + try{ + return b.readLine(); + } catch (IOException e) { + return "!quit"; + } + } +} diff --git a/rmi_exam/LaunchServer.java b/rmi_exam/LaunchServer.java new file mode 100644 index 0000000..2f47cec --- /dev/null +++ b/rmi_exam/LaunchServer.java @@ -0,0 +1,83 @@ +package rmi_exam; + +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.Iterator; + +/** + * Created by Giovanni on 16/01/2015. + */ +public class LaunchServer { + + public static void main (String[] args){ + + System.setSecurityManager(new SecurityManager()); + + try { + ServerInterface s = new Server(); + Registry r = LocateRegistry.getRegistry(); + r.rebind("server",s); + BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); + String comando = command(b); + while (true){ //supponiamo il server non esca + + if (!comando.startsWith("!")) + System.out.println("Parametro errato"); + else{ + comando = comando.substring(1); + if ( comando.split(" ")[0].equals("blocco") ){ + Iterator it = ((Server)s).getH().keySet().iterator(); + for (;it.hasNext();){ + ClientInterface t = it.next(); + if ( ((Server)s).getH().get(t).getName().equals(comando.split(" ")[1] )){ + ((Server)s).getH().get(t).setBlocked(true); + t.detto("bloccato"); + } + } + } else if (comando.split(" ")[0].equals("sblocco")){ + Iterator it = ((Server)s).getH().keySet().iterator(); + for (;it.hasNext();){ + ClientInterface t = it.next(); + if ( ((Server)s).getH().get(t).getName().equals(comando.split(" ")[1] )){ + ((Server)s).getH().get(t).setBlocked(false); + t.detto("sbloccato"); + } + } + } else if (comando.split(" ")[0].equals("sondaggio")){ + + ((Server)s).setsondaggio(true); + int start = 10; + String message_sondaggio = comando.substring(start); + Iterator it = ((Server)s).getH().keySet().iterator(); + for (;it.hasNext();){ + it.next().detto(message_sondaggio); + } + }else { + System.out.println("Comando ! non valido"); + } + } + comando = command(b); + } + } catch (RemoteException e) { + e.printStackTrace(); + } + finally { + System.exit(0); + } + + } + + public static String command (BufferedReader b){ + try{ + return b.readLine(); + } catch (IOException e) { + return "!quit"; + } + } + + +} diff --git a/rmi_exam/Server.java b/rmi_exam/Server.java new file mode 100644 index 0000000..74a3f75 --- /dev/null +++ b/rmi_exam/Server.java @@ -0,0 +1,82 @@ +package rmi_exam; + + +import java.rmi.RemoteException; +import java.rmi.server.UnicastRemoteObject; +import java.util.HashMap; +import java.util.Iterator; + +/** + * Created by Giovanni on 16/01/2015. + */ +public class Server extends UnicastRemoteObject implements ServerInterface { + + private HashMap h; + private boolean votazione_in_corso = false; + private boolean chaton=false; + + public Server() throws RemoteException{ + h=new HashMap(); + } + + public void iscriviti (ClientInterface c, String n) throws RemoteException { + + h.put(c,new ClientData(n)); + if (h.size()>=2){chaton=true;} + if (chaton){ + Iterator it = h.keySet().iterator(); + for(;it.hasNext();){ + ClientInterface t = it.next(); + t.detto(h.get(c).getName() + " e' entrato in chat");//anche a se stesso + } + + } + } + + public void disiscriviti( ClientInterface c) throws RemoteException { + String name = h.get(c).getName(); + h.remove(c); + if (chaton){ + Iterator it = h.keySet().iterator(); + for (;it.hasNext();){ + ClientInterface t = it.next(); + t.detto(name + " e' uscito dalla chat"); // non a se stesso + } + } + } + + public boolean dici (ClientInterface c, String m) throws RemoteException { + if (chaton && !h.get(c).getblocked()){ + Iterator it = h.keySet().iterator(); + for (;it.hasNext();){ + ClientInterface t = it.next(); + if(!t.equals(c)){ + t.detto(h.get(c).getName() + ":" + m); + } + } + return true; + } + return false; + } + + public boolean vota (ClientInterface c, boolean v) throws RemoteException{ + if (chaton && votazione_in_corso && !h.get(c).getblocked() && !h.get(c).gethavotato()){ + h.get(c).setHavotato(true); + h.get(c).setVoto(v); + System.out.println(h.get(c).getName() + " ha votato " + convert(v)); + return true; + } + return false; + } + + protected String convert(boolean in){ + if (in) + return "si"; + return "no"; + } + + public HashMap getH(){return h;} + public void setsondaggio(boolean value) {votazione_in_corso=value;} + + +} diff --git a/rmi_exam/ServerInterface.java b/rmi_exam/ServerInterface.java new file mode 100644 index 0000000..c245ff9 --- /dev/null +++ b/rmi_exam/ServerInterface.java @@ -0,0 +1,16 @@ +package rmi_exam; + + + +import java.rmi.Remote; +import java.rmi.RemoteException; + +/** + * Created by Giovanni on 16/01/2015. + */ +public interface ServerInterface extends Remote { + boolean dici (ClientInterface c, String m) throws RemoteException; + boolean vota (ClientInterface c, boolean v) throws RemoteException; + void iscriviti (ClientInterface c, String n) throws RemoteException; + void disiscriviti (ClientInterface c) throws RemoteException; +} diff --git a/rmi_exam/readme.txt b/rmi_exam/readme.txt new file mode 100644 index 0000000..51469bb --- /dev/null +++ b/rmi_exam/readme.txt @@ -0,0 +1,27 @@ +Struttura del compito\esame invariata. Fixato solo alcuni bug grafici ed errori di sintassi. +Ogni errore riportato qui sotto fa riferimento ad una sola linea di codice del documento consegnato all'esame. + +Bug grafici: +1)sostituito 'int start = comando.indexof("sondaggio") + 2' con 'int start = 10' in LaunchServer +2)sostituito 't.detto(h.get(t).getName() + "e' entrato in chat")' con 't.detto(h.get(c).getName() + "e' entrato in chat")' nel metodo iscriviti di Server +3)aggiunto 'String name = h.get(c).getName();' al metodo disiscriviti di Server +4)sostituito 't.detto(h.get(t).getName() + "e' uscito dalla chat")' con 't.detto(name + " e' uscito dalla chat");' nel metodo disicriviti di Server +5)sostituito 't.detto(h.get(t).getName() + ":" + m)' con 't.detto(h.get(c).getName() + ":" + m)' nel metodo dici di Server +6)sostituito 'System.out.println("Non puoi votare poichè non c'è una votazione in corso,sei stato bloccato o il tuo input è scorretto")' con 'System.out.println("Non puoi votare poichè non c'è una votazione in corso, hai gia votato, sei stato bloccato o il tuo input è scorretto");' nel while di LaunchClient +7)sostituito "t!=c" con "!t.equals(c)" in Server + +Errori di sintassi: +1)mancano tutti gli import +2)mancano le parentesi tonde nei getter di ClientData +3)mancano le parentesi tonde nel metodo getH di Server +4)manca void al ritorno del metodo setsondaggio in Server +5)manca ";" nell'ultima istruzione del metodo setsondaggio in Server +6)il ; va spostato all'interno della parentesi graffa nel metodo getname di Client +7)"new securitymanager()" in LaunchClient,LaunchServer va come argomento di System.setsecuritymanager e non come assegnazione +8)"command" usato al posto di "comando" in LaunchClient +9)manca println a System.out in LaunchClient +10)definito "ServerInterface s;" al di fuori del try altrimenti non visibile nello scoping di finally in LaunchClient +11)aggiunto try catch nel blocco finally in LaunchClient +12)"t.dici" invece di "t.detto" in LaunchServer +13)parentesi tonda saltata al termine di un if in LaunchServer +14)else mancante in LaunchClient,non avevo usato le parentesi \ No newline at end of file diff --git a/test/test.java b/test/test.java new file mode 100644 index 0000000..9dd4b72 --- /dev/null +++ b/test/test.java @@ -0,0 +1,34 @@ +package test; + +/** + * Created by Giovanni on 14/01/2015. + */ +public class test { + + public static void main (String[] args){ + + + System.out.print(fai(5)); + + + + } + + public static int fai (int a){ + try { + return a; + } + catch (Exception e) { + + System.out.print("sada"); + } + + + + finally { + //return 10; + } + return 0; + } + +}