blob: ebc1e8f80a2cd63ece1e3983ccbd0b0fb9237d32 [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:
21 Echo()
22 {
23 interestCount_ = 0;
24 }
25
Jeff Thompson9cc4be42013-08-27 18:12:41 -070026 void operator()
Jeff Thompson1656e6a2013-08-29 18:01:48 -070027 (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 -070028 ++interestCount_;
29
30 // Make and sign a Data packet.
31 Data data(interest->getName());
32 string content(string("Echo ") + interest->getName().toUri());
Jeff Thompson9cc4be42013-08-27 18:12:41 -070033 data.setContent((const unsigned char *)&content[0], content.size());
Jeff Thompson1ec4d0e2013-09-11 14:06:41 -070034 data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0);
Jeff Thompsoncf624b92013-08-23 20:51:06 -070035 KeyChain::defaultSign(data);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070036 Blob encodedData = data.wireEncode();
Jeff Thompsoncf624b92013-08-23 20:51:06 -070037
Jeff Thompson9cc4be42013-08-27 18:12:41 -070038 cout << "Sent content " << content << endl;
39 transport.send(*encodedData);
Jeff Thompsoncf624b92013-08-23 20:51:06 -070040 }
41
42 int interestCount_;
43};
44
45int 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 Thompson9cc4be42013-08-27 18:12:41 -070055 // The main event loop.
56 // Wait forever to receive one interest for the prefix.
Jeff Thompsoncf624b92013-08-23 20:51:06 -070057 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 Thompson1656e6a2013-08-29 18:01:48 -070062 } catch (std::exception& e) {
Jeff Thompsoncf624b92013-08-23 20:51:06 -070063 cout << "exception: " << e.what() << endl;
64 }
65 return 0;
66}