📄 talk.java
字号:
Enumeration each; discovery.getRemoteAdvertisements(null, DiscoveryService.ADV, PipeAdvertisement.NameTag, TalkNameTag+".*", 200, null); for (int i =0; i <= MAXRETRIES; i++ ) { try { Thread.sleep(WaitingTime); print("."); } catch (Exception e) {} } println(""); try { each = discovery.getLocalAdvertisements( DiscoveryService.ADV, PipeAdvertisement.NameTag, TalkNameTag +".*"); if ( each.hasMoreElements() ) { PipeAdvertisement adv = null; consoleMessage("Found the following talk registrations:"); while (each.hasMoreElements()) { try { adv = (PipeAdvertisement) each.nextElement(); String name = adv.getName(); if( name.startsWith( TalkNameTag ) ) { name = name.substring( TalkNameTag.length() + 1 ); } println( name ); } catch(Exception e) { continue; } } } } catch (Exception e) { printStackTrace( "Discovery failed", e ); return ShellApp.appMiscError; } return ShellApp.appNoError; } /** * {@inheritDoc} */ public void discoveryEvent(DiscoveryEvent event) { DiscoveryResponseMsg res = event.getResponse(); Enumeration each; Advertisement adv = null; if (res.getDiscoveryType() == DiscoveryService.ADV) { each = res.getAdvertisements(); synchronized(this) { while (each.hasMoreElements()) { try { adv = (Advertisement) each.nextElement(); if (adv instanceof PipeAdvertisement) { if( null == results ) { results = new ArrayList(); } results.add(adv); } } catch (Exception ex) {} } notify(); } } } private int logout(String[] args) { if (args.length != 2) { return syntaxError(); } String name = args[1]; if (!deamonRunning(name)) { consoleMessage( "User '" + name + "' is not listening."); return ShellApp.appMiscError; } stopDeamon(name); return ShellApp.appNoError; } private int sendMessage(String[] args) { String name = null; String srcName = null; if (args[0].equals("-u")) { if (args.length != 3) { return syntaxError(); } srcName = args[1]; name = args[2]; // check if the name is registered if (!deamonRunning(srcName)) { consoleMessage( "User '" + srcName + "' is not logged in."); return ShellApp.appMiscError; } } else { name = args[0]; // There is no name for the person sending message // use the peer nameinstead. srcName = getGroup().getPeerName(); } PipeAdvertisement adv = findUserAdv(name); if (adv == null) { consoleMessage( "User '" + name + "' is not a registered."); return ShellApp.appMiscError; } OutputPipe pipeOut = null; consoleMessage( "Found advertisement for '" + name + "'. Attempting to connect" ); for (int i=0; i<2; i++) { try { pipeOut = getGroup().getPipeService().createOutputPipe(adv, 25000 ); if (pipeOut != null) { break; } print( "." ); } catch (Exception e) { //timeout exception } } if (pipeOut == null) { consoleMessage( "User " + name + " is not listening. Try again later"); return ShellApp.appMiscError; } // Get the user text consoleMessage("Connection established to user " + name); consoleMessage("Type your message. To exit, type \".\" at beginning of line"); Message msg = null; String userInput = null; while (true) { try { userInput = waitForInput(); if (userInput == null) { break; } if (userInput.equals(".")) { break; } String imagePath = null; String imageCaption = null; if (userInput.startsWith(".image")) { int nextColon = userInput.indexOf(":"); if (nextColon == -1) continue; userInput = userInput.substring(nextColon + 1); nextColon = userInput.indexOf(":"); if (nextColon != -1) { imagePath = userInput.substring(0, nextColon); userInput = userInput.substring(nextColon + 1); } else { imagePath = userInput; userInput = ""; } nextColon = userInput.indexOf(":"); if (nextColon != -1) { imageCaption = userInput.substring(0, nextColon); userInput = userInput.substring(nextColon + 1); } else { imageCaption = userInput; userInput = ""; } } // Build a message msg = new Message(); msg.addMessageElement( new StringMessageElement( SenderMessage, userInput, null) ); if (srcName != null) { msg.addMessageElement( new StringMessageElement( SenderName, srcName, null ) ); } try { if (imagePath != null && !imagePath.equals("")) { InputStream imageFile = new FileInputStream(imagePath); msg.replaceMessageElement("talkx", new InputStreamMessageElement( "image", MimeMediaType.AOS, imageFile, null)); imageFile.close(); } if (imageCaption != null && !imageCaption.equals("")) { msg.replaceMessageElement("talkx", new StringMessageElement("image_caption", imageCaption, null)); } } catch (Exception any) { printStackTrace( "Failure doing image processing", any ); } msg.addMessageElement( new StringMessageElement( SENDERGROUPNAME,getGroup().getPeerGroupName(), null ) ); pipeOut.send(msg); } catch (Exception ex) { printStackTrace( "Failed to send message to user :" + name, ex ); return ShellApp.appMiscError; } } pipeOut.close(); return ShellApp.appNoError; } private int registerNewUser(String[] args) { if ((args.length != 2) && (args.length != 3)) { return syntaxError(); } String name = args[1]; String type = null; if (args.length == 3) { // Type has been specified String t = args [2]; if (t.equals("-secure") || t.equals("-s")) { type = PipeService.UnicastSecureType; } else if (t.equals("-propagate") || t.equals("-p")) { type = PipeService.PropagateType; } else { // Default is unicast type = PipeService.UnicastType; } } else { type = PipeService.UnicastType; } // Check if there is already a registered user of the // same name. consoleMessage("Creating pipe named :" + name + " of type :" +type); PipeAdvertisement adv = findUserAdv(name); if (adv != null) { consoleMessage("Sorry, user " + name + " is already registered"); return ShellApp.appMiscError; } try { // Create a pipe advertisement for this pipe. adv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement( PipeAdvertisement.getAdvertisementType() ); } catch( Exception all ) { printStackTrace( "Advertisement document could not be created", all ); return ShellApp.appMiscError; } ShellObject obj = env.get("stdgroup"); // extract the advertisement PeerGroup group = (PeerGroup) obj.getObject(); adv.setPipeID( IDFactory.newPipeID( (PeerGroupID) group.getPeerGroupID() ) ); adv.setName(TalkNameTag + "." + name); adv.setDescription( "Created by JXTA Shell 'talk' command" ); adv.setType(type); try { // Save the document into the public folder discovery.publish(adv, DiscoveryService.INFINITE_LIFETIME, DiscoveryService.DEFAULT_EXPIRATION ); } catch (Exception e2) { printStackTrace( "Advertisement could not be saved", e2 ); return ShellApp.appMiscError; } consoleMessage("User '" + name + "' is now registered"); return ShellApp.appNoError; } private boolean checkUserAdv(PipeAdvertisement adv, String name) { if (adv == null) { return false; } if (adv.getName() == null) { return false; } String str = adv.getName(); return (str.startsWith(TalkNameTag) && str.endsWith(name)); } private PipeAdvertisement findUserAdv(String name) { Enumeration advs = null; if (name.toUpperCase().equals("IP2PGRP")) { return getMyJxtaPipeAdv(); } // First look in the local storage try { advs = discovery.getLocalAdvertisements(DiscoveryService.ADV, PipeAdvertisement.NameTag, TalkNameTag+"."+name); PipeAdvertisement adv = null; while (advs.hasMoreElements()) { try { adv = (PipeAdvertisement) advs.nextElement(); if (adv.getName().endsWith(name)) { if (checkUserAdv(adv, name)) { return adv;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -