Fawkes API  Fawkes Development Version
qa_client.cpp
1 
2 /***************************************************************************
3  * qa_client.cpp - protobuf_comm client test program
4  *
5  * Created: Thu Jan 31 22:00:44 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/MachineInfo.pb.h>
39 #include <protobuf_comm/client.h>
40 
41 #include <boost/asio.hpp>
42 
43 using namespace protobuf_comm;
44 using namespace llsf_msgs;
45 
46 /// @cond QA
47 
48 static bool quit = false;
49 static ProtobufStreamClient client;
50 
51 void
52 signal_handler(const boost::system::error_code &error, int signum)
53 {
54  if (!error) {
55  quit = true;
56  }
57 }
58 
59 void
60 connected()
61 {
62  /*
63  Person p;
64  p.set_id(1);
65  p.set_name("Tim Niemueller");
66  p.set_email("niemueller@kbsg.rwth-aachen.de");
67  client.send(1, 2, p);
68  */
69 }
70 
71 void
72 handle_message(uint16_t comp_id, uint16_t msg_type, std::shared_ptr<google::protobuf::Message> msg)
73 {
74  printf("Received message of type %u\n", msg_type);
75  /*
76  std::shared_ptr<Person> p;
77  if ((p = std::dynamic_pointer_cast<Person>(msg))) {
78  printf("Person %i: %s <%s>\n", p->id(), p->name().c_str(), p->email().c_str());
79  }
80  */
81 }
82 
83 int
84 main(int argc, char **argv)
85 {
86  boost::asio::io_service io_service;
87 
88  boost::asio::deadline_timer timer_(io_service);
89  boost::asio::deadline_timer reconnect_timer_(io_service);
90  boost::asio::deadline_timer attmsg_timer_(io_service);
91  boost::asio::deadline_timer blink_timer_(io_service);
92 
93  client.signal_connected().connect(connected);
94  client.async_connect("127.0.0.1", 4444);
95 
96  //MessageRegister & message_register = client.message_register();
97  //message_register.add_message_type<Person>(1, 2);
98 
99  client.signal_received().connect(handle_message);
100 
101  // Construct a signal set registered for process termination.
102  boost::asio::signal_set signals(io_service, SIGINT, SIGTERM);
103 
104  // Start an asynchronous wait for one of the signals to occur.
105  signals.async_wait(signal_handler);
106 
107  do {
108  io_service.run();
109  io_service.reset();
110  } while (!quit);
111 
112  // Delete all global objects allocated by libprotobuf
113  google::protobuf::ShutdownProtobufLibrary();
114 }
115 
116 /// @endcond
boost::signals2::signal< void(uint16_t, uint16_t, std::shared_ptr< google::protobuf::Message >)> & signal_received()
Signal that is invoked when a message has been received.
Definition: client.h:93
boost::signals2::signal< void()> & signal_connected()
Signal that is invoked when the connection has been established.
Definition: client.h:111
void async_connect(const char *host, unsigned short port)
Asynchronous connect.
Definition: client.cpp:154
Stream client for protobuf message transmission.
Definition: client.h:55