Documentation
¶
Overview ¶
Armie
Async-RMI and Eventing framework.
Package armie provides a framework for async, symmetric RMI and Eventing. Connections facilitate both Requests (with responses) and Events (no response). Requests are issued asynchronously, and a single connection can have any number of outstanding requests (a map of request-ids is kept in order to route responses). Issuing a request returns a Future that can be used to acquire a response.
Example:
type Person struct { Name string Age int } func sayHello(person *Person) int { fmt.Printf("%s says 'Hello'.\n", person.Name) return person.Age } var handleReq armie.RequestHandler = func(request *armie.Request, response *armie.Response) { switch request.Method { case "HELLO": age, err := request.CallMethod(sayHello) if err != nil { response.Error(err.Error()) } else { response.Send(age) } } } var handleEvt armie.EventHandler = func(event *armie.Event) { switch event.Event { case "ARRIVED": var name string event.Decode(&name) fmt.Printf("%s has arrived.\n", name) } } func main() { s := armie.NewTCPServer(os.Stdout) // ConnectionHandler should wire up handlers on each // new connection to the server s.OnConnection(func(conn *armie.Conn) error { conn.OnRequest(handleReq) conn.OnEvent(handleEvt) return nil }) err := s.Listen(":9999") if err != nil { fmt.Printf("Error setting up server: %s", err.Error()) } // The client connection can optionally accept a connectionHandler, // or Request / Event Handlers can be wired up later with OnRequest // and OnEvent. The former method is necessary if you expect a request // or event to be waiting on the wire immediately. conn, err := armie.NewTCPConnection(":9999", os.Stdout, nil) if err != nil { fmt.Printf("Error connecting: %s", err.Error()) } joe := Person{ Name: "Joe", Age: 30, } conn.SendEvent("ARRIVED", "Joe") res, err := conn.SendRequest("HELLO", &joe) var joesAge int res.GetResult(&joesAge) fmt.Printf("Joe is %d.\n", joesAge) }
Output:
Joe has arrived. Joe says 'Hello'. Joe is 30.
Index ¶
Constants ¶
const ( REQUEST = iota + 1 RESPONSE EVENT )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn struct { Alive bool // contains filtered or unexported fields }
Conn is Symmetric. Both server and client can register RequestHandlers and EventHandlers, and both can SendEvent() and SendRequest().
func NewTCPConnection ¶
func (*Conn) Close ¶
Close the connection. Close will not return until the goroutine reading events and requests exits.
func (*Conn) OnRequest ¶
func (c *Conn) OnRequest(handler RequestHandler)
Register a request handler.
type ConnectionHandler ¶
type EventHandler ¶
type EventHandler func(event *Event)
type Future ¶
type Future struct {
// contains filtered or unexported fields
}
RPC future for awaiting responses to RMI requests
type Request ¶
An RMI request containing encoded arguments and a method name
func (*Request) CallMethod ¶
Decode request arguments to match the method args, and call the method. The method can return an error and, at most one other object, which will in-turn be returned by CallMethod.
type RequestHandler ¶
type Response ¶
type Response struct { Id uint64 ErrString string Result interface{} // contains filtered or unexported fields }
Response is passed to a RequestHandler to allow the RequestHandler to send a result or an error.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server provides a Listen(addr) method for accepting new connections. Register a ConnectionHandler with OnConnection() and use the ConnectionHandler to wire up a Request and / or Event handler, and do any other housekeeping required before requests and events are accepted.
func NewTCPServer ¶
func (*Server) Listen ¶
Listen at the given address for new connections. Address will be passed unmodified down to the underlying transport.
func (*Server) OnConnection ¶
func (serv *Server) OnConnection(handler ConnectionHandler)
Use the ConnectionHandler to wire up a Request and / or Event handler, and do any other housekeeping required before requests and events are accepted.