@Test public void testLinkQueue() throws Exception { Log.info(Log.FAC_TEST, "Started testLinkQueue"); String prefix = String.format("/repotest/test_%016X", rnd.nextLong()); String queuenamestring = prefix + "/queue"; String objnamestring = prefix + "/obj"; ContentName queuename = ContentName.fromNative(queuenamestring); ContentName objname = ContentName.fromNative(objnamestring); int objsize = 1024 * 600; CCNHandle recvhandle = CCNHandle.getHandle(); CCNHandle sendhandle = CCNHandle.open(recvhandle.keyManager()); char[] buf = new char[objsize]; Arrays.fill(buf, 'x'); String objfill = String.valueOf(buf); VersioningInterest vi = new VersioningInterest(recvhandle); TestListener listener = new TestListener(); vi.expressInterest(queuename, listener); Thread.sleep(1000); CCNStringObject so = new CCNStringObject(objname, objfill, SaveType.LOCALREPOSITORY, sendhandle); so.save(); so.close(); Link link = new Link(so.getVersionedName()); LinkObject lo = new LinkObject(queuename, link, SaveType.LOCALREPOSITORY, sendhandle); lo.save(); lo.close(); sendhandle.close(); // now see if we got it in the TestListener Assert.assertTrue(listener.cl.waitForValue(1, 60000)); ReceivedData rd = listener.received.get(0); ContentObject co = rd.object; CCNStringObject so2 = new CCNStringObject(co.name(), recvhandle); Assert.assertEquals(so, so2); Log.info(Log.FAC_TEST, "Completed testLinkQueue"); }
/** * This actual CCN loop to send/receive messages. Called by the UI class. This method blocks! If * the UI is not multi-threaded, you should start a thread to hold listen(). * * <p>When shutdown() is called, listen() will exit. * * @throws ConfigurationException * @throws IOException * @throws MalformedContentNameStringException */ public void listen() throws ConfigurationException, IOException, MalformedContentNameStringException, RuntimeException { // Also publish your keys under the chat "channel name" namespace if (_namespace.toString().startsWith("ccnx:/")) { UserConfiguration.setDefaultNamespacePrefix(_namespace.toString().substring(5)); } else { UserConfiguration.setDefaultNamespacePrefix(_namespace.toString()); } CCNHandle tempReadHandle = CCNHandle.getHandle(); // Writing must be on a different handle, to enable us to read back text // we have // written when nobody else is reading. CCNHandle tempWriteHandle = CCNHandle.open(); _readString = new CCNStringObject(_namespace, (String) null, SaveType.RAW, tempReadHandle); _readString.updateInBackground(true); String introduction = UserConfiguration.userName() + " has entered " + _namespace; _writeString = new CCNStringObject(_namespace, introduction, SaveType.RAW, tempWriteHandle); _writeString.save(); // Publish the user's friendly name as a speaker if needed // AND if this topic doesn't already have a speaker! if (this.currentUser.isSpeaker()) { if (this.userList.getSpeaker() == null || this.userList.getSpeaker().equals(this.currentUser)) { String friendlyNameNamespaceStr = _namespaceStr + TextConf.SPEAKER_COMPONENT; _spkrNamespace = KeyProfile.keyName( ContentName.fromURI(friendlyNameNamespaceStr), _writeString.getContentPublisher()); Log.info("**** Speaker Namespace is " + _spkrNamespace); // read the string here..... _readNameString = new CCNStringObject(_spkrNamespace, (String) null, SaveType.RAW, tempReadHandle); _readNameString.updateInBackground(true); String publishedNameStr = UserConfiguration.userName(); Log.info("*****I am adding my own speaker name as " + publishedNameStr); _writeNameString = new CCNStringObject(_spkrNamespace, publishedNameStr, SaveType.RAW, tempWriteHandle); _writeNameString.save(); } else { throw new RuntimeException( "The conference topic " + this.currentTopic.getTopicName() + " already has a Speaker (" + this.userList.getSpeaker().getUserName() + ")!!!"); } } else { // If the current user isn't a speaker, then they can't join a conference // that doesn't have one (i.e. they can't create a conference) if (this.userList.speaker == null) { throw new RuntimeException( "The conference topic " + this.currentTopic.getTopicName() + " doesn't have a Speaker yet so it isn't accepting non-Speakers!!!"); } } // Publish the user's friendly name under a new ContentName String friendlyNameNamespaceStr = _namespaceStr + TextConf.MEMBER_COMPONENT; _userNamespace = KeyProfile.keyName( ContentName.fromURI(friendlyNameNamespaceStr), _writeString.getContentPublisher()); Log.info("**** Friendly Namespace is " + _userNamespace); // read the string here..... _readNameString = new CCNStringObject(_userNamespace, (String) null, SaveType.RAW, tempReadHandle); _readNameString.updateInBackground(true); String publishedNameStr = UserConfiguration.userName(); Log.info("*****I am adding my own friendly name as " + publishedNameStr); _writeNameString = new CCNStringObject(_userNamespace, publishedNameStr, SaveType.RAW, tempWriteHandle); _writeNameString.save(); try { addNameToHash(_writeNameString.getContentPublisher(), _writeNameString.string()); } catch (IOException e) { System.err.println("Unable to read from " + _writeNameString + "for writing to hashMap"); e.printStackTrace(); } // Need to do synchronization for updates that come in while we're // processing last one. while (!_finished) { try { synchronized (_readString) { _readString.wait(CYCLE_TIME); } } catch (InterruptedException e) { } if (_readString.isSaved()) { Timestamp thisUpdate = _readString.getVersion(); if ((null == _lastUpdate) || thisUpdate.after(_lastUpdate)) { Log.info( "Got an update from " + _readString.getBaseName().toURIString() + ": " + _readString.getVersion() + "..." + _readString.string()); _lastUpdate = thisUpdate; // lookup friendly name to display for this user..... User userFriendlyName = getFriendlyName(_readString.getContentPublisher(), tempReadHandle, tempWriteHandle); if (!userFriendlyName.equals(this.currentUser)) { messageReceived(userFriendlyName, thisUpdate, _readString.string()); } } } } }
/** @param args */ public static void main(String[] args) { String extraUsage = ""; Log.setDefaultLevel(Level.WARNING); try { int offset = 0; SaveType type = SaveType.REPOSITORY; for (int i = 0; i < args.length; i++) { if (i == 0 && args[0].startsWith("[")) { extraUsage = args[0]; offset++; continue; } else if (args[i].equals("-h")) { usage(extraUsage); System.exit(0); } else if (!args[i].startsWith("-")) break; if (args[i].equals("-q")) { Log.setDefaultLevel(Level.WARNING); offset++; } if (args[i].equals("-r")) { type = SaveType.RAW; offset++; } } if (args.length - offset < 2) { usage(extraUsage); System.exit(1); } boolean hasAs = false; if (args.length - offset > 2) { if (args[offset + 2].equals("-as")) hasAs = true; else { usage(extraUsage); System.exit(1); } } ContentName linkName = ContentName.fromURI(args[offset++]); ContentName targetName = ContentName.fromURI(args[offset++]); Tuple<Integer, CCNHandle> tuple = null; if (hasAs) { tuple = CreateUserData.handleAs(args, offset); if (null == tuple) { usage(extraUsage); System.exit(1); } } // Can also use command line system properties and environment variables to // point this handle to the correct user. CCNHandle handle = ((null == tuple) || (null == tuple.second())) ? CCNHandle.getHandle() : tuple.second(); LinkObject theLink = new LinkObject(linkName, new Link(targetName), type, handle); theLink.save(); theLink.close(); System.out.println("Created link: " + theLink); handle.close(); } catch (Exception e) { handleException("Error: cannot initialize device. ", e); System.exit(-3); } }