init version of pingpong proto package

[TEST]

$ go test
Ping:127.0.0.1:9999/0x12345678901234567890
Pong:# Keys: 3
buf: [56 255 129 3 1 1 15 80 105 110 103 77 101 115 115 97 103 101 84 121 112 101 1 255 130 0 1 3 1 2 73 80 1 12 0 1 4 80 111 114 116 1 12 0 1 6 80 117 98 75 101 121 1 12 0 0 0 44 255 130 1 9 49 50 55 46 48 46 48 46 49 1 4 57 57 57 57 1 22 48 120 49 50 51 52 53 54 55 56 57 48 49 50 51 52 53 54 55 56 57 48 0]
buf: [42 255 131 3 1 1 15 80 111 110 103 77 101 115 115 97 103 101 84 121 112 101 1 255 132 0 1 1 1 7 80 117 98 75 101 121 115 1 255 134 0 0 0 22 255 133 2 1 1 8 91 93 115 116 114 105 110 103 1 255 134 0 1 12 0 0 53 255 132 1 3 15 48 120 49 49 49 49 49 49 49 49 49 49 49 49 49 15 48 120 50 50 50 50 50 50 50 50 50 50 50 50 50 15 48 120 51 51 51 51 51 51 51 51 51 51 51 51 51 0]
Ping:127.0.0.1:9999/0x12345678901234567890
Pong:# Keys: 3
PASS
ok      github.com/harmony-one/harmony/proto/node       0.005s

Signed-off-by: Leo Chen <leo@harmony.one>
pull/81/head
Leo Chen 6 years ago
parent 35ab1d11f6
commit 505de0e58e
  1. 94
      proto/node/pingpong.go
  2. 64
      proto/node/pingpong_test.go

@ -0,0 +1,94 @@
/*
Package proto/node implements the communication protocol among nodes.
pingpong.go adds support of ping/pong messages.
ping: from node to peers, sending IP/Port/PubKey info
TODO: add protocol version support
pong: peer responds to ping messages, sending all pubkeys known by peer
TODO:
* add the version of the protocol
*/
package node
import (
"bytes"
"encoding/gob"
"fmt"
"log"
)
type PingMessageType struct {
IP string
Port string
PubKey string
}
type PongMessageType struct {
PubKeys []string
}
func (p PingMessageType) String() string {
return fmt.Sprintf("%v:%v/%v", p.IP, p.Port, p.PubKey)
}
func (p PongMessageType) String() string {
return fmt.Sprintf("# Keys: %v", len(p.PubKeys))
}
// Deserialize Ping Message
func GetPingMessage(payload []byte) (*PingMessageType, error) {
ping := new(PingMessageType)
r := bytes.NewBuffer(payload)
decoder := gob.NewDecoder(r)
err := decoder.Decode(ping)
if err != nil {
log.Panic("Can't deserialize Ping message")
}
return ping, nil
}
// Deserialize Pong Message
func GetPongMessage(payload []byte) (*PongMessageType, error) {
pong := new(PongMessageType)
r := bytes.NewBuffer(payload)
decoder := gob.NewDecoder(r)
err := decoder.Decode(pong)
if err != nil {
log.Panic("Can't deserialize Pong message")
}
return pong, nil
}
// ConstructPingMessage contructs ping message from node to leader
func (ping PingMessageType) ConstructPingMessage() []byte {
var byteBuffer bytes.Buffer
encoder := gob.NewEncoder(&byteBuffer)
err := encoder.Encode(ping)
if err != nil {
log.Panic("Can't serialize Ping message", "error:", err)
return nil
}
return byteBuffer.Bytes()
}
// ConstructPongMessage contructs pong message from leader to node
func (pong PongMessageType) ConstructPongMessage() []byte {
var byteBuffer bytes.Buffer
encoder := gob.NewEncoder(&byteBuffer)
err := encoder.Encode(pong)
if err != nil {
log.Panic("Can't serialize Pong message", "error:", err)
return nil
}
return byteBuffer.Bytes()
}

@ -0,0 +1,64 @@
package node
import (
"fmt"
"strings"
"testing"
)
var (
p1 = PingMessageType{"127.0.0.1", "9999", "0x12345678901234567890"}
e1 = "127.0.0.1:9999/0x12345678901234567890"
p2 = PongMessageType{
[]string{
"0x1111111111111",
"0x2222222222222",
"0x3333333333333",
},
}
e2 = "# Keys: 3"
buf1 []byte
buf2 []byte
)
func TestString(test *testing.T) {
r1 := fmt.Sprintf("%v", p1)
if strings.Compare(r1, e1) != 0 {
test.Errorf("expect: %v, got: %v", e1, r1)
} else {
fmt.Printf("Ping:%v\n", p1)
}
r2 := fmt.Sprintf("%v", p2)
if strings.Compare(r2, e2) != 0 {
test.Errorf("expect: %v, got: %v", e2, r2)
} else {
fmt.Printf("Pong:%v\n", p2)
}
}
func TestSerialize(test *testing.T) {
buf1 = p1.ConstructPingMessage()
fmt.Printf("buf: %v\n", buf1)
buf2 = p2.ConstructPongMessage()
fmt.Printf("buf: %v\n", buf2)
}
func TestDeserialize(test *testing.T) {
ping, err := GetPingMessage(buf1)
if err != nil {
test.Error("Ping failed!")
}
fmt.Printf("Ping:%v\n", ping)
pong, err := GetPongMessage(buf2)
if err != nil {
test.Error("Pong failed!")
}
fmt.Printf("Pong:%v\n", pong)
}
Loading…
Cancel
Save