#include <buffer.h> #include <bwio.h> #include <hash-table.h> #include <nameserver.h> #include <syscall.h> #include <td.h> #include <tools.h> #include <types.h> extern Buffer*** TotalSendQ; void NameServer(){ int TIDSeq[HASH_SIZE]; int i = 0; for(;i<HASH_SIZE;i++){ TIDSeq[i] = -1; } char Names[HASH_SIZE*(NAME_LENGTH_MAX+1)]; // set up send q int MyID = MyTid(); int MyIndex = GetMemoryIndexINT(MyID); Buffer** AllSendQ = GetAllSendQ(); bwprintf(COM2,"NS Index %d\n\r",MyIndex); bwprintf(COM2,"NS AllSendQ add%d\n\r",AllSendQ); Buffer SendQ; int QStorage[MAX_NUM_TD]; SendQ.Storage = (void *)QStorage; SendQ.Size = MAX_NUM_TD; SendQ.Length = 0; SendQ.Head = 0; SendQ.Tail = 0; AllSendQ[MyIndex] = &SendQ; bwprintf(COM2,"SendQ Setup\n\r"); // initialize Names for(i=0;i<HASH_SIZE;i++){ Names[i*(NAME_LENGTH_MAX+1)-1] = 0; } HT HashTable; HashTable.TidSequence = TIDSeq; HashTable.NameSequence = Names; HashTable.size = HASH_SIZE; bwprintf(COM2,"HashTable Setup\n\r"); // some sort of while loop to receive and reply int TaskToReceive; NSReq ReceiveSlot; int Result; int StrLen; FOREVER{ TaskToReceive = NULL; BufferFIFO(&SendQ,(void *)&TaskToReceive); bwprintf(COM2,"NS_LOOP: Got %d from SendQ\n\r",TaskToReceive); if(TaskToReceive != NULL){ Result = Receive(TaskToReceive,(void*) &ReceiveSlot, sizeof(NSReq)); bwprintf(COM2,"NS_LOOP:Task %d Receive Result: %d\n\r",TaskToReceive,Result); bwprintf(COM2,"NS_LOOP: Msg:%s\n\r",ReceiveSlot.Msg); StrLen = stringLen(ReceiveSlot.Msg); //bwprintf(COM2,"NS_LOOP:after string length function\n\r"); if(ReceiveSlot.MsgType == WhoIS){ //bwprintf(COM2,"NS_LOOP: msg type == %d\n\r",ReceiveSlot.MsgType); //bwprintf(COM2,"NS_LOOP: before hash look up, Sender: %d\n\r",TaskToReceive); Result = Lookup(&HashTable, ReceiveSlot.Msg,StrLen); //bwprintf(COM2,"NS_LOOP: hash look up result %d\n\r",Result); Reply(TaskToReceive,(void *)(&Result), sizeof(int)); }else if(ReceiveSlot.MsgType == RegAS){ Result = InsertTo(&HashTable,ReceiveSlot.TaskID, ReceiveSlot.Msg,StrLen); bwprintf(COM2,"NS_USR: Inserted to %d, %d, %s\n\r",Result, &(Names[Result*17]),(Names + (Result * 17))); Reply(TaskToReceive,(void *)(&Result), sizeof(int)); } }else{ bwprintf(COM2,"NS has no sender, pass\n\r"); Pass(); } } } int ActualWhoIS(AP* Args){ asm volatile ("swi 9\n\t"); register int r0 asm ("r0"); int R = r0; return R; } int WhoIs(char* Name) { NSReq SendReq; int ReplyTID; int Status; AP Args; stringCopy(SendReq.Msg, Name, NS_MESSAGE_MAX_LENGTH); bwprintf(COM2,"WhoIs: Name: %s, SendReq.Msg: %s\n\r",Name, SendReq.Msg); SendReq.MsgType = WhoIS; bwprintf(COM2,"WhoIs: MsgType: %d\n\r",SendReq.MsgType); Args.arg1 = (void *)(&SendReq); Args.arg2 = (void *)sizeof(NSReq); Args.arg3 = (void *)(&ReplyTID); Args.arg4 = (void *)sizeof(int); bwprintf(COM2,"NS_USR: WhoIs %s\n\r",Name); Status = ActualWhoIS(&Args); if (Status == sizeof(int)) return ReplyTID; return NAMESERVER_TID_INVALID; } int ActualRegAS(AP* Args){ asm volatile ("swi 10\n\t"); register int r0 asm ("r0"); int R = r0; return R; } int RegisterAs (char* Name) { NSReq SendReq; int Result; int Status; AP Args; SendReq.TaskID = MyTid(); stringCopy(SendReq.Msg, Name, NS_MESSAGE_MAX_LENGTH); SendReq.MsgType = RegAS; bwprintf(COM2,"RegAS: MsgType %d\n\r",RegAS); Args.arg1 = (void *)(&SendReq); Args.arg2 = (void *)sizeof(NSReq); Args.arg3 = (void *)(&Result); Args.arg4 = (void *)sizeof(int); bwprintf(COM2,"NS_USR: registering %s\n\r",Name); Status = ActualRegAS(&Args); if (Status == sizeof(int)) return Result; return NAMESERVER_TID_INVALID; }