Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "os"
- "io"
- "net"
- "http"
- "log"
- "rpc"
- "rpc/jsonrpc"
- )
- type Core struct {
- }
- func (c *Core) Shutdown(args *[]string, reply *bool) os.Error {
- log.Print("Shutdown(",args,")")
- *reply = false;
- return nil
- }
- func NewCore() *Core {
- return &Core{}
- }
- type mconn struct {
- net.Conn
- buf io.ReadCloser
- closeSync chan bool
- }
- func (c *mconn) Read(b []byte) (n int, err os.Error) {
- n,err = io.ReadFull(c.buf, b)
- log.Print("READ: ", string(b))
- if err == os.EOF {
- return c.Conn.Read(b)
- }
- return
- }
- func (c *mconn) Write(b []byte) (n int, err os.Error) {
- n, err = c.Conn.Write(b)
- log.Print("Written: ",string(b)) // not received by client
- return
- }
- func JsonRpcHandler(w http.ResponseWriter, r *http.Request) {
- switch r.Method {
- case "GET": {
- log.Print("GET!")
- }
- case "POST": {
- log.Print("POST!")
- conn, _, err := w.(http.Hijacker).Hijack()
- // Wrap conn so that the content of r.Body is returned by Read()
- conn = &mconn{
- buf: r.Body,
- Conn: conn}
- io.WriteString(conn, "HTTP/1.0 200 Connected to RPC\n\n")
- if err != nil {
- log.Print("rpc hijacking ", r.RemoteAddr, ": ",
- err.String())
- return
- }
- jsonrpc.ServeConn(conn)
- log.Print("End")
- }
- default: {
- log.Print("Default data arriving. Dunno?")
- }
- }
- }
- func main() {
- coreInstance := NewCore()
- rpc.Register(coreInstance)
- http.HandleFunc("/rpc", JsonRpcHandler)
- err := http.ListenAndServe(":1335", nil)
- if err != nil {
- log.Fatal("ListenAndServe failed: ", err.String())
- }
- }
Add Comment
Please, Sign In to add comment