Rev 1593 | Rev 1595 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1592 | runge | 1 | #include <iostream> |
| 1594 | runge | 2 | #include <fstream> |
| 3 | #include <vector> |
||
| 4 | #include <string> |
||
| 1592 | runge | 5 | |
| 6 | #include <boost/bind.hpp> |
||
| 1594 | runge | 7 | #include <boost/program_options.hpp> |
| 1592 | runge | 8 | |
| 9 | #include "net/Manager.h" |
||
| 10 | #include "net/Types.h" |
||
| 11 | |||
| 1593 | runge | 12 | #include "timer/Manager.h" |
| 13 | #include "timer/Types.h" |
||
| 14 | |||
| 1594 | runge | 15 | #include "config/Manager.h" |
| 1593 | runge | 16 | |
| 1594 | runge | 17 | #include "logging/Logger.h" |
| 18 | |||
| 1592 | runge | 19 | using namespace atom; |
| 1593 | runge | 20 | |
| 1592 | runge | 21 | |
| 22 | net::Manager::Pointer NM = net::Manager::Instance(); |
||
| 23 | |||
| 1593 | runge | 24 | net::ServerId server_id = 0; |
| 25 | timer::TimerId timer_id; |
||
| 26 | |||
| 27 | void Main_SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state) |
||
| 1592 | runge | 28 | { |
| 29 | std::cout << "client:" << client_id << " - New state: " << client_state << std::endl; |
||
| 30 | } |
||
| 31 | |||
| 1593 | runge | 32 | void Main_SlotOnNewData(net::ClientId client_id, net::ServerId server_id, net::Buffer data) |
| 1592 | runge | 33 | { |
| 34 | std::string s(data.data()); |
||
| 35 | |||
| 36 | std::cout << "client:" << client_id << " - New data: " << s << std::endl; |
||
| 37 | |||
| 1593 | runge | 38 | net::Buffer out_data; |
| 1592 | runge | 39 | |
| 1593 | runge | 40 | strncpy(out_data.c_array(), data.data(), out_data.size()); |
| 1592 | runge | 41 | |
| 42 | NM->SendTo(client_id, out_data); |
||
| 43 | } |
||
| 44 | |||
| 1593 | runge | 45 | void Main_SlotOnTimeout(timer::TimerId timer_id) |
| 46 | { |
||
| 47 | std::cout << "timer:" << timer_id << " - expired" << std::endl; |
||
| 48 | |||
| 49 | std::string out_string = "Timeout!"; |
||
| 50 | |||
| 51 | net::Buffer out_data; |
||
| 52 | |||
| 53 | strncpy(out_data.c_array(), out_string.data(), out_data.size()); |
||
| 54 | |||
| 55 | NM->SendToAll(server_id, out_data); |
||
| 56 | } |
||
| 57 | |||
| 1592 | runge | 58 | int main(int argc, char **argv) |
| 59 | { |
||
| 1594 | runge | 60 | logging::Logger LOG("Main"); |
| 61 | |||
| 62 | config::Manager::Pointer CM = config::Manager::Instance(); |
||
| 1592 | runge | 63 | |
| 1594 | runge | 64 | if (!CM->Set(argc, argv)) |
| 65 | { |
||
| 66 | return 0; |
||
| 67 | } |
||
| 68 | |||
| 69 | if (CM->Exist("MonitorPort")) |
||
| 70 | { |
||
| 71 | LOG.Info("MonitorPort: " + boost::lexical_cast<std::string>(CM->GetAsInt("MonitorPort"))); |
||
| 72 | } |
||
| 73 | |||
| 74 | if (CM->Exist("CanNet")) |
||
| 75 | { |
||
| 76 | std::vector<std::string> cannets = CM->GetAsStringVector("CanNet"); |
||
| 77 | |||
| 78 | for (int n = 0; n < cannets.size(); n++) |
||
| 79 | { |
||
| 80 | LOG.Info("CanNet[" + boost::lexical_cast<std::string>(n) + "]=" + cannets[n]); |
||
| 81 | } |
||
| 82 | |||
| 83 | } |
||
| 84 | LOG.Info("Done!"); |
||
| 85 | |||
| 86 | return 0; |
||
| 87 | |||
| 88 | // Declare a group of options that will be |
||
| 89 | // allowed only on command line |
||
| 90 | boost::program_options::options_description generic("Generic options"); |
||
| 91 | generic.add_options() ("version,v", "print version string") |
||
| 92 | ("help", "produce help message"); |
||
| 93 | |||
| 94 | // Declare a group of options that will be |
||
| 95 | // allowed both on command line and in |
||
| 96 | // config file |
||
| 97 | boost::program_options::options_description config("Configuration"); |
||
| 98 | config.add_options() |
||
| 99 | ("MonitorPort", boost::program_options::value<int>()->default_value(2000), "TCP port to expose for monitoring") |
||
| 100 | ("CommandPort", boost::program_options::value<int>()->default_value(2001), "TCP port to expose for monitoring") |
||
| 101 | ("LogFile", boost::program_options::value<std::string>(), "File to log output to") |
||
| 102 | ("ScriptsPath", boost::program_options::value<std::string>(), "File to log output to") |
||
| 103 | ("ProtocolFile", boost::program_options::value<std::string>(), "File to log output to") |
||
| 104 | ("DaemonMode,D", "File to log output to") |
||
| 105 | ("CanNet_1", boost::program_options::value<std::vector<std::string> >(), "include path"); |
||
| 106 | |||
| 107 | LOG.Debug("a"); |
||
| 108 | boost::program_options::options_description cmdline_options; |
||
| 109 | cmdline_options.add(generic).add(config); |
||
| 110 | |||
| 111 | boost::program_options::options_description config_file_options; |
||
| 112 | config_file_options.add(config); |
||
| 113 | |||
| 114 | boost::program_options::options_description visible("Allowed options"); |
||
| 115 | visible.add(generic).add(config); |
||
| 116 | |||
| 117 | |||
| 118 | boost::program_options::variables_map vm; |
||
| 119 | store(boost::program_options::command_line_parser(argc, argv).options(cmdline_options).run(), vm); |
||
| 120 | LOG.Debug("b"); |
||
| 121 | std::ifstream ifs("atom.conf"); |
||
| 122 | store(parse_config_file(ifs, config_file_options), vm); |
||
| 123 | notify(vm); |
||
| 124 | |||
| 125 | LOG.Debug("c"); |
||
| 126 | |||
| 127 | if (vm.count("help")) |
||
| 128 | { |
||
| 129 | LOG.Info("Help"); |
||
| 130 | } |
||
| 131 | |||
| 132 | if (vm.count("MonitorPort")) |
||
| 133 | { |
||
| 134 | LOG.Info("MonitorPort: " + boost::lexical_cast<std::string>(vm["MonitorPort"].as<int>())); |
||
| 135 | } |
||
| 136 | |||
| 137 | if (vm.count("LogFile")) |
||
| 138 | { |
||
| 139 | LOG.Info("LogFile: " + vm["LogFile"].as<std::string>()); |
||
| 140 | } |
||
| 141 | |||
| 142 | if (vm.count("DaemonMode")) |
||
| 143 | { |
||
| 144 | LOG.Info("DaemonMode: " + vm["DaemonMode"].as<std::string>()); |
||
| 145 | } |
||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | net::Manager::Delete(); |
||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | return 0; |
||
| 163 | |||
| 164 | LOG.Error("test sdsdsds sds "); |
||
| 165 | return 0; |
||
| 1593 | runge | 166 | timer::Manager::Pointer TM = timer::Manager::Instance(); |
| 1592 | runge | 167 | |
| 168 | |||
| 1593 | runge | 169 | //TM->Set(10000, true); |
| 170 | |||
| 171 | TM->ConnectSlots(boost::bind(&Main_SlotOnTimeout, _1)); |
||
| 172 | |||
| 173 | |||
| 174 | std::cout << "connect slots2" << std::endl; |
||
| 175 | NM->ConnectSlots(boost::bind(&Main_SlotOnNewState, _1, _2, _3), boost::bind(&Main_SlotOnNewData, _1, _2, _3)); |
||
| 176 | |||
| 177 | |||
| 1592 | runge | 178 | try |
| 179 | { |
||
| 1593 | runge | 180 | server_id = NM->StartServer(net::PROTOCOL_TCP, 2000); |
| 181 | |||
| 182 | timer_id = TM->Set(10000, true); |
||
| 183 | |||
| 1592 | runge | 184 | /* |
| 185 | net::ClientId client_id_udp = NM->Connect(net::PROTOCOL_UDP, "192.168.1.250", 1100); |
||
| 186 | |||
| 187 | std::cout << "connected" << std::endl; |
||
| 188 | |||
| 189 | char c[2] = { 251, 0 }; |
||
| 190 | */ |
||
| 191 | //NM->SendTo(client_id_udp, std::string(c)); |
||
| 192 | } |
||
| 193 | catch (std::exception e) |
||
| 194 | { |
||
| 195 | std::cerr << e.what() << std::endl; |
||
| 196 | } |
||
| 197 | |||
| 198 | std::cout << "sleep" << std::endl; |
||
| 199 | |||
| 200 | sleep(100000000); |
||
| 201 | |||
| 202 | return 0; |
||
| 203 | } |