1 /*------------------------------------------------------------------------------
2 Name: SocketDriverFactory.cpp
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: The client driver for the socket protocol
6 ------------------------------------------------------------------------------*/
7 #include <util/ErrorCode.h>
8 #include <util/XmlBlasterException.h>
9 #include <util/Global.h>
10 #include <util/lexical_cast.h>
11 #include <client/protocol/socket/SocketDriverFactory.h>
12 #include <client/protocol/socket/SocketDriver.h>
13
14 namespace org {
15 namespace xmlBlaster {
16 namespace client {
17 namespace protocol {
18 namespace socket {
19
20 using namespace std;
21 using namespace org::xmlBlaster::util;
22
23 SocketDriverFactory::SocketDriverFactory(Global& global)
24 : ME("SocketDriverFactory"),
25 drivers_(),
26 getterMutex_()
27 {
28 I_Log& log = global.getLog("org.xmlBlaster.client.protocol.socket");
29 if (log.call()) log.call(ME, string("Constructor"));
30
31 //int args = global_.getArgs();
32 //const char * const* argc = global_.getArgc();
33 }
34
35 SocketDriverFactory::SocketDriverFactory(const SocketDriverFactory& factory)
36 : ME(factory.ME),
37 drivers_(),
38 getterMutex_()
39 {
40 throw util::XmlBlasterException(INTERNAL_NOTIMPLEMENTED, ME, "private copy constructor");
41 }
42
43 SocketDriverFactory& SocketDriverFactory::operator =(const SocketDriverFactory&)
44 {
45 throw util::XmlBlasterException(INTERNAL_NOTIMPLEMENTED, ME, "private assignement operator");
46 }
47
48 SocketDriverFactory::~SocketDriverFactory()
49 {
50 //if (log_.call()) log_.call(ME, "Destructor start");
51 thread::Lock lock(getterMutex_);
52 DriversMap::iterator iter = drivers_.begin();
53 while (iter != drivers_.end()) {
54 delete ((*iter).second).first;
55 iter++;
56 }
57 drivers_.erase(drivers_.begin(), drivers_.end());
58 //if (log_.trace()) log_.trace(ME, "erased all drivers");
59 }
60
61 SocketDriverFactory* SocketDriverFactory::factory_ = NULL;
62
63 SocketDriverFactory& SocketDriverFactory::getFactory(Global& global)
64 {
65 if(factory_ == NULL)
66 {
67 factory_ = new SocketDriverFactory(global);
68 org::xmlBlaster::util::Object_Lifetime_Manager::instance()->manage_object("XB_SocketDriverFactory", factory_); // if not pre-allocated.
69 }
70 return *factory_;
71 }
72
73 SocketDriver& SocketDriverFactory::getDriverInstance(Global* global)
74 {
75 string instanceName = lexical_cast<string>(global);
76 I_Log& log = global->getLog("org.xmlBlaster.client.protocol.socket");
77
78 if (log.call()) log.call(ME, string("getInstance for ") + instanceName);
79 SocketDriver* driver = NULL;
80 int count = 1;
81 {
82 thread::Lock lock(getterMutex_);
83 DriversMap::iterator iter = drivers_.find(global);
84 if (iter == drivers_.end()) {
85 if (log.trace()) log.trace(ME, string("created a new instance for ") + instanceName);
86 driver = new SocketDriver(*global, instanceName);
87 // initially the counter is set to 1
88 drivers_.insert(DriversMap::value_type(global, pair<SocketDriver*, int>(driver, 1)));
89 }
90 else {
91 driver = ((*iter).second).first;
92 count = ((*iter).second).second++; // this is the counter ...
93 }
94 }
95 if (log.trace())
96 log.trace(ME, string("number of instances for '") + instanceName + "' are " + lexical_cast<std::string>(count));
97 return *driver;
98 }
99
100
101 int SocketDriverFactory::killDriverInstance(Global* global)
102 {
103 string instanceName = lexical_cast<string>(global);
104 I_Log& log = global->getLog("org.xmlBlaster.client.protocol.socket");
105
106 log.call(ME, string("killDriverInstance with a total of ") + lexical_cast<string>(drivers_.size()) + " instances, looking for global " + global->getId() + " instanceName=" + instanceName);
107 thread::Lock lock(getterMutex_);
108 DriversMap::iterator iter = drivers_.find(global);
109 if (iter == drivers_.end()) return -1;
110 int ret = --(*iter).second.second;
111 if (log.trace()) log.trace(ME, string("instances before deleting ") + lexical_cast<std::string>(ret+1));
112 if (ret <= 0) {
113 if (log.trace()) log.trace(ME, string("kill instance '") + instanceName + "' will be deleted now");
114 // do remove it since the counter is zero
115 SocketDriver* driver = (*iter).second.first;
116 drivers_.erase(iter);
117 delete driver;
118 }
119 if (log.trace())
120 log.trace(ME, string("kill instance '") + instanceName + "' the number of references is " + lexical_cast<std::string>(ret));
121 return ret;
122 }
123
124 void SocketDriverFactory::run()
125 {
126 // if (log_.trace()) log_.trace(ME, "the socket loop starts now");
127 }
128
129
130 }}}}} // namespaces
131
syntax highlighted by Code2HTML, v. 0.9.1