Guest User

Untitled

a guest
Jan 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "os"
  5. "io"
  6. "net"
  7. "http"
  8. "log"
  9. "rpc"
  10. "rpc/jsonrpc"
  11. )
  12.  
  13.  
  14.  
  15. type Core struct {
  16. }
  17.  
  18. func (c *Core) Shutdown(args *[]string, reply *bool) os.Error {
  19. log.Print("Shutdown(",args,")")
  20. *reply = false;
  21. return nil
  22. }
  23.  
  24. func NewCore() *Core {
  25. return &Core{}
  26. }
  27.  
  28.  
  29. type mconn struct {
  30. net.Conn
  31. buf io.ReadCloser
  32. closeSync chan bool
  33. }
  34.  
  35. func (c *mconn) Read(b []byte) (n int, err os.Error) {
  36.  
  37. n,err = io.ReadFull(c.buf, b)
  38.  
  39. log.Print("READ: ", string(b))
  40.  
  41. if err == os.EOF {
  42. return c.Conn.Read(b)
  43. }
  44.  
  45. return
  46. }
  47.  
  48. func (c *mconn) Write(b []byte) (n int, err os.Error) {
  49. n, err = c.Conn.Write(b)
  50.  
  51. log.Print("Written: ",string(b)) // not received by client
  52.  
  53. return
  54. }
  55.  
  56.  
  57.  
  58. func JsonRpcHandler(w http.ResponseWriter, r *http.Request) {
  59.  
  60. switch r.Method {
  61.  
  62. case "GET": {
  63. log.Print("GET!")
  64. }
  65.  
  66. case "POST": {
  67. log.Print("POST!")
  68.  
  69.  
  70. conn, _, err := w.(http.Hijacker).Hijack()
  71.  
  72. // Wrap conn so that the content of r.Body is returned by Read()
  73. conn = &mconn{
  74. buf: r.Body,
  75. Conn: conn}
  76.  
  77. io.WriteString(conn, "HTTP/1.0 200 Connected to RPC\n\n")
  78.  
  79. if err != nil {
  80. log.Print("rpc hijacking ", r.RemoteAddr, ": ",
  81. err.String())
  82. return
  83. }
  84.  
  85. jsonrpc.ServeConn(conn)
  86.  
  87. log.Print("End")
  88. }
  89.  
  90. default: {
  91. log.Print("Default data arriving. Dunno?")
  92. }
  93. }
  94.  
  95.  
  96. }
  97.  
  98.  
  99. func main() {
  100.  
  101. coreInstance := NewCore()
  102.  
  103. rpc.Register(coreInstance)
  104.  
  105. http.HandleFunc("/rpc", JsonRpcHandler)
  106.  
  107. err := http.ListenAndServe(":1335", nil)
  108.  
  109. if err != nil {
  110. log.Fatal("ListenAndServe failed: ", err.String())
  111. }
  112. }
Add Comment
Please, Sign In to add comment