Generating unique ID for messages.
authorJiří Filipovič <fila@ics.muni.cz>
Fri, 19 Oct 2007 12:17:01 +0000 (12:17 +0000)
committerJiří Filipovič <fila@ics.muni.cz>
Fri, 19 Oct 2007 12:17:01 +0000 (12:17 +0000)
org.glite.lb.logger/src-nt/Message.H
org.glite.lb.logger/src-nt/MessageStore.H
org.glite.lb.logger/src-nt/MessageStore.cpp [new file with mode: 0644]
org.glite.lb.logger/src-nt/Properties.H

index 37492bf..725966a 100644 (file)
@@ -43,7 +43,7 @@ public:
        int getContentLength() const 
                { return m_length; }
 
-       std::string getProperty(const std::string &name, std::string &val) const
+       std::string getProperty(const std::string &name, std::string &val)
                { return m_properties.getProperty(name); }
        
        void setProperty(const std::string &name, std::string &val) 
index 86c747f..ff03a9b 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef _MESSAGE_STORE_H_
 #define _MESSAGE_STORE_H_
 
+#include <pthread.h>
+
 /** Permanent storage for messages and their states.
  */
 
@@ -43,7 +45,7 @@ public:
 
                /** Destructor.
                 */
-               ~ID();
+               ~ID() {};
 
                /** Assignment operator.
                 */
@@ -55,7 +57,7 @@ public:
 
                /** Comparison operator
                 */
-               int operator==();
+               int operator==(const ID& second);
 
                /** Get size needed for storage (from Storable).
                 */
@@ -63,13 +65,19 @@ public:
 
                /** Save ID (from Storable)
                 */
-               virtual int save(void* &data, int len) const;
+               virtual int save(void* data, int len) const;
 
                /** Load ID (from Storable)
                 */
                virtual int load(void* data, int len);
 
+       protected:
+               unsigned long long getID() {return id;}
+
        private:
+               static pthread_mutex_t counterLock;
+               static unsigned counter;
+               unsigned long long id;
        };
 };
 
diff --git a/org.glite.lb.logger/src-nt/MessageStore.cpp b/org.glite.lb.logger/src-nt/MessageStore.cpp
new file mode 100644 (file)
index 0000000..eb9de7a
--- /dev/null
@@ -0,0 +1,24 @@
+#include <pthread.h>
+#include <sys/time.h>
+#include <sstream>
+
+#include "MessageStore.H"
+
+pthread_mutex_t MessageStore::ID::counterLock = PTHREAD_MUTEX_INITIALIZER;
+unsigned MessageStore::ID::counter = 0;
+
+MessageStore::ID::ID(){
+       time_t t;
+       time(&t);
+       pthread_mutex_lock(&counterLock);
+       counter++;
+       id = ((unsigned long long) counter << 32) + t;
+       pthread_mutex_unlock(&counterLock);
+}
+
+std::string MessageStore::ID::toString() const{
+       std::ostringstream oss;
+       oss << id;
+       return oss.str();
+}
+
index a3a18eb..77d216d 100644 (file)
@@ -2,6 +2,7 @@
 #define _PROPERTIES_H_
 
 #include <map>
+#include <string>
 
 class Properties {
 public:
@@ -12,14 +13,14 @@ public:
                {}
 
        // accessors
-       string& getProperty(string &key) 
+       std::string& getProperty(const std::string &key) 
                { return properties[key]; }
 
-       void setProperty(string &key, string &val) 
+       void setProperty(const std::string &key, std::string &val) 
                { properties[key] = val; }
 
        // iterators
-       typedef std::map<string,string>::iterator  iterator;
+       typedef std::map<std::string,std::string>::iterator  iterator;
 
        iterator begin() 
                { return properties.begin(); }
@@ -29,7 +30,7 @@ public:
 
        
 private:
-       std::map<string,string> properties;
+       std::map<std::string,std::string> properties;
 };
 
 #endif