Browse Source

Merge pull request #2 from tendermint/cli

refactor dummy; flush tmsp reqs
pull/1780/head
Jae Kwon 9 years ago
parent
commit
8b9df7d685
6 changed files with 111 additions and 23 deletions
  1. +22
    -0
      cmd/dummy/main.go
  2. +7
    -0
      cmd/tmsp/cli.go
  3. +67
    -0
      example/counter.go
  4. +1
    -19
      example/dummy.go
  5. +1
    -1
      example/dummy_test.go
  6. +13
    -3
      server/server.go

+ 22
- 0
cmd/dummy/main.go View 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
})
}

cmd/cli.go → cmd/tmsp/cli.go View File


+ 67
- 0
example/counter.go View 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
}

example/main.go → example/dummy.go View File


example/main_test.go → example/dummy_test.go View File


+ 13
- 3
server/server.go View File

@ -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
}()
}
}()


Loading…
Cancel
Save