Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame^] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include <cstdlib> |
| 8 | #include <sstream> |
| 9 | #include <iostream> |
| 10 | #include <time.h> |
| 11 | #include "../ndn-cpp/face.hpp" |
Jeff Thompson | f2d2186 | 2013-08-26 11:47:51 -0700 | [diff] [blame] | 12 | #include "../ndn-cpp/security/key-chain.hpp" |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 13 | |
| 14 | using namespace std; |
| 15 | using namespace ndn; |
| 16 | using namespace ptr_lib; |
| 17 | using namespace func_lib; |
| 18 | |
| 19 | class Echo { |
| 20 | public: |
| 21 | Echo() |
| 22 | { |
| 23 | interestCount_ = 0; |
| 24 | } |
| 25 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 26 | void operator() |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 27 | (const ptr_lib::shared_ptr<const Name>& prefix, const ptr_lib::shared_ptr<const Interest>& interest, Transport& transport) { |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 28 | ++interestCount_; |
| 29 | |
| 30 | // Make and sign a Data packet. |
| 31 | Data data(interest->getName()); |
| 32 | string content(string("Echo ") + interest->getName().toUri()); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 33 | data.setContent((const unsigned char *)&content[0], content.size()); |
Jeff Thompson | 1ec4d0e | 2013-09-11 14:06:41 -0700 | [diff] [blame] | 34 | data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0); |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 35 | KeyChain::defaultSign(data); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 36 | Blob encodedData = data.wireEncode(); |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 37 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 38 | cout << "Sent content " << content << endl; |
| 39 | transport.send(*encodedData); |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | int interestCount_; |
| 43 | }; |
| 44 | |
| 45 | int main(int argc, char** argv) |
| 46 | { |
| 47 | try { |
| 48 | Face face("localhost"); |
| 49 | |
| 50 | Echo echo; |
| 51 | Name prefix("/testecho"); |
| 52 | cout << "Register prefix " << prefix.toUri() << endl; |
| 53 | face.registerPrefix(prefix, ref(echo)); |
| 54 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 55 | // The main event loop. |
| 56 | // Wait forever to receive one interest for the prefix. |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 57 | while (echo.interestCount_ < 1) { |
| 58 | face.processEvents(); |
| 59 | // We need to sleep for a few milliseconds so we don't use 100% of the CPU. |
| 60 | usleep(10000); |
| 61 | } |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 62 | } catch (std::exception& e) { |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 63 | cout << "exception: " << e.what() << endl; |
| 64 | } |
| 65 | return 0; |
| 66 | } |