00001
00002
00003
00004
00005
00006
00007
00008 #include "config.h"
00009 #include "ibrdtn/api/Client.h"
00010 #include "ibrdtn/api/StringBundle.h"
00011 #include "ibrcommon/net/tcpclient.h"
00012 #include "ibrcommon/thread/Mutex.h"
00013 #include "ibrcommon/thread/MutexLock.h"
00014 #include "ibrcommon/TimeMeasurement.h"
00015
00016 #include <iostream>
00017
00018 class EchoClient : public dtn::api::Client
00019 {
00020 public:
00021 EchoClient(dtn::api::Client::COMMUNICATION_MODE mode, string app, ibrcommon::tcpstream &stream)
00022 : dtn::api::Client(mode, app, stream), _stream(stream)
00023 {
00024 }
00025
00026 virtual ~EchoClient()
00027 {
00028 }
00029
00030 dtn::api::Bundle reply()
00031 {
00032 dtn::api::Bundle b = (*this).getBundle();
00033
00034
00035 return b;
00036 }
00037
00038 void echo(EID destination, int size, int lifetime)
00039 {
00040
00041 dtn::api::StringBundle b(destination);
00042
00043
00044 b.setLifetime(lifetime);
00045
00046
00047 char pattern[2000];
00048 for (int i = 0; i < 2000; i++)
00049 {
00050 pattern[i] = '0';
00051 pattern[i] += i % 10;
00052 }
00053
00054
00055 for (int i = 0; i < size; i += 2000)
00056 {
00057 if (i < (size - 2000))
00058 {
00059 b.append(string(pattern, 2000));
00060 }
00061 else
00062 {
00063 b.append(string(pattern, size - i));
00064 }
00065 }
00066
00067
00068 (*this) << b;
00069
00070
00071 flush();
00072 }
00073
00074 private:
00075 ibrcommon::tcpstream &_stream;
00076 };
00077
00078 void print_help()
00079 {
00080 cout << "-- dtnping (IBR-DTN) --" << endl;
00081 cout << "Syntax: dtnping [options] <dst>" << endl;
00082 cout << " <dst> set the destination eid (e.g. dtn://node/echo)" << endl;
00083 cout << "* optional parameters *" << endl;
00084 cout << " -h|--help display this text" << endl;
00085 cout << " --src <name> set the source application name (e.g. echo-client)" << endl;
00086 cout << " --nowait do not wait for a reply" << endl;
00087 cout << " --size the size of the payload" << endl;
00088 cout << " --count X send X echo in a row" << endl;
00089 cout << " --lifetime <seconds> set the lifetime of outgoing bundles; default: 30" << endl;
00090
00091 }
00092
00093 int main(int argc, char *argv[])
00094 {
00095 string ping_destination = "dtn://local/echo";
00096 string ping_source = "echo-client";
00097 int ping_size = 64;
00098 unsigned int lifetime = 30;
00099 bool wait_for_reply = true;
00100 size_t count = 1;
00101 dtn::api::Client::COMMUNICATION_MODE mode = dtn::api::Client::MODE_BIDIRECTIONAL;
00102
00103 if (argc == 1)
00104 {
00105 print_help();
00106 return 0;
00107 }
00108
00109 for (int i = 0; i < argc; i++)
00110 {
00111 string arg = argv[i];
00112
00113
00114 if (arg == "-h" || arg == "--help")
00115 {
00116 print_help();
00117 return 0;
00118 }
00119
00120 if (arg == "--nowait")
00121 {
00122 mode = dtn::api::Client::MODE_SENDONLY;
00123 wait_for_reply = false;
00124 }
00125
00126 if (arg == "--src" && argc > i)
00127 {
00128 ping_source = argv[i + 1];
00129 }
00130
00131 if (arg == "--size" && argc > i)
00132 {
00133 stringstream str_size;
00134 str_size.str( argv[i + 1] );
00135 str_size >> ping_size;
00136 }
00137
00138 if (arg == "--count" && argc > i)
00139 {
00140 stringstream str_count;
00141 str_count.str( argv[i + 1] );
00142 str_count >> count;
00143 }
00144
00145 if (arg == "--lifetime" && argc > i)
00146 {
00147 stringstream data; data << argv[i + 1];
00148 data >> lifetime;
00149 }
00150 }
00151
00152 size_t bundlecounter = 0;
00153
00154
00155 ping_destination = argv[argc - 1];
00156
00157 ibrcommon::TimeMeasurement tm;
00158
00159 try {
00160
00161 ibrcommon::tcpclient conn("127.0.0.1", 4550);
00162
00163
00164 EchoClient client(mode, ping_source, conn);
00165
00166
00167
00168 client.connect();
00169
00170
00171 EID addr = EID(ping_destination);
00172
00173 try {
00174 for (unsigned int i = 0; i < count; i++)
00175 {
00176 cout << "ECHO: " << addr.getNodeEID() << " ..."; cout.flush();
00177
00178
00179 tm.start();
00180
00181
00182 client.echo( addr, ping_size, lifetime );
00183
00184 if (wait_for_reply)
00185 {
00186
00187 dtn::api::Bundle relpy = client.reply();
00188
00189
00190 tm.stop();
00191
00192
00193 cout << tm << endl;
00194
00195 bundlecounter++;
00196 }
00197 else
00198 cout << endl;
00199 }
00200 } catch (dtn::api::ConnectionException ex) {
00201 cout << "Disconnected." << endl;
00202 } catch (ibrcommon::IOException ex) {
00203 cout << "Error while receiving a bundle." << endl;
00204 }
00205
00206
00207 client.close();
00208 conn.close();
00209
00210 } catch (ibrcommon::tcpclient::SocketException ex) {
00211 cerr << "Can not connect to the daemon. Does it run?" << endl;
00212 return -1;
00213 } catch (...) {
00214
00215 }
00216
00217 if (bundlecounter > 1)
00218 {
00219 std::cout << bundlecounter << " bundles received" << std::endl;
00220 }
00221
00222 return 0;
00223 }