package cap16.borsa;

import java.net.*;
import java.rmi.*;
import java.rmi.server.*;

public class Osservatore
{ Borsa borsa;
  public Osservatore(Borsa b)
  { borsa = b;
  }
  public void osserva(String t, double s)
    throws RemoteException, BorsaException
  { borsa.registra(t, s, new NotificaImpl());
  }
  public static void main(String[] args)
  { if (args.length != 3)
    { System.err.println(
        "Uso: java Osservatore url-servizio nome-titolo soglia");
      System.exit(1);
    }
    try
    { Borsa b = (Borsa) Naming.lookup(args[0]);
      Osservatore o = new Osservatore(b);
      o.osserva(args[1], Double.parseDouble(args[2]));
    }
    catch (RemoteException re)
    { System.err.println("Errore di rete");
    }
    catch (MalformedURLException mue)
    { System.err.println("URL errato");
    }
    catch (NotBoundException nbe)
    { System.err.println("Servizio non registrato");
    }
    catch (BorsaException be)
    { System.err.println("Titolo non presente");
    }
  }
  class NotificaImpl extends UnicastRemoteObject
    implements Notifica
  { NotificaImpl() throws RemoteException {}
    public void notifica(String titolo, double valore)
      throws RemoteException
    { System.out.println(
        "Il titolo " + titolo +
        " ha superato la soglia e vale " + valore);
    }
  }
}
