示例#1
0
  public static void main(String[] args) throws NamingException, RemoteException {
    System.setProperty("java.security.policy", "client.policy");
    System.setSecurityManager(new SecurityManager());
    Context namingContext = new InitialContext();

    System.out.print("RMI registry bindings: ");
    NamingEnumeration<NameClassPair> e = namingContext.list("rmi://localhost/");
    while (e.hasMore()) System.out.println(e.next().getName());

    String url = "rmi://localhost/central_warehouse";
    Warehouse centralWarehouse = (Warehouse) namingContext.lookup(url);

    Scanner in = new Scanner(System.in);
    System.out.print("Enter keywords: ");
    List<String> keywords = Arrays.asList(in.nextLine().split("\\s+"));
    Product prod = centralWarehouse.getProduct(keywords);

    System.out.println(prod.getDescription() + ": " + prod.getPrice());
  }
  public static void main(String args[]) {

    String host = "localhost";
    int port = 1099;

    if (args.length > 0) {
      host = args[0];
      port = Integer.parseInt(args[1]);
    }

    // Initialise auctions from permanent storage
    BufferedReader br = null;
    BufferedWriter bw = null;
    try {
      AuctionServer rmiServer = new AuctionServer();
      Registry reg = LocateRegistry.getRegistry(host, port);
      reg.rebind("auctionServer", rmiServer);
      System.out.println("AuctionServer is ready");
      System.out.println(
          "Select one of the two options :\n1: To save live auctions\n2: To load test auctions that expire in a minute (note you need to start 5 clients first)  ");
      Scanner sc = new Scanner(System.in);
      bw = new BufferedWriter(new FileWriter("auctionsSaved.dat"));
      switch (sc.nextInt()) {
        case 1:
          for (AuctionItem item : rmiServer.liveAuctionItems.values()) {
            bw.write(item.toString() + "\n");
          }
          System.out.println("Saved auctions state.");
          break;
        case 2:
          GenerateAuctions generator = new GenerateAuctions();
          generator.generateList();
          br = new BufferedReader(new FileReader("auctionsBackup.dat"));
          String currentLine;
          while ((currentLine = br.readLine()) != null) {
            String[] array = currentLine.split(",");
            String name = array[0];
            double min_item_value = Double.parseDouble(array[1]);
            Date closing_date = new SimpleDateFormat("d/M/y H:mm:s").parse(array[2]);
            int client = Integer.parseInt(array[3]);
            rmiServer.registerAuctionItem(name, min_item_value, closing_date, client);
          }
          System.out.println("Initialised auctions...");
          break;
      }

    } catch (RemoteException e) {
      System.out.println("Exception in AuctionServer.main " + e);
    } catch (MalformedURLException ue) {
      System.out.println("MalformedURLException in RMIServerImp.main " + ue);
    } catch (IOException e) {
      System.out.println("Could not open file ");
      e.printStackTrace();
    } catch (ParseException e) {
      System.out.println("Unable to parse date");
      e.printStackTrace();
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
          System.out.println("Unable to close file.");
        }
      }
      if (bw != null) {
        try {
          bw.close();
        } catch (IOException e) {
          System.out.println("Unable to close file.");
        }
      }
    }
  }
示例#3
0
  public static void main(String args[]) {
    if (args.length != 2) {
      System.err.println("Uso: ObservadorCentralita hostregistro numPuertoRegistro");
      return;
    }

    // Instancia gestor de seguridad
    if (System.getSecurityManager() == null) {
      System.setSecurityManager(new SecurityManager());
    }
    try {
      // Obtiene referencia remota del servicio de rmiregistry
      ServicioCentralita srv =
          (ServicioCentralita) Naming.lookup("//" + args[0] + ":" + args[1] + "/Centralita");
      //                                                            |               |-> Número de
      // puerto escucha
      //                                                            |-> Host en el que se ejecuta el
      // servicio

      String nombreServicio = "default";

      while (nombreServicio == "default") {
        // Captura el nombre del servicio
        System.out.println(
            "Elija el servicio:\n\t[1] Bomberos\n\t[2] Sanitarios\n\t[3] Policía\n\t[4] Guardia Civil");
        Scanner input = new Scanner(System.in);
        String tipoServicio = input.nextLine();

        switch (tipoServicio) {
          case "1":
            nombreServicio = "Bomberos";
            break;
          case "2":
            nombreServicio = "Sanitarios";
            break;
          case "3":
            nombreServicio = "Policia";
            break;
          case "4":
            nombreServicio = "Guardia Civil";
            break;
          default:
            nombreServicio = "default";
            break;
        }
      }

      // Crea nuevo observador y lo registra en la lista
      ObservadorImpl o = new ObservadorImpl(nombreServicio);

      // Llamada al método remoto 'addObservador' del servicio
      srv.addObservador(o, nombreServicio);

      // Comrpeuba el nombre del servicio para imprimir código de color
      if (nombreServicio.equals("Bomberos")) {
        System.out.println(
            "\u001B[31mObservador " + nombreServicio + " registrado en la centralita\u001B[0m\n");
      } else if (nombreServicio.equals("Sanitarios")) {
        System.out.println(
            "\u001B[35mObservador " + nombreServicio + " registrado en la centralita\u001B[0m\n");
      } else if (nombreServicio.equals("Policia")) {
        System.out.println(
            "\u001B[34mObservador " + nombreServicio + " registrado en la centralita\u001B[0m\n");
      } else if (nombreServicio.equals("Guardia Civil")) {
        System.out.println(
            "\u001B[32mObservador " + nombreServicio + " registrado en la centralita\u001B[0m\n");
      }

      // Se mantiene esperando hasta que se para con Ctrl+D
      System.out.println("Para salir, pulsar Ctrl+D\n");
      Scanner sleep = new Scanner(System.in);
      while (sleep.hasNextInt()) {}

      // Elimina de la lista al observador. Llamada al método remoto 'delObservador' del servicio
      srv.delObservador(o, nombreServicio);
      System.exit(0);
    }
    // Excepción RMI
    catch (RemoteException e) {
      System.err.println("Error de comunicación: " + e.toString());
    }
    // Excepción cliente
    catch (Exception e) {
      System.err.println("Excepción en ObservadorCentralita: ");
      e.printStackTrace();
    }
  }