openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

namer.cpp (1536B)


      1 /* Copyright 1999, Brian J. Swetland. All rights reserved.
      2 ** Distributed under the terms of the OpenBLT License
      3 */
      4 
      5 #include <blt/namer.h>
      6 #include <blt/Message.h>
      7 #include <blt/Connection.h>
      8 
      9 #include <blt/syscall.h>
     10 #include <string.h>
     11 #include <stdlib.h>
     12 
     13 using namespace BLT;
     14 
     15 typedef struct nentry nentry;
     16 
     17 struct nentry
     18 {
     19 	nentry *next;
     20 	int32 port;
     21 	char name[1];
     22 };
     23 
     24 static nentry *first = 0;
     25 
     26 static int32 
     27 do_namer_find(const char *name)
     28 {
     29 	nentry *e;
     30 	
     31 	if(!name) return -1;
     32 	
     33 	for(e = first; e; e = e->next){
     34 		if(!strcmp(e->name,name)) return e->port;
     35 	}
     36 	
     37 	return -1;
     38 }
     39 
     40 static int32 
     41 do_namer_register(int32 port, const char *name)
     42 {
     43 	nentry *e;
     44 	
     45 	if(!name) return -1;
     46 	if(port < 1) return -1;
     47 	
     48 	for(e = first; e; e = e->next){
     49 		if(!strcmp(e->name,name)) return -1;
     50 	}
     51 	
     52 	e = (nentry *) malloc(sizeof(nentry) + strlen(name));
     53 	
     54 	if(!e) return -1;
     55 	
     56 	e->port = port;
     57 	strcpy(e->name,name);
     58 	e->next = first;
     59 	first = e;
     60 	
     61 	return 0;
     62 }
     63 
     64 int main(void)
     65 {
     66 	Connection *cnxn = Connection::CreateService("namer");
     67 	Message msg, reply;
     68 
     69 	do_namer_register(NAMER_PORT,"namer");
     70 
     71 	while(cnxn->Recv(&msg) == 0){
     72 		int32 op = -1;
     73 		int32 port = -1;
     74 		const char *name = 0;
     75 		int32 res = -1;
     76 	
     77 		msg.GetInt32('code',&op);
     78 		msg.GetInt32('port',&port);
     79 		msg.GetString('name',&name);
     80 		
     81 		switch(op){
     82 		case NAMER_FIND:
     83 			res = do_namer_find(name);
     84 			break;
     85 			
     86 		case NAMER_REGISTER:
     87 			res = do_namer_register(port, name);
     88 			break;
     89 		}
     90 		
     91 		reply.Empty();
     92 		reply.PutInt32('resp',res);
     93 		msg.Reply(&reply);
     94 	}
     95 	
     96 	return 0;
     97 }