fayec/transport/transport.go

102 lines
2.9 KiB
Go
Raw Normal View History

2018-09-04 22:55:52 +00:00
package transport
import (
"github.com/thesyncim/faye/message"
"time"
)
2018-09-04 22:55:52 +00:00
// handshake, connect, disconnect, subscribe, unsubscribe and publish
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
}
type Transport interface {
Name() string
Init(options *Options) error
Options() *Options
Handshake() error
Connect() error
Disconnect() error
Subscribe(subscription string, onMessage func(message message.Data)) error
2018-09-04 22:55:52 +00:00
Unsubscribe(subscription string) error
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 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"
)
type EventMessage = int
const (
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]
}