root/lookup_service.cpp
/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- reset
- addKeyValue
- query
- registerData
- respondToQuery
1 /****************************************************************************
2 * Copyright (C) 2006-2013 by Jason Ansel, Kapil Arya, and Gene Cooperman *
3 * jansel@csail.mit.edu, kapil@ccs.neu.edu, gene@ccs.neu.edu *
4 * *
5 * This file is part of DMTCP. *
6 * *
7 * DMTCP is free software: you can redistribute it and/or *
8 * modify it under the terms of the GNU Lesser General Public License as *
9 * published by the Free Software Foundation, either version 3 of the *
10 * License, or (at your option) any later version. *
11 * *
12 * DMTCP is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with DMTCP:dmtcp/src. If not, see *
19 * <http://www.gnu.org/licenses/>. *
20 ****************************************************************************/
21
22 #include "lookup_service.h"
23 #include "../jalib/jassert.h"
24 #include "../jalib/jsocket.h"
25
26 using namespace dmtcp;
27
28 void LookupService::reset()
29 {
30 MapIterator i;
31 for (i = _maps.begin(); i != _maps.end(); i++) {
32 KeyValueMap &kvmap = i->second;
33 KeyValueMap::iterator it;
34 for (it = kvmap.begin(); it != kvmap.end(); it++) {
35 KeyValue *k = (KeyValue*)&(it->first);
36 KeyValue *v = it->second;
37 k->destroy();
38 v->destroy();
39 delete v;
40 }
41 kvmap.clear();
42 }
43 _maps.clear();
44 }
45
46 void LookupService::addKeyValue(string id,
47 const void *key, size_t keyLen,
48 const void *val, size_t valLen)
49 {
50 KeyValueMap &kvmap = _maps[id];
51
52 KeyValue k(key, keyLen);
53 KeyValue *v = new KeyValue(val, valLen);
54 if (kvmap.find(k) != kvmap.end()) {
55 JTRACE("Duplicate key");
56 }
57 kvmap[k] = v;
58 }
59
60 void LookupService::query(string id,
61 const void *key, size_t keyLen,
62 void **val, size_t *valLen)
63 {
64 KeyValueMap &kvmap = _maps[id];
65 KeyValue k(key, keyLen);
66 if (kvmap.find(k) == kvmap.end()) {
67 JTRACE("Lookup Failed, Key not found.");
68 *val = NULL;
69 *valLen = 0;
70 return;
71 }
72
73 KeyValue *v = kvmap[k];
74 *valLen = v->len();
75 *val = new char[v->len()];
76 memcpy(*val, v->data(), *valLen);
77 }
78
79 void LookupService::registerData(const DmtcpMessage& msg,
80 const void *data)
81 {
82 JASSERT (msg.keyLen > 0 && msg.valLen > 0 &&
83 msg.keyLen + msg.valLen == msg.extraBytes)
84 (msg.keyLen) (msg.valLen) (msg.extraBytes);
85 const void *key = data;
86 const void *val = (char *)key + msg.keyLen;
87 size_t keyLen = msg.keyLen;
88 size_t valLen = msg.valLen;
89 addKeyValue(msg.nsid, key, keyLen, val, valLen);
90 }
91
92 void LookupService::respondToQuery(jalib::JSocket& remote,
93 const DmtcpMessage& msg,
94 const void *key)
95 {
96 JASSERT (msg.keyLen > 0 && msg.keyLen == msg.extraBytes)
97 (msg.keyLen) (msg.extraBytes);
98 void *val = NULL;
99 size_t valLen = 0;
100
101 query(msg.nsid, key, msg.keyLen, &val, &valLen);
102
103 DmtcpMessage reply (DMT_NAME_SERVICE_QUERY_RESPONSE);
104 reply.keyLen = 0;
105 reply.valLen = valLen;
106 reply.extraBytes = reply.valLen;
107
108 remote << reply;
109 if (valLen > 0) {
110 remote.writeAll((char*)val, valLen);
111 }
112 delete [] (char*)val;
113 }