Fawkes API  Fawkes Development Version
qa_peer.cpp
1 
2 /***************************************************************************
3  * qa_peer.cpp - protobuf_comm broadcast peer test program
4  *
5  * Created: Mon Feb 04 18:25:41 2013
6  * Copyright 2013 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * - Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * - Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  * - Neither the name of the authors nor the names of its contributors
21  * may be used to endorse or promote products derived from this
22  * software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35  * OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <msgs/Person.pb.h>
39 #include <protobuf_comm/peer.h>
40 
41 #include <boost/asio.hpp>
42 #include <boost/lexical_cast.hpp>
43 
44 using namespace protobuf_comm;
45 using namespace llsf_msgs;
46 
47 /// @cond QA
48 
49 static bool quit = false;
51 
52 void
53 signal_handler(const boost::system::error_code &error, int signum)
54 {
55  if (!error) {
56  quit = true;
57  }
58 }
59 
60 void
61 handle_error(const boost::system::error_code &error)
62 {
63  printf("Error: %s\n", error.message().c_str());
64 }
65 
66 void
67 handle_message(boost::asio::ip::udp::endpoint & sender,
68  uint16_t component_id,
69  uint16_t msg_type,
70  std::shared_ptr<google::protobuf::Message> msg)
71 {
72  printf("Received message of type %u from %s\n", msg_type, sender.address().to_string().c_str());
73  std::shared_ptr<Person> p;
74  if ((p = std::dynamic_pointer_cast<Person>(msg))) {
75  printf("Person %i: %s <%s>\n", p->id(), p->name().c_str(), p->email().c_str());
76  }
77 
78  //server.send(client, component_id, msg_type, *p);
79 }
80 
81 int
82 main(int argc, char **argv)
83 {
84  unsigned short send_to_port = 1234;
85  unsigned short recv_on_port = 1234;
86  if (argc >= 3) {
87  send_to_port = boost::lexical_cast<unsigned short>(argv[1]);
88  recv_on_port = boost::lexical_cast<unsigned short>(argv[2]);
89  }
90  peer = new ProtobufBroadcastPeer("192.168.0.255", send_to_port, recv_on_port);
91 
92  boost::asio::io_service io_service;
93 
94  MessageRegister &message_register = peer->message_register();
95  message_register.add_message_type<Person>(1, 2);
96 
97  peer->signal_received().connect(handle_message);
98  peer->signal_error().connect(handle_error);
99 
100  // Construct a signal set registered for process termination.
101  boost::asio::signal_set signals(io_service, SIGINT, SIGTERM);
102 
103  // Start an asynchronous wait for one of the signals to occur.
104  signals.async_wait(signal_handler);
105 
106  if (argc >= 4) {
107  Person p;
108  p.set_id(1);
109  p.set_name("Tim Niemueller");
110  p.set_email("niemueller@kbsg.rwth-aachen.de");
111  peer->send(1, 2, p);
112  }
113 
114  do {
115  io_service.run();
116  io_service.reset();
117  } while (!quit);
118 
119  delete peer;
120 
121  // Delete all global objects allocated by libprotobuf
122  google::protobuf::ShutdownProtobufLibrary();
123 }
124 
125 /// @endcond
Register to map msg type numbers to Protobuf messages.
void add_message_type(std::string msg_type)
Add a message type from generated pool.
Communicate by broadcasting protobuf messages.
Definition: peer.h:57
signal_received_type & signal_received()
Signal that is invoked when a message has been received.
Definition: peer.h:144
void send(uint16_t component_id, uint16_t msg_type, google::protobuf::Message &m)
Send a message to other peers.
Definition: peer.cpp:576
MessageRegister & message_register()
Get the server's message register.
Definition: peer.h:116