Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / messenger / xio_server.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2013 CohortFS, LLC
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14
15 #include <sys/types.h>
16
17 #include <iostream>
18 #include <string>
19
20 using namespace std;
21
22 #include "common/config.h"
23 #include "msg/xio/XioMessenger.h"
24 #include "msg/xio/FastStrategy.h"
25 #include "msg/xio/QueueStrategy.h"
26 #include "common/Timer.h"
27 #include "common/ceph_argparse.h"
28 #include "global/global_init.h"
29 #include "global/signal_handler.h"
30 #include "perfglue/heap_profiler.h"
31 #include "common/address_helper.h"
32 #include "xio_dispatcher.h"
33
34 #define dout_subsys ceph_subsys_simple_server
35
36
37 int main(int argc, const char **argv)
38 {
39         vector<const char*> args;
40         Messenger *messenger;
41         Dispatcher *dispatcher;
42         std::vector<const char*>::iterator arg_iter;
43         std::string val;
44         entity_addr_t bind_addr;
45         int r = 0;
46
47         using std::endl;
48
49         std::string addr = "localhost";
50         std::string port = "1234";
51         bool dfast = false;
52
53         cout << "Xio Server starting..." << endl;
54
55         argv_to_vec(argc, argv, args);
56         env_to_vec(args);
57
58         global_init(NULL, args, CEPH_ENTITY_TYPE_ANY, CODE_ENVIRONMENT_DAEMON,
59                     0);
60
61         for (arg_iter = args.begin(); arg_iter != args.end();) {
62           if (ceph_argparse_witharg(args, arg_iter, &val, "--addr",
63                                     (char*) NULL)) {
64             addr = val;
65           } else if (ceph_argparse_witharg(args, arg_iter, &val, "--port",
66                                     (char*) NULL)) {
67             port = val;
68           }  else if (ceph_argparse_flag(args, arg_iter, "--dfast",
69                                            (char*) NULL)) {
70             dfast = true;
71           } else {
72             ++arg_iter;
73           }
74         };
75
76         string dest_str = "tcp://";
77         dest_str += addr;
78         dest_str += ":";
79         dest_str += port;
80         entity_addr_from_url(&bind_addr, dest_str.c_str());
81
82         DispatchStrategy* dstrategy;
83         if (dfast)
84           dstrategy = new FastStrategy();
85         else
86           dstrategy = new QueueStrategy(2);
87
88         messenger = new XioMessenger(g_ceph_context,
89                                      entity_name_t::MON(-1),
90                                      "xio_server",
91                                      0 /* nonce */,
92                                      0 /* cflags */,
93                                      dstrategy);
94
95         static_cast<XioMessenger*>(messenger)->set_magic(
96           MSG_MAGIC_REDUPE /* resubmit messages on delivery (REQUIRED) */ |
97           MSG_MAGIC_TRACE_CTR /* timing prints */);
98
99         messenger->set_default_policy(
100           Messenger::Policy::stateless_server(0));
101
102         r = messenger->bind(bind_addr);
103         if (r < 0)
104                 goto out;
105
106         // Set up crypto, daemonize, etc.
107         //global_init_daemonize(g_ceph_context, 0);
108         common_init_finish(g_ceph_context);
109
110         dispatcher = new XioDispatcher(messenger);
111
112         messenger->add_dispatcher_head(dispatcher); // should reach ready()
113         messenger->start();
114         messenger->wait(); // can't be called until ready()
115
116         // done
117         delete messenger;
118
119 out:
120         cout << "Simple Server exit" << endl;
121         return r;
122 }
123