Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

services.h

Go to the documentation of this file.
00001 /*
00002     Ishtar - A network transparent read/write interface with
00003     probing and enumeration support.
00004     service part : network transparent access to generic services
00005     Copyright (C) 2003-2005 Stephane Magnenat <nct@ysagoon.com>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 */
00021 
00022 /*
00023     Services defintion
00024 
00025     All things to have services working
00026 */
00027 
00028 #ifndef __ISHTAR_SERVICES_H
00029 #define __ISHTAR_SERVICES_H
00030 
00031 #include <ishtar/network.h>
00032 
00033 #include <iostream>
00034 #include <sstream>
00035 #include <string>
00036 #include <vector>
00037 #include <map>
00038 #include <set>
00039 #include <list>
00040 #include <assert.h>
00041 
00067 
00068 namespace Ishtar
00069 {
00071     const UInt32 PROTOCOL_VERSION = 2;
00072     
00073     // Vector value handling
00074     // =====================
00075     
00077     struct ValueVector
00078     {
00080         enum Type
00081         {
00083             INT=0,
00085             UINT,
00087             SHORT,
00089             USHORT,
00091             CHAR,
00093             UCHAR,
00095             FLOAT,
00097             DOUBLE,
00099             BOOL
00100         };
00101     
00103         virtual ~ValueVector() { }
00104     
00106         virtual void serialize(OutputStream *stream) const = 0;
00108         virtual void serializeSpecified(OutputStream *stream) const = 0;
00110         virtual void deserializeSpecified(InputStream *stream) = 0;
00111     
00113         virtual std::string toString(unsigned index) const = 0;
00115         virtual void fromString(unsigned index, const std::string &s) = 0;
00116     
00118         virtual Type getType(void) const = 0;
00120         virtual Size size(void) const = 0;
00121     
00123         static ValueVector *newOfType(Type type, Size size = 0);
00125         static ValueVector *deserialize(InputStream *stream);
00126     };
00127     
00129     template<typename ST>
00130     struct TypeSerializer
00131     {
00133         template<typename T>
00134         static void writeToStream(OutputStream *stream, T v)
00135         {
00136             ST sv = static_cast<ST>(v);
00137             stream->writeEndianIndependant(sv);
00138         }
00139         
00141         template<typename T>
00142         static void readFromStream(InputStream *stream, T *vRef)
00143         {
00144             ST sv = stream->readEndianIndependant<ST>();
00145             *vRef = static_cast<T>(sv);
00146         }
00147     };
00148 
00149 
00151     template<>
00152     struct TypeSerializer<double>
00153     {
00155         template<typename T>
00156         static void writeToStream(OutputStream *stream, T v)
00157         {
00158             double sv = static_cast<double>(v);
00159             stream->writeDouble(sv);
00160         }
00161         
00163         template<typename T>
00164         static void readFromStream(InputStream *stream, T *vRef)
00165         {
00166             double sv = stream->readDouble();
00167             *vRef = static_cast<T>(sv);
00168         }
00169     };
00170         
00172     template<typename T> struct TypeResolver { };
00174     template<> struct TypeResolver<Int32> : public TypeSerializer<Int32> { static const ValueVector::Type type = ValueVector::INT; };
00176     template<> struct TypeResolver<UInt32> : public TypeSerializer<UInt32> { static const ValueVector::Type type = ValueVector::UINT; };
00178     template<> struct TypeResolver<Int16> : public TypeSerializer<Int16> { static const ValueVector::Type type = ValueVector::SHORT; };
00180     template<> struct TypeResolver<UInt16> : public TypeSerializer<UInt16> { static const ValueVector::Type type = ValueVector::USHORT; };
00182     template<> struct TypeResolver<Int8> : public TypeSerializer<Int8> { static const ValueVector::Type type = ValueVector::CHAR; };
00184     template<> struct TypeResolver<UInt8> : public TypeSerializer<UInt8> { static const ValueVector::Type type = ValueVector::UCHAR; };
00186     template<> struct TypeResolver<float> : public TypeSerializer<float> { static const ValueVector::Type type = ValueVector::FLOAT; };
00188     template<> struct TypeResolver<double> : public TypeSerializer<double> { static const ValueVector::Type type = ValueVector::DOUBLE; };
00190     template<> struct TypeResolver<bool> : public TypeSerializer<UInt8> { static const ValueVector::Type type = ValueVector::BOOL; };
00191     
00193     template<typename T>
00194     struct ValueVectorTemplate: public ValueVector
00195     {
00196     public:
00198         std::vector<T> value;
00199     
00201         ValueVectorTemplate(Size size) { value.resize(size); }
00202     
00203         virtual void serialize(OutputStream *stream) const
00204         {
00205             stream->writeUInt32(TypeResolver<T>::type);
00206             serializeSpecified(stream);
00207         }
00208     
00209         virtual void serializeSpecified(OutputStream *stream) const
00210         {
00211             stream->writeUInt32(static_cast<UInt32>(value.size()));
00212             for (size_t i=0; i<value.size(); i++)
00213             {
00214                 TypeResolver<T>::writeToStream(stream, value[i]);
00215             }
00216         }
00217     
00218         virtual void deserializeSpecified(InputStream *stream)
00219         {
00220             size_t size = stream->readUInt32();
00221             value.resize(size);
00222             for (size_t i=0; i<size; i++)
00223             {
00224                 T v;
00225                 TypeResolver<T>::readFromStream(stream, &v);
00226                 value[i] = v;
00227             }
00228         }
00229     
00230         virtual std::string toString(unsigned index) const
00231         {
00232             std::ostringstream oss; oss << value.at(index);
00233             return oss.str();
00234         }
00235     
00236         virtual void fromString(unsigned index, const std::string &s)
00237         {
00238             std::istringstream iss(s);
00239             T v;
00240             iss >> v;
00241             value.at(index) = v;
00242         }
00243     
00244         virtual Size size(void) const
00245         {
00246             return value.size();
00247         }
00248         
00249         virtual Type getType(void) const
00250         {
00251             return TypeResolver<T>::type;
00252         }
00253     };
00254     
00256     typedef ValueVectorTemplate<Int32> IntValueVector;
00258     typedef ValueVectorTemplate<float> FloatValueVector;
00259     
00261     struct CharValueVector: public ValueVectorTemplate<UInt8>
00262     {
00264         CharValueVector(Size size = 0):ValueVectorTemplate<UInt8>(size) { }
00266         CharValueVector(const std::string &s);
00268         CharValueVector(const char *s);
00270         operator std::string() const;
00271     };
00272     
00273     
00274     // Base service handling
00275     // =====================
00276     
00278     class ServiceDescription
00279     {
00280     public:
00282         enum Flags
00283         {
00285             READ_ONLY=0x1,
00287             NAMED_VALUES=0x2,
00289             CONSTRAINT_VALUES=0x4,
00290         };
00291     
00293         CharValueVector name;
00295         ValueVector::Type type;
00297         UInt32 flags;
00299         UInt32 length;
00301         std::vector<CharValueVector> subnames;
00303         ValueVector *mins;
00305         ValueVector *maxs;
00306     
00307     public:
00309         ServiceDescription();
00311         ~ServiceDescription();
00313         void serialize(OutputStream *stream);
00315         void deserialize(InputStream *stream);
00316     };
00317     
00319     class Service
00320     {
00321     public:
00323         ServiceDescription description;
00324     
00325     public:
00327         Service() { }
00329         virtual ~Service() { }
00330     
00332         virtual ValueVector *getValues(void) { return NULL; }
00334         virtual void setValues(ValueVector *values) { }
00335     };
00336     
00338     class ServicesContainer
00339     {
00340     public:
00342         std::map<UInt32, Service *> services;
00344         std::map<Service *, UInt32> reverseServices;
00346         std::map<std::string, Service *> namedServices;
00348         UInt32 maxUID;
00349     
00350     public:
00352         ServicesContainer();
00354         virtual ~ServicesContainer();
00356         void addService(Service *s);
00358         void eraseService(Service *s);
00360         Service *findService(const std::string &name);
00361     };
00362     
00364     class ServicesServer: public NetworkServer, public ServicesContainer
00365     {
00366     protected:
00368         void incomingData(Socket *socket);
00370         void connectionClosed(Socket *socket);
00372         void incomingConnection(Socket *socket);
00373         
00374     public:
00376         ServicesServer(UInt16 port);
00378         virtual ~ServicesServer();
00379     };
00380     
00381     
00382     // Network access handling
00383     // =======================
00384     
00386     class Answer
00387     {
00388     public:
00390         enum Type
00391         {
00393             ERROR,
00395             SERVICE_LIST,
00397             VALUES
00398         };
00399     
00400     public:
00402         virtual ~Answer() { }
00404         virtual void serialize(OutputStream *stream) = 0;
00406         virtual void deserializeSpecified(InputStream *stream) = 0;
00408         static Answer *deserialize(InputStream *stream);
00409     };
00410     
00412     class Call
00413     {
00414     public:
00416         enum Type
00417         {
00419             HELLO=0,
00421             GET_SERVICE_LIST,
00423             GET_VALUES,
00425             SET_VALUES
00426         };
00427     
00428     public:
00430         virtual ~Call() { }
00432         virtual void serialize(OutputStream *stream) = 0;
00434         virtual void deserializeSpecified(InputStream *stream) = 0;
00436         static Call *deserialize(InputStream *stream);
00438         virtual Answer *execute(ServicesContainer *registrar) { return NULL; }
00439     };
00440     
00442     class HelloCall : public Call
00443     {
00444     public:
00446         enum NodeType
00447         {
00449             SERVER=0,
00451             CLIENT
00452         } nodeType;
00454         UInt32 version;
00455     
00456     public:
00458         HelloCall() { }
00460         HelloCall(NodeType t) { nodeType = t; version=PROTOCOL_VERSION; }
00462         virtual ~HelloCall() { }
00463         virtual void serialize(OutputStream *stream);
00464         virtual void deserializeSpecified(InputStream *stream);
00465     };
00466     
00468     class ErrorAnswer: public Answer
00469     {
00470     public:
00472         enum ErrorTypes
00473         {
00475             SERVICE_DOES_NOT_EXISTS = 0,
00476         } errorType;
00477     
00478     public:
00480         ErrorAnswer() { }
00482         ErrorAnswer(ErrorTypes error) { this->errorType = error; }
00484         virtual ~ErrorAnswer() { }
00485         virtual void serialize(OutputStream *stream);
00486         virtual void deserializeSpecified(InputStream *stream);
00487     };
00488     
00490     class GetServicesCall : public Call
00491     {
00492     public:
00494         virtual ~GetServicesCall() { }
00495         virtual void serialize(OutputStream *stream) { stream->writeUInt32(GET_SERVICE_LIST); }
00496         virtual void deserializeSpecified(InputStream *stream) { }
00497     
00498         virtual Answer *execute(ServicesContainer *registrar);
00499     };
00500     
00502     class ServiceAnswer: public Answer
00503     {
00504     public:
00506         typedef std::map<UInt32, ServiceDescription *> DescriptionMap;
00508         DescriptionMap descriptions;
00509     
00510     public:
00512         virtual ~ServiceAnswer();
00514         void deleteDescriptions(void);
00515         virtual void serialize(OutputStream *stream);
00516         virtual void deserializeSpecified(InputStream *stream);
00517     };
00518     
00520     class GetValuesCall : public Call
00521     {
00522     public:
00524         Int32 requestID;
00526         std::vector<UInt32> services;
00527     
00528     public:
00530         GetValuesCall() { requestID=0; }
00532         virtual ~GetValuesCall() { }
00533         virtual void serialize(OutputStream *stream);
00534         virtual void deserializeSpecified(InputStream *stream);
00535     
00536         virtual Answer *execute(ServicesContainer *registrar);
00537     };
00538     
00540     class ValuesAnswer: public Answer
00541     {
00542     public:
00544         Int32 requestID;
00546         typedef std::map<UInt32, ValueVector *> ValueVectorMap;
00548         ValueVectorMap values;
00549     
00550     public:
00552         virtual ~ValuesAnswer();
00554         void deleteValues(void);
00555         virtual void serialize(OutputStream *stream);
00556         virtual void deserializeSpecified(InputStream *stream);
00557     };
00558     
00560     class SetValuesCall : public Call
00561     {
00562     public:
00564         std::map<UInt32, ValueVector *> values;
00565     
00566     public:
00568         virtual~SetValuesCall();
00569     
00571         void deleteValues(void);
00572         virtual void serialize(OutputStream *stream);
00573         virtual void deserializeSpecified(InputStream *stream);
00574     
00575         virtual Answer *execute(ServicesContainer *registrar);
00576     };
00577 }
00578 
00579 #endif
00580 

Generated on Mon Oct 24 17:31:22 2005 for libishtarservices by  doxygen 1.4.2