Skip to content

Instantly share code, notes, and snippets.

@crazy2be
Created April 17, 2011 04:00
Show Gist options
  • Save crazy2be/923746 to your computer and use it in GitHub Desktop.
Save crazy2be/923746 to your computer and use it in GitHub Desktop.
package main
import (
"os"
"io"
"fmt"
"rpc"
"rpc/jsonrpc"
)
type ReadWriter struct {
io.ReadCloser
io.WriteCloser
}
func (rw *ReadWriter) Close() os.Error {
rw.ReadCloser.Close()
rw.WriteCloser.Close()
return nil
}
// Defined for the RPC package
type Module int
// Starts the module. ret is never changed.
func (m *Module) Start(name *string, ret *int) os.Error {
fmt.Println("Starting module:", *name)
*ret = 12390
err := os.NewError("testing blar")
return err
}
func (m *Module) Stop(name *string, ret *int) os.Error {
fmt.Println("Stopping module:", *name)
*ret = 394028
return nil
}
func main() {
sinpipe, coutpipe := io.Pipe()
cinpipe, soutpipe := io.Pipe()
rwc := new(ReadWriter)
rwc.ReadCloser = sinpipe
rwc.WriteCloser = soutpipe
serv := rpc.NewServer()
codec := jsonrpc.NewServerCodec(rwc)
fmt.Println("Made RPC server")
m := new(Module)
serv.Register(m)
fmt.Println("Registered module service")
go serv.ServeCodec(codec)
go sendTestData(coutpipe)
io.Copy(os.Stdout, cinpipe)
}
func sendTestData(w io.Writer) {
fmtstring := "{\"Method\": \"Module.%s\",\"Params\": [\"events\"], \"Id\": %d}"
fmt.Fprintf(w, fmtstring, "Start", 1)
fmt.Fprintf(w, fmtstring, "Stop", 2)
}
func monitorPipe(file *os.File) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment