Esempio n. 1
0
  /**
   * Make this transport as up and running.
   *
   * <p>When this method is called, all the services are already registered with the peergroup. So
   * we do not need to delay binding any further. All the public methods, which could be called
   * between init and startApp are defensive regarding the services possibly not being there.
   */
  public int startApp(String[] arg) {

    resolver = group.getResolverService();
    membership = group.getMembershipService();

    if (null == resolver) {
      if (LOG.isEnabledFor(Level.DEBUG)) {
        LOG.debug("Endpoint Router start stalled until resolver service available");
      }
      return Module.START_AGAIN_STALLED;
    }

    if (null == membership) {
      if (LOG.isEnabledFor(Level.DEBUG)) {
        LOG.debug("Endpoint Router start stalled until membership service available");
      }
      return Module.START_AGAIN_STALLED;
    }

    resolver.registerHandler(routerSName, this);
    // create and register the srdi service
    srdiIndex = new SrdiIndex(group, srdiIndexerFileName);
    // Srdi is a thread but we are not going to start,
    // since the service is reactive.
    srdi = new Srdi(group, routerSName, this, srdiIndex, 0, 0);
    resolver.registerSrdiHandler(routerSName, this);

    // obtain credential for resolver messages
    try {
      // FIXME 20041008 bondolo this needs to be converted to dynamically managing credentials.

      // get the only credential "nobody"
      credential = (Credential) membership.getDefaultCredential();
      if (null != credential) {
        credentialDoc = credential.getDocument(MimeMediaType.XMLUTF8);
      }
    } catch (Exception e) {
      if (LOG.isEnabledFor(Level.ERROR)) {
        LOG.error("failed to get credential", e);
      }
    }

    // get the RouteCM cache service
    routeCM = router.getRouteCM();

    return 0;
  }
Esempio n. 2
0
  ///////////////////////////////////////////////////////////////////
  ////                         public methods                    ////
  public void preinitialize() throws IllegalActionException {
    super.preinitialize();
    PropertyConfigurator.configure(System.getProperties());

    // if (configDir.hasToken()) {
    // String Dir = ((StringToken)configDir.getToken()).stringValue();
    // } else {
    String Dir = "c:/Cygwin/home/ellen_zh/ptII/ptolemy/actor/lib/jxta";

    // }
    // if (actorList.hasToken()) {
    // String _actorListFileName = ((StringToken)actorList.getToken()).stringValue();
    // } else {
    String _actorListFileName = "c:/Cygwin/home/ellen_zh/ptII/ptolemy/actor/lib/jxta/actors.xml";

    // }
    _properties = new Properties(System.getProperties());

    InputStream configProperties = null;

    try {
      configProperties = new FileInputStream(_CONFIG_FILE);
      _properties.load(configProperties);
    } catch (IOException e) {
      System.out.println(
          "Warning: Can't find configuration propertiees file. ' " + e.getMessage() + "'");
    } finally {
      if (configProperties != null) {
        try {
          configProperties.close();
        } catch (Throwable throwable) {
          System.out.println("Ignoring failure to close stream " + "on '" + _CONFIG_FILE + "'");
          throwable.printStackTrace();
        }
      }
    }

    PeerGroup netPeerGroup = null;

    try {
      netPeerGroup = PeerGroupFactory.newNetPeerGroup();
    } catch (PeerGroupException ex) {
      System.out.println("Error: cannot locate net peer group.\n" + ex.getMessage());
    }

    // load the peer group adv for actor exchange
    String groupAdvFileName = _properties.getProperty("GroupAdvFileName");

    if (groupAdvFileName == null) {
      System.out.println("Error: property undefined - GroupAdvFileName.\n");
    }

    PeerGroupAdvertisement groupAdv = null;

    try {
      groupAdv =
          (PeerGroupAdvertisement)
              AdvertisementFactory.newAdvertisement(
                  XML_MIME_TYPE, new FileInputStream(Dir + "/" + groupAdvFileName));
    } catch (FileNotFoundException ex) {
      System.out.println("Error: cannot find group adv file.\n" + ex.getMessage());
    } catch (IOException ex) {
      System.out.println("Error: reading group adv file.\n" + ex.getMessage());
    }

    System.out.println("peer groupAdv: " + groupAdvFileName);
    System.out.println("success before instantiate peer group");

    // instantiate the peer group for actor exchange
    try {
      _group = netPeerGroup.newGroup(groupAdv);
    } catch (PeerGroupException ex) {
      System.out.println("Error: cannot instantiate peer group.\n" + ex.getMessage());
    }

    // join the peer group for actor exchange
    // no authentication is done here
    // modeled after JoinDemo from JXTA Examples
    StructuredDocument identityInfo = null;

    try {
      AuthenticationCredential authCred = new AuthenticationCredential(_group, null, identityInfo);
      MembershipService membershipService = _group.getMembershipService();
      _authenticator = membershipService.apply(authCred);

      if (_authenticator.isReadyForJoin()) {
        _credential = membershipService.join(_authenticator);
        System.out.println("Info: join group successful.");
        _credential.getDocument(XML_MIME_TYPE).sendToStream(System.out);
      } else {
        System.out.println("Error: unable to join group.");
      }
    } catch (Exception ex) {
      System.out.println("Error: failure in authentication.\n" + ex.getMessage());
    }

    _discoveryService = _group.getDiscoveryService();
    _discoveryService.addDiscoveryListener(this);

    _resolverService = _group.getResolverService();

    // register this as a query handler
    _resolverService.registerHandler(_ACTOR_QUERY_HANDLER_NAME, this);

    // construct the actor query message
    StringBuffer queryTextBuffer = new StringBuffer("<?xml version=\"1.0\"?>\n\n");
    queryTextBuffer = queryTextBuffer.append("<ActorQuery>\n");
    queryTextBuffer = queryTextBuffer.append("What actors do you have?");
    queryTextBuffer = queryTextBuffer.append("\n</ActorQuery>\n");
    _actorQueryMessage =
        new ResolverQuery(_ACTOR_QUERY_HANDLER_NAME, null, null, queryTextBuffer.toString(), 0);
    _actorQueryMessage.setSrc(_group.getPeerID().toString());

    // construct the actor query response message
    if (_actorListFileName != null) {
      StringBuffer actorListText = new StringBuffer();

      BufferedReader fileReader = null;
      try {
        fileReader = new BufferedReader(new FileReader(_actorListFileName));
        String newline = System.getProperty("line.separator");

        while (true) {
          String line = fileReader.readLine();

          if (line == null) {
            break;
          }

          actorListText = actorListText.append(line);
          actorListText = actorListText.append(newline);
        }

        _actorQueryResponse =
            new ResolverResponse(_ACTOR_QUERY_HANDLER_NAME, null, 0, actorListText.toString());
      } catch (IOException ex) {
        System.out.println("Warning: error reading actor list file.\n" + ex.getMessage());
      } finally {
        if (fileReader != null) {
          try {
            fileReader.close();
          } catch (Exception ex) {
            System.out.println("Failed to close: " + ex.getMessage());
          }
        }
      }
    }
  }