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 "../driver_tester/driver_tester.h"
25 #include "goby/acomms/modemdriver/udp_driver.h"
26 #include <cstdlib>
27 
28 boost::asio::io_service io1, io2;
29 boost::shared_ptr<goby::acomms::ModemDriverBase> driver1, driver2;
30 
31 void handle_raw_incoming(int driver, const goby::acomms::protobuf::ModemRaw& raw)
32 {
33  std::cout << "Raw in (" << driver << "): " << raw.ShortDebugString() << std::endl;
34 }
35 
36 void handle_raw_outgoing(int driver, const goby::acomms::protobuf::ModemRaw& raw)
37 {
38  std::cout << "Raw out (" << driver << "): " << raw.ShortDebugString() << std::endl;
39 }
40 
41 int main(int argc, char* argv[])
42 {
43  goby::glog.add_stream(goby::common::logger::DEBUG3, &std::clog);
44  std::ofstream fout;
45 
46  if (argc == 2)
47  {
48  fout.open(argv[1]);
49  goby::glog.add_stream(goby::common::logger::DEBUG3, &fout);
50  }
51 
52  goby::glog.set_name(argv[0]);
53 
54  driver1.reset(new goby::acomms::UDPDriver(&io1));
55  driver2.reset(new goby::acomms::UDPDriver(&io2));
56 
57  goby::acomms::connect(&driver1->signal_raw_incoming, boost::bind(&handle_raw_incoming, 1, _1));
58  goby::acomms::connect(&driver2->signal_raw_incoming, boost::bind(&handle_raw_incoming, 2, _1));
59  goby::acomms::connect(&driver1->signal_raw_outgoing, boost::bind(&handle_raw_outgoing, 1, _1));
60  goby::acomms::connect(&driver2->signal_raw_outgoing, boost::bind(&handle_raw_outgoing, 2, _1));
61 
63 
64  cfg1.set_modem_id(1);
65 
66  srand(time(NULL));
67  int port1 = rand() % 1000 + 50000;
68  int port2 = port1 + 1;
69 
70  //gumstix
71  UDPDriverConfig::EndPoint* local_endpoint1 = cfg1.MutableExtension(UDPDriverConfig::local);
72  local_endpoint1->set_port(port1);
73 
74  cfg2.set_modem_id(2);
75 
76  // shore
77  UDPDriverConfig::EndPoint* local_endpoint2 = cfg2.MutableExtension(UDPDriverConfig::local);
78  local_endpoint2->set_port(port2);
79 
80  UDPDriverConfig::EndPoint* remote_endpoint1 = cfg1.MutableExtension(UDPDriverConfig::remote);
81 
82  remote_endpoint1->set_ip("localhost");
83  remote_endpoint1->set_port(port2);
84 
85  UDPDriverConfig::EndPoint* remote_endpoint2 = cfg2.MutableExtension(UDPDriverConfig::remote);
86 
87  remote_endpoint2->set_ip("127.0.0.1");
88  remote_endpoint2->set_port(port1);
89 
90  std::vector<int> tests_to_run;
91  tests_to_run.push_back(4);
92  tests_to_run.push_back(5);
93 
94  DriverTester tester(driver1, driver2, cfg1, cfg2, tests_to_run,
95  goby::acomms::protobuf::DRIVER_UDP);
96  return tester.run();
97 }
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
Definition: flex_ostream.h:67
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.
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