Fawkes API  Fawkes Development Version
qa_server.cpp
1 
2 /***************************************************************************
3  * qa_server.cpp - protobuf_comm server test program
4  *
5  * Created: Thu Jan 31 21:36:31 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 <protobuf_comm/server.h>
39 
40 #include <boost/asio.hpp>
41 
42 using namespace protobuf_comm;
43 //using namespace llsf_msgs;
44 
45 /// @cond QA
46 
47 static bool quit = false;
48 ProtobufStreamServer server(1234);
49 
50 void
51 signal_handler(const boost::system::error_code &error, int signum)
52 {
53  if (!error) {
54  quit = true;
55  }
56 }
57 
58 void
59 client_connected(ProtobufStreamServer::ClientID client, boost::asio::ip::tcp::endpoint &endpoint)
60 {
61  printf("Client %u (%s:%u) connected\n",
62  client,
63  endpoint.address().to_string().c_str(),
64  endpoint.port());
65 }
66 
67 void
68 client_disconnected(ProtobufStreamServer::ClientID client, const boost::system::error_code &error)
69 {
70  if (!error) {
71  printf("Client %u disconnected\n", client);
72  } else {
73  printf("Client %u disconnected, error was: %s\n", client, error.message().c_str());
74  }
75 }
76 
77 void
78 handle_message(ProtobufStreamServer::ClientID client,
79  uint16_t component_id,
80  uint16_t msg_type,
81  std::shared_ptr<google::protobuf::Message> msg)
82 {
83  printf("Received message of type %u from %u\n", msg_type, client);
84  /*
85  std::shared_ptr<Person> p;
86  if ((p = std::dynamic_pointer_cast<Person>(msg))) {
87  printf("Person %i: %s <%s>\n", p->id(), p->name().c_str(), p->email().c_str());
88  }
89 
90  server.send(client, component_id, msg_type, *p);
91  */
92 }
93 
94 int
95 main(int argc, char **argv)
96 {
97  boost::asio::io_service io_service;
98 
99  //MessageRegister & message_register = server.message_register();
100  //message_register.add_message_type<Person>(1, 2);
101 
102  server.signal_connected().connect(client_connected);
103  server.signal_disconnected().connect(client_disconnected);
104  server.signal_received().connect(handle_message);
105 
106  // Construct a signal set registered for process termination.
107  boost::asio::signal_set signals(io_service, SIGINT, SIGTERM);
108 
109  // Start an asynchronous wait for one of the signals to occur.
110  signals.async_wait(signal_handler);
111 
112  do {
113  io_service.run();
114  io_service.reset();
115  } while (!quit);
116 
117  // Delete all global objects allocated by libprotobuf
118  google::protobuf::ShutdownProtobufLibrary();
119 }
120 
121 /// @endcond
Stream server for protobuf message transmission.
Definition: server.h:62
unsigned int ClientID
ID to identify connected clients.
Definition: server.h:65