libchanner
This package makes it simple to plug into the power of the libchan RPC library. Elegant simplicity is achieved by providing a set of API functions which perform the repetitive server and client setup.
The ability to stop the server at any time is also included out of the box thanks to stoppableListener.
Get it
go get -u https://github.com/jaytaylor/libchanner/...
Important
This package only works with libchan v0.1.x series.
Code layout
Server resources are in chan_server.go
Client resources are in conn.go
Example usage
See example/main.go for the fully working example.
run it:
go run example/main.go
output:
Successfully decoded time struct! value=2015-04-17 11:34:01.046453821 -0700 PDT
Abreviated highlights:
(for demonstration purposes only, error checking omitted for brevity)
type (
RemoteRequest struct {
Command string
StatusChan libchan.Sender
}
RemoteResponse struct {
StatusCode int
Message string
Data []byte
}
)
func main() {
cs := libchanner.NewChanServer("127.0.0.1:8001", receiverHandler)
cs.Start()
defer func() {
cs.Stop()
}()
ch, _ := libchanner.DialChan("tcp", "127.0.0.1:8001")
req := &RemoteRequest{
Command: "CurrentTime",
StatusChan: ch.RemoteSender,
}
ch.Sender.Send(req)
response := &RemoteResponse{}
ch.Receiver.Receive(response)
}
func receiverHandler(receiver libchan.Receiver) {
for {
req := &RemoteRequest{}
if err := receiver.Receive(req); err != nil {
panic(fmt.Sprintf("unexpected error receiving remote struct: %s", err))
break
}
response := requestHandler(req)
if err := req.StatusChan.Send(response); err != nil {
panic(fmt.Sprintf("unexpected error sending result: %s", err))
}
}
}
func requestHandler(rr *RemoteRequest) *RemoteResponse {
response := &RemoteResponse{}
switch rr.Command {
case "CurrentTime":
data, err := json.Marshal(time.Now())
if err != nil {
response.StatusCode = 1
response.Message = err.Error()
break
}
response.Data = data
default:
response.StatusCode = 1
response.Message = "unknown command"
}
return response
}
also see the unit-tests for additional example usage.
Unit-tests
Running the unit-tests is straightforward and standard:
go test
License
Permissive MIT license.
Contact
You are more than welcome to open issues and send pull requests if you find a bug or want a new feature.
If you appreciate this library please feel free to drop me a line and let me know! It's always nice to hear from people who have benefitted from my work.
Email: jay at (my github username).com
Twitter: @jtaylor
Related work