8#include "daq/slc/nsm/NSMDataStore.h"
10#include <daq/slc/system/Time.h>
12#include <daq/slc/base/StringUtil.h>
17#include <netinet/in.h>
19#include <daq/slc/system/LockGuard.h>
25bool NSMDataStore::open(
unsigned short max)
27 size_t size = (max > 0) ? m_mutex.size() + m_cond.size() +
28 sizeof(Header) +
sizeof(Entry) * max : 0;
29 if (!m_mem.open(
"NSMDataStore", size)) {
33 char* buf = (
char*)m_mem.map();
36 buf += m_mutex.size();
39 m_header =
reinterpret_cast<Header*
>(buf);
40 buf +=
sizeof(Header);
41 m_entries =
reinterpret_cast<Entry*
>(buf);
43 m_header->maxentries = max;
48void NSMDataStore::init()
50 if (m_header == NULL)
return;
51 int max = m_header->maxentries;
52 memset(m_buf, 0, m_mem.size());
53 m_header->maxentries = max;
60 unsigned int revision,
61 const std::string& name,
62 const std::string& format,
66 unsigned int n = m_header->nentries;
67 for (
unsigned int i = 0; i < n; i++) {
68 if (m_entries[i].
id > 0 && name == m_entries[i].name) {
72 memset(&m_entries[n], 0,
sizeof(Entry));
73 m_entries[n].id = n + 1;
74 m_entries[n].rid = rid;
75 m_entries[n].addr = addr;
76 m_entries[n].size = size;
77 m_entries[n].revision = revision;
78 m_entries[n].utime = (
unsigned int)
Time().getSecond();
79 strcpy(m_entries[n].name, name.c_str());
80 strcpy(m_entries[n].format, format.c_str());
88 const unsigned int n = m_header->nentries;
89 for (
unsigned int i = 0; i < n; i++) {
90 if (m_entries[i].
id > 0 && name == m_entries[i].name) {
99 if (
id == 0)
return NULL;
101 const unsigned int n = m_header->nentries;
102 for (
unsigned int i = 0; i < n; i++) {
103 if (m_entries[i].
id > 0 &&
104 m_entries[i].
id == (
unsigned int)
id) {
105 return &m_entries[i];
114 if (
id == 0)
return NULL;
116 const unsigned int n = m_header->nentries;
117 for (
unsigned int i = 0; i < n; i++) {
118 if (m_entries[i].rid > 0 &&
119 m_entries[i].addr == (
unsigned int)addr &&
120 m_entries[i].rid == (
unsigned int)
id) {
121 return &m_entries[i];
127void NSMDataStore::unlink()
129 const unsigned int n = m_header->nentries;
130 for (
unsigned int i = 0; i < n; i++) {
131 char* name = m_entries[i].name;
132 if (strlen(name) > 0) {
133 std::string path =
"";
135 sa.sin_addr.s_addr = m_entries[i].addr;
136 if (m_entries[i].addr > 0) {
137 path = StringUtil::form(
"%s:%s",
138 inet_ntoa(sa.sin_addr), name);
140 path = StringUtil::form(
"127.0.0.1:%s", name);
142 shm_unlink(path.c_str());
Lock Guard for a Mutex instance.
Abstract base class for different kinds of events.