Belle II Software development
RFNodeManager Class Reference
Inheritance diagram for RFNodeManager:
RFSharedMem RFNSM RFConf SharedMem

Public Member Functions

 RFNodeManager (string &nodename)
 
int fork (char *script, int nargs, char **args)
 
RfShm_CellGetCell (int id)
 
void * ptr (void)
 
int shmid (void)
 
bool IsCreated (void)
 
void lock (void)
 
void unlock (void)
 
bool isLocked (void)
 
void AllocMem (char *format)
 
RfNodeInfoGetNodeInfo ()
 
char * getconf (const char *key1, const char *key2=NULL, const char *key3=NULL)
 
int getconfi (const char *key1, const char *key2=NULL, const char *key3=NULL)
 

Static Public Member Functions

static void signal_handler (int num)
 
static std::string getTmpFileName (std::string user, std::string name)
 
static bool getIdFromTmpFileName (std::string filename, int &shmid, int &semid)
 
static NSMcontextGetContext ()
 

Static Private Member Functions

static void m_Configure (NSMmsg *, NSMcontext *)
 
static void m_UnConfigure (NSMmsg *, NSMcontext *)
 
static void m_Start (NSMmsg *, NSMcontext *)
 
static void m_Stop (NSMmsg *, NSMcontext *)
 
static void m_Pause (NSMmsg *, NSMcontext *)
 
static void m_Resume (NSMmsg *, NSMcontext *)
 
static void m_Restart (NSMmsg *, NSMcontext *)
 
static void m_Status (NSMmsg *, NSMcontext *)
 
static void m_OK (NSMmsg *, NSMcontext *)
 
static void m_ERROR (NSMmsg *, NSMcontext *)
 

Private Attributes

int piperec [2]
 
int pipesend [2]
 
bool m_new {false}
 True if we created the ring buffer ourselves (and need to clean it).
 
key_t m_shmkey
 SHM key, see shmget(2).
 
key_t m_semkey
 Semaphore key.
 
int m_shmid {-1}
 shared memory id
 
int m_semid {-1}
 semaphore id
 
void * m_shmadr {nullptr}
 
std::string m_nodename
 
std::string m_formatfile
 
RfNodeInfom_info
 
FILE * m_fd
 

Static Private Attributes

static NSMcontextg_context = 0
 

Detailed Description

Definition at line 18 of file RFNodeManager.h.

Member Function Documentation

◆ AllocMem()

void AllocMem ( char *  format)
inherited

Definition at line 121 of file RFNSM.cc.

122{
123 printf("AllocMem : format file = %s\n", format);
124 // Allocate shared memory
125 m_info = (RfNodeInfo*)b2nsm_allocmem(m_nodename.c_str(), format,
126 1, 3);
127 if (!m_info) {
128 fprintf(RFNSMOUT, "RFNSM : %s allocmem failure, %s\n",
129 m_nodename.c_str(), b2nsm_strerror());
130 }
131
132 m_formatfile = string(format);
133
134}

◆ GetCell()

RfShm_Cell & GetCell ( int  id)
inherited

Definition at line 33 of file RFSharedMem.cc.

34{
35 RfShm_Data* rfshm = (RfShm_Data*) ptr();
36 return rfshm->cell[id];
37}

◆ getconf()

char * getconf ( const char *  key1,
const char *  key2 = NULL,
const char *  key3 = NULL 
)
inherited

Definition at line 34 of file RFConf.cc.

35{
36 char buf[1024], keybuf[256];
37 char* p, *q;
38
39 struct RFConf_t top, *cur;
40 top.key = NULL;
41
42 // Encode key2 and key3 in the first key if exist
43 // if (! key2) { key2 = key3; key3 = 0; }
44 // if (! key1) { key1 = key2; key2 = key3; key3 = 0; }
45 if (key3)
46 sprintf(keybuf, "%s.%s.%s", key1, key2, key3);
47 else if (key2)
48 sprintf(keybuf, "%s.%s", key1, key2);
49 else if (key1)
50 strcpy(keybuf, key1);
51 else
52 return NULL;
53
54 // Search for the record in the configuration file
55 rewind(m_fd);
56 if (! top.key) { /* the first invokation */
57 int line = 0;
58 cur = ⊤
59 while (fgets(buf, sizeof(buf), m_fd)) {
60 line++;
61 /* remove '\n' and skip too long line */
62 p = strchr(buf, '\n');
63 if (! p) {
64 fprintf(stderr, "RFConf : line %d too long\n", line);
65 while (fgets(buf, sizeof(buf), m_fd) && !strchr(buf, '\n'))
66 ;
67 continue;
68 }
69 *p = 0;
70 // printf ( "buf = %s\n", buf );
71
72 /* sorry for this very tricky code... */
73 char* keyp = nullptr;
74 char* delp = nullptr;
75 char* valp = nullptr;
76 for (p = buf; *p && *p != '#'; p++) {
77 if (! keyp) {
78 if (! isspace(*p)) keyp = p;
79 } else if (! delp) {
80 if (isspace(*p)) {
81 if (!isspace(*(p + 1)) && *(p + 1) != ':') {
82 fprintf(stderr, "RFConf : invalid key at line %d\n", line);
83 break;
84 }
85 *p = 0;
86 } else if (*p == ':') {
87 *(delp = p) = 0;
88 }
89 } else if (! valp) {
90 if (! isspace(*p)) {
91 valp = q = p;
92 q++;
93 }
94 } else if (! isspace(*p)) {
95 *q++ = *p;
96 } else if (! isspace(*(p + 1))) {
97 *q++ = ' ';
98 }
99 }
100 if (valp) {
101 for (*q-- = 0; isspace(*q); *q-- = 0)
102 ;
103 }
104 if (delp) {
105 cur->next = (RFConf_t*)malloc(sizeof(*cur));
106 cur = cur->next;
107 cur->next = 0;
108 cur->key = (char*)malloc(strlen(keyp) + 1);
109 strcpy(cur->key, keyp);
110 if (valp) {
111 cur->val = (char*)malloc(strlen(valp) + 1);
112 strcpy(cur->val, valp);
113 } else {
114 cur->val = 0;
115 }
116 }
117 }
118 } else if (! top.val) { /* the first invokation must have failed */
119 return NULL;
120 }
121
122 int nitem = 0;
123 for (cur = ⊤ cur; cur = cur->next) {
124 if (cur->key == NULL) continue;
125 if (strcmp(cur->key, keybuf) == 0) return cur->val;
126 nitem++;
127 }
128 printf("RFConf: Key %s not found\n", keybuf);
129 printf("nitem = %d, keybuf = %s\n", nitem, keybuf);
130 return NULL;
131}

◆ getconfi()

int getconfi ( const char *  key1,
const char *  key2 = NULL,
const char *  key3 = NULL 
)
inherited

Definition at line 133 of file RFConf.cc.

134{
135 return atoi(getconf(key1, key2, key3));
136}

◆ GetContext()

static NSMcontext * GetContext ( )
inlinestaticinherited

Definition at line 69 of file RFNSM.h.

69{ return g_context; }

◆ getIdFromTmpFileName()

bool getIdFromTmpFileName ( std::string  filename,
int &  shmid,
int &  semid 
)
staticinherited

Definition at line 177 of file SharedMem.cc.

178{
179 char shminfo[256];
180 int fd = open(filename.c_str(), O_RDONLY);
181 if (fd < 0) {
182 printf("SharedMem: error to reopen tmp file %s\n", filename.c_str());
183 return false;
184 }
185 shmid = -1;
186 semid = -1;
187 memset(shminfo, 0, sizeof(shminfo));
188 int n = read(fd, shminfo, sizeof(shminfo));
189 close(fd);
190 sscanf(shminfo, "%d %d", &shmid, &semid);
191 return (n >= 3 && shmid >= 0 && semid >= 0);
192}

◆ GetNodeInfo()

RfNodeInfo * GetNodeInfo ( )
inherited

Definition at line 272 of file RFNSM.cc.

273{
274 return m_info;
275}

◆ getTmpFileName()

std::string getTmpFileName ( std::string  user,
std::string  name 
)
staticinherited

Definition at line 172 of file SharedMem.cc.

173{
174 return string("/tmp/") + user + string("_SHM_") + name;
175}

◆ IsCreated()

bool IsCreated ( void  )
inherited

Definition at line 167 of file SharedMem.cc.

168{
169 return m_new;
170}
bool m_new
True if we created the ring buffer ourselves (and need to clean it).
Definition: SharedMem.h:38

◆ isLocked()

bool isLocked ( void  )
inherited

Definition at line 228 of file SharedMem.cc.

229{
230 int ignored = 0;
231 return (semctl(m_semid, 0, GETVAL, ignored) == 0); //0: locked, 1: unlocked
232}
int m_semid
semaphore id
Definition: SharedMem.h:43

◆ lock()

void lock ( void  )
inherited

Definition at line 194 of file SharedMem.cc.

195{
196 struct sembuf sb;
197 sb.sem_num = 0;
198 sb.sem_op = -1;
199 sb.sem_flg = 0;
200 while (semop(m_semid, &sb, 1) == -1) {
201 if (errno == EINTR) {
202 //interrupted by signal (e.g. window size changed), try again
203 continue;
204 } else {
205 perror("lock:semop");
206 exit(-1);
207 }
208 }
209}

◆ m_Configure()

void m_Configure ( NSMmsg msg,
NSMcontext ctx 
)
staticprivateinherited

Definition at line 137 of file RFNSM.cc.

138{
139 fflush(stdout);
140 int nsmstate = RFNSM_Status::Instance().get_state();
141 RFNSM_Status::Instance().set_state(RFSTATE_TRANSITION);
142 int stat = g_nsmserver->Configure(msg, ctx);
143 fflush(stdout);
144 if (stat == 0) {
145 RFNSM_Status::Instance().set_state(RFSTATE_CONFIGURED);
146 b2nsm_ok(msg, "Configured", NULL);
147 } else {
148 RFNSM_Status::Instance().set_state(nsmstate);
149 b2nsm_error(msg, NULL);
150 }
151}

◆ m_ERROR()

void m_ERROR ( NSMmsg ,
NSMcontext  
)
staticprivateinherited

Definition at line 263 of file RFNSM.cc.

264{
265 RFNSM_Status::Instance().set_flag(-1);
266 // printf ( "ERROR received. m_flag set to -1\n" );
267}

◆ m_OK()

void m_OK ( NSMmsg ,
NSMcontext  
)
staticprivateinherited

Definition at line 254 of file RFNSM.cc.

255{
256 RFNSM_Status& rfs = RFNSM_Status::Instance();
257 int flag = rfs.get_flag();
258 flag++;
259 rfs.set_flag(flag);
260 // printf ( "OK received. flag set to %d\n", flag );
261}

◆ m_Pause()

void m_Pause ( NSMmsg msg,
NSMcontext ctx 
)
staticprivateinherited

Definition at line 197 of file RFNSM.cc.

198{
199 int nsmstate = RFNSM_Status::Instance().get_state();
200 RFNSM_Status::Instance().set_state(RFSTATE_TRANSITION);
201 int stat = g_nsmserver->Pause(msg, ctx);
202 if (stat == 0) {
203 RFNSM_Status::Instance().set_state(RFSTATE_IDLE);
204 b2nsm_ok(msg, "Idle", NULL);
205 } else {
206 RFNSM_Status::Instance().set_state(nsmstate);
207 b2nsm_error(msg, NULL);
208 }
209}

◆ m_Restart()

void m_Restart ( NSMmsg msg,
NSMcontext ctx 
)
staticprivateinherited

Definition at line 225 of file RFNSM.cc.

226{
227 int nsmstate = RFNSM_Status::Instance().get_state();
228 RFNSM_Status::Instance().set_state(RFSTATE_TRANSITION);
229 int stat = g_nsmserver->Restart(msg, ctx);
230 if (stat == 0) {
231 RFNSM_Status::Instance().set_state(RFSTATE_CONFIGURED);
232 b2nsm_ok(msg, "Configured", NULL);
233 } else {
234 RFNSM_Status::Instance().set_state(nsmstate);
235 b2nsm_error(msg, NULL);
236 }
237}

◆ m_Resume()

void m_Resume ( NSMmsg msg,
NSMcontext ctx 
)
staticprivateinherited

Definition at line 211 of file RFNSM.cc.

212{
213 int nsmstate = RFNSM_Status::Instance().get_state();
214 RFNSM_Status::Instance().set_state(RFSTATE_TRANSITION);
215 int stat = g_nsmserver->Resume(msg, ctx);
216 if (stat == 0) {
217 RFNSM_Status::Instance().set_state(RFSTATE_RUNNING);
218 b2nsm_ok(msg, "Running", NULL);
219 } else {
220 RFNSM_Status::Instance().set_state(nsmstate);
221 b2nsm_error(msg, NULL);
222 }
223}

◆ m_Start()

void m_Start ( NSMmsg msg,
NSMcontext ctx 
)
staticprivateinherited

Definition at line 169 of file RFNSM.cc.

170{
171 int nsmstate = RFNSM_Status::Instance().get_state();
172 RFNSM_Status::Instance().set_state(RFSTATE_TRANSITION);
173 int stat = g_nsmserver->Start(msg, ctx);
174 if (stat == 0) {
175 RFNSM_Status::Instance().set_state(RFSTATE_RUNNING);
176 b2nsm_ok(msg, "Running", NULL);
177 } else {
178 RFNSM_Status::Instance().set_state(nsmstate);
179 b2nsm_error(msg, NULL);
180 }
181}

◆ m_Status()

void m_Status ( NSMmsg msg,
NSMcontext  
)
staticprivateinherited

Definition at line 239 of file RFNSM.cc.

240{
241 /* Old imp
242 int stat = g_nsmserver->Status(msg, ctx);
243 if (stat == 0)
244 b2nsm_ok(msg, "Status", NULL);
245 else
246 b2nsm_error(msg, NULL);
247 */
248 int curstate = RFNSM_Status::Instance().get_state();
249 b2nsm_ok(msg, RFSTATE[curstate].c_str(), NULL);
250
251}

◆ m_Stop()

void m_Stop ( NSMmsg msg,
NSMcontext ctx 
)
staticprivateinherited

Definition at line 183 of file RFNSM.cc.

184{
185 int nsmstate = RFNSM_Status::Instance().get_state();
186 RFNSM_Status::Instance().set_state(RFSTATE_TRANSITION);
187 int stat = g_nsmserver->Stop(msg, ctx);
188 if (stat == 0) {
189 RFNSM_Status::Instance().set_state(RFSTATE_CONFIGURED);
190 b2nsm_ok(msg, "Stopped", NULL);
191 } else {
192 RFNSM_Status::Instance().set_state(nsmstate);
193 b2nsm_error(msg, NULL);
194 }
195}

◆ m_UnConfigure()

void m_UnConfigure ( NSMmsg msg,
NSMcontext ctx 
)
staticprivateinherited

Definition at line 153 of file RFNSM.cc.

154{
155 fflush(stdout);
156 int nsmstate = RFNSM_Status::Instance().get_state();
157 RFNSM_Status::Instance().set_state(RFSTATE_TRANSITION);
158 int stat = g_nsmserver->UnConfigure(msg, ctx);
159 fflush(stdout);
160 if (stat == 0) {
161 RFNSM_Status::Instance().set_state(RFSTATE_UNCONFIGURED);
162 b2nsm_ok(msg, "Unconfigured", NULL);
163 } else {
164 RFNSM_Status::Instance().set_state(nsmstate);
165 b2nsm_error(msg, NULL);
166 }
167}

◆ ptr()

void * ptr ( void  )
inherited

Definition at line 157 of file SharedMem.cc.

158{
159 return (void*) m_shmadr;
160}

◆ shmid()

int shmid ( void  )
inherited

Definition at line 162 of file SharedMem.cc.

163{
164 return m_shmid;
165}
int m_shmid
shared memory id
Definition: SharedMem.h:42

◆ unlock()

void unlock ( void  )
inherited

Definition at line 211 of file SharedMem.cc.

212{
213 struct sembuf sb;
214 sb.sem_num = 0;
215 sb.sem_op = 1;
216 sb.sem_flg = 0;
217 while (semop(m_semid, &sb, 1) == -1) {
218 if (errno == EINTR) {
219 //interrupted by signal (e.g. window size changed), try again
220 continue;
221 } else {
222 perror("unlock:semop");
223 exit(-1);
224 }
225 }
226}

Member Data Documentation

◆ g_context

NSMcontext * g_context = 0
staticprivateinherited

Definition at line 75 of file RFNSM.h.

◆ m_fd

FILE* m_fd
privateinherited

Definition at line 38 of file RFConf.h.

◆ m_formatfile

std::string m_formatfile
privateinherited

Definition at line 73 of file RFNSM.h.

◆ m_info

RfNodeInfo* m_info
privateinherited

Definition at line 74 of file RFNSM.h.

◆ m_new

bool m_new {false}
privateinherited

True if we created the ring buffer ourselves (and need to clean it).

Definition at line 38 of file SharedMem.h.

◆ m_nodename

std::string m_nodename
privateinherited

Definition at line 72 of file RFNSM.h.

◆ m_semid

int m_semid {-1}
privateinherited

semaphore id

Definition at line 43 of file SharedMem.h.

◆ m_semkey

key_t m_semkey
privateinherited

Semaphore key.

Definition at line 40 of file SharedMem.h.

◆ m_shmadr

void* m_shmadr {nullptr}
privateinherited

Definition at line 44 of file SharedMem.h.

◆ m_shmid

int m_shmid {-1}
privateinherited

shared memory id

Definition at line 42 of file SharedMem.h.

◆ m_shmkey

key_t m_shmkey
privateinherited

SHM key, see shmget(2).

Definition at line 39 of file SharedMem.h.

◆ piperec

int piperec[2]
private

Definition at line 29 of file RFNodeManager.h.

◆ pipesend

int pipesend[2]
private

Definition at line 30 of file RFNodeManager.h.


The documentation for this class was generated from the following file: