Commit 61c7b24c authored by Bhavesh Yadav's avatar Bhavesh Yadav

Fixed find predecessor

parent 86cc9090
......@@ -102,9 +102,9 @@ void sendUDPToNode(char* msg,Node node, bool recvResponse, char* buffer) {
void initChordStructure(struct sockaddr_in *joinNode,sockaddr_t socketAddrServer){
chord.node.socketAddr = socketAddrServer;
// printf("%s",inet_ntoa(chord.node.socketAddr.sin_addr));
chord.node.nodeId = nodeToId(&chord.node.socketAddr);
chord.predecessor.nodeId = -1;
printf("node Id: %d",chord.node.nodeId);
chord.predecessor.nodeId = 0;
if (joinNode == NULL)
{
chord.successor = chord.node;
......@@ -126,10 +126,13 @@ Node closestPreceedingNode(int id){
Node* f=chord.fingerTable[0];
for (int i = m-1; i >=0; i--)
{
int nextId = f[m].nodeId;
int nextId = chord.fingerTable[i]->nodeId;
printf("nextid: %d",nextId);
if(nextId == 0)
continue;
if (nextId>chord.node.nodeId && nextId<=id)
{
return *(f+m);
return *chord.fingerTable[i];
}
}
......@@ -157,8 +160,8 @@ Node findSuccessor(int id, bool fixFinger){
//make_packet and send
sendUDPToNode(msg,preceedingNode,true,buffer);
//
}
printf("Returning preceeding node : %d",preceedingNode.nodeId);
return preceedingNode;
}
......
......@@ -26,4 +26,5 @@ void fixFingers();
void printFingerTable();
int keyToId(char * key);
void initChordStructure(struct sockaddr_in *joinNode, struct sockaddr_in sockAddrServer);
Node findSuccessor(int id, bool fixFinger);
\ No newline at end of file
Node findSuccessor(int id, bool fixFinger);
void sendUDPToNode(char* msg,Node node, bool recvResponse, char* buffer);
\ No newline at end of file
......@@ -90,7 +90,8 @@ static void *threadMainFunction(void *arg)
if (work != NULL) {
work->func(work->arg);
write(work->clientFD,work->arg,sizeof(char)*strlen(work->arg));
if(work->arg !=NULL);
write(work->clientFD,work->arg,sizeof(char)*strlen(work->arg));
deleteJob(work);
}
......@@ -260,10 +261,11 @@ void decodeRequestAndProcess(char* buffer) {
puts("key");
puts(request->key);
int id = keyToId(request->key);
Node node = findSuccessor(id, false); //find server where the key belongs
if(node.nodeId == chord.node.nodeId) { //key belongs to the server
printf("key id : %d",id);
Node successorNode = findSuccessor(id, false); //find server where the key belongs
if(successorNode.nodeId == chord.node.nodeId) { //key belongs to the server
puts("Node Id self");
printf("%d",node.nodeId);
printf("%d\n Key Id: %d,\n",chord.node.nodeId,id);
if(!strcmp(request->operation,"putreq")){
puts("putreq");
addKey(request->key,request->val);
......@@ -292,6 +294,9 @@ void decodeRequestAndProcess(char* buffer) {
}else
{
puts("Forward request");
printf("key id: %d, successor node id: %d",id,successorNode.nodeId);
sendUDPToNode(buffer,successorNode,false,NULL);
buffer = NULL;
}
// free(key);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment