/** initializes NetPeerGroup and the CMS */
  private void startJxta() {
    try {
      // create, and Start the default jxta NetPeerGroup
      netPeerGroup = PeerGroupFactory.newNetPeerGroup();

      // uncomment the following line if you want to start the app defined
      // the NetPeerGroup Advertisement (by default it's the shell)
      // in this case we want use jxta directly.

      // netPeerGroup.startApp(null);

      // instanciate and initialize a content management service for
      // the NetPeerGroup
      cms = new CMS();
      cms.init(netPeerGroup, null, null);

      // set up a ShareDemo directory inside the JXTA_HOME directory
      String homedir = System.getProperty("JXTA_HOME");
      homedir = (homedir != null) ? homedir + "ShareDemo" : "ShareDemo";

      // start CMS, creating a directory named ShareDemo to store the
      // ContentAdvertisement cache in.
      if (cms.startApp(new File(homedir)) == -1) {
        System.out.println("CMS initialization failed");
        System.exit(-1);
      }

    } catch (PeerGroupException e) {
      // could not instanciate the group, print the stack and exit
      System.out.println("fatal error : group creation failure");
      e.printStackTrace();
      System.exit(1);
    }
  }
示例#2
0
文件: Peer.java 项目: blickly/ptii
  ///////////////////////////////////////////////////////////////////
  ////                         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());
          }
        }
      }
    }
  }