📄 elanp2pserver.java
字号:
// the same pipe advertisement to talk to the server. When the client // discovers the module advertisement it will extract the pipe // advertisement to create its pipe. PipeAdvertisement pipeadv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType()); // Setup some of the information field about the servive. In this // example, we just set the name, provider and version and a pipe // advertisement. The module creates an input pipes to listen // on this pipe endpoint. pipeadv.setName("JXTA-ELAN"); PipeID pid = (PipeID) IDFactory.newPipeID(sessionGroup.getPeerGroupID()); pipeadv.setPipeID(pid); /* System.out.println("Reading in file " + FILENAME); PipeAdvertisement pipeadv = null; try { FileInputStream is = new FileInputStream(FILENAME); pipeadv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement( new MimeMediaType("text/xml"), is); is.close(); } catch (java.io.IOException e) { System.out.println("failed to read/parse pipe advertisement"); e.printStackTrace(); System.exit(-1); } */ // add the pipe advertisement to the ModuleSpecAdvertisement mdadv.setPipeAdvertisement(pipeadv); // display the advertisement as a plain text document. System.out.println("Created service advertisement:"); StructuredTextDocument doc = (StructuredTextDocument) mdadv.getDocument(new MimeMediaType( "text/plain")); StringWriter out = new StringWriter(); doc.sendToWriter(out); System.out.println(out.toString()); out.close(); // Ok the Module advertisement was created, just publish // it in my local cache and into the NetPeerGroup. sessionDiscoSvc.publish(mdadv, DiscoveryService.ADV); sessionDiscoSvc.remotePublish(mdadv, DiscoveryService.ADV); // We are now ready to start the service -- // create the input pipe endpoint clients will // use to connect to the service myPipe = pipeSvc.createInputPipe(pipeadv, this); } catch (Exception ex) { ex.printStackTrace(); System.out.println("Server: Error publishing the module"); } } // By implementing PipeMsgListener, we define this method to deal with // messages as they occur public void pipeMsgEvent(PipeMsgEvent event) { Message msg = null; try { msg = event.getMessage(); if (msg == null) { return; } } catch (Exception e) { e.printStackTrace(); return; } // Get message String commandID = msg.getString(COMMAND); String broadcastPipeString = msg.getString("BroadcastPipe"); if (broadcastPipeString != null) { try { InputStream is = new ByteArrayInputStream(broadcastPipeString.getBytes()); PipeAdvertisement broadcastPipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(new MimeMediaType( "text/xml"), is); broadcastPipe = pipeSvc.createOutputPipe(broadcastPipeAdv, 10000); System.out.println("broadcast pipe advertisement received: " + broadcastPipeAdv.getName()); } catch (IOException ex) { ex.printStackTrace(); } return; } if (commandID == null) { System.out.println("msg received is null"); } else { try { if (broadcastPipe != null) { msg = pipeSvc.createMessage(); msg.setString("ACK", commandID); // send the message to the service pipe broadcastPipe.send(msg); } System.out.println("Received command: " + commandID + " for transcription: " + transcription.getName()); } catch (Exception e) { e.printStackTrace(); } } CommandAction ca = ELANCommandFactory.getCommandAction(transcription, commandID); if (ca != null) { ca.externalCommand(null, null); } else { System.out.println("no command could be retrieved for received msg"); } } private PeerGroup createGroup() { PeerGroup pg; // new peer group PeerGroupAdvertisement adv; // advertisement for the new peer group System.out.println("Creating a new group advertisement"); try { // create a new all purpose peergroup. ModuleImplAdvertisement implAdv = netGroup.getAllPurposePeerGroupImplAdvertisement(); pg = netGroup.newGroup(null, // Assign new group ID implAdv, // The implem. adv transcription.getName(), // The name "testing group adv"); // Helpful descr. // print the name of the group and the peer group ID adv = pg.getPeerGroupAdvertisement(); PeerGroupID GID = adv.getPeerGroupID(); System.out.println(" Group = " + adv.getName() + "\n Group ID = " + GID.toString()); } catch (Exception eee) { System.out.println("Group creation failed with " + eee.toString()); return (null); } try { // publish this advertisement // (send out to other peers/rendezvous peers) netDiscoSvc.remotePublish(adv, DiscoveryService.GROUP); System.out.println("Group published successfully.\n"); } catch (Exception e) { System.out.println("Error publishing group advertisement"); e.printStackTrace(); return (null); } return (pg); } private void joinGroup(PeerGroup grp) { System.out.println("Joining peer group..."); StructuredDocument creds = null; try { // Generate the credentials for the Peer Group AuthenticationCredential authCred = new AuthenticationCredential(grp, null, creds); // Get the MembershipService from the peer group MembershipService membership = grp.getMembershipService(); // Get the Authenticator from the Authentication creds Authenticator auth = membership.apply(authCred); // Check if everything is okay to join the group if (auth.isReadyForJoin()) { Credential myCred = membership.join(auth); // what do I do with the credential it returns? System.out.println("Successfully joined group " + grp.getPeerGroupName()); // display the credential as a plain text document. System.out.println("\nCredential: "); StructuredTextDocument doc = (StructuredTextDocument) myCred.getDocument(new MimeMediaType( "text/plain")); StringWriter out = new StringWriter(); doc.sendToWriter(out); System.out.println(out.toString()); out.close(); } else { System.out.println("Failure: unable to join group"); } } catch (Exception e) { System.out.println("Failure in authentication."); e.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -