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: |
Jeff Thompson | e529ace | 2013-09-18 17:32:13 -0700 | [diff] [blame] | 21 | Echo(KeyChain &keyChain) |
| 22 | : keyChain_(keyChain), responseCount_(0) |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 23 | { |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Jeff Thompson | e529ace | 2013-09-18 17:32:13 -0700 | [diff] [blame] | 26 | // onInterest. |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 27 | void operator() |
Jeff Thompson | e529ace | 2013-09-18 17:32:13 -0700 | [diff] [blame] | 28 | (const shared_ptr<const Name>& prefix, const shared_ptr<const Interest>& interest, Transport& transport) |
| 29 | { |
| 30 | ++responseCount_; |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 31 | |
| 32 | // Make and sign a Data packet. |
| 33 | Data data(interest->getName()); |
| 34 | string content(string("Echo ") + interest->getName().toUri()); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 35 | data.setContent((const unsigned char *)&content[0], content.size()); |
Jeff Thompson | 1ec4d0e | 2013-09-11 14:06:41 -0700 | [diff] [blame] | 36 | data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0); |
Jeff Thompson | e529ace | 2013-09-18 17:32:13 -0700 | [diff] [blame] | 37 | keyChain_.signData(data); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 38 | Blob encodedData = data.wireEncode(); |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 39 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 40 | cout << "Sent content " << content << endl; |
| 41 | transport.send(*encodedData); |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 42 | } |
Jeff Thompson | e529ace | 2013-09-18 17:32:13 -0700 | [diff] [blame] | 43 | |
| 44 | // onRegisterFailed. |
| 45 | void operator()(const ptr_lib::shared_ptr<const Name>& prefix) |
| 46 | { |
| 47 | ++responseCount_; |
| 48 | cout << "Register failed for prefix " << prefix->toUri() << endl; |
| 49 | } |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 50 | |
Jeff Thompson | e529ace | 2013-09-18 17:32:13 -0700 | [diff] [blame] | 51 | KeyChain keyChain_; |
| 52 | int responseCount_; |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | int main(int argc, char** argv) |
| 56 | { |
| 57 | try { |
| 58 | Face face("localhost"); |
| 59 | |
Jeff Thompson | e529ace | 2013-09-18 17:32:13 -0700 | [diff] [blame] | 60 | shared_ptr<PrivateKeyStorage> privateKeyStorage(new PrivateKeyStorage()); |
| 61 | shared_ptr<IdentityManager> identityManager(new IdentityManager(privateKeyStorage)); |
| 62 | KeyChain keyChain(identityManager); |
| 63 | keyChain.setFace(&face); |
| 64 | |
| 65 | Echo echo(keyChain); |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 66 | Name prefix("/testecho"); |
| 67 | cout << "Register prefix " << prefix.toUri() << endl; |
Jeff Thompson | 6d7369b | 2013-09-19 14:40:21 -0700 | [diff] [blame^] | 68 | face.registerPrefix(prefix, ref(echo), ref(echo)); |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 69 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 70 | // The main event loop. |
| 71 | // Wait forever to receive one interest for the prefix. |
Jeff Thompson | e529ace | 2013-09-18 17:32:13 -0700 | [diff] [blame] | 72 | while (echo.responseCount_ < 1) { |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 73 | face.processEvents(); |
| 74 | // We need to sleep for a few milliseconds so we don't use 100% of the CPU. |
| 75 | usleep(10000); |
| 76 | } |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 77 | } catch (std::exception& e) { |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 78 | cout << "exception: " << e.what() << endl; |
| 79 | } |
| 80 | return 0; |
| 81 | } |