From 2de72d26cfcdced09e22fa7df925c671fb5c046a Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Fri, 27 Nov 2015 18:42:00 -0500 Subject: [PATCH 1/3] refactor dummy; flush tmsp reqs --- cmd/dummy/main.go | 22 ++++++++++++++++++++++ cmd/{ => tmsp}/cli.go | 7 +++++++ example/{main.go => dummy.go} | 20 +------------------- example/{main_test.go => dummy_test.go} | 2 +- 4 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 cmd/dummy/main.go rename cmd/{ => tmsp}/cli.go (96%) rename example/{main.go => dummy.go} (79%) rename example/{main_test.go => dummy_test.go} (98%) diff --git a/cmd/dummy/main.go b/cmd/dummy/main.go new file mode 100644 index 000000000..af2e1fa88 --- /dev/null +++ b/cmd/dummy/main.go @@ -0,0 +1,22 @@ +package main + +import ( + . "github.com/tendermint/go-common" + "github.com/tendermint/tmsp/example" + "github.com/tendermint/tmsp/server" +) + +func main() { + + // Start the listener + _, err := server.StartListener("tcp://127.0.0.1:8080", example.NewDummyApplication()) + if err != nil { + Exit(err.Error()) + } + + // Wait forever + TrapSignal(func() { + // Cleanup + }) + +} diff --git a/cmd/cli.go b/cmd/tmsp/cli.go similarity index 96% rename from cmd/cli.go rename to cmd/tmsp/cli.go index 6ff3109a7..1d1359c22 100644 --- a/cmd/cli.go +++ b/cmd/tmsp/cli.go @@ -121,6 +121,13 @@ func write(conn net.Conn, req types.Request) (types.Response, error) { if err != nil { return nil, err } + + // flush! + wire.WriteBinary(types.RequestFlush{}, conn, &n, &err) + if err != nil { + return nil, err + } + var res types.Response wire.ReadBinaryPtr(&res, conn, 0, &n, &err) return res, err diff --git a/example/main.go b/example/dummy.go similarity index 79% rename from example/main.go rename to example/dummy.go index 828ccb1ab..b3e9ae43c 100644 --- a/example/main.go +++ b/example/dummy.go @@ -1,30 +1,12 @@ -package main +package example import ( . "github.com/tendermint/go-common" "github.com/tendermint/go-merkle" "github.com/tendermint/go-wire" - "github.com/tendermint/tmsp/server" "github.com/tendermint/tmsp/types" ) -func main() { - - // Start the listener - _, err := server.StartListener("tcp://127.0.0.1:8080", NewDummyApplication()) - if err != nil { - Exit(err.Error()) - } - - // Wait forever - TrapSignal(func() { - // Cleanup - }) - -} - -//-------------------------------------------------------------------------------- - type DummyApplication struct { state merkle.Tree lastCommitState merkle.Tree diff --git a/example/main_test.go b/example/dummy_test.go similarity index 98% rename from example/main_test.go rename to example/dummy_test.go index a54752405..d0a40d878 100644 --- a/example/main_test.go +++ b/example/dummy_test.go @@ -1,4 +1,4 @@ -package main +package example import ( // "fmt" From c21c2ed69b243553dcff8da1d45b84d5f4a5621d Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Sun, 29 Nov 2015 03:43:49 -0500 Subject: [PATCH 2/3] counter example --- example/counter.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 example/counter.go diff --git a/example/counter.go b/example/counter.go new file mode 100644 index 000000000..f52997323 --- /dev/null +++ b/example/counter.go @@ -0,0 +1,69 @@ +package example + +import ( + "encoding/binary" + "fmt" + + . "github.com/tendermint/go-common" + "github.com/tendermint/tmsp/types" +) + +type CounterApplication struct { + hashCount int + lastHashCount int + + txCount int + lastTxCount int + + commitCount int +} + +func NewCounterApplication() *CounterApplication { + return &CounterApplication{} +} + +func (dapp *CounterApplication) Echo(message string) string { + return message +} + +func (dapp *CounterApplication) Info() []string { + return []string{Fmt("hash, tx, commit counts:%d, %d, %d", dapp.hashCount, dapp.txCount, dapp.commitCount)} +} + +func (dapp *CounterApplication) SetOption(key string, value string) types.RetCode { + return 0 +} + +func (dapp *CounterApplication) AppendTx(tx []byte) ([]types.Event, types.RetCode) { + dapp.txCount += 1 + return nil, 0 +} + +func (dapp *CounterApplication) GetHash() ([]byte, types.RetCode) { + fmt.Println("getting hash!") + hash := make([]byte, 32) + binary.PutVarint(hash, int64(dapp.hashCount)) + dapp.hashCount += 1 + return hash, 0 +} + +func (dapp *CounterApplication) Commit() types.RetCode { + dapp.lastHashCount = dapp.hashCount + dapp.lastTxCount = dapp.txCount + dapp.commitCount += 1 + return 0 +} + +func (dapp *CounterApplication) Rollback() types.RetCode { + dapp.hashCount = dapp.lastHashCount + dapp.txCount = dapp.lastTxCount + return 0 +} + +func (dapp *CounterApplication) AddListener(key string) types.RetCode { + return 0 +} + +func (dapp *CounterApplication) RemListener(key string) types.RetCode { + return 0 +} From fe782cb8acfb62d45bacf81a83024ce578e7ead8 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Sun, 29 Nov 2015 03:44:08 -0500 Subject: [PATCH 3/3] server: allow multiple connections --- example/counter.go | 2 -- server/server.go | 16 +++++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/example/counter.go b/example/counter.go index f52997323..1962cd91f 100644 --- a/example/counter.go +++ b/example/counter.go @@ -2,7 +2,6 @@ package example import ( "encoding/binary" - "fmt" . "github.com/tendermint/go-common" "github.com/tendermint/tmsp/types" @@ -40,7 +39,6 @@ func (dapp *CounterApplication) AppendTx(tx []byte) ([]types.Event, types.RetCod } func (dapp *CounterApplication) GetHash() ([]byte, types.RetCode) { - fmt.Println("getting hash!") hash := make([]byte, 32) binary.PutVarint(hash, int64(dapp.hashCount)) dapp.hashCount += 1 diff --git a/server/server.go b/server/server.go index f7e187381..075e3e66c 100644 --- a/server/server.go +++ b/server/server.go @@ -11,6 +11,8 @@ import ( "github.com/tendermint/tmsp/types" ) +var maxNumberConnections = 2 + func StartListener(protoAddr string, app types.Application) (net.Listener, error) { parts := strings.SplitN(protoAddr, "://", 2) proto, addr := parts[0], parts[1] @@ -22,7 +24,11 @@ func StartListener(protoAddr string, app types.Application) (net.Listener, error // A goroutine to accept a connection. go func() { + semaphore := make(chan struct{}, maxNumberConnections) + for { + semaphore <- struct{}{} + // Accept a connection conn, err := ln.Accept() if err != nil { @@ -38,9 +44,13 @@ func StartListener(protoAddr string, app types.Application) (net.Listener, error // Pull responses from 'responses' and write them to conn. go handleResponses(connClosed, responses, conn) - // Wait until connection is closed - <-connClosed - fmt.Println("Connection was closed. Waiting for new connection...") + go func() { + // Wait until connection is closed + <-connClosed + fmt.Println("Connection was closed. Waiting for new connection...") + + <-semaphore + }() } }()