UniSet  2.0.0
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 "Configuration.h"
00030 #include "UniSetTypes.h"
00031 #include "IOController_i.hh"
00032 
00033 namespace UniSetTypes
00034 {
00035     class Message
00036     {
00037         public:
00038             enum TypeOfMessage
00039              {
00040                 Unused,        // Сообщение не содержит информации
00041                 SensorInfo,
00042                 SysCommand, // Сообщение содержит системную команду
00043                 Confirm,    // Сообщение содержит подтверждение
00044                 Timer,        // Сообщения о срабатывании таймера
00045                 TheLastFieldOfTypeOfMessage // Обязательно оставьте последним
00046             };
00047 
00048             int type;    // Содержание сообщения (тип)
00049 
00050             enum Priority
00051             {
00052                 Low,
00053                 Medium,
00054                 High,
00055                 Super
00056             };
00057 
00058             Priority priority;
00059             ObjectId node;        // откуда
00060             ObjectId supplier;    // от кого
00061             ObjectId consumer;    // кому
00062             struct timeval tm;
00063 
00064             Message();
00065 
00066             // для оптимизации, делаем конструктор который не будет инициализировать свойства класса
00067             // это необходимо для VoidMessage, который конструируется при помощи memcpy
00068             Message( int dummy_init ){}
00069 
00070             template<class In>
00071             static const TransportMessage transport(const In& msg)
00072             {
00073                 TransportMessage tmsg;
00074                 assert(sizeof(UniSetTypes::RawDataOfTransportMessage)>=sizeof(msg));
00075                 std::memcpy(&tmsg.data,&msg,sizeof(msg));
00076                 return std::move(tmsg);
00077             }
00078     };
00079 
00080 
00081     class VoidMessage : public Message
00082     {
00083         public:
00084            // для оптимизации, делаем конструктор который не будет инициализировать свойства класса
00085            // это необходимо для VoidMessage, который конструируется при помощи memcpy
00086             VoidMessage( int dummy ):Message(dummy){}
00087 
00088             VoidMessage( const TransportMessage& tm );
00089             VoidMessage();
00090             inline bool operator < ( const VoidMessage& msg ) const
00091             {
00092                 if( priority != msg.priority )
00093                     return priority < msg.priority;
00094 
00095                 if( tm.tv_sec != msg.tm.tv_sec )
00096                     return tm.tv_sec >= msg.tm.tv_sec;
00097 
00098                 return tm.tv_usec >= msg.tm.tv_usec;
00099             }
00100 
00101             inline TransportMessage transport_msg() const
00102             {
00103                 return transport(*this);
00104             }
00105 
00106             UniSetTypes::ByteOfMessage data[sizeof(UniSetTypes::RawDataOfTransportMessage)-sizeof(Message)];
00107     };
00108 
00110     class SensorMessage : public Message
00111     {
00112         public:
00113 
00114             ObjectId id;
00115             long value;
00116             bool undefined;
00117 
00118             // время изменения состояния датчика
00119             long sm_tv_sec;
00120             long sm_tv_usec;
00121 
00122             UniversalIO::IOType sensor_type;
00123             IOController_i::CalibrateInfo ci;
00124 
00125             // для пороговых датчиков
00126             bool threshold;  
00127             UniSetTypes::ThresholdId tid;
00128 
00129             SensorMessage();
00130             SensorMessage(ObjectId id, long value, const IOController_i::CalibrateInfo& ci=IOController_i::CalibrateInfo(),
00131                             Priority priority = Message::Medium,
00132                             UniversalIO::IOType st = UniversalIO::AI,
00133                             ObjectId consumer=UniSetTypes::DefaultObjectId);
00134 
00135             SensorMessage(const VoidMessage *msg);
00136             inline TransportMessage transport_msg() const
00137             {
00138                 return transport(*this);
00139             }
00140     };
00141 
00143     class SystemMessage : public Message
00144     {
00145         public:
00146             enum Command
00147             {
00148                 StartUp,    
00149                 FoldUp,     
00150                 Finish,        
00151                 WatchDog,    
00152                 ReConfiguration,        
00153                 NetworkInfo,            
00158                 LogRotate    
00159             };
00160 
00161             SystemMessage();
00162             SystemMessage(Command command, Priority priority = Message::High,
00163                             ObjectId consumer=UniSetTypes::DefaultObjectId);
00164             SystemMessage(const VoidMessage *msg);
00165 
00166             inline TransportMessage transport_msg() const
00167             {
00168                 return transport(*this);
00169             }
00170 
00171             int command;
00172             long data[2];
00173     };
00174 
00176     class TimerMessage : public Message
00177     {
00178         public:
00179             TimerMessage();
00180             TimerMessage(UniSetTypes::TimerId id, Priority prior = Message::High,
00181                             ObjectId cons=UniSetTypes::DefaultObjectId);
00182             TimerMessage(const VoidMessage *msg);
00183             inline TransportMessage transport_msg() const
00184             {
00185                 return transport(*this);
00186             }
00187 
00188             UniSetTypes::TimerId id; 
00189     };
00190 
00192     class ConfirmMessage: public Message
00193     {
00194         public:
00195 
00196             inline TransportMessage transport_msg() const
00197             {
00198                 return transport(*this);
00199             }
00200 
00201             ConfirmMessage( const VoidMessage *msg );
00202 
00203             ConfirmMessage(long in_sensor_id,
00204                     double in_value,
00205                     time_t in_time,
00206                     time_t in_time_usec,
00207                     time_t in_confirm,
00208                     Priority in_priority = Message::Medium);
00209 
00210 
00211             long sensor_id;   /* ID датчика */
00212             double value;     /* значение датчика */
00213             time_t time;      /* время, когда датчик получил сигнал */
00214             time_t time_usec; /* время в микросекундах */
00215             time_t confirm;   /* время, когда произошло квитирование */
00216 
00217             bool broadcast;
00218 
00224             bool route;
00225 
00226         protected:
00227             ConfirmMessage();
00228     };
00229 
00230 }
00231 // --------------------------------------------------------------------------
00232 #endif // MessageType_H_