blob: 53581694d156ca8c29b949ab9a034e95ec990bbb [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()
26 (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 Thompsoncf624b92013-08-23 20:51:06 -070033 data.getSignedInfo().setTimestampMilliseconds(time(NULL) * 1000.0);
34 KeyChain::defaultSign(data);
Jeff Thompson9cc4be42013-08-27 18:12:41 -070035 shared_ptr<vector<unsigned char> > 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 }
61 } catch (std::exception &e) {
62 cout << "exception: " << e.what() << endl;
63 }
64 return 0;
65}