24#include "nsm2/belle2nsm.h"
28#include <readline/readline.h>
30 extern int add_history(
char*);
35const char* master_logdir =
".";
36FILE* master_logfp = 0;
44 static char logprefix[1024];
47 tm* cur = localtime(&now);
51 strcpy(logprefix, master_logdir);
52 if (! logprefix[0]) strcpy(logprefix,
".");
53 if (logprefix[strlen(logprefix) - 1] ==
'/') {
54 logprefix[strlen(logprefix) - 1] = 0;
56 if (stat(logprefix, &statbuf) >= 0 && S_ISDIR(statbuf.st_mode)) {
57 strcat(logprefix,
"/master");
61 sprintf(logfile,
"%s.%04d%02d%02d.log", logprefix,
62 cur->tm_year + 1900, cur->tm_mon + 1, cur->tm_mday);
63 if (master_logfp != stdout) {
64 if (master_logfp) fclose(master_logfp);
65 if (!(master_logfp = fopen(logfile,
"a"))) {
66 printf(
"cannot open logfile %s\n", logfile);
75xprintlog(
const char* fmt, ...)
77 void xlogtime(
char* buf);
84 fputs(datebuf, master_logfp);
85 vfprintf(master_logfp, fmt, ap);
86 fputc(
'\n', master_logfp);
101 gettimeofday(&now, 0);
102 cur = localtime((time_t*)&now.tv_sec);
116 sprintf(buf,
"%02d:%02d:%02d.%03d ",
117 cur->tm_hour, cur->tm_min, cur->tm_sec, (
int)now.tv_usec / 1000);
121xgetargs(
char* buf,
char* argv[])
123 int argc = 0, inword = 0, inquote = 0;
128 if (*(p + 1) ==
'n') {
129 memmove(p, p + 1, strlen(p));
132 memmove(p, p + 1, strlen(p));
134 }
else if (*p ==
'"') {
136 memmove(p, p + 1, strlen(p));
143 }
else if (isspace(*p) && !inquote && inword) {
146 }
else if (! isspace(*p) && !inword) {
153 if (inquote)
return -1;
162 xprintlog(
"OK received");
170 xprintlog(
"ERROR received");
179 xprintlog(
"MSG received");
185main(
int argc,
const char** argv)
187 const char* program = argv[0];
188 const char* nodename = argv[1];
189 const char myname[] =
"rfctl";
196 master_logfp = stdout;
204 if (! b2nsm_init(nodename)) {
205 xprintlog(
"%s: INIT %s", program, b2nsm_strerror());
208 xprintlog(
"%s: INIT", program);
209 b2nsm_logging(master_logfp);
212 if (b2nsm_callback(
"OK", ok_handler) < 0) {
213 xprintlog(
"%s: CALLBACK(OK) %s", program, b2nsm_strerror());
216 if (b2nsm_callback(
"ERROR", ok_handler) < 0) {
217 xprintlog(
"%s: CALLBACK(ERROR) %s", program, b2nsm_strerror());
227 prompt = (
char*)malloc(strlen(nodename) + 2);
228 strcpy(prompt, nodename);
245 if (input) free(input);
246 if (!(input = readline(prompt)))
248 buf = (
char*)malloc(strlen(input) + 1);
251 if (!prev || (*input && strcmp(prev, input) != 0)) {
253 if (prev) free(prev);
254 prev = (
char*)malloc(strlen(input) + 1);
258 static char buffer[1024];
260 printf(
"%s", prompt);
262 fgets(buffer,
sizeof(buffer), stdin);
263 }
while (! buffer[0]);
267 if ((ac = xgetargs(buf, av)) <= 0)
269 printf(
"ac=%d av[0]=%s\n", ac, av[0]);
271 if (strcasecmp(av[0],
"q") == 0 || strcasecmp(av[0],
"quit") == 0) {
273 }
else if (strcasecmp(av[0],
"start") == 0) {
274 if (ac < 4 || ! isdigit(av[2][0])) {
275 printf(
"usage: start <node> <exp-number> <run-number>\n");
278 pars[0] = atoi(av[2]);
279 pars[1] = atoi(av[3]);
280 b2nsm_sendreq(av[1],
"RC_START", 2, pars);
282 }
else if (strcasecmp(av[0],
"stop") == 0) {
284 printf(
"usage: start <node> <exp-number> <run-number>\n");
287 pars[0] = atoi(av[2]);
288 pars[1] = atoi(av[3]);
289 b2nsm_sendreq(av[1],
"RC_STOP", 2, pars);
291 }
else if (strcasecmp(av[0],
"config") == 0) {
293 printf(
"usage: config <node>\n");
297 strcpy(scrfile,
"run_processor.py");
298 b2nsm_sendany(av[1],
"RC_LOAD", 0, 0,
sizeof(scrfile) + 1, scrfile,
"sendreq");
300 }
else if (strcasecmp(av[0],
"unconfig") == 0) {
302 printf(
"usage: unconfig <node>\n");
304 b2nsm_sendreq(av[1],
"RC_ABORT", 0, 0);
306 }
else if (strcasecmp(av[0],
"restart") == 0) {
308 printf(
"usage: restart <node>\n");
310 b2nsm_sendreq(av[1],
"RC_RECOVER", 0, 0);
313 printf(
"unknown request %s\n", av[0]);