Exemplo n.º 1
0
  /** 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);
    }
  }
Exemplo n.º 2
0
  /** @param args the command line arguments */
  public static void main(String[] args) {

    try {

      // Removing any existing configuration?
      NetworkManager.RecursiveDelete(ConfigurationFile_RELAY);

      // Creation of the network manager
      final NetworkManager MyNetworkManager =
          new NetworkManager(
              NetworkManager.ConfigMode.RELAY, Name_RELAY, ConfigurationFile_RELAY.toURI());

      // Retrieving the network configurator
      NetworkConfigurator MyNetworkConfigurator = MyNetworkManager.getConfigurator();

      // Setting Configuration
      MyNetworkConfigurator.setUseMulticast(false);

      MyNetworkConfigurator.setTcpPort(TcpPort_RELAY);
      MyNetworkConfigurator.setTcpEnabled(true);
      MyNetworkConfigurator.setTcpIncoming(true);
      MyNetworkConfigurator.setTcpOutgoing(true);

      MyNetworkConfigurator.setHttpPort(HttpPort_RELAY);
      MyNetworkConfigurator.setHttpEnabled(true);
      MyNetworkConfigurator.setHttpIncoming(true);
      MyNetworkConfigurator.setHttpOutgoing(true);

      // Setting the Peer ID
      MyNetworkConfigurator.setPeerID(PID_RELAY);

      // Starting the JXTA network
      PeerGroup NetPeerGroup = MyNetworkManager.startNetwork();

      // Starting the connectivity monitor
      new ConnectivityMonitor(NetPeerGroup);

      // Stopping the network asynchronously
      ConnectivityMonitor.TheExecutor.schedule(
          new DelayedJxtaNetworkStopper(MyNetworkManager, "Click to stop " + Name_RELAY, "Stop"),
          0,
          TimeUnit.SECONDS);

    } catch (IOException Ex) {

      System.err.println(Ex.toString());

    } catch (PeerGroupException Ex) {

      System.err.println(Ex.toString());
    }
  }
  public static void main(String[] args) {

    try {

      // Removing any existing configuration?
      Tools.CheckForExistingConfigurationDeletion(Name, ConfigurationFile);

      // Creation of network manager
      NetworkManager MyNetworkManager =
          new NetworkManager(NetworkManager.ConfigMode.EDGE, Name, ConfigurationFile.toURI());

      // Retrieving the network configurator
      NetworkConfigurator MyNetworkConfigurator = MyNetworkManager.getConfigurator();

      // Checking if RendezVous_Adelaide_At_One_End should be a seed
      MyNetworkConfigurator.clearRendezvousSeeds();
      String TheSeed =
          "tcp://"
              + InetAddress.getLocalHost().getHostAddress()
              + ":"
              + RendezVous_Adelaide_At_One_End.TcpPort;
      Tools.CheckForRendezVousSeedAddition(Name, TheSeed, MyNetworkConfigurator);

      // Setting Configuration
      MyNetworkConfigurator.setTcpPort(TcpPort);
      MyNetworkConfigurator.setTcpEnabled(true);
      MyNetworkConfigurator.setTcpIncoming(true);
      MyNetworkConfigurator.setTcpOutgoing(true);

      // Setting the Peer ID
      Tools.PopInformationMessage(Name, "Setting the peer ID to :\n\n" + PID.toString());
      MyNetworkConfigurator.setPeerID(PID);

      // Starting the JXTA network
      Tools.PopInformationMessage(
          Name,
          "Start the JXTA network and to wait for a rendezvous connection with\n"
              + RendezVous_Adelaide_At_One_End.Name
              + " for maximum 2 minutes");
      PeerGroup NetPeerGroup = MyNetworkManager.startNetwork();

      // Disabling any rendezvous autostart
      NetPeerGroup.getRendezVousService().setAutoStart(false);

      if (MyNetworkManager.waitForRendezvousConnection(120000)) {
        Tools.popConnectedRendezvous(NetPeerGroup.getRendezVousService(), Name);
      } else {
        Tools.PopInformationMessage(Name, "Did not connect to a rendezvous");
      }

      // Preparing the listener and Creating the BiDiPipe
      PipeMsgListener MyListener = new Edge_Quinisela_At_The_Other_End();
      JxtaBiDiPipe MyBiDiPipe =
          new JxtaBiDiPipe(
              NetPeerGroup,
              RendezVous_Adelaide_At_One_End.GetPipeAdvertisement(),
              30000,
              MyListener);

      if (MyBiDiPipe.isBound()) {

        Tools.PopInformationMessage(Name, "Bidirectional pipe created!");

        // Sending a hello message !!!
        Message MyMessage = new Message();
        StringMessageElement MyStringMessageElement =
            new StringMessageElement("HelloElement", "Hello from " + Name, null);
        MyMessage.addMessageElement("DummyNameSpace", MyStringMessageElement);

        MyBiDiPipe.sendMessage(MyMessage);

        // Sleeping for 10 seconds
        Tools.GoToSleep(10000);

        // Sending a goodbye message !!!
        MyMessage = new Message();
        MyStringMessageElement =
            new StringMessageElement("HelloElement", "Goodbye from " + Name, null);
        MyMessage.addMessageElement("DummyNameSpace", MyStringMessageElement);

        MyBiDiPipe.sendMessage(MyMessage);

        // Sleeping for 10 seconds
        Tools.GoToSleep(10000);
      }

      // Closing the bidipipe
      MyBiDiPipe.close();

      // Stopping the network
      Tools.PopInformationMessage(Name, "Stop the JXTA network");
      MyNetworkManager.stopNetwork();

    } catch (IOException Ex) {

      // Raised when access to local file and directories caused an error
      Tools.PopErrorMessage(Name, Ex.toString());

    } catch (PeerGroupException Ex) {

      // Raised when the net peer group could not be created
      Tools.PopErrorMessage(Name, Ex.toString());
    }
  }
Exemplo n.º 4
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());
          }
        }
      }
    }
  }