postmaster

package module
v0.0.0-...-756ca65 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2014 License: GPL-2.0 Imports: 15 Imported by: 0

README

Postmaster

General purpose wamp messaging server

  • Pass messages following WAMP protocol
  • Support for server side message intercept
  • Support for server events OnAuthenticated and OnDisconnect per connection
  • Scalable (Initial work to make multi-instance aware)

##Getting Started

Here is an example server that can be made with Postmaster.

Features of this example:

  • Authentication
  • Callbacks
  • Unauthenticated RPC
  • Authenticated RPC
import (
    "postmaster"
    "code.google.com/p/go.net/websocket"
)

func main(){
    server := postmaster.NewServer()

	//Assign auth callbacks
	server.GetAuthSecret = lookupUser
	server.GetAuthPermissions = getUserPremissions
	server.OnAuthenticated = userConnected
	server.OnDisconnect = clientDisconnected

	server.MessageToPublish = interceptMessage

	//Unauth rpc
	server.RegisterUnauthRPC(baseURL+"helloWorld",helloWorld)
	
	//Setup Authenticated RPC Functions
	server.RegisterRPC(baseURL+"helloWorldAuth",helloWorld)

    s := websocket.Server{Handler: postmaster.HandleWebsocket(server), Handshake: nil}
	http.Handle("/", s)

	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.Fatal("ListenAndServe:", err)
	}
}

/*
    TODO: Define all of the callback functions

*/

##Callbacks

###Authentication

//Get the authentication secret for an authentication key, i.e. the user password for the user name. Return "" when the authentication key does not exist.
GetAuthSecret	func(authKey string)(string,error) // Required
//Get the permissions the session is granted when the authentication succeeds for the given key / extra information.
GetAuthPermissions func(authKey string,authExtra map[string]interface{})(Permissions,error) // Required
//Fired when client authentication was successful.
OnAuthenticated func(authKey string,authExtra map[string]interface{}, permission Permissions) // Optional

###Server Intercept

//Message interept
MessageToPublish PublishIntercept // Optional
//Fired when authenticated client disconnections
OnDisconnect func(authKey string,authExtra map[string]interface{}) //Optional

Documentation

Index

Constants

View Source
const (
	POSTMASTER_VERSION   = "0.2.0"
	POSTMASTER_SERVER_ID = "postmaster-" + POSTMASTER_VERSION
)
View Source
const ALLOWED_BACKLOG = 6
View Source
const PROTOCOL_VERSION = 1
View Source
const WAMP_BASE_URL = "http://api.wamp.ws/"

Auth: wamp cra

View Source
const WAMP_PROCEDURE_URL = WAMP_BASE_URL + "procedure#"

Variables

View Source
var (
	ErrInvalidURI          = &WAMPError{"invalid URI"}
	ErrInvalidNumArgs      = &WAMPError{"invalid number of arguments in message"}
	ErrUnsupportedProtocol = &WAMPError{"unsupported protocol"}
)

Functions

func HandleWebsocket

func HandleWebsocket(t Handler) func(*websocket.Conn)

HandleWebsocket is a Go1.0 shim for method values

Types

type CallErrorMsg

type CallErrorMsg struct {
	CallID       string
	ErrorURI     string
	ErrorDesc    string
	ErrorDetails interface{}
}

func (*CallErrorMsg) MarshalJSON

func (msg *CallErrorMsg) MarshalJSON() ([]byte, error)

func (*CallErrorMsg) UnmarshalJSON

func (msg *CallErrorMsg) UnmarshalJSON(jsonData []byte) error

type CallMsg

type CallMsg struct {
	CallID   string
	ProcURI  string
	CallArgs []interface{}
}

func (*CallMsg) MarshalJSON

func (msg *CallMsg) MarshalJSON() ([]byte, error)

func (*CallMsg) UnmarshalJSON

func (msg *CallMsg) UnmarshalJSON(jsonData []byte) error

type CallResultMsg

type CallResultMsg struct {
	CallID string
	Result interface{}
}

func (*CallResultMsg) MarshalJSON

func (msg *CallResultMsg) MarshalJSON() ([]byte, error)

func (*CallResultMsg) UnmarshalJSON

func (msg *CallResultMsg) UnmarshalJSON(jsonData []byte) error

type Connection

type Connection struct {
	Username string
	P        *Permissions //Permission for this client
	// contains filtered or unexported fields
}

type ConnectionID

type ConnectionID string

type EventMsg

type EventMsg struct {
	TopicURI string
	Event    interface{}
}

func (*EventMsg) MarshalJSON

func (msg *EventMsg) MarshalJSON() ([]byte, error)

func (*EventMsg) UnmarshalJSON

func (msg *EventMsg) UnmarshalJSON(jsonData []byte) error

type Handler

type Handler interface {
	HandleWebsocket(*websocket.Conn)
}

type MessageType

type MessageType int
const (
	WELCOME MessageType = iota
	PREFIX
	CALL
	CALLRESULT
	CALLERROR
	SUBSCRIBE
	UNSUBSCRIBE
	PUBLISH
	EVENT
)

type PendingAuth

type PendingAuth struct {
	// contains filtered or unexported fields
}

type Permissions

type Permissions struct {
	RPC    map[string]RPCPermission    //maps uri to RPCPermission
	PubSub map[string]PubSubPermission //maps uri to PubSubPermission
}

type PubSubPermission

type PubSubPermission struct {
	CanPublish   bool
	CanSubscribe bool
}

type PublishIntercept

type PublishIntercept func(id *Connection, msg PublishMsg) bool

type PublishMsg

type PublishMsg struct {
	TopicURI     string
	Event        interface{}
	ExcludeMe    bool
	ExcludeList  []string
	EligibleList []string
}

func (*PublishMsg) MarshalJSON

func (msg *PublishMsg) MarshalJSON() ([]byte, error)

func (*PublishMsg) UnmarshalJSON

func (msg *PublishMsg) UnmarshalJSON(jsonData []byte) error

type RPCError

type RPCError struct {
	URI         string
	Description string
	Details     interface{}
}

type RPCHandler

type RPCHandler func(*Connection, string, ...interface{}) (interface{}, *RPCError)

type RPCPermission

type RPCPermission bool

type Server

type Server struct {

	//Get the authentication secret for an authentication key, i.e. the user password for the user name. Return "" when the authentication key does not exist.
	GetAuthSecret func(authKey string) (string, error) // Required

	//Get the permissions the session is granted when the authentication succeeds for the given key / extra information.
	GetAuthPermissions func(authKey string, authExtra map[string]interface{}) (Permissions, error) // Required

	//Fired when client authentication was successful.
	OnAuthenticated func(authKey string, authExtra map[string]interface{}, permission Permissions) // Optional

	//Message interept
	MessageToPublish PublishIntercept // Optional

	//Fired when authenticated client disconnections
	OnDisconnect func(authKey string, authExtra map[string]interface{}) //Optional
	// contains filtered or unexported fields
}

Represents data storage per instance

func NewServer

func NewServer() *Server

func (*Server) HandleWebsocket

func (t *Server) HandleWebsocket(conn *websocket.Conn)

Starting point of websocket connection

  • Verify identity
  • Register send/recieve channel
  • Manage send/recieve for duration of connection

func (*Server) PublishEvent

func (t *Server) PublishEvent(uri string, msg interface{})

Publish event outside of normal client->client structure

func (*Server) RegisterRPC

func (t *Server) RegisterRPC(uri string, f RPCHandler)

func (*Server) RegisterUnauthRPC

func (t *Server) RegisterUnauthRPC(uri string, f RPCHandler)

func (*Server) UnregisterRPC

func (t *Server) UnregisterRPC(uri string)

func (*Server) UnregisterUnauthRPC

func (t *Server) UnregisterUnauthRPC(uri string)

type SubscribeMsg

type SubscribeMsg struct {
	TopicURI string
}

func (*SubscribeMsg) MarshalJSON

func (msg *SubscribeMsg) MarshalJSON() ([]byte, error)

func (*SubscribeMsg) UnmarshalJSON

func (msg *SubscribeMsg) UnmarshalJSON(jsonData []byte) error

type UnsubscribeMsg

type UnsubscribeMsg struct {
	TopicURI string
}

func (*UnsubscribeMsg) MarshalJSON

func (msg *UnsubscribeMsg) MarshalJSON() ([]byte, error)

func (*UnsubscribeMsg) UnmarshalJSON

func (msg *UnsubscribeMsg) UnmarshalJSON(jsonData []byte) error

type WAMPError

type WAMPError struct {
	Msg string
}

func (*WAMPError) Error

func (e *WAMPError) Error() string

type WelcomeMsg

type WelcomeMsg struct {
	SessionId       string
	ProtocolVersion int
	ServerIdent     string
}

func (*WelcomeMsg) MarshalJSON

func (msg *WelcomeMsg) MarshalJSON() ([]byte, error)

func (*WelcomeMsg) UnmarshalJSON

func (msg *WelcomeMsg) UnmarshalJSON(jsonData []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL