Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize 2 #53

Open
wants to merge 10 commits into
base: project1-master
Choose a base branch
from
28 changes: 28 additions & 0 deletions PROJ1/findSuccessor/findSuccessor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package findSuccessor

import (
"math"

nodeDefs "../utils/node_defs"
)

//FindSuccessor ... This function is used to find successor of node with ID targetID
func FindSuccessor(sponsoringNode *nodeDefs.Node, targetID int64, totalNodes int) (successor *nodeDefs.Node) {
if targetID > sponsoringNode.ChannelId && targetID <= sponsoringNode.Successor.ChannelId {
successor = sponsoringNode.Successor
} else {
precedingNode := findClosestPrecedingNode(sponsoringNode, targetID, totalNodes)
successor = FindSuccessor(precedingNode, targetID, totalNodes)
}
return successor
}

func findClosestPrecedingNode(sponsoringNode *nodeDefs.Node, targetID int64, totalNodes int) (precedingNode *nodeDefs.Node) {

for i := int(math.Log2(float64(totalNodes))); i >= 0; i-- {
if sponsoringNode.FingerTable[int64(i)].ChannelId > sponsoringNode.ChannelId && sponsoringNode.FingerTable[int64(i)].ChannelId < targetID {
precedingNode = sponsoringNode.FingerTable[int64(i)]
}
}
return
}
36 changes: 36 additions & 0 deletions PROJ1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,28 @@ func generate_channel_id() (rand_num int64){
}



//Stabilize ...
func Stabilize(n *node.Node) {
x := n.Successor
var message = msg.Message {Do:"find-ring-predecessor", TargetId: x, RespondTo: n.ChannelId}
string_message, err := json.Marshal(message)
check_error(err)
SendDataToNetwork(n.ChannelId, string(string_message))

x = GetDataFromBucket(n.ChannelId)

if x > n.ChannelId && x < n.Successor {
n.Successor = x
}
log.Println(n.Successor, n.ChannelId)

message = msg.Message {Do: "ring-notify", RespondTo: n.ChannelId}
string_message, err = json.Marshal(message)
check_error(err)
}


/*
Initializes the network with nodes with random identifiers.
Creates nodes with random identifiers and adds them to the network map.
Expand Down Expand Up @@ -534,6 +556,17 @@ func net_node(channel_id int64){
sponsoring_node_id := message.SponsoringNode
Join_ring(sponsoring_node_id, &node_obj)
ring_nodes.Store(channel_id, &node_obj)

var message = msg.Message {Do:"stabilize"}

string_message, err := json.Marshal(message)
check_error(err)
//Tell sponsoring_node_id to find the successor of the node_obj
SendDataToNetwork(node_obj.ChannelId, string(string_message))
//Wait to hear back what the successor is



}else{
log.Printf("\nNode %d is already in the ring; cannot join-ring\n", channel_id)
}
Expand Down Expand Up @@ -570,6 +603,8 @@ func net_node(channel_id int64){
}else if message.Do == "set-successor" {
//Set the successor as the target id
node_obj.Successor = message.TargetId
} else if message.Do == "stabilize" {
Stabilize(&node_obj)
}

if execute_fix_fingers == true {
Expand Down Expand Up @@ -726,6 +761,7 @@ func coordinator(prog_args []string){
check_error(err)
// Give a random node instructions
network[channel_id] <- string(modified_inst)
time.Sleep(5 * time.Second)
}

//Every 7 seconds, tell someone to fix their fingers
Expand Down
1 change: 0 additions & 1 deletion PROJ1/utils/message_defs/message_defs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package message_defs


/*
This struct defines the json messages for the
CHORD protocol.
Expand Down