blob: 718a534b53b2daeec5bc4faca4f7f898da18f306 [file] [log] [blame]
Jeff Thompsoncf624b92013-08-23 20:51:06 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsoncf624b92013-08-23 20:51:06 -07004 * 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 Thompsonf2d21862013-08-26 11:47:51 -070012#include "../ndn-cpp/security/key-chain.hpp"
Jeff Thompsoncf624b92013-08-23 20:51:06 -070013
14using namespace std;
15using namespace ndn;
16using namespace ptr_lib;
17using namespace func_lib;
18
19class Echo {
20public:
Jeff Thompsone529ace2013-09-18 17:32:13 -070021 Echo(KeyChain &keyChain)
22 : keyChain_(keyChain), responseCount_(0)
Jeff Thompsoncf624b92013-08-23 20:51:06 -070023 {
Jeff Thompsoncf624b92013-08-23 20:51:06 -070024 }
25
Jeff Thompsone529ace2013-09-18 17:32:13 -070026 // onInterest.
Jeff Thompson9cc4be42013-08-27 18:12:41 -070027 void operator()
Jeff Thompsone529ace2013-09-18 17:32:13 -070028 (const shared_ptr<const Name>& prefix, const shared_ptr<const Interest>& interest, Transport& transport)
29 {
30 ++responseCount_;
Jeff Thompsoncf624b92013-08-23 20:51:06 -070031
32 // Make and sign a Data packet.
33 Data data(interest->getName());
34 string content(string("Echo ") + interest->getName().toUri());
Jeff Thompson9cc4be42013-08-27 18:12:41 -070035 data.setContent((const unsigned char *)&content[0], content.size());
Jeff Thompson1ec4d0e2013-09-11 14:06:41 -070036 data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0);
Jeff Thompsone529ace2013-09-18 17:32:13 -070037 keyChain_.signData(data);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070038 Blob encodedData = data.wireEncode();
Jeff Thompsoncf624b92013-08-23 20:51:06 -070039
Jeff Thompson9cc4be42013-08-27 18:12:41 -070040 cout << "Sent content " << content << endl;
41 transport.send(*encodedData);
Jeff Thompsoncf624b92013-08-23 20:51:06 -070042 }
Jeff Thompsone529ace2013-09-18 17:32:13 -070043
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 Thompsoncf624b92013-08-23 20:51:06 -070050
Jeff Thompsone529ace2013-09-18 17:32:13 -070051 KeyChain keyChain_;
52 int responseCount_;
Jeff Thompsoncf624b92013-08-23 20:51:06 -070053};
54
55int main(int argc, char** argv)
56{
57 try {
58 Face face("localhost");
59
Jeff Thompsone529ace2013-09-18 17:32:13 -070060 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 Thompsoncf624b92013-08-23 20:51:06 -070066 Name prefix("/testecho");
67 cout << "Register prefix " << prefix.toUri() << endl;
Jeff Thompson6d7369b2013-09-19 14:40:21 -070068 face.registerPrefix(prefix, ref(echo), ref(echo));
Jeff Thompsoncf624b92013-08-23 20:51:06 -070069
Jeff Thompson9cc4be42013-08-27 18:12:41 -070070 // The main event loop.
71 // Wait forever to receive one interest for the prefix.
Jeff Thompsone529ace2013-09-18 17:32:13 -070072 while (echo.responseCount_ < 1) {
Jeff Thompsoncf624b92013-08-23 20:51:06 -070073 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 Thompson1656e6a2013-08-29 18:01:48 -070077 } catch (std::exception& e) {
Jeff Thompsoncf624b92013-08-23 20:51:06 -070078 cout << "exception: " << e.what() << endl;
79 }
80 return 0;
81}