Skip to content

Commit

Permalink
Rationalise RegisterNode, PacketIds
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdionysus committed Dec 19, 2019
1 parent e14d331 commit 6168e58
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions network/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type Peer struct {
ServerNetworkNode *consistenthash.ServerNetworkNode

// Replies contains the current outstanding requests to the peer
Replies map[consistenthash.NodeId]chan (*packets.Packet)
Replies map[packets.PacketId]chan (*packets.Packet)
}

// NewPeer returns a new Peer with the specified logger, server and address
Expand All @@ -77,7 +77,7 @@ func NewPeer(logger *util.Logger, server *TLSServer, address string) *Peer {
Server: server,
LastHeartbeat: time.Now(),
ServerNetworkNode: nil,
Replies: map[consistenthash.NodeId]chan (*packets.Packet){},
Replies: map[packets.PacketId]chan (*packets.Packet){},
}
return inst
}
Expand Down
2 changes: 1 addition & 1 deletion network/peer_CMD_DISTRIBUTION.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (peer *Peer) process_CMD_DISTRIBUTION(packet packets.Packet) {
peer.Logger.Debug("Peer", "%02X: Node Already Registered", peer.ServerNetworkNode.ID)
} else {
// Peer is new
_, err := peer.Server.ServerNode.RegisterNode(peer.ServerNetworkNode)
err := peer.Server.ServerNode.RegisterNode(peer.ServerNetworkNode)
if err != nil {
peer.Logger.Error("Peer", "%02X: Register Node Distribution Failed: %s", peer.ServerNetworkNode.ID, err.Error())
return
Expand Down
16 changes: 11 additions & 5 deletions packets/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ const (
CMD_DISTRIBUTION = 2
)

type PacketId ch.Key

func NewRandomPacketId() PacketId {
return PacketId(ch.NewRandomKey())
}

type Packet struct {
Command uint16
ID ch.NodeId
RequestID [16]byte
ID PacketId
RequestID PacketId
Sent time.Time

Payload interface{}
Expand All @@ -22,17 +28,17 @@ type Packet struct {
func NewPacket(command uint16, payload interface{}) *Packet {
inst := &Packet{
Command: command,
ID: ch.NewRandomNodeId(),
ID: NewRandomPacketId(),
Sent: time.Now(),
Payload: payload,
}
return inst
}

func NewResponsePacket(command uint16, requestid [16]byte, payload interface{}) *Packet {
func NewResponsePacket(command uint16, requestid PacketId, payload interface{}) *Packet {
inst := &Packet{
Command: command,
ID: [16]byte(ch.NewRandomKey()),
ID: NewRandomPacketId(),
RequestID: requestid,
Sent: time.Now(),
Payload: payload,
Expand Down

0 comments on commit 6168e58

Please sign in to comment.