47 #include <arpa/inet.h>
51 #include <sys/socket.h>
52 #include <sys/types.h>
61 bool g_verbose =
false;
62 const int DEFAULT_PORT = 11255;
63 const char *usage[] = {
64 "usage: sampleServer [OPTIONS]",
67 " --connect-to=<addr> Connect to a client listening on <addr> (TCP only)."
68 " --help Display this help text and exit",
69 " --port=<n> Specifies the port to listen on (default=11255)",
70 " --app-queue Use an application queue for handling requests",
71 " --reactors=<n> Specifies the number of reactors (default=1)",
72 " --delay=<ms> Milliseconds to wait before echoing message (default=0)",
73 " --udp Operate in UDP mode instead of TCP",
74 " --verbose,-v Generate verbose output",
76 "This is a sample program to test the AsyncComm library. It establishes",
77 "a connection with the sampleServer and sends each line of the input file",
78 "to the server. Each reply from the server is echoed to stdout.",
97 cbp->append_bytes((uint8_t *)m_event->payload,
98 m_event->payload_len);
99 int error = m_comm->send_response(m_event->addr, cbp);
118 : m_comm(comm), m_app_queue(app_queue) {
return; }
120 virtual void handle(
EventPtr &event_ptr) {
122 HT_INFO(
"Connection Established.");
125 if (event_ptr->error != 0) {
136 if (m_app_queue == 0) {
140 cbp->append_bytes((uint8_t *)event_ptr->payload,
141 event_ptr->payload_len);
143 this_thread::sleep_for(chrono::milliseconds(g_delay));
144 int error = m_comm->send_response(event_ptr->addr, cbp);
146 HT_ERRORF(
"Comm::send_response returned %s",
151 m_app_queue->add(
new RequestHandler(m_comm, event_ptr));
169 UdpDispatcher(
Comm *comm) : m_comm(comm) {
return; }
171 virtual void handle(
EventPtr &event_ptr) {
176 cbp->append_bytes((uint8_t *)event_ptr->payload,
177 event_ptr->payload_len);
179 this_thread::sleep_for(chrono::milliseconds(g_delay));
180 int error = m_comm->send_datagram(event_ptr->addr,
181 event_ptr->local_addr, cbp);
187 HT_ERRORF(
"Error : %s", event_ptr->to_str().c_str());
203 : m_dispatch_handler_ptr(dhp) {
return; }
206 dhp = m_dispatch_handler_ptr;
219 int main(
int argc,
char **argv) {
222 uint16_t port = DEFAULT_PORT;
223 int reactor_count = 2;
232 memset(&client_addr, 0,
sizeof(client_addr));
234 for (
int i=1; i<argc; i++) {
235 if (!strcmp(argv[i],
"--help"))
237 else if (!strcmp(argv[i],
"--app-queue")) {
240 else if (!strncmp(argv[i],
"--connect-to=", 13)) {
244 else if (!strncmp(argv[i],
"--port=", 7)) {
245 rval = atoi(&argv[i][7]);
246 if (rval <= 1024 || rval > 65535) {
247 cerr <<
"Invalid port. Must be in the range of 1024-65535." << endl;
250 port = (uint16_t)rval;
252 else if (!strncmp(argv[i],
"--reactors=", 11))
253 reactor_count = atoi(&argv[i][11]);
254 else if (!strncmp(argv[i],
"--delay=", 8))
255 g_delay = atoi(&argv[i][8]);
256 else if (!strcmp(argv[i],
"--udp"))
258 else if (!strcmp(argv[i],
"--verbose") || !strcmp(argv[i],
"-v"))
271 cout <<
"Listening on port " << port << endl;
273 cout <<
"Delay = " << g_delay << endl;
283 dhp = std::make_shared<Dispatcher>(comm, app_queue);
285 if (client_addr.sin_port != 0) {
286 if ((error = comm->connect(client_addr, local_addr, dhp)) !=
Error::OK) {
293 comm->listen(local_addr, handler_factory, dhp);
297 assert(client_addr.sin_port == 0);
298 dhp = std::make_shared<UdpDispatcher>(comm);
static Comm * instance()
Creates/returns singleton instance of the Comm class.
Declarations for ConnectionHandlerFactory.
Retrieves system information (hardware, installation directory, etc)
#define HT_WARNF(msg,...)
Declarations of ApplicationHandler.
static void initialize(uint16_t reactor_count)
Initializes I/O reactors.
Helper class for printing usage banners on the command line.
Abstract base class for application dispatch handlers registered with AsyncComm.
void init(int argc, char *argv[], const Desc *desc=NULL)
Initialize with default policy.
Abstract class for creating default application dispatch handlers.
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Connection established event.
Declarations for DispatchHandler.
static void join()
Joins with reactor threads.
std::shared_ptr< ConnectionHandlerFactory > ConnectionHandlerFactoryPtr
Smart pointer to ConnectionHandlerFactory.
const char * get_text(int error)
Returns a descriptive error message.
Encapsulate an internet address.
std::shared_ptr< CommBuf > CommBufPtr
Smart pointer to CommBuf.
Compatibility Macros for C/C++.
Initialization helper for applications.
int main(int argc, char **argv)
main function
Connection disconnected event.
Declarations for SockAddrMap.
static bool initialize(sockaddr_in *addr, const char *host, uint16_t port)
Initialize a sockaddr_in structure from host:port.
Entry point to AsyncComm service.
#define HT_INFOF(msg,...)
Internet address wrapper classes and utility functions.
Request/response message event.
void create_datagram_receive_socket(CommAddress &addr, int tos, const DispatchHandlerPtr &handler)
Creates a socket for receiving datagrams and attaches handler as the default dispatch handler...
Declarations for ReactorFactory.
This is a generic exception class for Hypertable.
Base clase for application handlers.
std::shared_ptr< DispatchHandler > DispatchHandlerPtr
Smart pointer to DispatchHandler.
Message buffer for holding data to be transmitted over a network.
#define HT_ERRORF(msg,...)
Declarations for ApplicationQueue.
static void dump_and_exit(const char **usage, int rcode=1)
Same as dump, but performs _exit(rcode) afterwards.
Error codes, Exception handling, error logging.
Address abstraction to hold either proxy name or IPv4:port address.