fayec/transport/transport.go

120 lines
4.3 KiB
Go
Raw Normal View History

2018-09-04 22:55:52 +00:00
package transport
import (
"github.com/thesyncim/faye/message"
"github.com/thesyncim/faye/subscription"
"time"
)
2018-09-04 22:55:52 +00:00
2018-09-06 14:31:05 +00:00
//Options represents the connection options to be used by a transport
2018-09-04 22:55:52 +00:00
type Options struct {
Url string
RetryInterval time.Duration
InExt []message.Extension
OutExt []message.Extension
2018-09-04 22:55:52 +00:00
//todo dial timeout
//todo read/write deadline
}
2018-09-06 14:31:05 +00:00
//Transport represents the transport to be used to comunicate with the faye server
2018-09-04 22:55:52 +00:00
type Transport interface {
2018-09-06 14:31:05 +00:00
//Name returns the transport name
2018-09-04 22:55:52 +00:00
Name() string
2018-09-06 14:31:05 +00:00
//Init initializes the transport with the provided options
2018-09-04 22:55:52 +00:00
Init(options *Options) error
2018-09-06 14:31:05 +00:00
//Options return the transport Options
2018-09-04 22:55:52 +00:00
Options() *Options
2018-09-06 14:31:05 +00:00
//Handshake initiates a connection negotiation by sending a message to the /meta/handshake channel.
2018-09-04 22:55:52 +00:00
Handshake() error
2018-09-06 14:31:05 +00:00
//Connect is called after a client has discovered the servers capabilities with a handshake exchange,
//a connection is established by sending a message to the /meta/connect channel
2018-09-04 22:55:52 +00:00
Connect() error
2018-09-06 14:31:05 +00:00
//Disconnect closes all subscriptions and inform the server to remove any client-related state.
//any subsequent method call to the transport object will result in undefined behaviour.
Disconnect() error
2018-09-06 14:31:05 +00:00
//Subscribe informs the server that messages published to that channel are delivered to itself.
Subscribe(channel string) (*subscription.Subscription, error)
2018-09-06 14:31:05 +00:00
//Unsubscribe informs the server that the client will no longer listen to incoming event messages on
//the specified channel/subscription
Unsubscribe(sub *subscription.Subscription) error
2018-09-06 14:31:05 +00:00
//Publish publishes events on a channel by sending event messages, the server MAY respond to a publish event
//if this feature is supported by the server use the OnPublishResponse to get the publish status.
2018-09-06 13:29:49 +00:00
Publish(subscription string, message message.Data) (id string, err error)
//OnPublishResponse sets the handler to be triggered if the server replies to the publish request
//according to the spec the server MAY reply to the publish request, so its not guaranteed that this handler will
//ever be triggered
//can be used to identify the status of the published request and for example retry failed published requests
OnPublishResponse(subscription string, onMsg func(message *message.Message))
2018-09-04 22:55:52 +00:00
}
2018-09-06 14:31:05 +00:00
//MetaMessage are channels commencing with the /meta/ segment ans, are the channels used by the faye protocol itself.
2018-09-06 13:29:49 +00:00
type MetaMessage = string
const (
2018-09-06 13:29:49 +00:00
MetaSubscribe MetaMessage = "/meta/subscribe"
MetaConnect MetaMessage = "/meta/connect"
MetaDisconnect MetaMessage = "/meta/disconnect"
MetaUnsubscribe MetaMessage = "/meta/unsubscribe"
MetaHandshake MetaMessage = "/meta/handshake"
)
2018-09-06 14:31:05 +00:00
//EventMessage are published in event messages sent from a faye client to a faye server
//and are delivered in event messages sent from a faye server to a faye client.
2018-09-06 13:29:49 +00:00
type EventMessage = int
const (
2018-09-06 14:31:05 +00:00
//
2018-09-06 13:29:49 +00:00
EventPublish EventMessage = iota
EventDelivery
)
type Reconnect = string
2018-09-04 22:55:52 +00:00
const (
//ReconnectRetry indicates that a client MAY attempt to reconnect with a /meta/connect message,
//after the interval (as defined by interval advice field or client-default backoff), and with the same credentials.
ReconnectRetry Reconnect = "retry"
//ReconnectHandshake indicates that the server has terminated any prior connection status and the client MUST reconnect
// with a /meta/handshake message.
//A client MUST NOT automatically retry when a reconnect advice handshake has been received.
ReconnectHandshake Reconnect = "handshake"
//ReconnectNone indicates a hard failure for the connect attempt.
//A client MUST respect reconnect advice none and MUST NOT automatically retry or handshake.
ReconnectNone Reconnect = "none"
2018-09-04 22:55:52 +00:00
)
2018-09-06 13:29:49 +00:00
var metaMessages = []MetaMessage{MetaSubscribe, MetaConnect, MetaUnsubscribe, MetaHandshake, MetaDisconnect}
2018-09-05 13:17:11 +00:00
2018-09-06 13:29:49 +00:00
func IsMetaMessage(msg *message.Message) bool {
for i := range metaMessages {
if msg.Channel == metaMessages[i] {
2018-09-05 13:17:11 +00:00
return true
}
}
return false
}
2018-09-06 13:29:49 +00:00
func IsEventDelivery(msg *message.Message) bool {
if msg.Data != nil {
return true
}
return false
}
func IsEventPublish(msg *message.Message) bool {
return !IsEventDelivery(msg)
}
2018-09-04 22:55:52 +00:00
var registeredTransports = map[string]Transport{}
func RegisterTransport(t Transport) {
registeredTransports[t.Name()] = t //todo validate
}
func GetTransport(name string) Transport {
return registeredTransports[name]
}