ffead.server.doc
ServicePool.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * ServicePool.cpp
18  *
19  * Created on: Jan 29, 2010
20  * Author: sumeet
21  */
22 
23 #include "ServicePool.h"
24 static ServicePool *instance=NULL;
25 static map<string,Service> servicePool;
26 ServicePool::ServicePool()
27 {
28  instance = NULL;
29 }
30 
31 ServicePool::~ServicePool() {
32  // TODO Auto-generated destructor stub
33 }
34 
35 ServicePool* ServicePool::getInstance()
36 {
37  if(instance==NULL)
38  instance = new ServicePool;
39  return instance;
40 }
41 
42 string ServicePool::registerService(string name,Service service)// will return a unique identification for this service
43 {
44  time_t rawtime;
45  struct tm * timeinfo;
46  time(&rawtime);
47  timeinfo = localtime(&rawtime);
48  timespec en;
49  clock_gettime(CLOCK_REALTIME, &en);
50 
51  string yr = CastUtil::lexical_cast<string>(timeinfo->tm_year);
52  string mo = CastUtil::lexical_cast<string>(timeinfo->tm_mon);
53  string da = CastUtil::lexical_cast<string>(timeinfo->tm_mday);
54  string hr = CastUtil::lexical_cast<string>(timeinfo->tm_hour);
55  string mm = CastUtil::lexical_cast<string>(timeinfo->tm_min);
56  string ms = CastUtil::lexical_cast<string>(((en.tv_sec * 1000000000) + en.tv_nsec)/1000000);
57 
58  string regName = (name+yr+mo+da+hr+mm+ms);
59  servicePool[regName] = service;
60  return regName;
61 }
62 
63 bool ServicePool::unRegisterService(string name)//unregister will require the unique id
64 {
65  map<string,Service>::iterator it;
66  if(it==servicePool.end())
67  return false;
68  it = servicePool.find(name);
69  servicePool.erase(it);
70  return true;
71 }
72 
73 vector<string> ServicePool::getServices(string access)//return a list of available services
74 {
75  vector<string> services;
76  map<string,Service>::iterator it;
77  for(it=servicePool.begin();it!=servicePool.end();++it)
78  {
79  services.push_back(it->first);
80  }
81  return services;
82 }
83 
84 Service ServicePool::getService(string regName)
85 {
86  return servicePool[regName];
87 }