Main Page | Class Hierarchy | Class List | File List | Class Members

/Users/baford/proj/netsteria/sst/lib/reg.h

00001 #ifndef SST_REG_H
00002 #define SST_REG_H
00003 
00004 #include <QHash>
00005 #include <QString>
00006 #include <QStringList>
00007 #include <QByteArray>
00008 #include <QFlags>
00009 
00010 namespace SST {
00011 
00012 class XdrStream;
00013 class Endpoint;
00014 
00015 
00016 #define REGSERVER_DEFAULT_PORT  8662
00017 
00018 // Control chunk magic value for the Netsteria registration protocol.
00019 // The upper byte is zero to distinguish control packets from flow packets.
00020 // 'Nrs': Netsteria registration server
00021 #define REG_MAGIC       (quint32)0x004e7273
00022 
00023 #define REG_REQUEST             0x100   // Client-to-server request
00024 #define REG_RESPONSE            0x200   // Server-to-client response
00025 #define REG_NOTIFY              0x300   // Server-to-client async callback
00026 
00027 #define REG_INSERT1             0x00    // Insert entry - preliminary request
00028 #define REG_INSERT2             0x01    // Insert entry - authenticated request
00029 #define REG_LOOKUP              0x02    // Lookup host by ID, optionally notify
00030 #define REG_SEARCH              0x03    // Search entry by keyword
00031 
00032 
00033 // A RegInfo object represents a client-specified block of information
00034 // about itself to be made publicly available to other clients
00035 // through a registration server.
00036 class RegInfo
00037 {
00038 public:
00039         // Attribute tags - will grow over time.
00040         // The upper 16 bits are for attribute property flags.
00041         enum AttrFlag {
00042                 Invalid         = 0x00000000,   // Invalid attribute
00043 
00044                 // Flags indicating useful properties of certain tags
00045                 SearchFlag      = 0x00010000,   // Search-worthy text (UTF-8)
00046 
00047                 // Specific binary tags useful for rendezvous
00048                 Endpoints       = 0x00000001,   // Private addrs for hole punch
00049 
00050                 // UTF-8 string tags representing advertised information
00051                 HostName        = 0x00010001,   // Name of host (machine)
00052                 OwnerName       = 0x00010002,   // Name of owner (human)
00053                 City            = 0x00010003,   // Metropolitan area
00054                 Region          = 0x00010004,   // State or other locality
00055                 Country         = 0x00010005,
00056         };
00057         Q_DECLARE_FLAGS(Attr, AttrFlag)
00058 
00059 private:
00060         QHash<Attr, QByteArray> at;
00061 
00062 
00063 public:
00064         // Basic attribute management methods
00065         inline QByteArray attr(Attr tag) const
00066                 { return at.value(tag); }
00067         inline void setAttr(Attr tag, const QByteArray &value)
00068                 { at.insert(tag, value); }
00069         inline void remove(Attr tag)
00070                 { setAttr(tag, QByteArray()); }
00071         inline bool isEmpty() const { return at.isEmpty(); }
00072         inline QList<Attr> tags() const { return at.keys(); }
00073 
00074         // String attribute get/set
00075         inline QString string(Attr tag) const
00076                 { return QString::fromUtf8(at.value(tag)); }
00077         inline void setString(Attr tag, const QString &value)
00078                 { at.insert(tag, value.toUtf8()); }
00079 
00080         // Type-specific methods for individual attributes
00081         inline QString hostName() const { return string(HostName); }
00082         inline QString ownerName() const { return string(OwnerName); }
00083         inline QString city() const { return string(City); }
00084         inline QString region() const { return string(Region); }
00085         inline QString country() const { return string(Country); }
00086 
00087         inline void setHostName(const QString &str)
00088                 { setString(HostName, str); }
00089         inline void setOwnerName(const QString &str)
00090                 { setString(OwnerName, str); }
00091         inline void setCity(const QString &str)
00092                 { setString(City, str); }
00093         inline void setRegion(const QString &str)
00094                 { setString(Region, str); }
00095         inline void setCountry(const QString &str)
00096                 { setString(Country, str); }
00097 
00098         // Advertised private endpoints
00099         QList<Endpoint> endpoints() const;
00100         void setEndpoints(const QList<Endpoint> &endpoints);
00101 
00102         // Return all the words appearing in all searchable string attrs.
00103         QStringList keywords() const;
00104 
00105         // Serialization of file metadata
00106         void encode(XdrStream &ws) const;
00107         void decode(XdrStream &ws);
00108         QByteArray encode() const;
00109         static RegInfo decode(const QByteArray &data);
00110 
00111         // Constructors
00112         inline RegInfo() { }
00113         inline RegInfo(const RegInfo &other) : at(other.at) { }
00114         inline RegInfo(const QByteArray &data) { *this = decode(data); }
00115 };
00116 
00117 Q_DECLARE_OPERATORS_FOR_FLAGS(RegInfo::Attr)
00118 
00119 inline XdrStream &operator<<(XdrStream &xs, const RegInfo &fi)
00120         { fi.encode(xs); return xs; }
00121 inline XdrStream &operator>>(XdrStream &xs, RegInfo &fi)
00122         { fi.decode(xs); return xs; }
00123 
00124 
00125 } // namespace SST
00126 
00127 #endif  // SST_REG_H

Generated on Wed Mar 28 11:48:05 2007 for SST by doxygen 1.3.4