Rev 1598 | Rev 1600 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1598 | runge | 1 | /* |
| 2 | * |
||
| 3 | * Copyright (C) 2010 Mattias Runge |
||
| 4 | * |
||
| 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 |
||
| 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 |
||
| 13 | * GNU General Public License for more details. |
||
| 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., |
||
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||
| 18 | * |
||
| 19 | */ |
||
| 1592 | runge | 20 | |
| 1599 | runge | 21 | #include <signal.h> |
| 22 | |||
| 1594 | runge | 23 | #include <boost/program_options.hpp> |
| 1599 | runge | 24 | #include <boost/thread/mutex.hpp> |
| 25 | #include <boost/thread/condition.hpp> |
||
| 26 | #include <boost/thread/locks.hpp> |
||
| 1592 | runge | 27 | |
| 1599 | runge | 28 | #include "logging/Logger.h" |
| 1592 | runge | 29 | #include "net/Manager.h" |
| 1593 | runge | 30 | #include "timer/Manager.h" |
| 1594 | runge | 31 | #include "config/Manager.h" |
| 1599 | runge | 32 | #include "can/Protocol.h" |
| 33 | #include "broker/Manager.h" |
||
| 34 | #include "can/Manager.h" |
||
| 1596 | runge | 35 | #include "can/Network.h" |
| 36 | #include "can/Monitor.h" |
||
| 37 | |||
| 1592 | runge | 38 | using namespace atom; |
| 1593 | runge | 39 | |
| 1596 | runge | 40 | logging::Logger LOG("Main"); |
| 1599 | runge | 41 | std::vector<broker::Subscriber::Pointer> subscribers; |
| 42 | boost::condition on_message_condition; |
||
| 43 | boost::mutex guard_mutex; |
||
| 1592 | runge | 44 | |
| 1599 | runge | 45 | void Handler(int status); |
| 1598 | runge | 46 | void CleanUp(); |
| 1592 | runge | 47 | |
| 48 | int main(int argc, char **argv) |
||
| 49 | { |
||
| 1599 | runge | 50 | LOG.Info("Atom version 1.5.0 starting..."); |
| 51 | LOG.Info("Written by Mattias Runge 2010."); |
||
| 52 | LOG.Info("Released under GPL version 2."); |
||
| 53 | |||
| 54 | signal(SIGTERM, Handler); |
||
| 55 | signal(SIGINT, Handler); |
||
| 56 | signal(SIGQUIT, Handler); |
||
| 57 | signal(SIGABRT, Handler); |
||
| 58 | signal(SIGPIPE, Handler); |
||
| 59 | |||
| 60 | config::Manager::Create(); |
||
| 61 | |||
| 1598 | runge | 62 | if (!config::Manager::Instance()->Set(argc, argv)) |
| 1594 | runge | 63 | { |
| 1598 | runge | 64 | CleanUp(); |
| 65 | return EXIT_SUCCESS; |
||
| 1594 | runge | 66 | } |
| 67 | |||
| 1599 | runge | 68 | if (config::Manager::Instance()->Exist("LogFile")) |
| 69 | { |
||
| 70 | logging::Logger::OpenFile(config::Manager::Instance()->GetAsString("LogFile")); |
||
| 71 | } |
||
| 72 | |||
| 73 | if (config::Manager::Instance()->Exist("LogLevel")) |
||
| 74 | { |
||
| 75 | logging::Logger::SetLevel((logging::Logger::Level)config::Manager::Instance()->GetAsInt("LogLevel")); |
||
| 76 | } |
||
| 77 | |||
| 78 | if (config::Manager::Instance()->Exist("daemon")) |
||
| 79 | { |
||
| 80 | LOG.Info("Entering daemon mode..."); |
||
| 81 | |||
| 82 | if (daemon(0, 0) == -1) |
||
| 83 | { |
||
| 84 | LOG.Error("Could not enter daemon mode. Exiting..."); |
||
| 85 | CleanUp(); |
||
| 86 | return EXIT_FAILURE; |
||
| 87 | } |
||
| 88 | |||
| 89 | LOG.Info("Deamon mode entered successfully!"); |
||
| 90 | } |
||
| 91 | |||
| 92 | timer::Manager::Create(); |
||
| 93 | broker::Manager::Create(); |
||
| 94 | net::Manager::Create(); |
||
| 95 | can::Protocol::Create(); |
||
| 96 | can::Manager::Create(); |
||
| 97 | |||
| 1598 | runge | 98 | if (config::Manager::Instance()->Exist("ProtocolFile")) |
| 1596 | runge | 99 | { |
| 1598 | runge | 100 | can::Protocol::Instance()->Load(config::Manager::Instance()->GetAsString("ProtocolFile")); |
| 1596 | runge | 101 | } |
| 102 | |||
| 1598 | runge | 103 | if (config::Manager::Instance()->Exist("MonitorPort")) |
| 1594 | runge | 104 | { |
| 1598 | runge | 105 | subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort")))); |
| 1594 | runge | 106 | } |
| 107 | |||
| 1598 | runge | 108 | subscribers.push_back(can::Manager::Instance()); |
| 109 | |||
| 110 | if (config::Manager::Instance()->Exist("CanNet")) |
||
| 1594 | runge | 111 | { |
| 1598 | runge | 112 | type::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet"); |
| 1594 | runge | 113 | |
| 1598 | runge | 114 | for (int n = 0; n < cannetworks.size(); n++) |
| 1594 | runge | 115 | { |
| 1598 | runge | 116 | subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n]))); |
| 1594 | runge | 117 | } |
| 118 | } |
||
| 1596 | runge | 119 | |
| 1599 | runge | 120 | LOG.Info("Initialization complete."); |
| 1596 | runge | 121 | |
| 1599 | runge | 122 | boost::mutex::scoped_lock guard(guard_mutex); |
| 123 | on_message_condition.wait(guard); |
||
| 1594 | runge | 124 | |
| 1599 | runge | 125 | LOG.Info("Cleaning up..."); |
| 126 | |||
| 1598 | runge | 127 | CleanUp(); |
| 1594 | runge | 128 | |
| 1599 | runge | 129 | LOG.Info("Thank you for using Atom. Goodbye!"); |
| 130 | |||
| 1598 | runge | 131 | return EXIT_SUCCESS; |
| 1592 | runge | 132 | } |
| 1598 | runge | 133 | |
| 134 | void CleanUp() |
||
| 135 | { |
||
| 1599 | runge | 136 | subscribers.clear(); |
| 1598 | runge | 137 | config::Manager::Delete(); |
| 1599 | runge | 138 | timer::Manager::Delete(); |
| 139 | can::Manager::Delete(); |
||
| 140 | can::Protocol::Delete(); |
||
| 141 | broker::Manager::Delete(); |
||
| 1598 | runge | 142 | net::Manager::Delete(); |
| 1599 | runge | 143 | } |
| 144 | |||
| 145 | void Handler(int status) |
||
| 146 | { |
||
| 147 | std::string signal_name = "Unknown"; |
||
| 148 | |||
| 149 | switch (status) |
||
| 150 | { |
||
| 151 | case SIGTERM: |
||
| 152 | signal_name = "Terminate"; |
||
| 153 | break; |
||
| 154 | |||
| 155 | case SIGINT: |
||
| 156 | signal_name = "Interupt"; |
||
| 157 | break; |
||
| 158 | |||
| 159 | case SIGQUIT: |
||
| 160 | signal_name = "Quit"; |
||
| 161 | break; |
||
| 162 | |||
| 163 | case SIGABRT: |
||
| 164 | signal_name = "Abort"; |
||
| 165 | break; |
||
| 166 | |||
| 167 | case SIGIO: |
||
| 168 | signal_name = "I/O"; |
||
| 169 | break; |
||
| 170 | |||
| 171 | case SIGPIPE: |
||
| 172 | signal_name = "Pipe"; |
||
| 173 | break; |
||
| 174 | } |
||
| 175 | |||
| 176 | LOG.Debug("Received signal " + signal_name + "(" + boost::lexical_cast<std::string>(status) + ")."); |
||
| 177 | |||
| 178 | if (status == SIGPIPE) |
||
| 179 | { |
||
| 180 | return; |
||
| 181 | } |
||
| 182 | |||
| 183 | on_message_condition.notify_all(); |
||
| 1598 | runge | 184 | } |