1 #include "daq/slc/nsm/NSMDataStore.h"
3 #include <daq/slc/system/Time.h>
5 #include <daq/slc/base/StringUtil.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
12 #include <daq/slc/system/LockGuard.h>
18 bool NSMDataStore::open(
unsigned short max)
20 size_t size = (max > 0) ? m_mutex.size() + m_cond.size() +
21 sizeof(Header) +
sizeof(Entry) * max : 0;
22 if (!m_mem.open(
"NSMDataStore", size)) {
26 char* buf = (
char*)m_mem.map();
29 buf += m_mutex.size();
32 m_header = (Header*)buf;
33 buf +=
sizeof(Header);
34 m_entries = (Entry*)buf;
36 m_header->maxentries = max;
41 void NSMDataStore::init()
43 if (m_header == NULL)
return;
44 int max = m_header->maxentries;
45 memset(m_buf, 0, m_mem.size());
46 m_header->maxentries = max;
53 unsigned int revision,
54 const std::string& name,
55 const std::string& format,
59 unsigned int n = m_header->nentries;
60 for (
unsigned int i = 0; i < n; i++) {
61 if (m_entries[i].
id > 0 && name == m_entries[i].name) {
65 memset(&m_entries[n], 0,
sizeof(Entry));
66 m_entries[n].id = n + 1;
67 m_entries[n].rid = rid;
68 m_entries[n].addr = addr;
69 m_entries[n].size = size;
70 m_entries[n].revision = revision;
71 m_entries[n].utime = (
unsigned int)
Time().getSecond();
72 strcpy(m_entries[n].name, name.c_str());
73 strcpy(m_entries[n].format, format.c_str());
81 const unsigned int n = m_header->nentries;
82 for (
unsigned int i = 0; i < n; i++) {
83 if (m_entries[i].
id > 0 && name == m_entries[i].name) {
92 if (
id == 0)
return NULL;
94 const unsigned int n = m_header->nentries;
95 for (
unsigned int i = 0; i < n; i++) {
96 if (m_entries[i].
id > 0 &&
97 m_entries[i].
id == (
unsigned int)
id) {
107 if (
id == 0)
return NULL;
109 const unsigned int n = m_header->nentries;
110 for (
unsigned int i = 0; i < n; i++) {
111 if (m_entries[i].rid > 0 &&
112 m_entries[i].addr == (
unsigned int)addr &&
113 m_entries[i].rid == (
unsigned int)
id) {
114 return &m_entries[i];
120 void NSMDataStore::unlink()
122 const unsigned int n = m_header->nentries;
123 for (
unsigned int i = 0; i < n; i++) {
124 char* name = m_entries[i].name;
125 if (strlen(name) > 0) {
126 std::string path =
"";
128 sa.sin_addr.s_addr = m_entries[i].addr;
129 if (m_entries[i].addr > 0) {
130 path = StringUtil::form(
"%s:%s",
131 inet_ntoa(sa.sin_addr), name);
133 path = StringUtil::form(
"127.0.0.1:%s", name);
135 shm_unlink(path.c_str());