mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-05 07:37:14 +00:00
initial commit
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package main
|
||||
|
||||
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", &DummyApplication{})
|
||||
if err != nil {
|
||||
Exit(err.Error())
|
||||
}
|
||||
|
||||
// Wait forever
|
||||
TrapSignal(func() {
|
||||
// Cleanup
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
type DummyApplication struct {
|
||||
state merkle.Tree
|
||||
lastCommitState merkle.Tree
|
||||
}
|
||||
|
||||
func NewDummyApplication() *DummyApplication {
|
||||
state := merkle.NewIAVLTree(
|
||||
wire.BasicCodec,
|
||||
wire.BasicCodec,
|
||||
0,
|
||||
nil,
|
||||
)
|
||||
return &DummyApplication{
|
||||
state: state,
|
||||
lastCommitState: state,
|
||||
}
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) Echo(message string) (types.RetCode, string) {
|
||||
return 0, message
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) AppendTx(tx []byte) types.RetCode {
|
||||
dapp.state.Set(tx, tx)
|
||||
return 0
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) GetHash() ([]byte, types.RetCode) {
|
||||
hash := dapp.state.Hash()
|
||||
return hash, 0
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) Commit() types.RetCode {
|
||||
dapp.lastCommitState = dapp.state.Copy()
|
||||
return 0
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) Rollback() types.RetCode {
|
||||
dapp.state = dapp.lastCommitState.Copy()
|
||||
return 0
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) SetEventsMode(mode types.EventsMode) types.RetCode {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) AddListener(key string) types.RetCode {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) RemListener(key string) types.RetCode {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) GetEvents() []types.Event {
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/tendermint/go-common"
|
||||
"github.com/tendermint/go-wire"
|
||||
"github.com/tendermint/tmsp/server"
|
||||
"github.com/tendermint/tmsp/types"
|
||||
)
|
||||
|
||||
func TestStream(t *testing.T) {
|
||||
|
||||
// Start the listener
|
||||
_, err := server.StartListener("tcp://127.0.0.1:8080", NewDummyApplication())
|
||||
if err != nil {
|
||||
Exit(err.Error())
|
||||
}
|
||||
|
||||
// Connect to the socket
|
||||
conn, err := Connect("tcp://127.0.0.1:8080")
|
||||
if err != nil {
|
||||
Exit(err.Error())
|
||||
}
|
||||
|
||||
// Read response data
|
||||
go func() {
|
||||
for {
|
||||
var n int64
|
||||
var err error
|
||||
var res types.Response
|
||||
wire.ReadBinaryPtr(&res, conn, &n, &err)
|
||||
if err != nil {
|
||||
Exit(err.Error())
|
||||
}
|
||||
// fmt.Println("Read", n)
|
||||
}
|
||||
}()
|
||||
|
||||
// Write requests
|
||||
for {
|
||||
var n int64
|
||||
var err error
|
||||
var req types.Request = types.RequestAppendTx{TxBytes: []byte("test")}
|
||||
wire.WriteBinary(req, conn, &n, &err)
|
||||
if err != nil {
|
||||
Exit(err.Error())
|
||||
}
|
||||
// fmt.Println("Wrote", n)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user