Goby v2
test.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 //
5 //
6 // This file is part of the Goby Underwater Autonomy Project Binaries
7 // ("The Goby Binaries").
8 //
9 // The Goby Binaries are free software: you can redistribute them and/or modify
10 // them under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // The Goby Binaries are distributed in the hope that they will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
21 
22 // tests functionality of the UDPDriver
23 
24 #include "goby/acomms/acomms_helpers.h"
25 #include "goby/acomms/connect.h"
26 #include "goby/acomms/modemdriver/udp_driver.h"
27 #include "goby/common/logger.h"
28 #include "goby/util/binary.h"
29 
30 using namespace goby::common::logger;
31 using namespace goby::acomms;
33 using goby::util::as;
34 using namespace boost::posix_time;
35 
36 boost::asio::io_service io1;
37 boost::shared_ptr<goby::acomms::UDPDriver> driver1;
38 
39 void handle_data_request1(protobuf::ModemTransmission* msg);
40 void handle_modify_transmission1(protobuf::ModemTransmission* msg);
41 void handle_transmit_result1(const protobuf::ModemTransmission& msg);
42 void handle_data_receive1(const protobuf::ModemTransmission& msg);
43 
44 int main(int argc, char* argv[])
45 {
46  goby::glog.add_stream(goby::common::logger::DEBUG3, &std::clog);
47  std::ofstream fout;
48 
49  if (argc < 6)
50  {
51  std::cerr << "Usage: test_udpdriver2 remote_addr remote_port remote_id local_port local_id "
52  "[is_quiet]"
53  << std::endl;
54  exit(1);
55  }
56 
57  std::string remote_addr = argv[1];
58  int remote_port = as<int>(argv[2]);
59  int remote_id = as<int>(argv[3]);
60  int local_port = as<int>(argv[4]);
61  int local_id = as<int>(argv[5]);
62 
63  bool is_quiet = false;
64  if (argc == 7)
65  is_quiet = as<bool>(argv[6]);
66 
67  goby::glog.set_name(argv[0]);
68 
69  driver1.reset(new goby::acomms::UDPDriver(&io1));
70 
72 
73  cfg1.set_modem_id(local_id);
74 
75  UDPDriverConfig::EndPoint* local_endpoint1 = cfg1.MutableExtension(UDPDriverConfig::local);
76 
77  local_endpoint1->set_port(local_port);
78 
79  UDPDriverConfig::EndPoint* remote_endpoint1 = cfg1.MutableExtension(UDPDriverConfig::remote);
80 
81  remote_endpoint1->set_ip(remote_addr);
82  remote_endpoint1->set_port(remote_port);
83 
84  goby::acomms::connect(&driver1->signal_receive, &handle_data_receive1);
85  goby::acomms::connect(&driver1->signal_transmit_result, &handle_transmit_result1);
86  goby::acomms::connect(&driver1->signal_modify_transmission, &handle_modify_transmission1);
87  goby::acomms::connect(&driver1->signal_data_request, &handle_data_request1);
88 
89  goby::glog << cfg1.DebugString() << std::endl;
90 
91  driver1->startup(cfg1);
92 
93  int i = 0;
94  while (((i / 10) < 1))
95  {
96  driver1->do_work();
97 
98  usleep(100000);
99  ++i;
100  }
101 
102  goby::glog << group("test") << "Test 1" << std::endl;
103 
105 
106  transmit.set_type(protobuf::ModemTransmission::DATA);
107  transmit.set_src(local_id);
108  transmit.set_dest(remote_id);
109  transmit.set_rate(0);
110  transmit.set_ack_requested(true);
111 
112  i = 0;
113  for (;;)
114  // while(((i / 10) < 100))
115  {
116  driver1->do_work();
117 
118  if ((i % 100 == 0) && !is_quiet)
119  driver1->handle_initiate_transmission(transmit);
120 
121  usleep(100000);
122  ++i;
123  }
124 
125  goby::glog << group("test") << "all tests passed" << std::endl;
126 }
127 
128 void handle_data_request1(protobuf::ModemTransmission* msg)
129 {
130  goby::glog << group("driver1") << "Data request: " << *msg << std::endl;
131 
132  static int i = 0;
133  msg->add_frame(std::string(32, i++ % 256));
134 
135  goby::glog << group("driver1") << "Post data request: " << *msg << std::endl;
136 }
137 
138 void handle_modify_transmission1(protobuf::ModemTransmission* msg)
139 {
140  goby::glog << group("driver1") << "Can modify: " << *msg << std::endl;
141 }
142 
143 void handle_transmit_result1(const protobuf::ModemTransmission& msg)
144 {
145  goby::glog << group("driver1") << "Completed transmit: " << msg << std::endl;
146 }
147 
148 void handle_data_receive1(const protobuf::ModemTransmission& msg)
149 {
150  goby::glog << group("driver1") << "Received: " << msg << std::endl;
151 }
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
Definition: flex_ostream.h:67
ReturnType goby_time()
Returns current UTC time as a boost::posix_time::ptime.
Definition: time.h:104
void connect(Signal *signal, Slot slot)
connect a signal to a slot (e.g. function pointer)
Definition: connect.h:36
common::FlexOstream glog
Access the Goby logger through this object.
Objects pertaining to acoustic communications (acomms)
void add_stream(logger::Verbosity verbosity=logger::VERBOSE, std::ostream *os=0)
Attach a stream object (e.g. std::cout, std::ofstream, ...) to the logger with desired verbosity...
Definition: flex_ostream.h:96