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