You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package server
  2. import (
  3. "bufio"
  4. "fmt"
  5. "net"
  6. "strings"
  7. . "github.com/tendermint/go-common"
  8. "github.com/tendermint/go-wire"
  9. "github.com/tendermint/tmsp/types"
  10. )
  11. func StartListener(protoAddr string, app types.Application) (net.Listener, error) {
  12. parts := strings.SplitN(protoAddr, "://", 2)
  13. proto, addr := parts[0], parts[1]
  14. ln, err := net.Listen(proto, addr)
  15. if err != nil {
  16. return nil, err
  17. }
  18. // A goroutine to accept a connection.
  19. go func() {
  20. for {
  21. // Accept a connection
  22. conn, err := ln.Accept()
  23. if err != nil {
  24. Exit("Failed to accept connection")
  25. } else {
  26. fmt.Println("Accepted a new connection")
  27. }
  28. connClosed := make(chan struct{}, 2) // Push to signal connection closed
  29. responses := make(chan types.Response, 1000) // A channel to buffer responses
  30. // Read requests from conn and deal with them
  31. go handleRequests(app, connClosed, conn, responses)
  32. // Pull responses from 'responses' and write them to conn.
  33. go handleResponses(connClosed, responses, conn)
  34. // Wait until connection is closed
  35. <-connClosed
  36. fmt.Println("Connection was closed. Waiting for new connection...")
  37. }
  38. }()
  39. return ln, nil
  40. }
  41. // Read requests from conn and deal with them
  42. func handleRequests(app types.Application, connClosed chan struct{}, conn net.Conn, responses chan<- types.Response) {
  43. var count int
  44. var bufReader = bufio.NewReader(conn)
  45. for {
  46. var n int
  47. var err error
  48. var req types.Request
  49. wire.ReadBinaryPtr(&req, bufReader, 0, &n, &err)
  50. if err != nil {
  51. fmt.Println("Error in handleRequests:", err.Error())
  52. connClosed <- struct{}{}
  53. return
  54. }
  55. count++
  56. handleRequest(app, req, responses)
  57. }
  58. }
  59. func handleRequest(app types.Application, req types.Request, responses chan<- types.Response) {
  60. switch req := req.(type) {
  61. case types.RequestEcho:
  62. msg := app.Echo(req.Message)
  63. responses <- types.ResponseEcho{msg}
  64. case types.RequestFlush:
  65. responses <- types.ResponseFlush{}
  66. case types.RequestInfo:
  67. data := app.Info()
  68. responses <- types.ResponseInfo{data}
  69. case types.RequestSetOption:
  70. retCode := app.SetOption(req.Key, req.Value)
  71. responses <- types.ResponseSetOption{retCode}
  72. case types.RequestAppendTx:
  73. events, retCode := app.AppendTx(req.TxBytes)
  74. responses <- types.ResponseAppendTx{retCode}
  75. for _, event := range events {
  76. responses <- types.ResponseEvent{event}
  77. }
  78. case types.RequestGetHash:
  79. hash, retCode := app.GetHash()
  80. responses <- types.ResponseGetHash{retCode, hash}
  81. case types.RequestCommit:
  82. retCode := app.Commit()
  83. responses <- types.ResponseCommit{retCode}
  84. case types.RequestRollback:
  85. retCode := app.Rollback()
  86. responses <- types.ResponseRollback{retCode}
  87. case types.RequestAddListener:
  88. retCode := app.AddListener(req.EventKey)
  89. responses <- types.ResponseAddListener{retCode}
  90. case types.RequestRemListener:
  91. retCode := app.RemListener(req.EventKey)
  92. responses <- types.ResponseRemListener{retCode}
  93. default:
  94. responses <- types.ResponseException{"Unknown request"}
  95. }
  96. }
  97. // Pull responses from 'responses' and write them to conn.
  98. func handleResponses(connClosed chan struct{}, responses <-chan types.Response, conn net.Conn) {
  99. var count int
  100. var bufWriter = bufio.NewWriter(conn)
  101. for {
  102. var res = <-responses
  103. var n int
  104. var err error
  105. wire.WriteBinary(res, bufWriter, &n, &err)
  106. if err != nil {
  107. fmt.Println(err.Error())
  108. connClosed <- struct{}{}
  109. return
  110. }
  111. if _, ok := res.(types.ResponseFlush); ok {
  112. err = bufWriter.Flush()
  113. if err != nil {
  114. fmt.Println(err.Error())
  115. connClosed <- struct{}{}
  116. return
  117. }
  118. }
  119. count++
  120. }
  121. }