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) { ...@@ -102,9 +102,9 @@ void sendUDPToNode(char* msg,Node node, bool recvResponse, char* buffer) {
void initChordStructure(struct sockaddr_in *joinNode,sockaddr_t socketAddrServer){ void initChordStructure(struct sockaddr_in *joinNode,sockaddr_t socketAddrServer){
chord.node.socketAddr = socketAddrServer; chord.node.socketAddr = socketAddrServer;
// printf("%s",inet_ntoa(chord.node.socketAddr.sin_addr));
chord.node.nodeId = nodeToId(&chord.node.socketAddr); 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) if (joinNode == NULL)
{ {
chord.successor = chord.node; chord.successor = chord.node;
...@@ -126,10 +126,13 @@ Node closestPreceedingNode(int id){ ...@@ -126,10 +126,13 @@ Node closestPreceedingNode(int id){
Node* f=chord.fingerTable[0]; Node* f=chord.fingerTable[0];
for (int i = m-1; i >=0; i--) 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) if (nextId>chord.node.nodeId && nextId<=id)
{ {
return *(f+m); return *chord.fingerTable[i];
} }
} }
...@@ -157,8 +160,8 @@ Node findSuccessor(int id, bool fixFinger){ ...@@ -157,8 +160,8 @@ Node findSuccessor(int id, bool fixFinger){
//make_packet and send //make_packet and send
sendUDPToNode(msg,preceedingNode,true,buffer); sendUDPToNode(msg,preceedingNode,true,buffer);
// //
} }
printf("Returning preceeding node : %d",preceedingNode.nodeId);
return preceedingNode; return preceedingNode;
} }
......
...@@ -26,4 +26,5 @@ void fixFingers(); ...@@ -26,4 +26,5 @@ void fixFingers();
void printFingerTable(); void printFingerTable();
int keyToId(char * key); int keyToId(char * key);
void initChordStructure(struct sockaddr_in *joinNode, struct sockaddr_in sockAddrServer); void initChordStructure(struct sockaddr_in *joinNode, struct sockaddr_in sockAddrServer);
Node findSuccessor(int id, bool fixFinger); Node findSuccessor(int id, bool fixFinger);
\ No newline at end of file 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) ...@@ -90,7 +90,8 @@ static void *threadMainFunction(void *arg)
if (work != NULL) { if (work != NULL) {
work->func(work->arg); 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); deleteJob(work);
} }
...@@ -260,10 +261,11 @@ void decodeRequestAndProcess(char* buffer) { ...@@ -260,10 +261,11 @@ void decodeRequestAndProcess(char* buffer) {
puts("key"); puts("key");
puts(request->key); puts(request->key);
int id = keyToId(request->key); int id = keyToId(request->key);
Node node = findSuccessor(id, false); //find server where the key belongs printf("key id : %d",id);
if(node.nodeId == chord.node.nodeId) { //key belongs to the server 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"); puts("Node Id self");
printf("%d",node.nodeId); printf("%d\n Key Id: %d,\n",chord.node.nodeId,id);
if(!strcmp(request->operation,"putreq")){ if(!strcmp(request->operation,"putreq")){
puts("putreq"); puts("putreq");
addKey(request->key,request->val); addKey(request->key,request->val);
...@@ -292,6 +294,9 @@ void decodeRequestAndProcess(char* buffer) { ...@@ -292,6 +294,9 @@ void decodeRequestAndProcess(char* buffer) {
}else }else
{ {
puts("Forward request"); puts("Forward request");
printf("key id: %d, successor node id: %d",id,successorNode.nodeId);
sendUDPToNode(buffer,successorNode,false,NULL);
buffer = NULL;
} }
// free(key); // 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