2018-09-04 22:55:52 +00:00
|
|
|
|
package transport
|
|
|
|
|
|
2018-09-06 12:28:54 +00:00
|
|
|
|
import (
|
2018-09-24 15:14:38 +00:00
|
|
|
|
"crypto/tls"
|
2023-09-19 02:01:38 +00:00
|
|
|
|
"gitea.watsonlabs.net/watsonb8/fayec/message"
|
2018-09-11 11:30:07 +00:00
|
|
|
|
"net/http"
|
2018-09-06 12:28:54 +00:00
|
|
|
|
"time"
|
|
|
|
|
)
|
2018-09-04 22:55:52 +00:00
|
|
|
|
|
2023-09-19 01:58:15 +00:00
|
|
|
|
// Options represents the connection options to be used by a transport
|
2018-09-04 22:55:52 +00:00
|
|
|
|
type Options struct {
|
2018-09-11 11:30:07 +00:00
|
|
|
|
Headers http.Header
|
2018-09-24 14:13:57 +00:00
|
|
|
|
Cookies http.CookieJar
|
2018-09-24 15:14:38 +00:00
|
|
|
|
TLS *tls.Config
|
2018-09-11 11:30:07 +00:00
|
|
|
|
|
2018-09-24 14:48:38 +00:00
|
|
|
|
MaxRetries int
|
2018-09-11 11:30:07 +00:00
|
|
|
|
RetryInterval time.Duration
|
|
|
|
|
DialDeadline time.Duration
|
|
|
|
|
ReadDeadline time.Duration
|
|
|
|
|
WriteDeadline time.Duration
|
2018-09-04 22:55:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-19 01:58:15 +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-24 14:13:57 +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-11 10:09:30 +00:00
|
|
|
|
Init(endpoint string, 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-24 14:13:57 +00:00
|
|
|
|
Handshake(msg *message.Message) (*message.Message, error)
|
|
|
|
|
//Init is called after a client has discovered the server’s capabilities with a handshake exchange,
|
2018-09-06 14:31:05 +00:00
|
|
|
|
//a connection is established by sending a message to the /meta/connect channel
|
2018-09-24 14:13:57 +00:00
|
|
|
|
Connect(msg *message.Message) error
|
2018-09-06 14:31:05 +00:00
|
|
|
|
//Disconnect closes all subscriptions and inform the server to remove any client-related state.
|
2018-09-07 12:32:26 +00:00
|
|
|
|
//any subsequent method call to the transport object will result in undefined behaviour.
|
2018-09-24 14:13:57 +00:00
|
|
|
|
Disconnect(msg *message.Message) error
|
|
|
|
|
//SendMessage sens a message through the transport
|
|
|
|
|
SendMessage(msg *message.Message) error
|
2018-09-06 13:29:49 +00:00
|
|
|
|
|
2018-09-24 14:13:57 +00:00
|
|
|
|
SetOnMessageReceivedHandler(onMsg func(msg *message.Message))
|
2018-09-06 13:29:49 +00:00
|
|
|
|
|
2018-09-24 14:13:57 +00:00
|
|
|
|
//SetOnTransportUpHandler is called when the transport is connected
|
|
|
|
|
SetOnTransportUpHandler(callback func())
|
2018-09-05 13:17:11 +00:00
|
|
|
|
|
2018-09-24 14:13:57 +00:00
|
|
|
|
//SetOnTransportDownHandler is called when the transport goes down
|
|
|
|
|
SetOnTransportDownHandler(callback func(error))
|
2018-09-06 13:29:49 +00:00
|
|
|
|
|
2018-09-24 14:13:57 +00:00
|
|
|
|
//handled by dispatcher
|
|
|
|
|
SetOnErrorHandler(onError func(err error))
|
2018-09-06 13:29:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
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]
|
|
|
|
|
}
|