UniSet  2.2.1
MessageType.h
См. документацию.
00001 /* This file is part of the UniSet project
00002  * Copyright (c) 2002 Free Software Foundation, Inc.
00003  * Copyright (c) 2002 Pavel Vainerman
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 // --------------------------------------------------------------------------
00024 // --------------------------------------------------------------------------
00025 #ifndef MessageType_H_
00026 #define MessageType_H_
00027 // --------------------------------------------------------------------------
00028 #include <sys/time.h>
00029 #include <ostream>
00030 #include "Configuration.h"
00031 #include "UniSetTypes.h"
00032 #include "IOController_i.hh"
00033 // --------------------------------------------------------------------------
00034 namespace UniSetTypes
00035 {
00036     class Message
00037     {
00038         public:
00039             enum TypeOfMessage
00040             {
00041                 Unused,        // Сообщение не содержит информации
00042                 SensorInfo,
00043                 SysCommand, // Сообщение содержит системную команду
00044                 Confirm,    // Сообщение содержит подтверждение
00045                 Timer,        // Сообщения о срабатывании таймера
00046                 TheLastFieldOfTypeOfMessage // Обязательно оставьте последним
00047             };
00048 
00049             int type = { Unused };    // Содержание сообщения (тип)
00050 
00051             enum Priority
00052             {
00053                 Low,
00054                 Medium,
00055                 High,
00056                 Super
00057             };
00058 
00059             Priority priority = { Medium };
00060             ObjectId node = { UniSetTypes::DefaultObjectId };      // откуда
00061             ObjectId supplier = { UniSetTypes::DefaultObjectId };  // от кого
00062             ObjectId consumer = { UniSetTypes::DefaultObjectId };  // кому
00063             struct timeval tm = { 0, 0 };
00064 
00065 
00066             Message( Message&& ) = default;
00067             Message& operator=(Message&& ) = default;
00068             Message( const Message& ) = default;
00069             Message& operator=(const Message& ) = default;
00070 
00071             Message();
00072 
00073             // для оптимизации, делаем конструктор который не будет инициализировать свойства класса
00074             // это необходимо для VoidMessage, который конструируется при помощи memcpy
00075             Message( int dummy_init ) {}
00076 
00077             template<class In>
00078             static const TransportMessage transport(const In& msg)
00079             {
00080                 TransportMessage tmsg;
00081                 assert(sizeof(UniSetTypes::RawDataOfTransportMessage) >= sizeof(msg));
00082                 std::memcpy(&tmsg.data, &msg, sizeof(msg));
00083                 tmsg.consumer = msg.consumer;
00084                 return std::move(tmsg);
00085             }
00086     };
00087 
00088     std::ostream& operator<<( std::ostream& os, const Message::TypeOfMessage& t );
00089 
00090     class VoidMessage : public Message
00091     {
00092         public:
00093 
00094             VoidMessage( VoidMessage&& ) = default;
00095             VoidMessage& operator=(VoidMessage&& ) = default;
00096             VoidMessage( const VoidMessage& ) = default;
00097             VoidMessage& operator=( const VoidMessage& ) = default;
00098 
00099             // для оптимизации, делаем конструктор который не будет инициализировать свойства класса
00100             // это необходимо для VoidMessage, который конструируется при помощи memcpy
00101             VoidMessage( int dummy ): Message(dummy) {}
00102 
00103             VoidMessage( const TransportMessage& tm );
00104             VoidMessage();
00105             inline bool operator < ( const VoidMessage& msg ) const
00106             {
00107                 if( priority != msg.priority )
00108                     return priority < msg.priority;
00109 
00110                 if( tm.tv_sec != msg.tm.tv_sec )
00111                     return tm.tv_sec >= msg.tm.tv_sec;
00112 
00113                 return tm.tv_usec >= msg.tm.tv_usec;
00114             }
00115 
00116             inline TransportMessage transport_msg() const
00117             {
00118                 return transport(*this);
00119             }
00120 
00121             UniSetTypes::ByteOfMessage data[sizeof(UniSetTypes::RawDataOfTransportMessage) - sizeof(Message)];
00122     };
00123 
00125     class SensorMessage : public Message
00126     {
00127         public:
00128 
00129             ObjectId id;
00130             long value;
00131             bool undefined;
00132 
00133             // время изменения состояния датчика
00134             long sm_tv_sec;
00135             long sm_tv_usec;
00136 
00137             UniversalIO::IOType sensor_type;
00138             IOController_i::CalibrateInfo ci;
00139 
00140             // для пороговых датчиков
00141             bool threshold;  
00142             UniSetTypes::ThresholdId tid;
00143 
00144             SensorMessage( SensorMessage&& ) = default;
00145             SensorMessage& operator=(SensorMessage&& ) = default;
00146             SensorMessage( const SensorMessage& ) = default;
00147             SensorMessage& operator=( const SensorMessage& ) = default;
00148 
00149             SensorMessage();
00150             SensorMessage(ObjectId id, long value, const IOController_i::CalibrateInfo& ci = IOController_i::CalibrateInfo(),
00151                           Priority priority = Message::Medium,
00152                           UniversalIO::IOType st = UniversalIO::AI,
00153                           ObjectId consumer = UniSetTypes::DefaultObjectId);
00154 
00155             SensorMessage(const VoidMessage* msg);
00156             inline TransportMessage transport_msg() const
00157             {
00158                 return transport(*this);
00159             }
00160     };
00161 
00163     class SystemMessage : public Message
00164     {
00165         public:
00166             enum Command
00167             {
00168                 Unknown,
00169                 StartUp,    
00170                 FoldUp,     
00171                 Finish,        
00172                 WatchDog,    
00173                 ReConfiguration,        
00174                 NetworkInfo,            
00179                 LogRotate    
00180             };
00181 
00182             SystemMessage( SystemMessage&& ) = default;
00183             SystemMessage& operator=(SystemMessage&& ) = default;
00184             SystemMessage( const SystemMessage& ) = default;
00185             SystemMessage& operator=( const SystemMessage& ) = default;
00186 
00187             SystemMessage();
00188             SystemMessage(Command command, Priority priority = Message::High,
00189                           ObjectId consumer = UniSetTypes::DefaultObjectId);
00190             SystemMessage(const VoidMessage* msg);
00191 
00192             inline TransportMessage transport_msg() const
00193             {
00194                 return transport(*this);
00195             }
00196 
00197             int command;
00198             long data[2];
00199     };
00200     std::ostream& operator<<( std::ostream& os, const SystemMessage::Command& c );
00201 
00202 
00204     class TimerMessage : public Message
00205     {
00206         public:
00207             TimerMessage( TimerMessage&& ) = default;
00208             TimerMessage& operator=(TimerMessage&& ) = default;
00209             TimerMessage( const TimerMessage& ) = default;
00210             TimerMessage& operator=( const TimerMessage& ) = default;
00211 
00212             TimerMessage();
00213             TimerMessage(UniSetTypes::TimerId id, Priority prior = Message::High,
00214                          ObjectId cons = UniSetTypes::DefaultObjectId);
00215             TimerMessage(const VoidMessage* msg);
00216             inline TransportMessage transport_msg() const
00217             {
00218                 return transport(*this);
00219             }
00220 
00221             UniSetTypes::TimerId id; 
00222     };
00223 
00225     class ConfirmMessage: public Message
00226     {
00227         public:
00228 
00229             inline TransportMessage transport_msg() const
00230             {
00231                 return transport(*this);
00232             }
00233 
00234             ConfirmMessage( const VoidMessage* msg );
00235 
00236             ConfirmMessage(long in_sensor_id,
00237                            double in_value,
00238                            time_t in_time,
00239                            time_t in_time_usec,
00240                            time_t in_confirm,
00241                            Priority in_priority = Message::Medium);
00242 
00243             ConfirmMessage( ConfirmMessage&& ) = default;
00244             ConfirmMessage& operator=(ConfirmMessage&& ) = default;
00245             ConfirmMessage( const ConfirmMessage& ) = default;
00246             ConfirmMessage& operator=( const ConfirmMessage& ) = default;
00247 
00248             long sensor_id;   /* ID датчика */
00249             double value;     /* значение датчика */
00250             time_t time;      /* время, когда датчик получил сигнал */
00251             time_t time_usec; /* время в микросекундах */
00252             time_t confirm;   /* время, когда произошло квитирование */
00253 
00254             bool broadcast;
00255 
00261             bool route;
00262 
00263         protected:
00264             ConfirmMessage();
00265     };
00266 
00267 }
00268 // --------------------------------------------------------------------------
00269 #endif // MessageType_H_