Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-who committed May 3, 2024
1 parent e5b57ed commit 658063b
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 45 deletions.
13 changes: 9 additions & 4 deletions spit/advertise-mc.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ void *advertiseMC(void *arg) {

clusterType *cluster = tc->cluster;

int count = 0;
int addrlen, sock, cnt;
double starttime = timeAsDouble();

Expand Down Expand Up @@ -74,6 +73,8 @@ void *advertiseMC(void *arg) {
// interfacesScan(n);
char *uniquemac = NULL, *adv_ip = NULL;

unsigned long monotonic = 0;

double last = 0;
while (keepRunning) {
double now = timeAsDouble();
Expand Down Expand Up @@ -103,6 +104,7 @@ void *advertiseMC(void *arg) {
keyvalueType *kv = keyvalueInit();
keyvalueSetString(kv, "action", "hello");
keyvalueSetString(kv, "node", uniquemac);
keyvalueSetLong(kv, "mono", ++monotonic);
keyvalueSetString(kv, "hostname", buf.nodename);
keyvalueSetString(kv, "nodename", buf.nodename);
char *hwtype = hwMachine();
Expand Down Expand Up @@ -149,7 +151,9 @@ void *advertiseMC(void *arg) {
// keyvalueSetString(kv, "shell", "stush");
keyvalueSetLong(kv, "started", (long)starttime);
char *message = keyvalueDumpAsString(kv);

// fprintf(stderr,"mc:%s\n", message);

cnt = sendto(sock, message, strlen(message), 0,
(struct sockaddr *) &addr, addrlen);
if (cnt < 0) {
Expand All @@ -158,10 +162,11 @@ void *advertiseMC(void *arg) {
free(message);
keyvalueFree(kv);

double taken = timeAsDouble() - now;
if (taken > 5) taken = 5;


count++;
sleep (cluster->id < 1 ? 1 : MIN(5,cluster->id)); // every 5 secs, as >10 sec is misbehaving
fprintf(stderr,"sleeping for %.2lf\n", 5-taken);
sleep(5 - taken);
}
return NULL;
}
2 changes: 1 addition & 1 deletion spit/blockdeviceMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ int main(int argc, char *argv[]) {

jsonValue *j = bdScan();

char *s = jsonValueDump(j);
char *s = jsonValueDump(j, 1);

printf("%s\n", s);
free(s);
Expand Down
6 changes: 3 additions & 3 deletions spit/blockdevices.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void blockDevicesScan(blockDevicesType *bd) {
if (st.st_mode | S_IFBLK) {
int fd = open(path, O_RDONLY);
if (fd >= 0) {
fprintf(stderr,"%s\n", path);
// fprintf(stderr,"%s\n", path);
keyvalueType *k = keyvalueInit();

char *suf = getSuffix(path);
Expand All @@ -74,7 +74,7 @@ void blockDevicesScan(blockDevicesType *bd) {
keyvalueSetLong(k, "minor", minor);


fprintf(stderr," m %d %d\n", major, minor);
// fprintf(stderr," m %d %d\n", major, minor);

int rot = getValueFromFile(isr, 1);

Expand All @@ -99,7 +99,7 @@ void blockDevicesScan(blockDevicesType *bd) {
} else if (major >= 243) {
keyvalueSetString(k, "type", "Virtual");
} else {
fprintf(stderr,"issue!\n");
fprintf(stderr,"issue with major=%d!\n", major);
}
}

Expand Down
37 changes: 24 additions & 13 deletions spit/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,42 @@ jsonElements *jsonInit(void) {
return j;
}

char * jsonValueDump(jsonValue *val) {



char * jsonValueDump(jsonValue *val, int pretty) {
int slen = 1000000;
char *s = calloc(slen, 1);
char *ret = s;
char spaces[100]; memset(spaces, ' ', 100);


if (val->type == J_NUMBER) s += sprintf(s, "%ld ", val->number);
else if (val->type == J_STRING) s += sprintf(s, "\"%s\" ", val->string);
if (val->type == J_NUMBER) s += sprintf(s, "%ld", val->number);
else if (val->type == J_STRING) s += sprintf(s, "\"%s\"", val->string);
else if (val->type == J_ARRAY) {
s += sprintf(s, "[");
spaces[pretty] = 0;
s += sprintf(s, "%s[%s", spaces, pretty ? "\n" : "");
spaces[pretty] = ' ';

for (size_t i = 0; i < val->array->num; i++) {
if (i > 0)
s += sprintf(s, ",");
s = stpcpy(s, jsonValueDump(val->array->value[i]));
s = stpcpy(s, jsonValueDump(val->array->value[i], pretty+2));
}
s += sprintf(s, "]");
spaces[pretty] = 0;
s += sprintf(s, "%s]%s", spaces, pretty? "\n" : "");
spaces[pretty] = 32;
} else if (val->type == J_OBJECT) {
s += sprintf(s, "{");
spaces[pretty] = 0;
s += sprintf(s, "%s{%s", spaces, pretty? "\n" : "");
for (size_t i = 0; i < val->object->num; i++) {
if (i > 0)
s += sprintf(s, ",");
s += sprintf(s, "\"%s\": ", val->object->key[i]);
s = stpcpy(s, jsonValueDump(val->object->value[i]));
s += sprintf(s, ",%s", pretty ? "\n" : "");
s += sprintf(s, "%s\"%s\": ", spaces, val->object->key[i]);
s = stpcpy(s, jsonValueDump(val->object->value[i], pretty+2));
}
s += sprintf(s, "}");
s += sprintf(s, "%s}%s", spaces, pretty ? "\n" : "");
spaces[pretty] = 32;
} else if (val->type == J_TRUE) {
s += sprintf(s, "true");
} else if (val->type == J_FALSE) {
Expand All @@ -46,14 +57,14 @@ char * jsonValueDump(jsonValue *val) {



char * jsonElementsDump(jsonElements *j) {
char * jsonElementsDump(jsonElements *j, int pretty) {
char *s = calloc(100000,1);
char *ret = s;

for (size_t i = 0; i < j->num; i++) {
if (i > 0)
s += sprintf(s, ",");
s = stpcpy(s,jsonValueDump(j->value[i]));
s = stpcpy(s,jsonValueDump(j->value[i], pretty));
}

return ret;
Expand Down
4 changes: 2 additions & 2 deletions spit/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ typedef struct {


jsonElements *jsonInit(void);
char * jsonElementsDump(jsonElements *j);
char * jsonValueDump(jsonValue *j);
char * jsonElementsDump(jsonElements *j, int pretty);
char * jsonValueDump(jsonValue *j, int pretty);

jsonValue *jsonValueNumber(long num);
jsonValue *jsonValueString(char *str);
Expand Down
2 changes: 1 addition & 1 deletion spit/jsonMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main(void) {
jsonElementAdd(j, o);


jsonElementsDump(j);
jsonElementsDump(j, 1);


return 0;
Expand Down
2 changes: 2 additions & 0 deletions spit/keyvalue.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ void keyvalueParsePair(keyvalueType *kv, char *pp) {
keyvalueSetLong(kv, key, atol(value));
} else {
fprintf(stderr,"pair with '%s' has no value\n", pp);
abort();
}
}
}



keyvalueType *keyvalueInitFromString(char *par) {
// fprintf(stderr,"init from string: %s\n",par);
keyvalueType *p = keyvalueInit();
char *parse = strdup(par);
char *tok = strtok(parse, " ");
Expand Down
13 changes: 2 additions & 11 deletions spit/nfsexports.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,9 @@ void nfsCheckMissing(nfsRangeExports *n) {
char *devpath = strtok(ss, " \t");

if (devpath) {
int fd = open(devpath, O_RDONLY);
if (fd >= 0) {
unsigned int major, minor;
majorAndMinor(fd, &major, &minor);
if (major == 253) {
n->exports[i].type = strdup("Device-Mapper");
}

// fprintf(stderr,"%s ---> %s -> %u:%u\n", fullp, devpath, major, minor);
}
bdType *bd = bdInit(devpath);
bdFree(&bd);
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion spit/nfsexportsMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ int main(void) {
nfsRangeExports *n = nfsExportsInit();

jsonValue* json = nfsExportsJSON(n);
printf("%s\n", jsonValueDump(json));
printf("%s\n", jsonValueDump(json, 1));

nfsExportsFree(&n);
free(n);
Expand Down
17 changes: 15 additions & 2 deletions spit/respond-mc.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void *respondMC(void *arg) {
int sock, cnt;
socklen_t addrlen;
struct ip_mreq mreq;
char *message = calloc(300, 1); assert(message);
char *message = calloc(1500, 1); assert(message);

/* set up socket */
sock = socket(AF_INET, SOCK_DGRAM, 0);
Expand Down Expand Up @@ -117,7 +117,7 @@ void *respondMC(void *arg) {
fprintf(stderr,"node status: good: %zd, bad: %zd\n", nodesGood, nodesBad);
}

cnt = recvfrom(sock, message, 300, 0,
cnt = recvfrom(sock, message, 1500, 0,
(struct sockaddr *) &addr, &addrlen);
if (cnt < 0) {
fprintf(stderr, "no broadcast is observed, even though this service is running. open port %d/UDP\n", tc->serverport);
Expand Down Expand Up @@ -159,7 +159,19 @@ void *respondMC(void *arg) {

char *tmp = keyvalueGetString(kv, "nodename"); // human readable
keyvalueSetString(tc->cluster->node[nodeid]->info, "nodename", tmp);
//free(tmp);

unsigned long oldmono = keyvalueGetLong(tc->cluster->node[nodeid]->info, "mono");
unsigned long newmono = keyvalueGetLong(kv, "mono");
if (newmono != (oldmono + 1)) {
fprintf(stderr,"*missed a packet: old mono = %ld, new mono = %ld\n", oldmono, newmono);
} else {
fprintf(stderr,"*correct* got the next sequence: %s\n", tmp);
}
keyvalueSetLong(tc->cluster->node[nodeid]->info, "mono", newmono);

free(tmp);


tmp = keyvalueGetString(kv, "nodeHW"); // human readable
keyvalueSetString(tc->cluster->node[nodeid]->info, "nodehw", tmp ? tmp : "");
Expand All @@ -179,6 +191,7 @@ void *respondMC(void *arg) {
keyvalueSetString(tc->cluster->node[nodeid]->info, "biosdate", tmp);
free(tmp);


keyvalueSetLong(tc->cluster->node[nodeid]->info, "HDDcount", keyvalueGetLong(kv, "HDDcount"));
keyvalueSetLong(tc->cluster->node[nodeid]->info, "HDDsizeGB", keyvalueGetLong(kv, "HDDsizeGB"));
keyvalueSetLong(tc->cluster->node[nodeid]->info, "SSDcount", keyvalueGetLong(kv, "SSDcount"));
Expand Down
4 changes: 2 additions & 2 deletions spit/sat.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ void *receiver(void *arg) {
nfsRangeExports *n = nfsExportsInit();

jsonValue* json = nfsExportsJSON(n);
char *ss = jsonValueDump(json);
char *ss = jsonValueDump(json, 1);
if (socksend(connfd, ss, 0, 1) < 0) {
perror("socksendcluster");
}
Expand All @@ -406,7 +406,7 @@ void *receiver(void *arg) {
} else if (strncmp(buffer,"block", 5)==0) {
jsonValue *j = bdScan();

char * s = jsonValueDump(j);
char * s = jsonValueDump(j, 1);
if (socksend(connfd, s, 0, 0) < 0) {
perror("socksendblock");
}
Expand Down
12 changes: 7 additions & 5 deletions spit/stush.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#include "simpsock.h"
#include "iprange.h"

#define SAT_PORT 6000

extern char **environ;

size_t warningsBeforeExit = 3;
Expand Down Expand Up @@ -556,7 +558,7 @@ void cmd_booking(const int tty, char *second) {

void cmd_sum(const int tty) {
if (tty) {}
int fd = sockconnect("127.0.0.1", 1600, 10);
int fd = sockconnect("127.0.0.1", SAT_PORT, 10);
if (fd > 0) {
if (socksend(fd, "sum\n", 0, 1)) {
char *buffer = calloc(1024, 1); assert(buffer);
Expand All @@ -576,7 +578,7 @@ void cmd_sum(const int tty) {

void cmd_clusternodes(const int tty, int good) {
if (tty) {}
int fd = sockconnect("127.0.0.1", 1600, 10);
int fd = sockconnect("127.0.0.1", SAT_PORT, 10);
if (fd > 0) {
if (socksend(fd, good ? "good\n" : "bad\n", 0, 1)) {
char *buffer = calloc(1024, 1); assert(buffer);
Expand All @@ -596,7 +598,7 @@ void cmd_clusternodes(const int tty, int good) {

void cmd_cluster(const int tty) {
if (tty) {}
int fd = sockconnect("127.0.0.1", 1600, 5);
int fd = sockconnect("127.0.0.1", SAT_PORT, 5);
if (fd > 0) {
if (socksend(fd, "cluster\n", 0, 1)) {
char *buffer = calloc(1024, 1); assert(buffer);
Expand Down Expand Up @@ -1161,7 +1163,7 @@ void cmd_listPCI(int tty, size_t filterclass, char *label) {
// from getifaddrs man page
void cmd_listNICs(int tty) {
if (tty) {}
int fd = sockconnect("127.0.0.1", 1600, 10);
int fd = sockconnect("127.0.0.1", SAT_PORT, 10);
if (fd > 0) {
socksend(fd, "interfaces\n", 0, 1);
char *buffer = calloc(1024, 1); assert(buffer);
Expand Down Expand Up @@ -1360,7 +1362,7 @@ size_t countDriveBlockDevices(void) {

void cmd_listDriveBlockDevices(int tty) {
if (tty) {}
int fd = sockconnect("127.0.0.1", 1600, 10);
int fd = sockconnect("127.0.0.1", SAT_PORT, 10);
if (fd > 0) {
socksend(fd, "block\n", 0, 1);
char *buffer = calloc(1024, 1); assert(buffer);
Expand Down

0 comments on commit 658063b

Please sign in to comment.