Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-who committed Jan 31, 2024
1 parent d0091d0 commit d21097c
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 21 deletions.
2 changes: 1 addition & 1 deletion spit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ target_link_libraries(pair spitlib m )
add_executable(raidsimulation raidsimulationmain.c failureType.c deviceProbs.c numList.c )
target_link_libraries(raidsimulation m )

add_executable(stush stush.c snack.h snack.c failureType.c deviceProbs.c pciUtils.c dns.c qr/qrcodegen.c tpmtotp/base32.c TOTP-MCU/sha1.c TOTP-MCU/TOTP.c auth.c qr.c status.c iprange.c simpmail.c simpmail.h simpsock.c simpsock.h blockdevices.c keyvalue.c)
add_executable(stush stush.c snack.h snack.c failureType.c deviceProbs.c pciUtils.c dns.c qr/qrcodegen.c tpmtotp/base32.c TOTP-MCU/sha1.c TOTP-MCU/TOTP.c auth.c qr.c status.c iprange.c simpmail.c simpmail.h simpsock.c simpsock.h blockdevices.c keyvalue.c advertise-mc.c respond-mc.c cluster.c)
target_link_libraries(stush spitlib m pthread aio numa readline pci uuid)


Expand Down
4 changes: 2 additions & 2 deletions spit/blockdevices.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void blockDevicesScan(blockDevicesType *bd) {
if (stat(path, &st) == 0) {
if (st.st_mode | S_IFBLK) {
int fd = open(path, O_RDONLY);
if (fd) {
if (fd >= 0) {
keyvalueType *k = keyvalueInit();

char *suf = getSuffix(path);
Expand Down Expand Up @@ -123,7 +123,7 @@ void blockDevicesScan(blockDevicesType *bd) {
free(model);
free(scsi);
}
}
} // fd
close(fd);
}
}
Expand Down
4 changes: 2 additions & 2 deletions spit/blockdevicesMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ int main() {
blockDevicesType *bd = blockDevicesInit();

blockDevicesScan(bd);
blockDevicesScan(bd);
blockDevicesScan(bd);
// blockDevicesScan(bd);
// blockDevicesScan(bd);

for (size_t i = 0; i < bd->num; i++) {
char *s = keyvalueDumpAsString(bd->devices[i].kv);
Expand Down
23 changes: 19 additions & 4 deletions spit/cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// a cluster all has the same port
clusterType * clusterInit(const size_t port) {
fprintf(stderr,"clusterInit on port %zd\n", port);
clusterType *p = calloc(1, sizeof(clusterType)); assert(p);
clusterType *p = calloc(sizeof(clusterType), 1); assert(p);
p->port = port;

return p;
Expand Down Expand Up @@ -49,7 +49,7 @@ int clusterAddNode(clusterType *c, const char *nodename, const double createdtim

c->id++;
c->node = realloc(c->node, c->id * sizeof(clusterNodeType *));
c->node[index] = calloc(1, sizeof(clusterNodeType)); // create cluster node
c->node[index] = calloc(sizeof(clusterNodeType), 1); // create cluster node
c->node[index]->name = strdup(nodename);
c->node[index]->ipaddress = strdup(nodename);
c->node[index]->created = createdtime;
Expand Down Expand Up @@ -86,8 +86,8 @@ char *clusterDumpJSONString(clusterType *c) {

// sort
if (c)
for (size_t i = 0; i < c->id-1; i++) {
for (size_t j = i+1; j < c->id; j++) {
for (int i = 0; i < c->id-1; i++) {
for (int j = i+1; j < c->id; j++) {
if (strcmp(c->node[i]->ipaddress, c->node[j]->ipaddress) > 0) {
// swap
clusterNodeType t = *c->node[i];
Expand All @@ -106,6 +106,7 @@ char *clusterDumpJSONString(clusterType *c) {
for (size_t i = 0; i < c->id; i++) {
buf += sprintf(buf, " {\n");
buf += sprintf(buf, " \"node\": \"%s\",\n", c->node[i]->name);
buf += sprintf(buf, " \"hostname\": \"%s\",\n", c->node[i]->hostname);
buf += sprintf(buf, " \"lastseen\": %.0lf,\n", now - c->node[i]->seen);
buf += sprintf(buf, " \"created\": %lf,\n", c->node[i]->created);
buf += sprintf(buf, " \"age\": %lf,\n", now - c->node[i]->created);
Expand Down Expand Up @@ -158,3 +159,17 @@ char *clusterGetNodeIP(clusterType *c, size_t nodeid) {
void clusterUpdateSeen(clusterType *c, const size_t nodeid) {
c->node[nodeid]->seen = timeAsDouble();
}

void clusterFree(clusterType *c) {
const size_t index = c->id;
for (int i = 0; i <index; i++) {
free(c->node[i]->name);
free(c->node[i]->hostname);
free(c->node[i]->ipaddress);
free(c->node[i]->osrelease);
free(c->node[i]);
}
free(c->node);
free(c);
}

2 changes: 1 addition & 1 deletion spit/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ typedef struct {


typedef struct {
size_t id; // count
int id; // count
size_t port;
double latestchange;

Expand Down
6 changes: 5 additions & 1 deletion spit/clusterMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ int main() {
printf("%s", s);
free(s);


clusterType *c = clusterInit(1600);

printf("\n");
s=clusterDumpJSONString(c);
printf("%s", s);
free(s);

clusterAddNode(c, "stu", timeAsDouble());
c->node[0]->hostname = strdup("cool");
clusterAddNode(c, "nic", timeAsDouble());
clusterAddNode(c, "nic", timeAsDouble());

Expand All @@ -27,5 +29,7 @@ int main() {
printf("%s", s);
free(s);

clusterFree(c);

return 0;
}
1 change: 1 addition & 0 deletions spit/interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ void interfacesFree(interfacesIntType *n) {
}
free(p->addr); p->addr = NULL;
free(p->devicename); p->devicename = NULL;
free(p->label); p->label = NULL;
free(p->hw); p->hw = NULL;
free(p); p = NULL;
}
Expand Down
9 changes: 7 additions & 2 deletions spit/keyvalue.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ void keyvalueParsePair(keyvalueType *kv, char *pp) {
char *key = pp;
char *value = ch+1;

keyvalueSetString(kv, strdup(key), strdup(value));
keyvalueSetString(kv, key, value);
} else {
ch = strchr(pp, ';'); // longs
if (ch) {
*ch = 0;
char *key = pp;
char *value = ch+1;
keyvalueSetLong(kv, strdup(key), atol(value));
keyvalueSetLong(kv, key, atol(value));
} else {
fprintf(stderr,"pair with '%s' has no value\n", pp);
}
Expand All @@ -60,6 +60,7 @@ keyvalueType *keyvalueInitFromString(char *par) {
keyvalueParsePair(p, tok);

while ((tok = strtok(NULL, " "))) {
// fprintf(stderr,"storing: %s\n", tok);

keyvalueParsePair(p, tok);
}
Expand Down Expand Up @@ -228,6 +229,10 @@ long keyvalueGetLong(keyvalueType *kv, const char *key) {

char *keyvalueGetString(keyvalueType *kv, const char *key) {
int index = keyvalueFindKey(kv, key);
if (index < 0) {
fprintf(stderr,"can't find: %s\n", key);
return NULL;
}
assert(kv->pairs[index].type != 1);
if (index >= 0) {
return kv->pairs[index].value;
Expand Down
17 changes: 11 additions & 6 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(200, 1); assert(message);
char *message = calloc(300, 1); assert(message);

/* set up socket */
sock = socket(AF_INET, SOCK_DGRAM, 0);
Expand Down Expand Up @@ -78,15 +78,15 @@ void *respondMC(void *arg) {


while (1) {
cnt = recvfrom(sock, message, 200, 0,
cnt = recvfrom(sock, message, 300, 0,
(struct sockaddr *) &addr, &addrlen);
if (cnt < 0) {
perror("recvfrom");
// exit(1);
} else if (cnt == 0) {
break;
}
if (cnt < 200) message[cnt] = 0; // make it terminated nicely
if (cnt < 300) message[cnt] = 0; // make it terminated nicely

// fprintf(stderr, "**NEW** should try and connect to '%s' message = \"%s\"\n", inet_ntoa(addr.sin_addr), message);

Expand All @@ -112,8 +112,10 @@ void *respondMC(void *arg) {

if ((nodeid = clusterFindNode(cluster, nodename)) < 0) {
// add and say hi
fprintf(stderr, "adding node %s\n", nodename);
char *hostname = keyvalueGetString(kv, "hostname");
fprintf(stderr, "adding node %s (%s)\n", nodename, hostname);
nodeid = clusterAddNode(cluster, nodename, startedtime);
cluster->node[nodeid]->hostname= hostname;
cluster->node[nodeid]->HDDcount = keyvalueGetLong(kv, "HDDcount");
cluster->node[nodeid]->HDDsizeGB= keyvalueGetLong(kv, "HDDsizeGB");
cluster->node[nodeid]->SSDcount = keyvalueGetLong(kv, "SSDcount");
Expand All @@ -123,12 +125,15 @@ void *respondMC(void *arg) {
cluster->node[nodeid]->Cores = keyvalueGetLong(kv, "Cores");
}

keyvalueFree(kv);

if (strcmp(clusterGetNodeIP(cluster, nodeid), node) != 0) {
fprintf(stderr, "updating node %s IP %s\n", cluster->node[nodeid]->name, node);
// fprintf(stderr, "updating nodeid %d, %s IP %s: '^%s'\n", nodeid, cluster->node[nodeid]->name, node, message);
clusterSetNodeIP(cluster, nodeid, node);
cluster->node[nodeid]->hostname= keyvalueGetString(kv, "hostname");
}

keyvalueFree(kv);


clusterUpdateSeen(cluster, nodeid);

Expand Down
3 changes: 1 addition & 2 deletions spit/sat.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,11 @@ void startThreads(interfacesIntType *n, const int serverport) {
}
}

sleep(100);

for (size_t i = 0; i < num; i++) {
pthread_join(pt[i], NULL);
// printf("thread %zd finished %d\n", i, keepRunning);
}
clusterFree(cluster);
free(tc);
free(pt);
}
Expand Down

0 comments on commit d21097c

Please sign in to comment.