Permalink
Browse files

Simple telnet interface

  • Loading branch information...
1 parent bb48066 commit 7d2a64491337b8abe2060e394ced71a137aaef0a @brendonh committed Feb 12, 2013
Showing with 379 additions and 21 deletions.
  1. +28 −0 argparser.go
  2. +20 −5 httprpc.go
  3. +13 −2 server.go
  4. +7 −1 service.go
  5. +3 −8 session.go
  6. +269 −0 telnet.go
  7. +14 −4 types.go
  8. +25 −1 websocket.go
View
@@ -15,6 +15,7 @@ func Parse(argspec []APIArg, args APIData) (
for _, arg := range argspec {
givenVal, ok := args[arg.Name]
+
if !ok {
if arg.Default != nil {
parsedArgs[arg.Name] = arg.Default
@@ -75,6 +76,32 @@ func convertArgVal(arg APIArg, val interface{}) (
return true, nil, int(i)
}
}
+
+ case UIntArg:
+ var uval int
+ switch val.(type) {
+ case int8:
+ uval = int(val.(int8))
+ case int16:
+ uval = int(val.(int16))
+ case int32:
+ uval = int(val.(int32))
+ case float64:
+ uval = int(val.(float64))
+ case string:
+ ival, err := strconv.ParseInt(val.(string), 10, 64)
+ if err != nil {
+ return false, nil, nil
+ }
+ uval = int(ival)
+ }
+ if uval < 0 {
+ var errors = list.New()
+ errors.PushBack("Must be >= 0")
+ return false, errors, nil
+ }
+ return true, nil, uval
+
case FloatArg:
return true, nil, val.(float64)
case StringArg:
@@ -93,6 +120,7 @@ func convertArgVal(arg APIArg, val interface{}) (
func stringArgType(argType int) string {
switch argType {
case IntArg: return "int"
+ case UIntArg: return "uint"
case FloatArg: return "float"
case StringArg: return "string"
case NestedArg: return "nested"
View
@@ -16,6 +16,7 @@ type HttpRpcEndpoint struct {
listener net.Listener
context ServerContext
resolver SessionResolver
+ logPrefix string
}
@@ -27,12 +28,23 @@ func NewHttpRpcEndpoint(address string, context ServerContext, resolver SessionR
Address: address,
context: context,
resolver: resolver,
+ logPrefix: "HTTP " + address,
}
}
+
+type HttpSessionConnection struct {
+}
+
+func (sessConn *HttpSessionConnection) Send(msg []byte) {
+ fmt.Printf("HTTP session send: %v\n", msg)
+}
+
+
func DefaultSessionResolver(req *http.Request, endpoint *HttpRpcEndpoint) (Session, error) {
- // XXX TODO: Session tracking
- return endpoint.context.CreateSession(endpoint), nil
+ // XXX TODO: Session tracking, sending
+ var sender = &HttpSessionConnection{}
+ return endpoint.context.CreateSession(sender), nil
}
func (endpoint *HttpRpcEndpoint) Start() bool {
@@ -42,7 +54,7 @@ func (endpoint *HttpRpcEndpoint) Start() bool {
listener, error := net.Listen("tcp", endpoint.Address)
if error != nil {
- fmt.Printf("Error starting HTTP RPC endpoint: %v\n", error)
+ endpoint.Log("Error starting HTTP RPC endpoint: %v", error)
return false
}
@@ -53,7 +65,7 @@ func (endpoint *HttpRpcEndpoint) Start() bool {
mux.Handle("/", endpoint)
go http.Serve(listener, mux)
- fmt.Printf("HTTP endpoint started at %s\n", endpoint.Address)
+ endpoint.Log("HTTP endpoint started at %s", endpoint.Address)
return true
}
@@ -65,14 +77,17 @@ func (endpoint *HttpRpcEndpoint) Stop() bool {
}
if error := endpoint.listener.Close(); error != nil {
- fmt.Printf("Error stopping HTTP RPC endpoint: %v\n", error)
+ endpoint.Log("Error stopping HTTP RPC endpoint: %v", error)
return false
}
endpoint.listener = nil
return true
}
+func (endpoint *HttpRpcEndpoint) Log(fmt string, args... interface{}) {
+ endpoint.context.LogPrefix(endpoint.logPrefix, fmt, args...)
+}
func (endpoint *HttpRpcEndpoint) ServeHTTP(response http.ResponseWriter, req *http.Request) {
bits := strings.SplitN(req.URL.Path[1:], "/", 2)
View
@@ -2,13 +2,15 @@ package goservice
import (
"os"
+ "log"
)
type Server struct {
services API
sessionCreator SessionCreator
endpoints []Endpoint
+ logger *log.Logger
stopper chan os.Signal
}
@@ -18,6 +20,7 @@ func NewServer(services API, sessionCreator SessionCreator) *Server {
services: services,
sessionCreator: sessionCreator,
endpoints: make([]Endpoint, 0),
+ logger: log.New(os.Stdout, "", log.LstdFlags),
}
}
@@ -46,6 +49,14 @@ func (server *Server) API() API {
return server.services
}
-func (server *Server) CreateSession(endpoint Endpoint) Session {
- return server.sessionCreator(endpoint)
+func (server *Server) CreateSession(conn SessionConnection) Session {
+ return server.sessionCreator(conn)
}
+
+func (server *Server) Log(format string, args... interface{}) {
+ server.LogPrefix("Server", format, args...)
+}
+
+func (server *Server) LogPrefix(prefix string, format string, args... interface{}) {
+ server.logger.Printf("[ %-20s ] " + format + "\n", append([]interface{} { prefix }, args...)...)
+}
View
@@ -51,6 +51,9 @@ func (service *Service) FindMethod(methodName string) *APIMethod {
return &method
}
+func (service *Service) GetMethods() map[string]APIMethod {
+ return service.Methods
+}
// ------------------------------------------
// Collections
@@ -66,6 +69,10 @@ var requestArgSpec = []APIArg {
APIArg{Name: "data", ArgType: RawArg},
}
+func (collection *ServiceCollection) GetServices() map[string]APIService {
+ return collection.Services
+}
+
func (collection ServiceCollection) HandleRequest(request APIData, session Session, context ServerContext) APIData {
ok, resolutionErrors, args := Parse(requestArgSpec, request)
@@ -78,7 +85,6 @@ func (collection ServiceCollection) HandleRequest(request APIData, session Sessi
args["method"].(string),
args["data"].(APIData),
session, context))
-
}
func (collection ServiceCollection) HandleCall(
View
@@ -8,20 +8,15 @@ import (
)
-func NewBasicSession(endpoint Endpoint) *BasicSession {
+func BasicSessionCreator(sessConn SessionConnection) Session {
return &BasicSession {
id: uuid.New(),
user: nil,
Mutex: new(sync.Mutex),
- endpoint: endpoint,
+ connection: sessConn,
}
}
-
-func BasicSessionCreator(endpoint Endpoint) Session {
- return NewBasicSession(endpoint)
-}
-
// ------------------------------------------
// Session API
// ------------------------------------------
@@ -40,5 +35,5 @@ func (session *BasicSession) SetUser(user User) {
}
func (session *BasicSession) Send(msg []byte) {
- fmt.Printf("Session send: %s: %v\n", session.id, msg)
+ session.connection.Send(msg)
}
Oops, something went wrong.

0 comments on commit 7d2a644

Please sign in to comment.