Skip to content

Commit

Permalink
add hex to hash
Browse files Browse the repository at this point in the history
  • Loading branch information
TATAUFO committed Feb 15, 2020
1 parent 5166e76 commit 8aff720
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
14 changes: 13 additions & 1 deletion common/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package common

import "fmt"
import (
"encoding/hex"
"fmt"
)

// Hash is fixed length []byte
type Hash [HashLength]byte
Expand Down Expand Up @@ -58,3 +61,12 @@ func Hash2Bytes(h Hash) (b []byte) {
copy(b, h[:])
return b
}

// String2Hash is transform string to Hash
func String2Hash(s string) (Hash, error) {
h, err := hex.DecodeString(s)
if err != nil {
return Hash{}, err
}
return Bytes2Hash(h), nil
}
1 change: 1 addition & 0 deletions galaxy/wave.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
CmdPing = "ping"
CmdPong = "aler"
CmdUser = "user"
CmdPeers = "peers"
)

var (
Expand Down
27 changes: 27 additions & 0 deletions galaxy/wavepeers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2019 The PDU Authors
// This file is part of the PDU library.
//
// The PDU library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The PDU library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the PDU library. If not, see <http://www.gnu.org/licenses/>.

package galaxy

// WavePeers implements the Wave interface and represent node peers.
type WavePeers struct {
Peers []string `json:"peers"` //
}

// Command returns the protocol command string for the wave.
func (w *WavePeers) Command() string {
return CmdPeers
}
17 changes: 14 additions & 3 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ func (n *Node) SetNodes(nodes string) error {
if err != nil {
return err
}
n.peers[common.Bytes2Hash([]byte(userID))] = currentPeer
h, err := common.String2Hash(userID)
if err != nil {
return err
}
n.peers[h] = currentPeer
}

return nil
Expand Down Expand Up @@ -151,9 +155,16 @@ func (n *Node) loadPeers() error {
for _, row := range rows {
var newPeer peer.Peer
if err := json.Unmarshal(row.V, &newPeer); err != nil {
return err
log.Error(err)
continue
}
h, err := common.String2Hash(row.K)
if err != nil {
log.Error(err)
continue
}
n.peers[common.Bytes2Hash([]byte(row.K))] = &newPeer
n.peers[h] = &newPeer
log.Info("Peers load", newPeer.Url(), "by", common.Hash2String(h))
}
return nil
}
Expand Down

0 comments on commit 8aff720

Please sign in to comment.