Rev 1596 | Rev 1599 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1596 | Rev 1598 | ||
|---|---|---|---|
| Line -... | Line 1... | ||
| - | 1 | /* |
|
| - | 2 | * |
|
| 1 |
|
3 | * Copyright (C) 2010 Mattias Runge |
| - | 4 | * |
|
| 2 |
|
5 | * This program is free software; you can redistribute it and/or modify |
| - | 6 | * it under the terms of the GNU General Public License as published by |
|
| - | 7 | * the Free Software Foundation; either version 2 of the License, or |
|
| 3 |
|
8 | * (at your option) any later version. |
| - | 9 | * |
|
| - | 10 | * This program is distributed in the hope that it will be useful, |
|
| - | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| - | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 4 |
|
13 | * GNU General Public License for more details. |
| 5 | 14 | * |
|
| - | 15 | * You should have received a copy of the GNU General Public License along |
|
| - | 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
|
| 6 |
|
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| - | 18 | * |
|
| - | 19 | */ |
|
| 7 | 20 | ||
| 8 | #include <boost/bind.hpp> |
- | |
| 9 | #include <boost/program_options.hpp> |
21 | #include <boost/program_options.hpp> |
| 10 | 22 | ||
| 11 | #include "net/Manager.h" |
23 | #include "net/Manager.h" |
| 12 | #include "net/types.h" |
24 | #include "net/types.h" |
| 13 | 25 | ||
| 14 | #include "timer/Manager.h" |
26 | #include "timer/Manager.h" |
| 15 | #include "timer/types.h" |
27 | #include "timer/types.h" |
| 16 | 28 | ||
| 17 | #include "config/Manager.h" |
29 | #include "config/Manager.h" |
| 18 | 30 | ||
| 19 | #include "logging/Logger.h" |
31 | #include "logging/Logger.h" |
| 20 | 32 | ||
| 21 | #include "can/Network.h" |
33 | #include "can/Network.h" |
| 22 | #include "can/Monitor.h" |
34 | #include "can/Monitor.h" |
| - | 35 | #include "can/Manager.h" |
|
| 23 | #include "can/Protocol.h" |
36 | #include "can/Protocol.h" |
| 24 | 37 | ||
| 25 | using namespace atom; |
38 | using namespace atom; |
| 26 | 39 | ||
| 27 | logging::Logger LOG("Main"); |
40 | logging::Logger LOG("Main"); |
| 28 | 41 | ||
| 29 | net::Manager::Pointer NM = net::Manager::Instance(); |
- | |
| 30 | - | ||
| 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) |
- | |
| 35 | { |
- | |
| 36 | std::cout << "client:" << client_id << " - New state: " << client_state << std::endl; |
- | |
| 37 | } |
- | |
| 38 | - | ||
| 39 | void Main_SlotOnNewData(net::ClientId client_id, net::ServerId server_id, type::Byteset data) |
- | |
| 40 | { |
- | |
| 41 | char* t = new char[2048]; |
- | |
| 42 | - | ||
| 43 | memset(t, 0, sizeof(t)); |
- | |
| 44 | - | ||
| 45 | //LOG.Info("size:" + boost::lexical_cast<std::string>(data.GetSize()*3+2)); |
- | |
| 46 | - | ||
| 47 | for (unsigned int n = 0; n < data.GetSize(); n++) |
- | |
| 48 | { |
- | |
| 49 | unsigned char c = data[n]; |
- | |
| 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 |
|
42 | void CleanUp(); |
| 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 | |
- | |
| 75 | // NM->SendToAll(server_id, out_data);*/ |
- | |
| 76 | } |
- | |
| 77 | 43 | ||
| 78 | int main(int argc, char **argv) |
44 | int main(int argc, char **argv) |
| 79 | { |
45 | { |
| 80 | std::vector<broker::Subscriber::Pointer> subscribers; |
46 | std::vector<broker::Subscriber::Pointer> subscribers; |
| 81 | 47 | ||
| 82 | config::Manager::Pointer CM = config::Manager::Instance(); |
- | |
| 83 | - | ||
| 84 | if (! |
48 | if (!config::Manager::Instance()->Set(argc, argv)) |
| 85 | { |
49 | { |
| 86 |
|
50 | CleanUp(); |
| 87 | return |
51 | return EXIT_SUCCESS; |
| 88 | } |
52 | } |
| 89 | 53 | ||
| 90 | if ( |
54 | if (config::Manager::Instance()->Exist("ProtocolFile")) |
| 91 | { |
55 | { |
| 92 | LOG.Info("ProtocolFile: " + CM->GetAsString("ProtocolFile")); |
- | |
| 93 | - | ||
| 94 | can::Protocol::Instance()->Load( |
56 | can::Protocol::Instance()->Load(config::Manager::Instance()->GetAsString("ProtocolFile")); |
| 95 | } |
57 | } |
| 96 | 58 | ||
| 97 | if ( |
59 | if (config::Manager::Instance()->Exist("MonitorPort")) |
| 98 | { |
60 | { |
| 99 | LOG.Info("MonitorPort: " + boost::lexical_cast<std::string>(CM->GetAsInt("MonitorPort"))); |
- | |
| 100 | - | ||
| 101 | subscribers.push_back(can::Monitor::Pointer(new can::Monitor( |
61 | subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort")))); |
| 102 | } |
- | |
| 103 | - | ||
| 104 | if (CM->Exist("CanNet")) |
- | |
| 105 | { |
- | |
| 106 | type::StringList cannets = CM->GetAsStringVector("CanNet"); |
- | |
| 107 | - | ||
| 108 | for (int n = 0; n < cannets.size(); n++) |
- | |
| 109 | { |
- | |
| 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 | - | ||
| 115 | } |
62 | } |
| 116 | 63 | ||
| - | 64 | subscribers.push_back(can::Manager::Instance()); |
|
| - | 65 | ||
| - | 66 | if (config::Manager::Instance()->Exist("CanNet")) |
|
| - | 67 | { |
|
| - | 68 | type::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet"); |
|
| 117 | 69 | ||
| 118 | - | ||
| 119 | /* timer::Manager::Pointer TM = timer::Manager::Instance(); |
- | |
| 120 | |
- | |
| 121 | |
- | |
| 122 | TM->Set(10000, true); |
- | |
| 123 | |
- | |
| 124 | TM->ConnectSlots(boost::bind(&Main_SlotOnTimeout, _1)); |
- | |
| 125 | */ |
- | |
| 126 | - | ||
| 127 | // std::cout << "connect slots2" << std::endl; |
- | |
| 128 | // NM->ConnectSlots(boost::bind(&Main_SlotOnNewState, _1, _2, _3), boost::bind(&Main_SlotOnNewData, _1, _2, _3)); |
- | |
| 129 | - | ||
| 130 | - | ||
| 131 | try |
- | |
| 132 | { |
- | |
| 133 | // server_id = NM->StartServer(net::PROTOCOL_SERIAL, 2000); |
- | |
| 134 | - | ||
| 135 | // timer_id = TM->Set(10000, true); |
- | |
| 136 | - | ||
| 137 | - | ||
| 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); |
- | |
| 140 | - | ||
| 141 |
|
70 | for (int n = 0; n < cannetworks.size(); n++) |
| 142 | - | ||
| 143 | /*char c[2] = { 251, 0 }; |
- | |
| 144 | |
- | |
| 145 | std::cout << "before send " << std::endl; |
- | |
| 146 | |
- | |
| 147 | NM->SendTo(client_id_udp, c);*/ |
- | |
| 148 | } |
- | |
| 149 | catch (std::exception e) |
- | |
| 150 | { |
71 | { |
| 151 |
|
72 | subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n]))); |
| 152 | } |
73 | } |
| 153 | 74 | ||
| - | 75 | } |
|
| - | 76 | ||
| - | 77 | sleep(100000000); |
|
| - | 78 | ||
| 154 |
|
79 | subscribers.clear(); |
| 155 | 80 | ||
| 156 |
|
81 | CleanUp(); |
| 157 | 82 | ||
| 158 | return |
83 | return EXIT_SUCCESS; |
| - | 84 | } |
|
| - | 85 | ||
| - | 86 | void CleanUp() |
|
| - | 87 | { |
|
| - | 88 | config::Manager::Delete(); |
|
| - | 89 | net::Manager::Delete(); |
|
| - | 90 | can::Manager::Delete(); |
|
| - | 91 | timer::Manager::Delete(); |
|
| 159 | } |
92 | } |