public static void main(String[] args) {
    String host = args[0];
    int port = Integer.parseInt(args[1]);
    long id = Long.parseLong(args[2]);
    String character = args[3];
    long actorId = Long.parseLong(args[4]);

    // Install an RMISecurityManager, if there is not a
    // SecurityManager already installed
    if (System.getSecurityManager() == null) {
      System.setSecurityManager(new RMISecurityManager());
    }

    String name = "rmi://" + host + ":" + port + "/MovieDatabase";

    try {
      MovieDatabase db = (MovieDatabase) Naming.lookup(name);
      db.noteCharacter(id, character, actorId);

      Movie movie = db.getMovie(id);
      out.println(movie.getTitle());
      for (Map.Entry entry : movie.getCharacters().entrySet()) {
        out.println("  " + entry.getKey() + "\t" + entry.getValue());
      }

    } catch (RemoteException | NotBoundException | MalformedURLException ex) {
      ex.printStackTrace(System.err);
    }
  }
Example #2
0
  private Descrittore contatta_server(String nome) {

    // variabili per l'RMI
    RMIServerInt serv = null; // server
    Descrittore descr_rit = null; // descrittore ritornato

    if (nome == null) {
      System.out.println("## contatta_server di Download ha ricevuto parametro null ! ");
      return null;
    }

    System.out.println("@ provo a contattare il server RMI ");
    // ################  RMI ################
    if (System.getSecurityManager() == null) {
      System.setSecurityManager(new SecurityManager());
    }

    Object o = null;
    try {
      // o = Naming.lookup("rmi://192.168.0.10:1099/srmi");
      Registry registry = LocateRegistry.getRegistry(server.getHostAddress());
      o = registry.lookup("srmi");

    } catch (RemoteException e) {
      System.out.println("## Problemi nell'RMI di Download - contatta_server di " + nome);
      e.printStackTrace();
    } catch (NotBoundException e) {
      System.out.println("## Problemi nell'RMI di Download - contatta_server di " + nome);
      e.printStackTrace();
    }

    if (o == null) {
      System.out.println(
          "## l'RMI di Download - contatta_server di " + nome + " ha ritornato l'oggetto o null");
      return null;
    }

    serv = (RMIServerInt) o;

    try {
      descr_rit = serv.lookup(nome, InetAddress.getLocalHost());
    } catch (RemoteException e) {
      e.printStackTrace();
      System.out.println("## Problemi con Lookup di " + nome);
      return null;

    } catch (UnknownHostException e) {
      e.printStackTrace();
      System.out.println("## Problemi con Lookup di " + nome);
      return null;
    }

    return descr_rit;
  }
Example #3
0
  // we define the method for getting a video link when pressing enter
  public String getvidlink() {
    try {
      System.setSecurityManager(new SecurityManager());

      ipadd = tfIP.getText();
      Interface client = (Interface) Naming.lookup("rmi://" + ipadd + "/getvid");

      // Get the String entered into the TextField tfInput, convert to int
      link = tfInput.getText();
      vlink = client.getvid(link);

    } catch (Exception e) {
      System.out.println("[System] Server failed: " + e);
    }
    return vlink;
  }
  public TransactionManagerAccessor(String propFileName) {
    LookupLocator locator = null;
    ServiceRegistrar registrar = null;

    // Security manager

    try {
      System.setSecurityManager(new RMISecurityManager());
    } catch (Exception e) {
      e.printStackTrace();
    }

    // Get properties from property file

    initProperties(propFileName);

    try {
      // Get lookup service locator at "jini://hostname"
      // use default port and register of the locator
      locator = new LookupLocator(jiniURL);
      registrar = locator.getRegistrar();

      // Space name provided in property file
      ServiceTemplate template;
      // Specify the service requirement, array (length 1) of
      // instances of Class
      Class[] types = new Class[] {TransactionManager.class};
      template = new ServiceTemplate(null, types, null);

      // Get manager, 10 attempts!
      for (int i = 0; i < 10; i++) {
        Object obj = registrar.lookup(template);

        if (obj instanceof TransactionManager) {
          manager = (TransactionManager) obj;
          break;
        }
        System.out.println("BasicService. TransactionManager not " + "available. Trying again...");
        Thread.sleep(MAX_LOOKUP_WAIT);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Example #5
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());
  }
Example #6
0
  private static void setup() {

    try {

      NotActivatableInterface rsi; // Remote server interface

      System.setSecurityManager(new RMISecurityManager());

      rsi = (NotActivatableInterface) Activatable.register(ACTIVATION_DESC);
      System.out.println("Got stub for " + SERVER_OBJECT + " implementation");

      Naming.rebind(SERVER_OBJECT, rsi);
      System.out.println("Exported " + SERVER_OBJECT + " implementation");

    } catch (Exception e) {
      System.err.println("Exception: " + e);
      e.printStackTrace();
    }
  }
  public static void main(String[] args) throws Throwable {

    for (Boolean hasSM : new boolean[] {false, true}) {
      if (hasSM) System.setSecurityManager(new PermissiveSecurityManger());
      for (Charset cs : Charset.availableCharsets().values()) {
        if ("ISO-2022-CN".equals(cs.name())
            || "x-COMPOUND_TEXT".equals(cs.name())
            || "x-JISAutoDetect".equals(cs.name())) continue;
        System.out.printf("Testing(sm=%b) " + cs.name() + "....", hasSM);
        // full bmp first
        char[] bmpCA = new char[0x10000];
        for (int i = 0; i < 0x10000; i++) {
          bmpCA[i] = (char) i;
        }
        byte[] sbBA = new byte[0x100];
        for (int i = 0; i < 0x100; i++) {
          sbBA[i] = (byte) i;
        }
        test(cs, bmpCA, sbBA);
        // "randomed" sizes
        Random rnd = new Random();
        for (int i = 0; i < 10; i++) {
          int clen = rnd.nextInt(0x10000);
          int blen = rnd.nextInt(0x100);
          // System.out.printf("    blen=%d, clen=%d%n", blen, clen);
          test(cs, Arrays.copyOf(bmpCA, clen), Arrays.copyOf(sbBA, blen));
          // add a pair of surrogates
          int pos = clen / 2;
          if ((pos + 1) < blen) {
            bmpCA[pos] = '\uD800';
            bmpCA[pos + 1] = '\uDC00';
          }
          test(cs, Arrays.copyOf(bmpCA, clen), Arrays.copyOf(sbBA, blen));
        }

        testMixed(cs);
        System.out.println("done!");
      }
    }
  }
Example #8
0
  // we define the method for downloading when pressing on the download button
  public void qrvidlink() {
    try {
      System.setSecurityManager(new SecurityManager());

      ipadd = tfIP.getText();
      Interface client = (Interface) Naming.lookup("rmi://" + ipadd + "/getvid");

      // Get the String entered into the TextField tfInput, convert to int
      link = tfInput.getText();
      vlink = client.getvid(link);

      // here we receive the image serialized into bytes from the server and
      // saved it on the client as png image
      byte[] bytimg = client.qrvid(vlink);
      BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytimg));
      File outputfile = new File("qrcode.png");
      ImageIO.write(img, "png", outputfile);

      // img= new ImageIcon(bytimg.toByteArray());
    } catch (Exception e) {
      System.out.println("[System] Server failed: " + e);
    }
  }
  public static void main(String[] args) throws Throwable {
    final int itrs = Integer.getInteger("iterations", 100000);
    // final int itrs = Integer.getInteger("iterations", 12);
    final int size = Integer.getInteger("size", 2048);
    final int subsize = Integer.getInteger("subsize", 128);
    final int maxchar = Integer.getInteger("maxchar", 128);
    final String regex = System.getProperty("filter");
    final Pattern filter = (regex == null) ? null : Pattern.compile(regex);
    final boolean useSecurityManager = Boolean.getBoolean("SecurityManager");
    if (useSecurityManager) System.setSecurityManager(new PermissiveSecurityManger());
    final Random rnd = new Random();

    String[] csns =
        new String[] {
          "Big5",
          "Johab",
          "EUC_CN",
          "EUC_KR",
          "MS932",
          "MS936",
          "MS949",
          "MS950",
          "GBK",
          "Big5_HKSCS",
          "Big5_HKSCS_2001",
          "Big5_Solaris",
          "MS950_HKSCS",
          "MS950_HKSCS_XP",
          "IBM1364",
          "IBM1381",
          "IBM1383",
          "IBM930",
          "IBM933",
          "IBM935",
          "IBM937",
          "IBM939",
          "IBM942",
          "IBM943",
          "IBM948",
          "IBM949",
          "IBM950",
          "IBM970",
        };

    ArrayList<long[]> sum = new ArrayList<>();

    for (final String csn : csns) {
      final Charset cs = Charset.forName(csn);
      List<Integer> cps = new ArrayList<>(0x4000);
      int off = 0;
      int cp = 0;
      int n = 0;
      CharsetEncoder enc = cs.newEncoder();
      while (cp < 0x10000 && n < cps.size()) {
        if (enc.canEncode((char) cp)) {
          cps.add(cp);
          n++;
        }
        cp++;
      }
      Collections.shuffle(cps);
      char[] ca = new char[cps.size()];
      for (int i = 0; i < cps.size(); i++) ca[i] = (char) (int) cps.get(i);

      System.out.printf("%n--------%s---------%n", csn);
      for (int sz = 8; sz <= 2048; sz *= 2) {
        System.out.printf("   [len=%d]%n", sz);

        final char[] chars = Arrays.copyOf(ca, sz);
        final String str = new String(chars);
        final byte[] bs = str.getBytes(cs);

        Job[] jobs = {
          new Job("String decode: csn") {
            public void work() throws Throwable {
              for (int i = 0; i < itrs; i++) new String(bs, csn);
            }
          },
          new Job("String decode: cs") {
            public void work() throws Throwable {
              for (int i = 0; i < itrs; i++) new String(bs, cs);
            }
          },
          new Job("String encode: csn") {
            public void work() throws Throwable {
              for (int i = 0; i < itrs; i++) str.getBytes(csn);
            }
          },
          new Job("String encode: cs") {
            public void work() throws Throwable {
              for (int i = 0; i < itrs; i++) str.getBytes(cs);
            }
          },
        };
        sum.add(time(jobs));
      }
    }
  }
Example #10
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();
    }
  }