Skip to content

Commit

Permalink
Remove channel
Browse files Browse the repository at this point in the history
  • Loading branch information
dix975 committed Aug 21, 2018
1 parent 1b5fa8b commit 5484e3c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
6 changes: 4 additions & 2 deletions cmd/eos-p2p-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ func peer(address string, chainID []byte) *p2p.Peer {

func main() {

chainID, err := hex.DecodeString("aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906")
chainID, err := hex.DecodeString("9bf6c5d3610260507f3a37340c43ff186c1810c984e9ad0b99b6fb8d6a3c94a3")
//chainID, err := hex.DecodeString("aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906")
if err != nil {
log.Fatal("Chain id:", err)
}

route := &p2p.Route{
Origin: peer("localhost:6789", chainID),
Origin: peer("localhost:9876", chainID),
//Origin: peer("35.203.0.168:9876", chainID),
Destination: peer("localhost:9875", chainID),
}

Expand Down
9 changes: 5 additions & 4 deletions cmd/freeze/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"fmt"

"github.com/eoscanada/eos-go"
"github.com/eoscanada/eos-go/p2p"
)

Expand All @@ -24,13 +23,13 @@ func peer(address string, chainID []byte) *p2p.Peer {
return p2p.NewPeer(originConnection)

}
func readMessage(connection *p2p.Connection, msgChannel chan *eos.P2PMessageEnvelope, errChannel chan error) {
func readMessage(connection *p2p.Connection) {
for {
msg, err := connection.Read()
if err != nil {
errChannel <- fmt.Errorf("read message from %s: %s", connection., err)
log.Fatal(fmt.Errorf("read message from: %s", err))
}
msgChannel <- msg
fmt.Println("MSG:", msg)
}
}

Expand All @@ -47,7 +46,9 @@ func main() {
HeadBlockTime: time.Now(),
}
peer := peer("localhost:9876", chainID)
go readMessage(peer.Connection)
peer.Connection.SendHandshake(dummyHandshakeInfo)
peer.Connection.SendSyncRequest(1, 10)
fmt.Println("Handshake sent!")

select {}
Expand Down
2 changes: 2 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func (e *Encoder) Encode(v interface{}) (err error) {
return e.writeName(name)
case string:
return e.writeString(cv)
case CompressionType:
return e.writeByte(uint8(cv))
case TransactionStatus:
return e.writeByte(uint8(cv))
case IDListMode:
Expand Down
27 changes: 10 additions & 17 deletions p2p/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ func (p *Proxy) registerHandler(handler Handler) {
p.handlers = append(p.handlers, handler)
}

func readMessage(connection *Connection, msgChannel chan *eos.P2PMessageEnvelope, errChannel chan error) {
func readMessage(connection *Connection, handle handle, errChannel chan error) {
for {
msg, err := connection.Read()
if err != nil {
errChannel <- fmt.Errorf("read message from %s: %s", connection.address, err)
}
msgChannel <- msg
err = handle(msg)
if err != nil {
errChannel <- err
}
}
}

Expand Down Expand Up @@ -139,7 +142,7 @@ func (p *Proxy) handleDefault(envelope *eos.P2PMessageEnvelope, peer *Peer) erro
case *eos.GoAwayMessage:
return fmt.Errorf("handling message: go away: reason [%d]", m.Reason)
default:
err := p.send(envelope.P2PMessage, peer)
_, err := peer.Connection.nodeConnection.Write(envelope.Raw)
if err != nil {
return fmt.Errorf("handleDefault: %s", err)
}
Expand Down Expand Up @@ -167,13 +170,13 @@ func (p *Proxy) triggerHandshake() error {
return p.route.Destination.Connection.SendHandshake(dummyHandshakeInfo)
}

type handle func(envelope *eos.P2PMessageEnvelope) error

func (p *Proxy) Start() {
originChannel := make(chan *eos.P2PMessageEnvelope)
destinationChannel := make(chan *eos.P2PMessageEnvelope)
errorChannel := make(chan error)

go readMessage(p.route.Origin.Connection, originChannel, errorChannel)
go readMessage(p.route.Destination.Connection, destinationChannel, errorChannel)
go readMessage(p.route.Origin.Connection, p.handleOrigin, errorChannel)
go readMessage(p.route.Destination.Connection, p.handleDestination, errorChannel)

err := p.triggerHandshake()
if err != nil {
Expand All @@ -182,16 +185,6 @@ func (p *Proxy) Start() {

for {
select {
case msg := <-originChannel:
err := p.handleOrigin(msg)
if err != nil {
log.Fatal("proxy: handle origin:", err)
}
case msg := <-destinationChannel:
err := p.handleDestination(msg)
if err != nil {
log.Fatal("proxy: handle destination:", err)
}
case err := <-errorChannel:
log.Fatal(err)
}
Expand Down

0 comments on commit 5484e3c

Please sign in to comment.