blob: 87fad400a9fcc25183cb14e79bc8313e9d6b6e52 [file] [log] [blame]
Jeff Thompsoncf624b92013-08-23 20:51:06 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
6#include <cstdlib>
7#include <sstream>
8#include <iostream>
9#include <time.h>
10#include "../ndn-cpp/face.hpp"
Jeff Thompsonf2d21862013-08-26 11:47:51 -070011#include "../ndn-cpp/security/key-chain.hpp"
Jeff Thompsoncf624b92013-08-23 20:51:06 -070012
13using namespace std;
14using namespace ndn;
15using namespace ptr_lib;
16using namespace func_lib;
17
18class Echo {
19public:
20 Echo()
21 {
22 interestCount_ = 0;
23 }
24
Jeff Thompson9cc4be42013-08-27 18:12:41 -070025 void operator()
Jeff Thompson1656e6a2013-08-29 18:01:48 -070026 (const ptr_lib::shared_ptr<const Name>& prefix, const ptr_lib::shared_ptr<const Interest>& interest, Transport& transport) {
Jeff Thompsoncf624b92013-08-23 20:51:06 -070027 ++interestCount_;
28
29 // Make and sign a Data packet.
30 Data data(interest->getName());
31 string content(string("Echo ") + interest->getName().toUri());
Jeff Thompson9cc4be42013-08-27 18:12:41 -070032 data.setContent((const unsigned char *)&content[0], content.size());
Jeff Thompson1ec4d0e2013-09-11 14:06:41 -070033 data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0);
Jeff Thompsoncf624b92013-08-23 20:51:06 -070034 KeyChain::defaultSign(data);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070035 Blob encodedData = data.wireEncode();
Jeff Thompsoncf624b92013-08-23 20:51:06 -070036
Jeff Thompson9cc4be42013-08-27 18:12:41 -070037 cout << "Sent content " << content << endl;
38 transport.send(*encodedData);
Jeff Thompsoncf624b92013-08-23 20:51:06 -070039 }
40
41 int interestCount_;
42};
43
44int main(int argc, char** argv)
45{
46 try {
47 Face face("localhost");
48
49 Echo echo;
50 Name prefix("/testecho");
51 cout << "Register prefix " << prefix.toUri() << endl;
52 face.registerPrefix(prefix, ref(echo));
53
Jeff Thompson9cc4be42013-08-27 18:12:41 -070054 // The main event loop.
55 // Wait forever to receive one interest for the prefix.
Jeff Thompsoncf624b92013-08-23 20:51:06 -070056 while (echo.interestCount_ < 1) {
57 face.processEvents();
58 // We need to sleep for a few milliseconds so we don't use 100% of the CPU.
59 usleep(10000);
60 }
Jeff Thompson1656e6a2013-08-29 18:01:48 -070061 } catch (std::exception& e) {
Jeff Thompsoncf624b92013-08-23 20:51:06 -070062 cout << "exception: " << e.what() << endl;
63 }
64 return 0;
65}