Midnight Cow / ThingerIO

Dependencies:   DHT WIZnetInterface mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers thinger_message.hpp Source File

thinger_message.hpp

00001 // The MIT License (MIT)
00002 //
00003 // Copyright (c) 2015 THINGER LTD
00004 // Author: alvarolb@gmail.com (Alvaro Luis Bustamante)
00005 //
00006 // Permission is hereby granted, free of charge, to any person obtaining a copy
00007 // of this software and associated documentation files (the "Software"), to deal
00008 // in the Software without restriction, including without limitation the rights
00009 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010 // copies of the Software, and to permit persons to whom the Software is
00011 // furnished to do so, subject to the following conditions:
00012 //
00013 // The above copyright notice and this permission notice shall be included in
00014 // all copies or substantial portions of the Software.
00015 //
00016 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022 // THE SOFTWARE.
00023 
00024 #ifndef THINGER_MESSAGE_HPP
00025 #define THINGER_MESSAGE_HPP
00026 
00027 #include "pson.h"
00028 
00029 namespace thinger{
00030 
00031     class thinger_message{
00032     public:
00033         enum message_type{ MESSAGE = 1, KEEP_ALIVE = 2 };
00034         enum signal_flag { NONE=0, REQUEST_OK = 1, REQUEST_ERROR = 2 };
00035         enum fields{ STREAM_ID = 1, SIGNAL_FLAG = 2, THING_ID = 3, RESOURCE = 4, ACTION = 5, PSON = 6};
00036 
00037     public:
00038 
00039         thinger_message(thinger_message& other) : message_type_(other.message_type_), stream_id(other.stream_id), flag(REQUEST_OK), thing_id(NULL), resource(NULL), data(NULL), data_allocated(false){
00040         }
00041 
00042         thinger_message() : message_type_(MESSAGE), stream_id(0), flag(NONE), thing_id(NULL), resource(NULL), data(NULL), data_allocated(false)
00043         {}
00044 
00045         ~thinger_message(){
00046             protoson::pool.deallocate(thing_id);
00047             destroy(resource, protoson::pool);
00048             if(data_allocated){
00049                 destroy(data, protoson::pool);
00050             }
00051         }
00052 
00053     private:
00054         message_type message_type_;
00055         uint32_t stream_id;
00056         signal_flag flag;
00057         char* thing_id;
00058         protoson::pson* resource;
00059         protoson::pson* data;
00060         bool data_allocated;
00061 
00062     public:
00063 
00064         message_type get_message_type(){
00065             return message_type_;
00066         }
00067 
00068         uint32_t get_stream_id(){
00069             return stream_id;
00070         }
00071 
00072         signal_flag get_signal_flag(){
00073             return flag;
00074         }
00075 
00076         const char* get_thing_id(){
00077             return thing_id;
00078         }
00079 
00080         bool has_data(){
00081             return data!=NULL;
00082         }
00083 
00084         bool has_resource(){
00085             return resource!=NULL;
00086         }
00087 
00088     public:
00089         void set_stream_id(uint32_t stream_id) {
00090             thinger_message::stream_id = stream_id;
00091         }
00092 
00093         void set_signal_flag(signal_flag const &flag) {
00094             thinger_message::flag = flag;
00095         }
00096 
00097         void set_thing_id(const char *thing_id) {
00098             size_t str_size = strlen(thing_id);
00099             this->thing_id = (char*)protoson::pool.allocate(str_size+1);
00100             memcpy(this->thing_id, thing_id, str_size+1);
00101         }
00102 
00103         void set_message_type(message_type type){
00104             message_type_ = type;
00105         }
00106 
00107     public:
00108 
00109         void operator=(const char* str){
00110             ((protoson::pson &) * this) = str;
00111         }
00112 
00113         operator protoson::pson&(){
00114             if(data==NULL){
00115                 data = new (protoson::pool) protoson::pson;
00116                 data_allocated = true;
00117             }
00118             return *data;
00119         }
00120 
00121         protoson::pson_array& resources(){
00122             return (protoson::pson_array&)get_resources();
00123         }
00124 
00125         protoson::pson& get_resources(){
00126             if(resource==NULL){
00127                 resource = new (protoson::pool) protoson::pson;
00128             }
00129             return *resource;
00130         }
00131 
00132         protoson::pson& get_data(){
00133             return *this;
00134         }
00135 
00136         void set_data(protoson::pson& pson_data){
00137             if(data==NULL){
00138                 data = &pson_data;
00139                 data_allocated = false;
00140             }
00141         }
00142 
00143     };
00144 }
00145 
00146 #endif