all repos — xrxs @ a0163016c2c5ba07b55da0c59872cc925c4cceb2

experimental networked application/game server with 9p

we can write to ctl; thanks sigrid :)
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAmDbbNUACgkQO3+8IhRO
Y5jw+Q//QN7xkFkmiHcv25i8CqbaqbyQMXXDATbhZZgqhSgxbJQlyrpsRplzobC9
ni+iqkbjW1wttvIHBw/qBtNfCT4zQbvQ4YE8TJfIIHkvHv6RKnqkqszpqSjMdTqC
TBsDE0xwWcZtFLtnrgxtvOR/hph+pbCtxt6FQjcr3ec3SWegTpcyKVea6ZnLo6MZ
KncDX9DypVINFvSkeQ3N1N/l0cSCeQ9vTMBqSSpL3r2QQFc7RjaAEylQsJker82a
ejOZDkpaF72RVUdCvkcKtyXrbEk32UpWgOxyVlO/aSW/tPb5TW7xJjAVePhu+tL0
So0KhCnj514a8EJ/kCuDVGFzV2h4miAbZG8KPCslOfRiptlWAQ+Y2D0SUbD2ZITS
OdkhxEtz6mefjj05xsSHkOK4icCDWAVfs3kPhZnWHRRhdpishhLWMNZ19wN+V0Qw
1xzG/Q1UQOWZtnFUtyD7V6eAY2valVt5sCoiE4PtePM1Ozm7Niv2F19EQIKI36Dp
mOSVB+MDhLfsB4WfqaHH3m67v+l1rn3j2GlOUyUiDUG2HkGgw5L2nbymwr55HSD7
yoXzZOBNon+GTx5RbRrQoztyCKGsRdYivnmu62S976HXoVmIo2AAdPsVP5esQBKV
VKi+cJ8TBhJMI9Y/jo2GIT7DP4GMQJG/gPa2O/tqvYl7CwsWpwM=
=3L/h
-----END PGP SIGNATURE-----
commit

a0163016c2c5ba07b55da0c59872cc925c4cceb2

parent

28775d390edcbfcf1e27d596d3da124714eeeadb

2 files changed, 65 insertions(+), 108 deletions(-)

jump to
A hash.c

@@ -0,0 +1,16 @@

+#include <stdio.h> + +unsigned long long hash(char* str) { + unsigned long long h; + unsigned char* p; + + h = 0; + for (p = (unsigned char*)str; *p != '\0'; p++) + h = 37 * h + *p; + return h; // or, h % ARRAY_SIZE; +} + +int main(int argc, char** argv) { + printf("%llu\n", hash(argv[1])); + return 0; +}
M xrxs.cxrxs.c

@@ -34,32 +34,35 @@

static char Ebad[] = "something bad happened"; static char Enomem[] = "no memory"; -typedef struct Ramfile Ramfile; -struct Ramfile { +enum { CTL = 1, SLOT, CART, REALM, UNIVERSE }; + +typedef struct Aux Aux; +struct Aux { + int type; char* data; - int ndata; + int count; }; void fsread(Req* r) { - Ramfile* rf; + Aux* a; vlong offset; long count; - rf = r->fid->file->aux; + a = r->fid->file->aux; offset = r->ifcall.offset; count = r->ifcall.count; /*print("read %ld %lld\n", *count, offset); */ - if (offset >= rf->ndata) { + if (offset >= a->count) { r->ofcall.count = 0; respond(r, nil); return; } - if (offset + count >= rf->ndata) - count = rf->ndata - offset; + if (offset + count >= a->count) + count = a->count - offset; - memmove(r->ofcall.data, rf->data + offset, count); + memmove(r->ofcall.data, a->data + offset, count); r->ofcall.count = count; respond(r, nil); }

@@ -76,25 +79,25 @@ }

void fswrite(Req* r) { void* v; - Ramfile* rf; + Aux* a; vlong offset; long count; - rf = r->fid->file->aux; + a = r->fid->file->aux; offset = r->ifcall.offset; count = r->ifcall.count; - if (offset + count >= rf->ndata) { - v = realloc(rf->data, offset + count); + if (offset + count >= a->count) { + v = realloc(a->data, offset + count); if (v == nil) { respond(r, Enomem); return; } - rf->data = v; - rf->ndata = offset + count; - r->fid->file->dir.length = rf->ndata; + a->data = v; + a->count = offset + count; + r->fid->file->dir.length = a->count; } - memmove(rf->data + offset, r->ifcall.data, count); + memmove(a->data + offset, r->ifcall.data, count); r->ofcall.count = count; respond(r, nil); }

@@ -102,87 +105,48 @@

void handlectl(Req* r) { char cmd[16]; char* c = r->ifcall.data; + int i; - while (*c && *c != ' ') { - ccat(cmd, *c); + for (i = 0; i < r->ifcall.count; i++) { + ccat(cmd, *c++); } unsigned long long const cmd_hashv = hash(cmd); switch (cmd_hashv) { case SHUTDOWN_HASHV: - serving = 0; + printf("what"); break; default: break; } - r->ofcall.count = 16; - r->fid->file->dir.length = 16; - respond(r, nil); + r->ofcall.count = r->ifcall.count; + r->fid->file->dir.length = r->ifcall.count; } void xrxswrite(Req* r) { - unsigned int filename_hashv = hash(r->ifcall.name); - printf(r->ifcall.name); - switch (filename_hashv) { - case CTL_HASHV: + Aux* a = r->fid->file->aux; + switch (a->type) { + case CTL: handlectl(r); break; default: - fswrite(r); break; } + respond(r, nil); } -void fscreate(Req* r) { - Ramfile* rf; - File* f; - printf("eee"); - if (f = createfile(r->fid->file, r->ifcall.name, nil, r->ifcall.perm, nil)) { - rf = emalloc9p(sizeof *rf); - f->aux = rf; - r->fid->file = f; - r->ofcall.qid = f->dir.qid; - respond(r, nil); - return; - } - respond(r, Ebad); -} +void fsopen(Req* r) { respond(r, nil); } -void xrxscreate(Req* r) { - unsigned int filename_hashv = hash(r->ifcall.name); - printf("fff"); - switch (filename_hashv) { - case CTL_HASHV: - handlectl(r); - break; - default: - fscreate(r); - break; - } -} - -void fsopen(Req* r) { - Ramfile* rf; - printf("ooo"); - rf = r->fid->file->aux; - - if (rf && (r->ifcall.mode & OTRUNC)) { - rf->ndata = 0; - r->fid->file->dir.length = 0; - } - - respond(r, nil); -} +void wstat(Req* r) { respond(r, nil); } void fsdestroyfile(File* f) { - Ramfile* rf; - - /*fprint(2, "clunk\n"); */ - rf = f->aux; - if (rf) { - free(rf->data); - free(rf); + Aux* a = f->aux; + if (a && a->data) { + free(a->data); + free(a); + } else if (a) { + free(a); } }

@@ -211,36 +175,12 @@ self[i] = nil;

return self; } -Ramfile* createrf(Tree* t, char* path, char* filename, int perm) { - Ramfile* rf; - File* f = walkfile(t->root, path); - createfile(f, filename, nil, perm, nil); - rf = (Ramfile*)emalloc9p(sizeof *rf); - f->aux = rf; - return rf; -} - -void writerf(Tree* t, char* path, char* data) { - Ramfile* rf; - File* f = walkfile(t->root, path); - rf = (Ramfile*)emalloc9p(sizeof *rf); - rf->data = data; - rf->ndata = slen(data); - f->aux = rf; -} - -void xrxswstat(Req* r) { - r->fid->omode = OWRITE; - respond(r, nil); -} - Srv fs = { .open = fsopen, .read = fsread, .write = xrxswrite, - .create = xrxscreate, - .wstat = xrxswstat -}; + .create = fsopen, + .wstat = wstat}; int threadmaybackground(void) { return 1; }

@@ -250,9 +190,10 @@ char* mtpt = nil;

char* usocket = nil; int i; String** cart; - char cartsloc[256] = "./carts/"; - char ctlbuf[128] = "some data"; - Ramfile ctlfile = {ctlbuf, 128}; + Aux* ctlfile = malloc(sizeof(Aux)); + ctlfile->type = CTL; + ctlfile->data = malloc(256 * sizeof(char)); + ctlfile->count = 0; /* if -m PATH is supplied, mount on PATH */ /* if -s NAME is supplied, create a socket for the namespace */

@@ -270,18 +211,18 @@ }

} } + fs.foreground = 1; fs.tree = alloctree(nil, nil, DMDIR | 0777, fsdestroyfile); tree = fs.tree; - createfile(tree->root, "carts", nil, DMDIR | 0555, nil); - createfile(tree->root, "ctl", nil, 0600, &ctlfile); - + closefile(createfile(tree->root, "carts", nil, DMDIR | 0555, nil)); + closefile(createfile(tree->root, "ctl", nil, DMAPPEND | 0300, ctlfile)); String** carts = listdir("carts/"); cart = carts; while (*cart) { - createfile(walkfile(tree->root, "carts"), (*cart)->base, nil, 0444, nil); + closefile( + createfile(walkfile(tree->root, "carts"), (*cart)->base, nil, 0444, nil)); cart++; } - fs.foreground = 1; while (serving) { if (argc >= 3) {