📄 elanp2p.java
字号:
/* * File: ElanP2P.java * Project: MPI Linguistic Application * Date: 02 May 2007 * * Copyright (C) 2001-2007 Max Planck Institute for Psycholinguistics * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* * Created on Feb 17, 2004 * * First onset to make Elan a service provider for collaborative annotation services * */package mpi.eudico.p2p;import mpi.eudico.client.annotator.*;import mpi.eudico.client.annotator.commands.*;import mpi.eudico.server.corpora.clom.Transcription;import mpi.eudico.server.corpora.clomimpl.abstr.TranscriptionImpl;import net.jxta.credential.AuthenticationCredential;import net.jxta.credential.Credential;import net.jxta.discovery.DiscoveryService;import net.jxta.document.AdvertisementFactory;import net.jxta.document.Element;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocument;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.StructuredDocumentUtils;import net.jxta.document.StructuredTextDocument;import net.jxta.endpoint.Message;import net.jxta.exception.PeerGroupException;import net.jxta.id.IDFactory;import net.jxta.impl.util.BidirectionalPipeService;import net.jxta.membership.Authenticator;import net.jxta.membership.MembershipService;import net.jxta.peergroup.PeerGroup;import net.jxta.peergroup.PeerGroupFactory;import net.jxta.peergroup.PeerGroupID;import net.jxta.pipe.InputPipe;import net.jxta.pipe.OutputPipe;import net.jxta.pipe.PipeID;import net.jxta.pipe.PipeMsgEvent;import net.jxta.pipe.PipeMsgListener;import net.jxta.pipe.PipeService;import net.jxta.platform.ModuleClassID;import net.jxta.platform.ModuleSpecID;import net.jxta.protocol.ModuleClassAdvertisement;import net.jxta.protocol.ModuleImplAdvertisement;import net.jxta.protocol.ModuleSpecAdvertisement;import net.jxta.protocol.PeerGroupAdvertisement;import net.jxta.protocol.PipeAdvertisement;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.StringWriter;import java.net.URL;import java.util.Enumeration;import java.util.Hashtable;import javax.swing.JOptionPane;/** * @author hennie * */public class ElanP2P implements PipeMsgListener { //public class ElanP2P implements BidirectionalPipeService.MessageListener, Runnable { private static boolean jxtaInitialized = false; private static PeerGroup netGroup = null; private static DiscoveryService netDiscoSvc; // private final static String SERVICE = "JXTASPEC:ELAN"; // service name /** Holds value of property DOCUMENT ME! */ private final static String TAG = "DataTag"; // tag in message /** Holds value of property DOCUMENT ME! */ private final static String COMMAND = "Command"; /** Holds value of property DOCUMENT ME! */ private final static String ELAN_COMMAND = "elanCommand"; /** Holds value of property DOCUMENT ME! */ private final static String PARAMETER1 = "parameter1"; /** Holds value of property DOCUMENT ME! */ private final static String PARAMETER2 = "parameter2"; /** Holds value of property DOCUMENT ME! */ private final static String SET_EAF = "setEAF"; /** Holds value of property DOCUMENT ME! */ private final static String EAF_STRING = "EAFString"; /** Holds value of property DOCUMENT ME! */ private final static String INPIPE_OFFERED = "InpipeOffered"; /** Holds value of property DOCUMENT ME! */ private final static String PARTICIPANT_MAIL = "ParticipantMail"; /** Holds value of property DOCUMENT ME! */ private final static String PARTICIPANT_NAME = "ParticipantName"; /** Holds value of property DOCUMENT ME! */ private final static String GET_PARTICIPANTS = "GetParticipants"; /** Holds value of property DOCUMENT ME! */ private final static String ADD_PARTICIPANT = "AddParticipant"; /** Holds value of property DOCUMENT ME! */ private final static String TRANSCRIPTION_ID = "TranscriptionID"; /** Holds value of property DOCUMENT ME! */ private final static String FILENAME = "pipeserver.adv"; // file containing pipe advert. /** Holds value of property DOCUMENT ME! */ private final static String REQUEST_CONTROL = "RequestControl"; /** Holds value of property DOCUMENT ME! */ private final static String LEAVE_SESSION = "LeaveSession"; /** Holds value of property DOCUMENT ME! */ private final static String GET_SESSION_INFO = "GetSessionInfo"; /** Holds value of property DOCUMENT ME! */ private final static String SESSION_INFO = "SessionInfo"; private PeerGroup sessionGroup = null; private DiscoveryService sessionDiscoSvc; private PipeService pipeSvc; private InputPipe inputPipe; // input pipe for the service private OutputPipe outputPipe; private Message msg; // message received on input pipe private OutputPipe broadcastPipe; private BidirectionalPipeService.Pipe pipe = null; private BidirectionalPipeService.AcceptPipe acceptPipe = null; private Transcription transcription; private ViewerManager2 viewerManager; private ElanFrame2 frame; private ElanLayoutManager layoutManager; private P2P2Here p2p2Here; private CollaborationPanel collaborationPanel; private Hashtable outputPipeHash; private boolean hasControl = false; private String localEmail; private String localName; /** * Constructor for P2P client * use setManagers for ViewerManager2 and LayoutManager after they are constructed in init elan * p2p initiator already has an .eaf open so all managers are available * ElanFrame is needed by p2p client for openEAF(String fullPath) */ public ElanP2P(ElanFrame2 frame) { this.frame = frame; // do init in here? initP2P(); } /** * Creates a new ElanP2P instance * * @param theTranscription DOCUMENT ME! */ public ElanP2P(Transcription theTranscription) { transcription = theTranscription; } /** * DOCUMENT ME! * * @param viewerManager DOCUMENT ME! * @param layoutManager DOCUMENT ME! */ public void setManagers(ViewerManager2 viewerManager, ElanLayoutManager layoutManager) { this.viewerManager = viewerManager; this.layoutManager = layoutManager; transcription = viewerManager.getTranscription(); p2p2Here = new P2P2Here(this, viewerManager, frame); collaborationPanel = new CollaborationPanel(this); populateCollaborationPanel(); layoutManager.add(collaborationPanel); } private void initP2P() { if (!jxtaInitialized) { System.out.println("Setup Elan for p2p services"); startJxta(); jxtaInitialized = true; } } /** * DOCUMENT ME! * * @param command DOCUMENT ME! */ public void sendCommand(String command) { sendCommand(command, "", ""); } /** * DOCUMENT ME! * * @param command DOCUMENT ME! * @param parameter1 DOCUMENT ME! */ public void sendCommand(String command, String parameter1) { sendCommand(command, parameter1, ""); } /** * DOCUMENT ME! * * @param command DOCUMENT ME! * @param parameter1 DOCUMENT ME! * @param parameter2 DOCUMENT ME! */ public void sendCommand(String command, String parameter1, String parameter2) { sendCommand(COMMAND, command, parameter1, parameter2); } /** * DOCUMENT ME! * * @param command DOCUMENT ME! */ public void sendElanCommand(String command) { sendElanCommand(command, "", ""); } /** * DOCUMENT ME! * * @param command DOCUMENT ME! * @param parameter1 DOCUMENT ME! */ public void sendElanCommand(String command, String parameter1) { sendElanCommand(command, parameter1, ""); } /** * DOCUMENT ME! * * @param command DOCUMENT ME! * @param parameter1 DOCUMENT ME! * @param parameter2 DOCUMENT ME! */ public void sendElanCommand(String command, String parameter1, String parameter2) { if (hasControl) { sendCommand(ELAN_COMMAND, command, parameter1, parameter2); } } private void sendCommand(String type, String command, String parameter1, String parameter2) { if (pipeSvc == null) { // not yet connected return; } try { // just to be sure if (parameter1 == null) { parameter1 = ""; } // just to be sure if (parameter2 == null) { parameter2 = ""; } Message msg = pipeSvc.createMessage(); msg.setString(type, command); msg.setString(PARAMETER1, parameter1); msg.setString(PARAMETER2, parameter2); if (outputPipe != null) { outputPipe.send(msg); } System.out.println("sent command: " + command + " par1 = " + parameter1 + " par2 = " + parameter2 + "\n"); } catch (IOException e) { e.printStackTrace(); } } private static void startJxta() { try { // create, and Start the default jxta NetPeerGroup netGroup = PeerGroupFactory.newNetPeerGroup(); } catch (PeerGroupException e) { // could not instanciate the group, print the stack and exit System.out.println("fatal error : group creation failure"); e.printStackTrace(); return; } // get the discovery, and pipe service System.out.println("Getting NetDiscoveryService"); netDiscoSvc = netGroup.getDiscoveryService(); // System.out.println("Getting PipeService"); // pipeSvc = group.getPipeService(); } /** * DOCUMENT ME! * * @param theName DOCUMENT ME! * @param theEmail DOCUMENT ME! */ public void startServer(String theName, String theEmail) { hasControl = true; localName = theName; localEmail = theEmail; populateCollaborationPanel(); sessionGroup = createGroup(); if (sessionGroup != null) { joinGroup(sessionGroup); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -