package helloWorld;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import helloWorld.interfaces.HelloInterface;
/**
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Hello extends UnicastRemoteObject implements HelloInterface{
private String msg = "Hello World!";
private int cont=0; //Indica quanti client devono ankora leggere il messaggio
private int tot=1; //Numero totale di client connessi
/**
* @throws RemoteException
*/
protected Hello() throws RemoteException {
super();
}
/* (non-Javadoc)
* @see helloWorld.interfaces.HelloInterface#say()
*/
public void regCl() throws RemoteException{
tot++;
System.out.println("Un nuovo client si č registrato, client connessi: "+tot); //Avviso sul server
}
public String say() throws RemoteException {
return msg;
}
public void setMsg(String input) throws RemoteException {
msg=input;
// System.out.println("msg: "+msg); Stampo su server il valore inserito
}
public synchronized void setCont() throws RemoteException {
cont=tot;
}
public synchronized void decCont() throws RemoteException {
cont--;
}
public int getCont() throws RemoteException {
return cont;
}
//--------THREADS----------------
//Thread di scrittura del messaggio
public static class ThreadScrittura extends Thread{
HelloInterface hello;
public ThreadScrittura(HelloInterface hello)
{
this.hello=hello;
}
public synchronized void scrivi(){
try{
System.out.println("scrivi -Cont:"+ hello.getCont());
while(hello.getCont()>0) //Finke pieno aspetto
{
try {
System.out.println("Aspetto a scrivere "+hello.getCont());
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
ConsoleReader cr=new ConsoleReader(System.in);
System.out.print(">> ");
String txt=cr.readLine();
hello.setMsg(txt);
hello.setCont(); //ho messo un nuovo mex
notifyAll();
System.out.println("notify dello scrivi eseguita");
}catch (RemoteException e) {
e.printStackTrace();
System.out.println("Eccezione remota");
System.exit(1);
}
}
public void run()
{
while(true)
{
scrivi();
}
}
}
//Thread di lettura del messaggio
public static class ThreadLettura extends Thread{
HelloInterface hello;
public ThreadLettura(HelloInterface hello)
{
this.hello=hello;
}
public synchronized void leggi(){
try{
System.out.println("leggi-Cont:"+hello.getCont() );
while(hello.getCont()<=0)//Finke vuoto aspetto
{
try {
System.out.println("Sto aspettando a leggere "+hello.getCont());
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
String msg=hello.say(); //Ottengo il messaggio
System.out.println("--"+msg);
hello.decCont(); //Messaggio vuoto
System.out.println(hello.getCont());
}catch (RemoteException e) {
e.printStackTrace();
System.out.println("Eccezione remota");
System.exit(1);
}
notifyAll();
System.out.println("notify del leggi eseguita");
}
public void run()
{
while(true)
{
leggi();
}
}
}
}