📄 elanp2p.java
字号:
} */ } catch (Exception ex) { ex.printStackTrace(); System.out.println("Client: Error sending message to the service"); } } private void offerInputPipe(PeerGroup sessionPeerGroup) { try { // Create a pipe advertisement for the Service. PipeAdvertisement pipeadv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType()); pipeadv.setName("JXTA-ELAN"); PipeID pid = (PipeID) IDFactory.newPipeID(sessionPeerGroup.getPeerGroupID()); pipeadv.setPipeID(pid); // We are now ready to start the service -- // create the input pipe endpoint clients will // use to connect to the service inputPipe = pipeSvc.createInputPipe(pipeadv, this); // send pipe advertisement // create the pipe message StructuredTextDocument doc = (StructuredTextDocument) pipeadv.getDocument(new MimeMediaType( "text/xml")); StringWriter out = new StringWriter(); doc.sendToWriter(out); msg = pipeSvc.createMessage(); msg.setString(INPIPE_OFFERED, out.toString()); msg.setString(PARTICIPANT_MAIL, localEmail); msg.setString(PARTICIPANT_NAME, localName); // send the message to the service pipe outputPipe.send(msg); System.out.println("Input pipe advertisement returned"); out.close(); } catch (Exception ex) { ex.printStackTrace(); System.out.println("Server: Error publishing the module"); } } private void buildPipeInfrastructure() { msg = pipeSvc.createMessage(); msg.setString("Init", "PipeAdvertisements"); try { outputPipe.send(msg); } catch (IOException ex) { ex.printStackTrace(); } } private void sendCommands() { // read command from command line String command = null; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Type command: "); try { while ((command = br.readLine()) != null) { // create the pipe message msg = pipeSvc.createMessage(); msg.setString(COMMAND, command); // send the message to the service pipe outputPipe.send(msg); System.out.println("Command: \"" + command + "\" sent to the Server"); System.out.println("\nType command: "); } } catch (IOException e) { e.printStackTrace(); } } // 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; } // look for internal command String command = msg.getString(COMMAND); if (command != null) { String par1 = msg.getString(PARAMETER1); String par2 = msg.getString(PARAMETER2); System.out.println("received command: " + command + " with p1: " + par1 + " and p2: " + par2); if (command.equals(GET_PARTICIPANTS)) { sendCommand(ADD_PARTICIPANT, localName, localEmail); } else if (command.equals(ADD_PARTICIPANT)) { collaborationPanel.addParticipant(par1, par2); collaborationPanel.setControllingParticipant(par2); // collaborationPanel.addParticipant(localName, localEmail); } else if (command.equals(REQUEST_CONTROL)) { collaborationPanel.setControllingParticipant(par1); hasControl = false; } else if (command.equals(LEAVE_SESSION)) { collaborationPanel.removeParticipant(par1); // close the connection to that participant... // voor nu zorg dat er geen berichten meer verstuurd worden pipeSvc = null; } else if (command.equals(GET_SESSION_INFO)) { sendCommand(SESSION_INFO, collaborationPanel.getSharedDocumentName(), collaborationPanel.getChairName()); } else if (command.equals(SESSION_INFO)) { collaborationPanel.setSessionName(par1); collaborationPanel.setChairName(par2); collaborationPanel.setSharedDocumentName(par1); } } // look for specific elan message command = msg.getString(ELAN_COMMAND); if (command != null) { String parameter1 = msg.getString(PARAMETER1); String parameter2 = msg.getString(PARAMETER2); // must be handled here because no p2p2Here yet if (command.equals(SET_EAF)) { String eafString = parameter1; // save in user dir en openEAF in elanFrame aanroepen try { String fullPath = Constants.USERHOME + Constants.FILESEPARATOR + "p2p.eaf"; File p2pEafFile = new File(fullPath); FileWriter out = new FileWriter(p2pEafFile); out.write(eafString, 0, eafString.length()); out.flush(); out.close(); frame.openEAF(fullPath); } catch (Exception e) { e.printStackTrace(); } } else { p2p2Here.handleCommand(command, parameter1, parameter2); } return; } String offeredInputPipeString = msg.getString(INPIPE_OFFERED); if (offeredInputPipeString != null) { try { InputStream is = new ByteArrayInputStream(offeredInputPipeString.getBytes()); PipeAdvertisement offeredPipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(new MimeMediaType( "text/xml"), is); OutputPipe p = pipeSvc.createOutputPipe(offeredPipeAdv, 10000); String participantEmailString = msg.getString(PARTICIPANT_MAIL); String participantNameString = msg.getString(PARTICIPANT_NAME); if (participantEmailString != null) { if (outputPipeHash == null) { outputPipeHash = new Hashtable(); } outputPipeHash.put(participantEmailString, p); collaborationPanel.addParticipant(participantNameString, participantEmailString); // revise later if (outputPipe == null) { outputPipe = p; } System.out.println("pipe advertisement received: " + offeredPipeAdv.getName() + " from: " + participantEmailString); System.out.println( "sending eaf document to newly connected in pipe"); sendEAFDocument(p); } } catch (IOException ex) { ex.printStackTrace(); } return; } } private void sendEAFDocument(OutputPipe theOutPipe) { if (transcription != null) { // save changes to document first? try { String eafPath = ((TranscriptionImpl) transcription).getFullPath(); eafPath = "/" + eafPath.substring(5); File eafFile = new File(eafPath); FileReader in = new FileReader(eafFile); BufferedReader br = new BufferedReader(in); int c; StringBuffer sb = new StringBuffer(); while ((c = br.read()) != -1) { sb.append((char) c); } br.close(); String eafString = sb.toString(); // send it to the peer, must use this outputPipe so do not use sendElanCommand Message msg = pipeSvc.createMessage(); msg.setString(ELAN_COMMAND, SET_EAF); msg.setString(PARAMETER1, eafString); theOutPipe.send(msg); } catch (Exception e) { e.printStackTrace(); } } } /** * DOCUMENT ME! * * @param message DOCUMENT ME! * @param pipe DOCUMENT ME! */ public void messageReceived(Message message, OutputPipe pipe) { System.out.println("message received:" + message.getString(COMMAND)); } 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(); } } /** * DOCUMENT ME! */ public void requestControl() { sendCommand(REQUEST_CONTROL, localEmail); hasControl = true; } /** * DOCUMENT ME! */ public void leaveSession() { sendCommand(LEAVE_SESSION, localEmail); pipeSvc = null; } /** * DOCUMENT ME! */ public void populateCollaborationPanel() { if (localEmail != null) { collaborationPanel.setLocalParticipantName(localName); collaborationPanel.setLocalParticipantMail(localEmail); collaborationPanel.addParticipant(localName, localEmail); } if (hasControl) { collaborationPanel.setControllingParticipant(localEmail); } if (outputPipe != null) { sendCommand(GET_PARTICIPANTS); sendCommand(GET_SESSION_INFO); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -