Rev 1595 | Rev 1598 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1595 | Rev 1596 | ||
|---|---|---|---|
| Line 4... | Line 4... | ||
| 4 | #include <string> |
4 | #include <string> |
| - | 5 | ||
| - | 6 | #include <stdio.h> |
|
| 5 | 7 | ||
| 6 | #include <boost/bind.hpp> |
8 | #include <boost/bind.hpp> |
| 7 | #include <boost/program_options.hpp> |
9 | #include <boost/program_options.hpp> |
| 8 | 10 | ||
| 9 | #include "net/Manager.h" |
11 | #include "net/Manager.h" |
| 10 | #include "net/types.h" |
12 | #include "net/types.h" |
| 11 | 13 | ||
| 12 | #include "timer/Manager.h" |
14 | #include "timer/Manager.h" |
| 13 | #include "timer/types.h" |
15 | #include "timer/types.h" |
| 14 | 16 | ||
| 15 | #include "config/Manager.h" |
17 | #include "config/Manager.h" |
| 16 | 18 | ||
| 17 | #include "logging/Logger.h" |
19 | #include "logging/Logger.h" |
| 18 | 20 | ||
| - | 21 | #include "can/Network.h" |
|
| - | 22 | #include "can/Monitor.h" |
|
| - | 23 | #include "can/Protocol.h" |
|
| - | 24 | ||
| 19 | using namespace atom; |
25 | using namespace atom; |
| 20 | 26 | ||
| - | 27 | logging::Logger LOG("Main"); |
|
| 21 | 28 | ||
| 22 | net::Manager::Pointer NM = net::Manager::Instance(); |
29 | net::Manager::Pointer NM = net::Manager::Instance(); |
| 23 | 30 | ||
| 24 | net::ServerId server_id = 0; |
31 | net::ServerId server_id = 0; |
| 25 | timer::TimerId timer_id; |
32 | timer::TimerId timer_id; |
| 26 | 33 | ||
| 27 | void Main_SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state) |
34 | void Main_SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state) |
| 28 | { |
35 | { |
| 29 | std::cout << "client:" << client_id << " - New state: " << client_state << std::endl; |
36 | std::cout << "client:" << client_id << " - New state: " << client_state << std::endl; |
| 30 | } |
37 | } |
| 31 | 38 | ||
| 32 | void Main_SlotOnNewData(net::ClientId client_id, net::ServerId server_id, |
39 | void Main_SlotOnNewData(net::ClientId client_id, net::ServerId server_id, type::Byteset data) |
| 33 | { |
40 | { |
| 34 |
|
41 | char* t = new char[2048]; |
| 35 | - | ||
| 36 | std::cout << "client:" << client_id << " - New data: " << s << std::endl; |
- | |
| 37 | 42 | ||
| 38 |
|
43 | memset(t, 0, sizeof(t)); |
| 39 | 44 | ||
| 40 |
|
45 | //LOG.Info("size:" + boost::lexical_cast<std::string>(data.GetSize()*3+2)); |
| 41 | - | ||
| 42 | NM->Disconnect(client_id); |
- | |
| 43 | 46 | ||
| 44 |
|
47 | for (unsigned int n = 0; n < data.GetSize(); n++) |
| 45 | } |
- | |
| 46 | - | ||
| 47 | void Main_SlotOnTimeout(timer::TimerId timer_id) |
- | |
| 48 | { |
48 | { |
| 49 | std::cout << "timer:" << timer_id << " - expired" << std::endl; |
- | |
| 50 | - | ||
| 51 | std::string out_string = "Timeout!"; |
- | |
| 52 | - | ||
| 53 |
|
49 | unsigned char c = data[n]; |
| 54 | - | ||
| 55 | strncpy(out_data.c_array(), out_string.data(), out_data.size()); |
- | |
| 56 | 50 | ||
| - | 51 | //LOG.Info(boost::lexical_cast<std::string>(n) + ":" + boost::lexical_cast<std::string>((unsigned int)c)); |
|
| - | 52 | ||
| - | 53 | //std::cout << std::dec << n << ":(dec)" << std::dec << (unsigned int)c << std::endl; |
|
| - | 54 | std::cout << std::dec << n << ":(hex)" << std::hex << (unsigned int)c << std::endl; |
|
| - | 55 | ||
| - | 56 | sprintf(t + n*2, "%02X", c); |
|
| - | 57 | } |
|
| - | 58 | ||
| - | 59 | ||
| - | 60 | std::cout << std::dec << "client:" << client_id << " - New data: " << t << std::endl; |
|
| - | 61 | ||
| - | 62 | delete [] t; |
|
| - | 63 | } |
|
| - | 64 | ||
| - | 65 | void Main_SlotOnTimeout(timer::TimerId timer_id) |
|
| - | 66 | { |
|
| - | 67 | /* std::cout << "timer:" << timer_id << " - expired" << std::endl; |
|
| - | 68 | |
|
| - | 69 | std::string out_string = "Timeout!"; |
|
| - | 70 | |
|
| - | 71 | type::Byteset out_data; |
|
| - | 72 | |
|
| - | 73 | strncpy(out_data.GetRaw(), out_string.data(), out_data.size()); |
|
| - | 74 | |
|
| 57 |
|
75 | // NM->SendToAll(server_id, out_data);*/ |
| 58 | } |
76 | } |
| 59 | 77 | ||
| 60 | int main(int argc, char **argv) |
78 | int main(int argc, char **argv) |
| 61 | { |
79 | { |
| 62 |
|
80 | std::vector<broker::Subscriber::Pointer> subscribers; |
| 63 | 81 | ||
| 64 | config::Manager::Pointer CM = config::Manager::Instance(); |
82 | config::Manager::Pointer CM = config::Manager::Instance(); |
| 65 | 83 | ||
| 66 | if (!CM->Set(argc, argv)) |
84 | if (!CM->Set(argc, argv)) |
| 67 | { |
85 | { |
| - | 86 | NM->Delete(); |
|
| 68 | return 0; |
87 | return 0; |
| 69 | } |
88 | } |
| 70 | 89 | ||
| - | 90 | if (CM->Exist("ProtocolFile")) |
|
| - | 91 | { |
|
| - | 92 | LOG.Info("ProtocolFile: " + CM->GetAsString("ProtocolFile")); |
|
| - | 93 | ||
| - | 94 | can::Protocol::Instance()->Load(CM->GetAsString("ProtocolFile")); |
|
| - | 95 | } |
|
| - | 96 | ||
| 71 | if (CM->Exist("MonitorPort")) |
97 | if (CM->Exist("MonitorPort")) |
| 72 | { |
98 | { |
| 73 | LOG.Info("MonitorPort: " + boost::lexical_cast<std::string>(CM->GetAsInt("MonitorPort"))); |
99 | LOG.Info("MonitorPort: " + boost::lexical_cast<std::string>(CM->GetAsInt("MonitorPort"))); |
| - | 100 | ||
| - | 101 | subscribers.push_back(can::Monitor::Pointer(new can::Monitor(CM->GetAsInt("MonitorPort")))); |
|
| 74 | } |
102 | } |
| 75 | 103 | ||
| 76 | if (CM->Exist("CanNet")) |
104 | if (CM->Exist("CanNet")) |
| 77 | { |
105 | { |
| 78 |
|
106 | type::StringList cannets = CM->GetAsStringVector("CanNet"); |
| 79 | 107 | ||
| 80 | for (int n = 0; n < cannets.size(); n++) |
108 | for (int n = 0; n < cannets.size(); n++) |
| 81 | { |
109 | { |
| 82 | LOG.Info("CanNet[" + boost::lexical_cast<std::string>(n) + "]=" + cannets[n]); |
110 | LOG.Info("CanNet[" + boost::lexical_cast<std::string>(n) + "]=" + cannets[n]); |
| - | 111 | ||
| - | 112 | subscribers.push_back(can::Network::Pointer(new can::Network(cannets[n]))); |
|
| - | 113 | } |
|
| - | 114 | ||
| 83 | } |
115 | } |
| 84 | 116 | ||
| 85 |
|
117 | |
| 86 | 118 | ||
| 87 | /* timer::Manager::Pointer TM = timer::Manager::Instance(); |
119 | /* timer::Manager::Pointer TM = timer::Manager::Instance(); |
| 88 | |
120 | |
| 89 | |
121 | |
| 90 | TM->Set(10000, true); |
122 | TM->Set(10000, true); |
| 91 | |
123 | |
| 92 | TM->ConnectSlots(boost::bind(&Main_SlotOnTimeout, _1)); |
124 | TM->ConnectSlots(boost::bind(&Main_SlotOnTimeout, _1)); |
| 93 | */ |
125 | */ |
| 94 | 126 | ||
| 95 |
|
127 | // std::cout << "connect slots2" << std::endl; |
| 96 |
|
128 | // NM->ConnectSlots(boost::bind(&Main_SlotOnNewState, _1, _2, _3), boost::bind(&Main_SlotOnNewData, _1, _2, _3)); |
| 97 | 129 | ||
| 98 | 130 | ||
| 99 | try |
131 | try |
| 100 | { |
132 | { |
| 101 | // server_id = NM->StartServer(net::PROTOCOL_SERIAL, 2000); |
133 | // server_id = NM->StartServer(net::PROTOCOL_SERIAL, 2000); |
| 102 | 134 | ||
| 103 | // timer_id = TM->Set(10000, true); |
135 | // timer_id = TM->Set(10000, true); |
| 104 | 136 | ||
| 105 | 137 | ||
| - | 138 | //net::ClientId client_id_udp = NM->Connect(net::PROTOCOL_UDP, "192.168.1.250", 1100); |
|
| 106 |
|
139 | //net::ClientId client_id_udp = NM->Connect(net::PROTOCOL_SERIAL, "/dev/ttyUSB0", 57600); |
| 107 | 140 | ||
| 108 | std |
141 | //std::cout << "connected" << std::endl; |
| 109 | 142 | ||
| 110 |
|
143 | /*char c[2] = { 251, 0 }; |
| 111 | 144 | |
|
| 112 | std::cout << "before send " << std::endl; |
145 | std::cout << "before send " << std::endl; |
| 113 | 146 | |
|
| 114 | NM |
147 | NM->SendTo(client_id_udp, c);*/ |
| 115 | } |
148 | } |
| 116 | catch (std::exception e) |
149 | catch (std::exception e) |
| 117 | { |
150 | { |
| 118 | std::cerr << e.what() << std::endl; |
151 | std::cerr << e.what() << std::endl; |
| 119 | } |
152 | } |