mirror of
https://github.com/fluencelabs/tendermint
synced 2025-07-31 20:21:56 +00:00
Merge pull request #2 from tendermint/cli
refactor dummy; flush tmsp reqs
This commit is contained in:
22
cmd/dummy/main.go
Normal file
22
cmd/dummy/main.go
Normal file
@@ -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
|
||||
})
|
||||
|
||||
}
|
@@ -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
|
67
example/counter.go
Normal file
67
example/counter.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
. "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) {
|
||||
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
|
||||
}
|
@@ -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
|
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package example
|
||||
|
||||
import (
|
||||
// "fmt"
|
@@ -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
|
||||
}()
|
||||
}
|
||||
|
||||
}()
|
||||
|
Reference in New Issue
Block a user