⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elanp2p.java

📁 编辑视频文件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        try {            System.out.println("Share " + transcription.getName() +                " in p2p annotation session");            collaborationPanel.setSessionName(transcription.getName());            collaborationPanel.setChairName(localEmail);            collaborationPanel.setSharedDocumentName(transcription.getName());            sessionDiscoSvc = sessionGroup.getDiscoveryService();            //			System.out.println("Getting Session PipeService");            //			pipeSvc = sessionGroup.getPipeService();            if (pipeSvc == null) {                System.out.println("Getting Session PipeService");                pipeSvc = sessionGroup.getPipeService();            }            // Create the Module class advertisement associated with the service            // We build the module class advertisement using the Advertisement            // Factory class by passing it the type of the advertisement we            // want to construct. The Module class advertisement is a            // a very small advertisement that only advertises the existence            // of service. In order to access the service, a peer will            // have to discover the associated module spec advertisement.            ModuleClassAdvertisement mcadv = (ModuleClassAdvertisement) AdvertisementFactory.newAdvertisement(ModuleClassAdvertisement.getAdvertisementType());            mcadv.setName("JXTAMOD:ELAN");            mcadv.setDescription(                "First attempt to provide p2p annotation services");            ModuleClassID mcID = IDFactory.newModuleClassID();            mcadv.setModuleClassID(mcID);            // Ok the Module Class advertisement was created, just publish            // it in my local cache and to my peergroup. This            // is the NetPeerGroup            sessionDiscoSvc.publish(mcadv, DiscoveryService.ADV);            sessionDiscoSvc.remotePublish(mcadv, DiscoveryService.ADV);            // Create the Module Spec advertisement associated with the service            // We build the module Spec Advertisement using the advertisement            // Factory class by passing in the type of the advertisement we            // want to construct. The Module Spec advertisement will contain            // all the information necessary for a client to contact the service            // for instance it will contain a pipe advertisement to            // be used to contact the service            ModuleSpecAdvertisement mdadv = (ModuleSpecAdvertisement) AdvertisementFactory.newAdvertisement(ModuleSpecAdvertisement.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.            mdadv.setName(transcription.getName());            mdadv.setVersion("Version 1.0");            mdadv.setCreator("mpi.nl");            ModuleSpecID id = (ModuleSpecID) IDFactory.newModuleSpecID(mcID);            mdadv.setModuleSpecID(id);            mdadv.setSpecURI("http://www.mpi.nl/tools");            // Create a pipe advertisement for the Service. The client MUST use            // 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);            /*    BidirectionalPipeService pipeService = new BidirectionalPipeService(sessionGroup);                BidirectionalPipeService.AcceptPipe acceptPipe = pipeService.bind("JXTA-ELAN");                PipeAdvertisement pipeadv = acceptPipe.getAdvertisement(); */            // 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            inputPipe = pipeSvc.createInputPipe(pipeadv, this);            //			new Thread(this).start();        } catch (Exception ex) {            ex.printStackTrace();            System.out.println("Server: Error publishing the module");        }    }    /*    public void run() {            while (null == pipe) {                try {                    pipe = acceptPipe.accept(30000, this);                } catch (Exception e) {    // InterruptedException or IOException                    // e.printStackTrace();                }            }        } */    public void startClient(String key, String theName, String theEmail) {        System.out.println("Starting Client peer ....");        hasControl = false;        localName = theName;        localEmail = theEmail;        PeerGroup sessionGroup = discoverGroup(key);        if (sessionGroup == null) {            // hardcoded message string...            JOptionPane.showMessageDialog(frame,                "The requested document was not found", "Warning",                JOptionPane.WARNING_MESSAGE);            return;        }        if (sessionDiscoSvc == null) {            sessionDiscoSvc = sessionGroup.getDiscoveryService();        }        if (pipeSvc == null) {            System.out.println("Getting Session PipeService");            pipeSvc = sessionGroup.getPipeService();        }        connectToDocument(sessionGroup);        offerInputPipe(sessionGroup);        //		buildPipeInfrastructure();        //sendCommands();    }    private PeerGroup discoverGroup(String key) {        String groupName = "";        Enumeration en = null;        PeerGroup sessionPeerGroup = null;        // read group name from command line        //	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        //	System.out.println("Type key: ");        try {            //	if ((groupName = br.readLine()) != null) {            if (key != null) {                groupName = key;                System.out.println("Searching for the " + groupName + " Group");                final int timeout = 20000;                long start = System.currentTimeMillis();                while ((System.currentTimeMillis() - start) <= timeout) {                    try {                        // let's look first in our local cache to see                        // if we have it! We try to discover an advertisement                        // which has the (Name, JXTASPEC:JXTA-EX1) tag value                        //                        en = netDiscoSvc.getLocalAdvertisements(DiscoveryService.GROUP,                                "Name", groupName);                        // Found it! Stop searching and go send a message.                        if ((en != null) && en.hasMoreElements()) {                            break;                        }                        // We could not find anything in our local cache, so let's send a                        // remote discovery request searching for the service advertisement                        netDiscoSvc.getRemoteAdvertisements(null,                            DiscoveryService.GROUP, "Name", groupName, 1, null);                        // The discovery is asynchronous as we do not know                        // how long is going to take                        try { // sleep as much as we want. Yes we                            // could implement asynchronous listener pipe...                            Thread.sleep(2000);                        } catch (Exception e) {                        }                    } catch (IOException e) {                        // found nothing!  move on                    }                    System.out.print(".");                }                if ((en == null) || !en.hasMoreElements()) {                    System.out.println("\nNo group found");                    return null;                }                System.out.println("We found the group!");                String str = null;                PeerGroupAdvertisement pgAdv = null;                while (en.hasMoreElements()) {                    try {                        pgAdv = (PeerGroupAdvertisement) en.nextElement();                        // let's print the advertisement as a plain text document                        StructuredTextDocument doc = (StructuredTextDocument) pgAdv.getDocument(new MimeMediaType(                                    "text/plain"));                        StringWriter out = new StringWriter();                        doc.sendToWriter(out);                        System.out.println(out.toString());                        out.close();                        System.out.println(" Peer Group = " + pgAdv.getName());                        if (pgAdv.getName().equals(groupName)) {                            try {                                sessionPeerGroup = netGroup.newGroup(pgAdv);                            } catch (PeerGroupException e) {                                System.out.println(                                    "error creating PeerGroup from advertisement");                            }                            joinGroup(sessionPeerGroup);                        }                    } catch (java.io.IOException e) {                        // got a bad response. continue to the next response                        System.out.println("error parsing response element");                        e.printStackTrace();                        continue;                    }                }                 // end while            }        } catch (Exception e) {            e.printStackTrace();        }        return sessionPeerGroup;    }    private void connectToDocument(PeerGroup sessionPeerGroup) {        if (sessionPeerGroup == null) {            System.out.println("Peer group for session not avaible");            return;        }        String groupName = sessionPeerGroup.getPeerGroupName();        // let's try to locate the service advertisement groupName        // we will loop until we find it!        System.out.println("Searching for the " + groupName +            " Service advertisement");        Enumeration en = null;        while (true) {            try {                // let's look first in our local cache to see                // if we have it! We try to discover an advertisement                // which has the (Name, JXTASPEC:JXTA-EX1) tag value                //                en = sessionDiscoSvc.getLocalAdvertisements(DiscoveryService.ADV,                        "Name", groupName);                // Found it! Stop searching and go send a message.                if ((en != null) && en.hasMoreElements()) {                    break;                }                // We could not find anything in our local cache, so let's send a                // remote discovery request searching for the service advertisement                sessionDiscoSvc.getRemoteAdvertisements(null,                    DiscoveryService.ADV, "Name", groupName, 1, null);                // The discovery is asynchronous as we do not know                // how long is going to take                try { // sleep as much as we want. Yes we                    // could implement asynchronous listener pipe...                    Thread.sleep(2000);                } catch (Exception e) {                }            } catch (IOException e) {                // found nothing!  move on            }            System.out.print(".");        }        System.out.println("We found the service advertisement:");        // Ok get the service advertisement as a ModuleSpecAdvertisement        ModuleSpecAdvertisement mdsadv = (ModuleSpecAdvertisement) en.nextElement();        try {            // let's print the advertisement as a plain text document            StructuredTextDocument doc = (StructuredTextDocument) mdsadv.getDocument(new MimeMediaType(                        "text/plain"));            StringWriter out = new StringWriter();            doc.sendToWriter(out);            System.out.println(out.toString());            out.close();            // Get the pipe advertisement -- need it to talk to the service            PipeAdvertisement pipeadv = mdsadv.getPipeAdvertisement();            if (pipeadv == null) {                System.out.println("Error -- Null pipe advertisement!");                System.exit(1);            }            // create the output pipe endpoint to connect            // to the server, try 3 times to bind the pipe endpoint to            // the listening endpoint pipe of the service            outputPipe = null;            for (int i = 0; i < 3; i++) {                System.out.println("Trying to bind to pipe...");                try {                    outputPipe = pipeSvc.createOutputPipe(pipeadv, 10000);                    break;                } catch (java.io.IOException e) {                    // go try again;                }            }            if (outputPipe == null) {                System.out.println("Error resolving pipe endpoint");                System.exit(1);            } else {                if (outputPipeHash == null) {                    outputPipeHash = new Hashtable();                }                outputPipeHash.put("KNOEP", outputPipe);            }            /*            BidirectionalPipeService.Pipe pipe = null;                        BidirectionalPipeService pipeService = new BidirectionalPipeService(sessionPeerGroup);                        while (null == pipe) {                            pipe = pipeService.connect(pipeadv, 30000);                        }                        InputPipe input = pipe.getInputPipe();                        while (true) {                            Message message = input.waitForMessage();                            System.out.println(message.getString("ACK"));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -