mirror of
https://github.com/minio/minio.git
synced 2024-12-24 22:25:54 -05:00
Add support for MQTT server as a notification target (#4474)
This implementation is similar to AMQP notifications: * Notifications are published on a single topic as a JSON feed * Topic is configurable, as is the QoS. Uses the paho.mqtt.golang library for the mqtt connection, and supports connections over tcp and websockets, with optional secure tls support. * Additionally the minio server configuration has been bumped up so mqtt configuration can be added. * Configuration migration code is added with tests. MQTT is an ISO standard M2M/IoT messaging protocol and was originally designed for applications for limited bandwidth networks. Today it's use is growing in the IoT space.
This commit is contained in:
parent
af8071c86a
commit
8293f546af
@ -144,6 +144,9 @@ func isValidQueueID(queueARN string) bool {
|
|||||||
if isAMQPQueue(sqsARN) { // AMQP eueue.
|
if isAMQPQueue(sqsARN) { // AMQP eueue.
|
||||||
amqpN := serverConfig.Notify.GetAMQPByID(sqsARN.AccountID)
|
amqpN := serverConfig.Notify.GetAMQPByID(sqsARN.AccountID)
|
||||||
return amqpN.Enable && amqpN.URL != ""
|
return amqpN.Enable && amqpN.URL != ""
|
||||||
|
} else if isMQTTQueue(sqsARN) {
|
||||||
|
mqttN := serverConfig.Notify.GetMQTTByID(sqsARN.AccountID)
|
||||||
|
return mqttN.Enable && mqttN.Broker != ""
|
||||||
} else if isNATSQueue(sqsARN) {
|
} else if isNATSQueue(sqsARN) {
|
||||||
natsN := serverConfig.Notify.GetNATSByID(sqsARN.AccountID)
|
natsN := serverConfig.Notify.GetNATSByID(sqsARN.AccountID)
|
||||||
return natsN.Enable && natsN.Address != ""
|
return natsN.Enable && natsN.Address != ""
|
||||||
@ -251,6 +254,7 @@ func validateNotificationConfig(nConfig notificationConfig) APIErrorCode {
|
|||||||
// Unmarshals input value of AWS ARN format into minioSqs object.
|
// Unmarshals input value of AWS ARN format into minioSqs object.
|
||||||
// Returned value represents minio sqs types, currently supported are
|
// Returned value represents minio sqs types, currently supported are
|
||||||
// - amqp
|
// - amqp
|
||||||
|
// - mqtt
|
||||||
// - nats
|
// - nats
|
||||||
// - elasticsearch
|
// - elasticsearch
|
||||||
// - redis
|
// - redis
|
||||||
@ -273,6 +277,8 @@ func unmarshalSqsARN(queueARN string) (mSqs arnSQS) {
|
|||||||
switch sqsType {
|
switch sqsType {
|
||||||
case queueTypeAMQP:
|
case queueTypeAMQP:
|
||||||
mSqs.Type = queueTypeAMQP
|
mSqs.Type = queueTypeAMQP
|
||||||
|
case queueTypeMQTT:
|
||||||
|
mSqs.Type = queueTypeMQTT
|
||||||
case queueTypeNATS:
|
case queueTypeNATS:
|
||||||
mSqs.Type = queueTypeNATS
|
mSqs.Type = queueTypeNATS
|
||||||
case queueTypeElastic:
|
case queueTypeElastic:
|
||||||
|
@ -358,6 +358,11 @@ func TestUnmarshalSQSARN(t *testing.T) {
|
|||||||
queueARN: "arn:minio:sqs:us-east-1:1:amqp",
|
queueARN: "arn:minio:sqs:us-east-1:1:amqp",
|
||||||
Type: "amqp",
|
Type: "amqp",
|
||||||
},
|
},
|
||||||
|
// Valid mqtt queue arn.
|
||||||
|
{
|
||||||
|
queueARN: "arn:minio:sqs:us-east-1:1:mqtt",
|
||||||
|
Type: "mqtt",
|
||||||
|
},
|
||||||
// Invalid empty queue arn.
|
// Invalid empty queue arn.
|
||||||
{
|
{
|
||||||
queueARN: "",
|
queueARN: "",
|
||||||
|
@ -141,7 +141,13 @@ func migrateConfig() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fallthrough
|
fallthrough
|
||||||
case v18:
|
case "18":
|
||||||
|
// Migrate version '17' to '18'.
|
||||||
|
if err = migrateV18ToV19(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
case v19:
|
||||||
// No migration needed. this always points to current version.
|
// No migration needed. this always points to current version.
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
@ -1366,3 +1372,109 @@ func migrateV17ToV18() error {
|
|||||||
log.Printf(configMigrateMSGTemplate, configFile, cv17.Version, srvConfig.Version)
|
log.Printf(configMigrateMSGTemplate, configFile, cv17.Version, srvConfig.Version)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func migrateV18ToV19() error {
|
||||||
|
configFile := getConfigFile()
|
||||||
|
|
||||||
|
cv18 := &serverConfigV18{}
|
||||||
|
_, err := quick.Load(configFile, cv18)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return nil
|
||||||
|
} else if err != nil {
|
||||||
|
return fmt.Errorf("Unable to load config version ‘18’. %v", err)
|
||||||
|
}
|
||||||
|
if cv18.Version != "18" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy over fields from V18 into V19 config struct
|
||||||
|
srvConfig := &serverConfigV18{
|
||||||
|
Logger: &loggers{},
|
||||||
|
Notify: ¬ifier{},
|
||||||
|
}
|
||||||
|
srvConfig.Version = "19"
|
||||||
|
srvConfig.Credential = cv18.Credential
|
||||||
|
srvConfig.Region = cv18.Region
|
||||||
|
if srvConfig.Region == "" {
|
||||||
|
// Region needs to be set for AWS Signature Version 4.
|
||||||
|
srvConfig.Region = globalMinioDefaultRegion
|
||||||
|
}
|
||||||
|
|
||||||
|
srvConfig.Logger.Console = cv18.Logger.Console
|
||||||
|
srvConfig.Logger.File = cv18.Logger.File
|
||||||
|
|
||||||
|
// check and set notifiers config
|
||||||
|
if len(cv18.Notify.AMQP) == 0 {
|
||||||
|
srvConfig.Notify.AMQP = make(map[string]amqpNotify)
|
||||||
|
srvConfig.Notify.AMQP["1"] = amqpNotify{}
|
||||||
|
} else {
|
||||||
|
// New deliveryMode parameter is added for AMQP,
|
||||||
|
// default value is already 0, so nothing to
|
||||||
|
// explicitly migrate here.
|
||||||
|
srvConfig.Notify.AMQP = cv18.Notify.AMQP
|
||||||
|
}
|
||||||
|
if len(cv18.Notify.ElasticSearch) == 0 {
|
||||||
|
srvConfig.Notify.ElasticSearch = make(map[string]elasticSearchNotify)
|
||||||
|
srvConfig.Notify.ElasticSearch["1"] = elasticSearchNotify{
|
||||||
|
Format: formatNamespace,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
srvConfig.Notify.ElasticSearch = cv18.Notify.ElasticSearch
|
||||||
|
}
|
||||||
|
if len(cv18.Notify.Redis) == 0 {
|
||||||
|
srvConfig.Notify.Redis = make(map[string]redisNotify)
|
||||||
|
srvConfig.Notify.Redis["1"] = redisNotify{
|
||||||
|
Format: formatNamespace,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
srvConfig.Notify.Redis = cv18.Notify.Redis
|
||||||
|
}
|
||||||
|
if len(cv18.Notify.PostgreSQL) == 0 {
|
||||||
|
srvConfig.Notify.PostgreSQL = make(map[string]postgreSQLNotify)
|
||||||
|
srvConfig.Notify.PostgreSQL["1"] = postgreSQLNotify{
|
||||||
|
Format: formatNamespace,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
srvConfig.Notify.PostgreSQL = cv18.Notify.PostgreSQL
|
||||||
|
}
|
||||||
|
if len(cv18.Notify.Kafka) == 0 {
|
||||||
|
srvConfig.Notify.Kafka = make(map[string]kafkaNotify)
|
||||||
|
srvConfig.Notify.Kafka["1"] = kafkaNotify{}
|
||||||
|
} else {
|
||||||
|
srvConfig.Notify.Kafka = cv18.Notify.Kafka
|
||||||
|
}
|
||||||
|
if len(cv18.Notify.NATS) == 0 {
|
||||||
|
srvConfig.Notify.NATS = make(map[string]natsNotify)
|
||||||
|
srvConfig.Notify.NATS["1"] = natsNotify{}
|
||||||
|
} else {
|
||||||
|
srvConfig.Notify.NATS = cv18.Notify.NATS
|
||||||
|
}
|
||||||
|
if len(cv18.Notify.Webhook) == 0 {
|
||||||
|
srvConfig.Notify.Webhook = make(map[string]webhookNotify)
|
||||||
|
srvConfig.Notify.Webhook["1"] = webhookNotify{}
|
||||||
|
} else {
|
||||||
|
srvConfig.Notify.Webhook = cv18.Notify.Webhook
|
||||||
|
}
|
||||||
|
if len(cv18.Notify.MySQL) == 0 {
|
||||||
|
srvConfig.Notify.MySQL = make(map[string]mySQLNotify)
|
||||||
|
srvConfig.Notify.MySQL["1"] = mySQLNotify{
|
||||||
|
Format: formatNamespace,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
srvConfig.Notify.MySQL = cv18.Notify.MySQL
|
||||||
|
}
|
||||||
|
|
||||||
|
// V18 will not have mqtt support, so we add that here.
|
||||||
|
srvConfig.Notify.MQTT = make(map[string]mqttNotify)
|
||||||
|
srvConfig.Notify.MQTT["1"] = mqttNotify{}
|
||||||
|
|
||||||
|
// Load browser config from existing config in the file.
|
||||||
|
srvConfig.Browser = cv18.Browser
|
||||||
|
|
||||||
|
if err = quick.Save(configFile, srvConfig); err != nil {
|
||||||
|
return fmt.Errorf("Failed to migrate config from ‘%s’ to ‘%s’. %v", cv18.Version, srvConfig.Version, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf(configMigrateMSGTemplate, configFile, cv18.Version, srvConfig.Version)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -122,11 +122,14 @@ func TestServerConfigMigrateInexistentConfig(t *testing.T) {
|
|||||||
if err := migrateV17ToV18(); err != nil {
|
if err := migrateV17ToV18(); err != nil {
|
||||||
t.Fatal("migrate v17 to v18 should succeed when no config file is found")
|
t.Fatal("migrate v17 to v18 should succeed when no config file is found")
|
||||||
}
|
}
|
||||||
|
if err := migrateV18ToV19(); err != nil {
|
||||||
|
t.Fatal("migrate v18 to v19 should succeed when no config file is found")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test if a config migration from v2 to v18 is successfully done
|
// Test if a config migration from v2 to v19 is successfully done
|
||||||
func TestServerConfigMigrateV2toV18(t *testing.T) {
|
func TestServerConfigMigrateV2toV19(t *testing.T) {
|
||||||
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Init Test config failed")
|
t.Fatalf("Init Test config failed")
|
||||||
@ -166,7 +169,7 @@ func TestServerConfigMigrateV2toV18(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check the version number in the upgraded config file
|
// Check the version number in the upgraded config file
|
||||||
expectedVersion := v18
|
expectedVersion := v19
|
||||||
if serverConfig.Version != expectedVersion {
|
if serverConfig.Version != expectedVersion {
|
||||||
t.Fatalf("Expect version "+expectedVersion+", found: %v", serverConfig.Version)
|
t.Fatalf("Expect version "+expectedVersion+", found: %v", serverConfig.Version)
|
||||||
}
|
}
|
||||||
@ -246,6 +249,9 @@ func TestServerConfigMigrateFaultyConfig(t *testing.T) {
|
|||||||
if err := migrateV17ToV18(); err == nil {
|
if err := migrateV17ToV18(); err == nil {
|
||||||
t.Fatal("migrateConfigV17ToV18() should fail with a corrupted json")
|
t.Fatal("migrateConfigV17ToV18() should fail with a corrupted json")
|
||||||
}
|
}
|
||||||
|
if err := migrateV18ToV19(); err == nil {
|
||||||
|
t.Fatal("migrateConfigV18ToV19() should fail with a corrupted json")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test if all migrate code returns error with corrupted config files
|
// Test if all migrate code returns error with corrupted config files
|
||||||
|
@ -449,3 +449,22 @@ type serverConfigV17 struct {
|
|||||||
// Notification queue configuration.
|
// Notification queue configuration.
|
||||||
Notify *notifier `json:"notify"`
|
Notify *notifier `json:"notify"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// serverConfigV18 server configuration version '18' which is like
|
||||||
|
// version '17' except it adds support for "deliveryMode" parameter in
|
||||||
|
// the AMQP notification target.
|
||||||
|
type serverConfigV18 struct {
|
||||||
|
sync.RWMutex
|
||||||
|
Version string `json:"version"`
|
||||||
|
|
||||||
|
// S3 API configuration.
|
||||||
|
Credential credential `json:"credential"`
|
||||||
|
Region string `json:"region"`
|
||||||
|
Browser BrowserFlag `json:"browser"`
|
||||||
|
|
||||||
|
// Additional error logging configuration.
|
||||||
|
Logger *loggers `json:"logger"`
|
||||||
|
|
||||||
|
// Notification queue configuration.
|
||||||
|
Notify *notifier `json:"notify"`
|
||||||
|
}
|
||||||
|
@ -27,18 +27,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Config version
|
// Config version
|
||||||
const v18 = "18"
|
const v19 = "19"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// serverConfig server config.
|
// serverConfig server config.
|
||||||
serverConfig *serverConfigV18
|
serverConfig *serverConfigV19
|
||||||
serverConfigMu sync.RWMutex
|
serverConfigMu sync.RWMutex
|
||||||
)
|
)
|
||||||
|
|
||||||
// serverConfigV18 server configuration version '18' which is like
|
// serverConfigV19 server configuration version '19' which is like
|
||||||
// version '17' except it adds support for "deliveryMode" parameter in
|
// version '18' except it adds support for MQTT notifications.
|
||||||
// the AMQP notification target.
|
type serverConfigV19 struct {
|
||||||
type serverConfigV18 struct {
|
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
|
|
||||||
@ -55,7 +54,7 @@ type serverConfigV18 struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetVersion get current config version.
|
// GetVersion get current config version.
|
||||||
func (s *serverConfigV18) GetVersion() string {
|
func (s *serverConfigV19) GetVersion() string {
|
||||||
s.RLock()
|
s.RLock()
|
||||||
defer s.RUnlock()
|
defer s.RUnlock()
|
||||||
|
|
||||||
@ -63,7 +62,7 @@ func (s *serverConfigV18) GetVersion() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetRegion set new region.
|
// SetRegion set new region.
|
||||||
func (s *serverConfigV18) SetRegion(region string) {
|
func (s *serverConfigV19) SetRegion(region string) {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
defer s.Unlock()
|
defer s.Unlock()
|
||||||
|
|
||||||
@ -71,7 +70,7 @@ func (s *serverConfigV18) SetRegion(region string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetRegion get current region.
|
// GetRegion get current region.
|
||||||
func (s *serverConfigV18) GetRegion() string {
|
func (s *serverConfigV19) GetRegion() string {
|
||||||
s.RLock()
|
s.RLock()
|
||||||
defer s.RUnlock()
|
defer s.RUnlock()
|
||||||
|
|
||||||
@ -79,7 +78,7 @@ func (s *serverConfigV18) GetRegion() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetCredentials set new credentials.
|
// SetCredentials set new credentials.
|
||||||
func (s *serverConfigV18) SetCredential(creds credential) {
|
func (s *serverConfigV19) SetCredential(creds credential) {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
defer s.Unlock()
|
defer s.Unlock()
|
||||||
|
|
||||||
@ -88,7 +87,7 @@ func (s *serverConfigV18) SetCredential(creds credential) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetCredentials get current credentials.
|
// GetCredentials get current credentials.
|
||||||
func (s *serverConfigV18) GetCredential() credential {
|
func (s *serverConfigV19) GetCredential() credential {
|
||||||
s.RLock()
|
s.RLock()
|
||||||
defer s.RUnlock()
|
defer s.RUnlock()
|
||||||
|
|
||||||
@ -96,7 +95,7 @@ func (s *serverConfigV18) GetCredential() credential {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetBrowser set if browser is enabled.
|
// SetBrowser set if browser is enabled.
|
||||||
func (s *serverConfigV18) SetBrowser(b bool) {
|
func (s *serverConfigV19) SetBrowser(b bool) {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
defer s.Unlock()
|
defer s.Unlock()
|
||||||
|
|
||||||
@ -105,7 +104,7 @@ func (s *serverConfigV18) SetBrowser(b bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetCredentials get current credentials.
|
// GetCredentials get current credentials.
|
||||||
func (s *serverConfigV18) GetBrowser() bool {
|
func (s *serverConfigV19) GetBrowser() bool {
|
||||||
s.RLock()
|
s.RLock()
|
||||||
defer s.RUnlock()
|
defer s.RUnlock()
|
||||||
|
|
||||||
@ -113,7 +112,7 @@ func (s *serverConfigV18) GetBrowser() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Save config.
|
// Save config.
|
||||||
func (s *serverConfigV18) Save() error {
|
func (s *serverConfigV19) Save() error {
|
||||||
s.RLock()
|
s.RLock()
|
||||||
defer s.RUnlock()
|
defer s.RUnlock()
|
||||||
|
|
||||||
@ -121,9 +120,9 @@ func (s *serverConfigV18) Save() error {
|
|||||||
return quick.Save(getConfigFile(), s)
|
return quick.Save(getConfigFile(), s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newServerConfigV18() *serverConfigV18 {
|
func newServerConfigV19() *serverConfigV19 {
|
||||||
srvCfg := &serverConfigV18{
|
srvCfg := &serverConfigV19{
|
||||||
Version: v18,
|
Version: v19,
|
||||||
Credential: mustGetNewCredential(),
|
Credential: mustGetNewCredential(),
|
||||||
Region: globalMinioDefaultRegion,
|
Region: globalMinioDefaultRegion,
|
||||||
Browser: true,
|
Browser: true,
|
||||||
@ -137,6 +136,8 @@ func newServerConfigV18() *serverConfigV18 {
|
|||||||
// Make sure to initialize notification configs.
|
// Make sure to initialize notification configs.
|
||||||
srvCfg.Notify.AMQP = make(map[string]amqpNotify)
|
srvCfg.Notify.AMQP = make(map[string]amqpNotify)
|
||||||
srvCfg.Notify.AMQP["1"] = amqpNotify{}
|
srvCfg.Notify.AMQP["1"] = amqpNotify{}
|
||||||
|
srvCfg.Notify.MQTT = make(map[string]mqttNotify)
|
||||||
|
srvCfg.Notify.MQTT["1"] = mqttNotify{}
|
||||||
srvCfg.Notify.ElasticSearch = make(map[string]elasticSearchNotify)
|
srvCfg.Notify.ElasticSearch = make(map[string]elasticSearchNotify)
|
||||||
srvCfg.Notify.ElasticSearch["1"] = elasticSearchNotify{}
|
srvCfg.Notify.ElasticSearch["1"] = elasticSearchNotify{}
|
||||||
srvCfg.Notify.Redis = make(map[string]redisNotify)
|
srvCfg.Notify.Redis = make(map[string]redisNotify)
|
||||||
@ -159,7 +160,7 @@ func newServerConfigV18() *serverConfigV18 {
|
|||||||
// found, otherwise use default parameters
|
// found, otherwise use default parameters
|
||||||
func newConfig() error {
|
func newConfig() error {
|
||||||
// Initialize server config.
|
// Initialize server config.
|
||||||
srvCfg := newServerConfigV18()
|
srvCfg := newServerConfigV19()
|
||||||
|
|
||||||
// If env is set override the credentials from config file.
|
// If env is set override the credentials from config file.
|
||||||
if globalIsEnvCreds {
|
if globalIsEnvCreds {
|
||||||
@ -237,8 +238,8 @@ func checkDupJSONKeys(json string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getValidConfig - returns valid server configuration
|
// getValidConfig - returns valid server configuration
|
||||||
func getValidConfig() (*serverConfigV18, error) {
|
func getValidConfig() (*serverConfigV19, error) {
|
||||||
srvCfg := &serverConfigV18{
|
srvCfg := &serverConfigV19{
|
||||||
Region: globalMinioDefaultRegion,
|
Region: globalMinioDefaultRegion,
|
||||||
Browser: true,
|
Browser: true,
|
||||||
}
|
}
|
||||||
@ -248,8 +249,8 @@ func getValidConfig() (*serverConfigV18, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if srvCfg.Version != v18 {
|
if srvCfg.Version != v19 {
|
||||||
return nil, fmt.Errorf("configuration version mismatch. Expected: ‘%s’, Got: ‘%s’", v18, srvCfg.Version)
|
return nil, fmt.Errorf("configuration version mismatch. Expected: ‘%s’, Got: ‘%s’", v19, srvCfg.Version)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load config file json and check for duplication json keys
|
// Load config file json and check for duplication json keys
|
@ -80,13 +80,21 @@ func TestServerConfig(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set new console logger.
|
// Set new console logger.
|
||||||
// Set new Webhook notification id.
|
// Set new MySQL notification id.
|
||||||
serverConfig.Notify.SetMySQLByID("2", mySQLNotify{})
|
serverConfig.Notify.SetMySQLByID("2", mySQLNotify{})
|
||||||
savedNotifyCfg6 := serverConfig.Notify.GetMySQLByID("2")
|
savedNotifyCfg6 := serverConfig.Notify.GetMySQLByID("2")
|
||||||
if !reflect.DeepEqual(savedNotifyCfg6, mySQLNotify{}) {
|
if !reflect.DeepEqual(savedNotifyCfg6, mySQLNotify{}) {
|
||||||
t.Errorf("Expecting Webhook config %#v found %#v", mySQLNotify{}, savedNotifyCfg6)
|
t.Errorf("Expecting Webhook config %#v found %#v", mySQLNotify{}, savedNotifyCfg6)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set new console logger.
|
||||||
|
// Set new MQTT notification id.
|
||||||
|
serverConfig.Notify.SetMQTTByID("2", mqttNotify{})
|
||||||
|
savedNotifyCfg7 := serverConfig.Notify.GetMQTTByID("2")
|
||||||
|
if !reflect.DeepEqual(savedNotifyCfg7, mqttNotify{}) {
|
||||||
|
t.Errorf("Expecting Webhook config %#v found %#v", mqttNotify{}, savedNotifyCfg7)
|
||||||
|
}
|
||||||
|
|
||||||
consoleLogger := NewConsoleLogger()
|
consoleLogger := NewConsoleLogger()
|
||||||
serverConfig.Logger.SetConsole(consoleLogger)
|
serverConfig.Logger.SetConsole(consoleLogger)
|
||||||
consoleCfg := serverConfig.Logger.GetConsole()
|
consoleCfg := serverConfig.Logger.GetConsole()
|
||||||
@ -109,8 +117,8 @@ func TestServerConfig(t *testing.T) {
|
|||||||
serverConfig.Logger.SetFile(fileLogger)
|
serverConfig.Logger.SetFile(fileLogger)
|
||||||
|
|
||||||
// Match version.
|
// Match version.
|
||||||
if serverConfig.GetVersion() != v18 {
|
if serverConfig.GetVersion() != v19 {
|
||||||
t.Errorf("Expecting version %s found %s", serverConfig.GetVersion(), v18)
|
t.Errorf("Expecting version %s found %s", serverConfig.GetVersion(), v19)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to save.
|
// Attempt to save.
|
||||||
@ -223,7 +231,7 @@ func TestValidateConfig(t *testing.T) {
|
|||||||
|
|
||||||
configPath := filepath.Join(rootPath, minioConfigFile)
|
configPath := filepath.Join(rootPath, minioConfigFile)
|
||||||
|
|
||||||
v := v18
|
v := v19
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
configData string
|
configData string
|
||||||
@ -309,6 +317,9 @@ func TestValidateConfig(t *testing.T) {
|
|||||||
|
|
||||||
// Test 28 - Test valid Format for Redis
|
// Test 28 - Test valid Format for Redis
|
||||||
{`{"version": "` + v + `", "credential": { "accessKey": "minio", "secretKey": "minio123" }, "region": "us-east-1", "browser": "on", "notify": { "redis": { "1": { "enable": true, "format": "namespace", "address": "example.com:80", "password": "xxx", "key": "key1" } }}}`, true},
|
{`{"version": "` + v + `", "credential": { "accessKey": "minio", "secretKey": "minio123" }, "region": "us-east-1", "browser": "on", "notify": { "redis": { "1": { "enable": true, "format": "namespace", "address": "example.com:80", "password": "xxx", "key": "key1" } }}}`, true},
|
||||||
|
|
||||||
|
// Test 29 - Test MQTT
|
||||||
|
{`{"version": "` + v + `", "credential": { "accessKey": "minio", "secretKey": "minio123" }, "region": "us-east-1", "browser": "on", "notify": { "mqtt": { "1": { "enable": true, "broker": "", "topic": "", "qos": 0, "clientId": "", "username": "", "password": ""}}}}`, false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, testCase := range testCases {
|
for i, testCase := range testCases {
|
@ -609,6 +609,25 @@ func loadAllQueueTargets() (map[string]*logrus.Logger, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load all mqtt targets, initialize their respective loggers.
|
||||||
|
for accountID, mqttN := range serverConfig.Notify.GetMQTT() {
|
||||||
|
if !mqttN.Enable {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if queueARN, err := addQueueTarget(queueTargets, accountID, queueTypeMQTT, newMQTTNotify); err != nil {
|
||||||
|
if _, ok := err.(net.Error); ok {
|
||||||
|
err = &net.OpError{
|
||||||
|
Op: "Connecting to " + queueARN,
|
||||||
|
Net: "tcp",
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Load all nats targets, initialize their respective loggers.
|
// Load all nats targets, initialize their respective loggers.
|
||||||
for accountID, natsN := range serverConfig.Notify.GetNATS() {
|
for accountID, natsN := range serverConfig.Notify.GetNATS() {
|
||||||
if !natsN.Enable {
|
if !natsN.Enable {
|
||||||
|
@ -184,7 +184,7 @@ func newGatewayLayer(backendType gatewayBackend, endpoint, accessKey, secretKey
|
|||||||
// only used in memory.
|
// only used in memory.
|
||||||
func newGatewayConfig(accessKey, secretKey, region string) error {
|
func newGatewayConfig(accessKey, secretKey, region string) error {
|
||||||
// Initialize server config.
|
// Initialize server config.
|
||||||
srvCfg := newServerConfigV18()
|
srvCfg := newServerConfigV19()
|
||||||
|
|
||||||
// If env is set for a fresh start, save them to config file.
|
// If env is set for a fresh start, save them to config file.
|
||||||
srvCfg.SetCredential(credential{
|
srvCfg.SetCredential(credential{
|
||||||
|
@ -32,6 +32,7 @@ type notifier struct {
|
|||||||
Kafka kafkaConfigs `json:"kafka"`
|
Kafka kafkaConfigs `json:"kafka"`
|
||||||
Webhook webhookConfigs `json:"webhook"`
|
Webhook webhookConfigs `json:"webhook"`
|
||||||
MySQL mySQLConfigs `json:"mysql"`
|
MySQL mySQLConfigs `json:"mysql"`
|
||||||
|
MQTT mqttConfigs `json:"mqtt"`
|
||||||
// Add new notification queues.
|
// Add new notification queues.
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,6 +55,25 @@ func (a amqpConfigs) Validate() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type mqttConfigs map[string]mqttNotify
|
||||||
|
|
||||||
|
func (a mqttConfigs) Clone() mqttConfigs {
|
||||||
|
a2 := make(mqttConfigs, len(a))
|
||||||
|
for k, v := range a {
|
||||||
|
a2[k] = v
|
||||||
|
}
|
||||||
|
return a2
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a mqttConfigs) Validate() error {
|
||||||
|
for k, v := range a {
|
||||||
|
if err := v.Validate(); err != nil {
|
||||||
|
return fmt.Errorf("MQTT [%s] configuration invalid: %s", k, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type natsConfigs map[string]natsNotify
|
type natsConfigs map[string]natsNotify
|
||||||
|
|
||||||
func (a natsConfigs) Clone() natsConfigs {
|
func (a natsConfigs) Clone() natsConfigs {
|
||||||
@ -215,6 +235,9 @@ func (n *notifier) Validate() error {
|
|||||||
if err := n.MySQL.Validate(); err != nil {
|
if err := n.MySQL.Validate(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := n.MQTT.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,6 +259,24 @@ func (n *notifier) GetAMQPByID(accountID string) amqpNotify {
|
|||||||
return n.AMQP[accountID]
|
return n.AMQP[accountID]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *notifier) SetMQTTByID(accountID string, mqttn mqttNotify) {
|
||||||
|
n.Lock()
|
||||||
|
defer n.Unlock()
|
||||||
|
n.MQTT[accountID] = mqttn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *notifier) GetMQTT() map[string]mqttNotify {
|
||||||
|
n.RLock()
|
||||||
|
defer n.RUnlock()
|
||||||
|
return n.MQTT.Clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *notifier) GetMQTTByID(accountID string) mqttNotify {
|
||||||
|
n.RLock()
|
||||||
|
defer n.RUnlock()
|
||||||
|
return n.MQTT[accountID]
|
||||||
|
}
|
||||||
|
|
||||||
func (n *notifier) SetNATSByID(accountID string, natsn natsNotify) {
|
func (n *notifier) SetNATSByID(accountID string, natsn natsNotify) {
|
||||||
n.Lock()
|
n.Lock()
|
||||||
defer n.Unlock()
|
defer n.Unlock()
|
||||||
|
@ -30,6 +30,8 @@ const (
|
|||||||
|
|
||||||
// Static string indicating queue type 'amqp'.
|
// Static string indicating queue type 'amqp'.
|
||||||
queueTypeAMQP = "amqp"
|
queueTypeAMQP = "amqp"
|
||||||
|
// Static string indicating queue type 'mqtt'.
|
||||||
|
queueTypeMQTT = "mqtt"
|
||||||
// Static string indicating queue type 'nats'.
|
// Static string indicating queue type 'nats'.
|
||||||
queueTypeNATS = "nats"
|
queueTypeNATS = "nats"
|
||||||
// Static string indicating queue type 'elasticsearch'.
|
// Static string indicating queue type 'elasticsearch'.
|
||||||
@ -80,6 +82,25 @@ func isAMQPQueue(sqsArn arnSQS) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns true if mqttARN is for an MQTT queue.
|
||||||
|
func isMQTTQueue(sqsArn arnSQS) bool {
|
||||||
|
if sqsArn.Type != queueTypeMQTT {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
mqttL := serverConfig.Notify.GetMQTTByID(sqsArn.AccountID)
|
||||||
|
if !mqttL.Enable {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// Connect to mqtt server to validate.
|
||||||
|
mqttC, err := dialMQTT(mqttL)
|
||||||
|
if err != nil {
|
||||||
|
errorIf(err, "Unable to connect to mqtt service. %#v", mqttL)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer mqttC.Client.Disconnect(250)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// Returns true if natsArn is for an NATS queue.
|
// Returns true if natsArn is for an NATS queue.
|
||||||
func isNATSQueue(sqsArn arnSQS) bool {
|
func isNATSQueue(sqsArn arnSQS) bool {
|
||||||
if sqsArn.Type != queueTypeNATS {
|
if sqsArn.Type != queueTypeNATS {
|
||||||
|
123
cmd/notify-mqtt.go
Normal file
123
cmd/notify-mqtt.go
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
/*
|
||||||
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"io/ioutil"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
)
|
||||||
|
|
||||||
|
type mqttNotify struct {
|
||||||
|
Enable bool `json:"enable"`
|
||||||
|
Broker string `json:"broker"`
|
||||||
|
Topic string `json:"topic"`
|
||||||
|
QoS int `json:"qos"`
|
||||||
|
ClientID string `json:"clientId"`
|
||||||
|
User string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mqttNotify) Validate() error {
|
||||||
|
if !m.Enable {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if _, err := checkURL(m.Broker); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type mqttConn struct {
|
||||||
|
params mqttNotify
|
||||||
|
Client MQTT.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func dialMQTT(mqttL mqttNotify) (mqttConn, error) {
|
||||||
|
if !mqttL.Enable {
|
||||||
|
return mqttConn{}, errNotifyNotEnabled
|
||||||
|
}
|
||||||
|
connOpts := &MQTT.ClientOptions{
|
||||||
|
ClientID: mqttL.ClientID,
|
||||||
|
CleanSession: true,
|
||||||
|
Username: mqttL.User,
|
||||||
|
Password: mqttL.Password,
|
||||||
|
MaxReconnectInterval: 1 * time.Second,
|
||||||
|
KeepAlive: 30 * time.Second,
|
||||||
|
TLSConfig: tls.Config{RootCAs: globalRootCAs},
|
||||||
|
}
|
||||||
|
connOpts.AddBroker(mqttL.Broker)
|
||||||
|
client := MQTT.NewClient(connOpts)
|
||||||
|
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||||||
|
return mqttConn{}, token.Error()
|
||||||
|
}
|
||||||
|
return mqttConn{Client: client, params: mqttL}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMQTTNotify(accountID string) (*logrus.Logger, error) {
|
||||||
|
mqttL := serverConfig.Notify.GetMQTTByID(accountID)
|
||||||
|
|
||||||
|
//connect to MQTT Server
|
||||||
|
mqttC, err := dialMQTT(mqttL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
mqttLog := logrus.New()
|
||||||
|
|
||||||
|
// Disable writing to console.
|
||||||
|
mqttLog.Out = ioutil.Discard
|
||||||
|
|
||||||
|
// Add a mqtt hook.
|
||||||
|
mqttLog.Hooks.Add(mqttC)
|
||||||
|
|
||||||
|
// Set default JSON formatter
|
||||||
|
mqttLog.Formatter = new(logrus.JSONFormatter)
|
||||||
|
|
||||||
|
// successfully enabled all MQTTs
|
||||||
|
return mqttLog, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fire if called when an event should be sent to the message broker.
|
||||||
|
func (q mqttConn) Fire(entry *logrus.Entry) error {
|
||||||
|
body, err := entry.String()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !q.Client.IsConnected() {
|
||||||
|
if token := q.Client.Connect(); token.Wait() && token.Error() != nil {
|
||||||
|
return token.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
token := q.Client.Publish(q.params.Topic, byte(q.params.QoS), false, body)
|
||||||
|
if token.Wait() && token.Error() != nil {
|
||||||
|
return token.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Levels is available logging levels.
|
||||||
|
func (q mqttConn) Levels() []logrus.Level {
|
||||||
|
return []logrus.Level{
|
||||||
|
logrus.InfoLevel,
|
||||||
|
}
|
||||||
|
}
|
@ -108,7 +108,7 @@ func initConfig() {
|
|||||||
// Config file does not exist, we create it fresh and return upon success.
|
// Config file does not exist, we create it fresh and return upon success.
|
||||||
if isFile(getConfigFile()) {
|
if isFile(getConfigFile()) {
|
||||||
fatalIf(migrateConfig(), "Config migration failed.")
|
fatalIf(migrateConfig(), "Config migration failed.")
|
||||||
fatalIf(loadConfig(), "Unable to load config version: '%s'.", v18)
|
fatalIf(loadConfig(), "Unable to load config version: '%s'.", v19)
|
||||||
} else {
|
} else {
|
||||||
fatalIf(newConfig(), "Unable to initialize minio config for the first time.")
|
fatalIf(newConfig(), "Unable to initialize minio config for the first time.")
|
||||||
log.Println("Created minio configuration file successfully at " + getConfigDir())
|
log.Println("Created minio configuration file successfully at " + getConfigDir())
|
||||||
|
@ -6,6 +6,7 @@ mechanism and can be published to the following targets:
|
|||||||
| Notification Targets|
|
| Notification Targets|
|
||||||
|:---|
|
|:---|
|
||||||
| [`AMQP`](#AMQP) |
|
| [`AMQP`](#AMQP) |
|
||||||
|
| [`MQTT`](#MQTT) |
|
||||||
| [`Elasticsearch`](#Elasticsearch) |
|
| [`Elasticsearch`](#Elasticsearch) |
|
||||||
| [`Redis`](#Redis) |
|
| [`Redis`](#Redis) |
|
||||||
| [`NATS`](#NATS) |
|
| [`NATS`](#NATS) |
|
||||||
@ -134,6 +135,113 @@ python rabbit.py
|
|||||||
‘{“Records”:[{“eventVersion”:”2.0",”eventSource”:”aws:s3",”awsRegion”:”us-east-1",”eventTime”:”2016–09–08T22:34:38.226Z”,”eventName”:”s3:ObjectCreated:Put”,”userIdentity”:{“principalId”:”minio”},”requestParameters”:{“sourceIPAddress”:”10.1.10.150:44576"},”responseElements”:{},”s3":{“s3SchemaVersion”:”1.0",”configurationId”:”Config”,”bucket”:{“name”:”images”,”ownerIdentity”:{“principalId”:”minio”},”arn”:”arn:aws:s3:::images”},”object”:{“key”:”myphoto.jpg”,”size”:200436,”sequencer”:”147279EAF9F40933"}}}],”level”:”info”,”msg”:””,”time”:”2016–09–08T15:34:38–07:00"}\n
|
‘{“Records”:[{“eventVersion”:”2.0",”eventSource”:”aws:s3",”awsRegion”:”us-east-1",”eventTime”:”2016–09–08T22:34:38.226Z”,”eventName”:”s3:ObjectCreated:Put”,”userIdentity”:{“principalId”:”minio”},”requestParameters”:{“sourceIPAddress”:”10.1.10.150:44576"},”responseElements”:{},”s3":{“s3SchemaVersion”:”1.0",”configurationId”:”Config”,”bucket”:{“name”:”images”,”ownerIdentity”:{“principalId”:”minio”},”arn”:”arn:aws:s3:::images”},”object”:{“key”:”myphoto.jpg”,”size”:200436,”sequencer”:”147279EAF9F40933"}}}],”level”:”info”,”msg”:””,”time”:”2016–09–08T15:34:38–07:00"}\n
|
||||||
```
|
```
|
||||||
|
|
||||||
|
<a name="MQTT"></a>
|
||||||
|
## Publish Minio events MQTT
|
||||||
|
|
||||||
|
Install an MQTT Broker from [here](https://mosquitto.org/).
|
||||||
|
|
||||||
|
### Step 1: Add MQTT endpoint to Minio
|
||||||
|
|
||||||
|
the default location of Minio server configuration file is ``~./minio/config.json``. The MQTT configuration is location in the `mqtt` key under the `notify` top-level key. Create a configuration key-value pair here for your MQTT instance. The key is a name for your MQTT endpoint, and the value is a collection of key-value parameters described in the table below.
|
||||||
|
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
|:---|:---|:---|
|
||||||
|
| `enable` | _bool_ | (Required) Is this server endpoint configuration active/enabled? |
|
||||||
|
| `broker` | _string_ | (Required) MQTT server endpoint, e.g. `tcp://localhost:1883` |
|
||||||
|
| `topic` | _string_ | (Required) Name of the MQTT topic to publish on, e.g. `minio` |
|
||||||
|
| `qos` | _int_ | Set the Quality of Service Level |
|
||||||
|
| `clientId` | _string_ | Unique ID for the MQTT broker to identify Minio |
|
||||||
|
| `username` | _string_ | Username to connect to the MQTT server (if required) |
|
||||||
|
| `password` | _string_ | Password to connect to the MQTT server (if required) |
|
||||||
|
|
||||||
|
An example configuration for MQTT is shown below:
|
||||||
|
|
||||||
|
```json
|
||||||
|
"mqtt": {
|
||||||
|
"1": {
|
||||||
|
"enable": true,
|
||||||
|
"broker": "tcp://localhost:1883",
|
||||||
|
"topic": "minio",
|
||||||
|
"qos": 1,
|
||||||
|
"clientId": "minio",
|
||||||
|
"username": "",
|
||||||
|
"password": ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
After updating the configuration file, restart the Minio sever to put the changes into effect. The server will print a line like `SQS ARNs: arn:minio:sqs:us-east-1:1:mqtt` at start-up if there were no errors.
|
||||||
|
|
||||||
|
Minio supports any MQTT server that supports MQTT 3.1 or 3.1.1 and can connect to them over TCP, TLS, or a Websocket connection using ``tcp://``, ``tls://``, or ``ws://`` respectively as the scheme for the broker url. See the [Go Client](http://www.eclipse.org/paho/clients/golang/) documentation for more information.
|
||||||
|
|
||||||
|
Note that, you can add as many MQTT server endpoint configurations as needed by providing an identifier (like "1" in the example above) for the MQTT instance and an object of per-server configuration parameters.
|
||||||
|
|
||||||
|
|
||||||
|
### Step 2: Enable bucket notification using Minio client
|
||||||
|
|
||||||
|
We will enable bucket event notification to trigger whenever a JPEG image is uploaded or deleted ``images`` bucket on ``myminio`` server. Here ARN value is ``arn:minio:sqs:us-east-1:1:mqtt``.
|
||||||
|
|
||||||
|
```
|
||||||
|
mc mb myminio/images
|
||||||
|
mc events add myminio/images arn:minio:sqs:us-east-1:1:mqtt --suffix .jpg
|
||||||
|
mc events list myminio/images
|
||||||
|
arn:minio:sqs:us-east-1:1:amqp s3:ObjectCreated:*,s3:ObjectRemoved:* Filter: suffix=”.jpg”
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 3: Test on MQTT
|
||||||
|
|
||||||
|
The python program below waits on mqtt topic ``/minio`` and prints event notifications on the console. We use [paho-mqtt](https://pypi.python.org/pypi/paho-mqtt/) library to do this.
|
||||||
|
|
||||||
|
```py
|
||||||
|
#!/usr/bin/env python
|
||||||
|
from __future__ import print_function
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
|
# The callback for when the client receives a CONNACK response from the server.
|
||||||
|
def on_connect(client, userdata, flags, rc):
|
||||||
|
print("Connected with result code", rc)
|
||||||
|
|
||||||
|
# Subscribing in on_connect() means that if we lose the connection and
|
||||||
|
# reconnect then subscriptions will be renewed.
|
||||||
|
client.subscribe("/minio")
|
||||||
|
|
||||||
|
# The callback for when a PUBLISH message is received from the server.
|
||||||
|
def on_message(client, userdata, msg):
|
||||||
|
print(msg.payload)
|
||||||
|
|
||||||
|
client = mqtt.Client()
|
||||||
|
client.on_connect = on_connect
|
||||||
|
client.on_message = on_message
|
||||||
|
|
||||||
|
client.connect("localhost:1883", 1883, 60)
|
||||||
|
|
||||||
|
# Blocking call that processes network traffic, dispatches callbacks and
|
||||||
|
# handles reconnecting.
|
||||||
|
# Other loop*() functions are available that give a threaded interface and a
|
||||||
|
# manual interface.
|
||||||
|
client.loop_forever()
|
||||||
|
```
|
||||||
|
|
||||||
|
Execute this example python program to watch for MQTT events on the console.
|
||||||
|
|
||||||
|
```py
|
||||||
|
python mqtt.py
|
||||||
|
```
|
||||||
|
|
||||||
|
Open another terminal and upload a JPEG image into ``images`` bucket.
|
||||||
|
|
||||||
|
```
|
||||||
|
mc cp myphoto.jpg myminio/images
|
||||||
|
```
|
||||||
|
|
||||||
|
You should receive the following event notification via MQTT once the upload completes.
|
||||||
|
|
||||||
|
```py
|
||||||
|
python mqtt.py
|
||||||
|
{“Records”:[{“eventVersion”:”2.0",”eventSource”:”aws:s3",”awsRegion”:”us-east-1",”eventTime”:”2016–09–08T22:34:38.226Z”,”eventName”:”s3:ObjectCreated:Put”,”userIdentity”:{“principalId”:”minio”},”requestParameters”:{“sourceIPAddress”:”10.1.10.150:44576"},”responseElements”:{},”s3":{“s3SchemaVersion”:”1.0",”configurationId”:”Config”,”bucket”:{“name”:”images”,”ownerIdentity”:{“principalId”:”minio”},”arn”:”arn:aws:s3:::images”},”object”:{“key”:”myphoto.jpg”,”size”:200436,”sequencer”:”147279EAF9F40933"}}}],”level”:”info”,”msg”:””,”time”:”2016–09–08T15:34:38–07:00"}
|
||||||
|
```
|
||||||
|
|
||||||
<a name="Elasticsearch"></a>
|
<a name="Elasticsearch"></a>
|
||||||
## Publish Minio events via Elasticsearch
|
## Publish Minio events via Elasticsearch
|
||||||
|
|
||||||
|
@ -77,6 +77,7 @@ minio server ~/Photos
|
|||||||
|:---|:---|:---|
|
|:---|:---|:---|
|
||||||
|``notify``| |Notify enables bucket notification events for lambda computing via the following targets.|
|
|``notify``| |Notify enables bucket notification events for lambda computing via the following targets.|
|
||||||
|``notify.amqp``| |[Configure to publish Minio events via AMQP target.](http://docs.minio.io/docs/minio-bucket-notification-guide#AMQP)|
|
|``notify.amqp``| |[Configure to publish Minio events via AMQP target.](http://docs.minio.io/docs/minio-bucket-notification-guide#AMQP)|
|
||||||
|
|``notify.mqtt``| |[Configure to publish Minio events via MQTT target.](http://docs.minio.io/docs/minio-bucket-notification-guide#MQTT)|
|
||||||
|``notify.elasticsearch``| |[Configure to publish Minio events via Elasticsearch target.](http://docs.minio.io/docs/minio-bucket-notification-guide#Elasticsearch)|
|
|``notify.elasticsearch``| |[Configure to publish Minio events via Elasticsearch target.](http://docs.minio.io/docs/minio-bucket-notification-guide#Elasticsearch)|
|
||||||
|``notify.redis``| |[Configure to publish Minio events via Redis target.](http://docs.minio.io/docs/minio-bucket-notification-guide#Redis)|
|
|``notify.redis``| |[Configure to publish Minio events via Redis target.](http://docs.minio.io/docs/minio-bucket-notification-guide#Redis)|
|
||||||
|``notify.nats``| |[Configure to publish Minio events via NATS target.](http://docs.minio.io/docs/minio-bucket-notification-guide#NATS)|
|
|``notify.nats``| |[Configure to publish Minio events via NATS target.](http://docs.minio.io/docs/minio-bucket-notification-guide#NATS)|
|
||||||
|
56
vendor/github.com/eclipse/paho.mqtt.golang/CONTRIBUTING.md
generated
vendored
Normal file
56
vendor/github.com/eclipse/paho.mqtt.golang/CONTRIBUTING.md
generated
vendored
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
Contributing to Paho
|
||||||
|
====================
|
||||||
|
|
||||||
|
Thanks for your interest in this project.
|
||||||
|
|
||||||
|
Project description:
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
The Paho project has been created to provide scalable open-source implementations of open and standard messaging protocols aimed at new, existing, and emerging applications for Machine-to-Machine (M2M) and Internet of Things (IoT).
|
||||||
|
Paho reflects the inherent physical and cost constraints of device connectivity. Its objectives include effective levels of decoupling between devices and applications, designed to keep markets open and encourage the rapid growth of scalable Web and Enterprise middleware and applications. Paho is being kicked off with MQTT publish/subscribe client implementations for use on embedded platforms, along with corresponding server support as determined by the community.
|
||||||
|
|
||||||
|
- https://projects.eclipse.org/projects/technology.paho
|
||||||
|
|
||||||
|
Developer resources:
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
Information regarding source code management, builds, coding standards, and more.
|
||||||
|
|
||||||
|
- https://projects.eclipse.org/projects/technology.paho/developer
|
||||||
|
|
||||||
|
Contributor License Agreement:
|
||||||
|
------------------------------
|
||||||
|
|
||||||
|
Before your contribution can be accepted by the project, you need to create and electronically sign the Eclipse Foundation Contributor License Agreement (CLA).
|
||||||
|
|
||||||
|
- http://www.eclipse.org/legal/CLA.php
|
||||||
|
|
||||||
|
Contributing Code:
|
||||||
|
------------------
|
||||||
|
|
||||||
|
The Go client is developed in Github, see their documentation on the process of forking and pull requests; https://help.github.com/categories/collaborating-on-projects-using-pull-requests/
|
||||||
|
|
||||||
|
Git commit messages should follow the style described here;
|
||||||
|
|
||||||
|
http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
|
||||||
|
|
||||||
|
Contact:
|
||||||
|
--------
|
||||||
|
|
||||||
|
Contact the project developers via the project's "dev" list.
|
||||||
|
|
||||||
|
- https://dev.eclipse.org/mailman/listinfo/paho-dev
|
||||||
|
|
||||||
|
Search for bugs:
|
||||||
|
----------------
|
||||||
|
|
||||||
|
This project uses Github issues to track ongoing development and issues.
|
||||||
|
|
||||||
|
- https://github.com/eclipse/paho.mqtt.golang/issues
|
||||||
|
|
||||||
|
Create a new bug:
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
Be sure to search for existing bugs before you create another one. Remember that contributions are always welcome!
|
||||||
|
|
||||||
|
- https://github.com/eclipse/paho.mqtt.golang/issues
|
15
vendor/github.com/eclipse/paho.mqtt.golang/DISTRIBUTION
generated
vendored
Normal file
15
vendor/github.com/eclipse/paho.mqtt.golang/DISTRIBUTION
generated
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
|
||||||
|
Eclipse Distribution License - v 1.0
|
||||||
|
|
||||||
|
Copyright (c) 2007, Eclipse Foundation, Inc. and its licensors.
|
||||||
|
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||||
|
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||||
|
Neither the name of the Eclipse Foundation, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
87
vendor/github.com/eclipse/paho.mqtt.golang/LICENSE
generated
vendored
Normal file
87
vendor/github.com/eclipse/paho.mqtt.golang/LICENSE
generated
vendored
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
Eclipse Public License - v 1.0
|
||||||
|
|
||||||
|
THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
|
||||||
|
|
||||||
|
1. DEFINITIONS
|
||||||
|
|
||||||
|
"Contribution" means:
|
||||||
|
|
||||||
|
a) in the case of the initial Contributor, the initial code and documentation distributed under this Agreement, and
|
||||||
|
|
||||||
|
b) in the case of each subsequent Contributor:
|
||||||
|
|
||||||
|
i) changes to the Program, and
|
||||||
|
|
||||||
|
ii) additions to the Program;
|
||||||
|
|
||||||
|
where such changes and/or additions to the Program originate from and are distributed by that particular Contributor. A Contribution 'originates' from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor's behalf. Contributions do not include additions to the Program which: (i) are separate modules of software distributed in conjunction with the Program under their own license agreement, and (ii) are not derivative works of the Program.
|
||||||
|
|
||||||
|
"Contributor" means any person or entity that distributes the Program.
|
||||||
|
|
||||||
|
"Licensed Patents" mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program.
|
||||||
|
|
||||||
|
"Program" means the Contributions distributed in accordance with this Agreement.
|
||||||
|
|
||||||
|
"Recipient" means anyone who receives the Program under this Agreement, including all Contributors.
|
||||||
|
|
||||||
|
2. GRANT OF RIGHTS
|
||||||
|
|
||||||
|
a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, distribute and sublicense the Contribution of such Contributor, if any, and such derivative works, in source code and object code form.
|
||||||
|
|
||||||
|
b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in source code and object code form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder.
|
||||||
|
|
||||||
|
c) Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to distribute the Program, it is Recipient's responsibility to acquire that license before distributing the Program.
|
||||||
|
|
||||||
|
d) Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement.
|
||||||
|
|
||||||
|
3. REQUIREMENTS
|
||||||
|
|
||||||
|
A Contributor may choose to distribute the Program in object code form under its own license agreement, provided that:
|
||||||
|
|
||||||
|
a) it complies with the terms and conditions of this Agreement; and
|
||||||
|
|
||||||
|
b) its license agreement:
|
||||||
|
|
||||||
|
i) effectively disclaims on behalf of all Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose;
|
||||||
|
|
||||||
|
ii) effectively excludes on behalf of all Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits;
|
||||||
|
|
||||||
|
iii) states that any provisions which differ from this Agreement are offered by that Contributor alone and not by any other party; and
|
||||||
|
|
||||||
|
iv) states that source code for the Program is available from such Contributor, and informs licensees how to obtain it in a reasonable manner on or through a medium customarily used for software exchange.
|
||||||
|
|
||||||
|
When the Program is made available in source code form:
|
||||||
|
|
||||||
|
a) it must be made available under this Agreement; and
|
||||||
|
|
||||||
|
b) a copy of this Agreement must be included with each copy of the Program.
|
||||||
|
|
||||||
|
Contributors may not remove or alter any copyright notices contained within the Program.
|
||||||
|
|
||||||
|
Each Contributor must identify itself as the originator of its Contribution, if any, in a manner that reasonably allows subsequent Recipients to identify the originator of the Contribution.
|
||||||
|
|
||||||
|
4. COMMERCIAL DISTRIBUTION
|
||||||
|
|
||||||
|
Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor ("Commercial Contributor") hereby agrees to defend and indemnify every other Contributor ("Indemnified Contributor") against any losses, damages and costs (collectively "Losses") arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense.
|
||||||
|
|
||||||
|
For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages.
|
||||||
|
|
||||||
|
5. NO WARRANTY
|
||||||
|
|
||||||
|
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement , including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations.
|
||||||
|
|
||||||
|
6. DISCLAIMER OF LIABILITY
|
||||||
|
|
||||||
|
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||||
|
|
||||||
|
7. GENERAL
|
||||||
|
|
||||||
|
If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable.
|
||||||
|
|
||||||
|
If Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 2(b) shall terminate as of the date such litigation is filed.
|
||||||
|
|
||||||
|
All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive.
|
||||||
|
|
||||||
|
Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. The Eclipse Foundation is the initial Agreement Steward. The Eclipse Foundation may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to distribute the Program (including its Contributions) under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved.
|
||||||
|
|
||||||
|
This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation.
|
62
vendor/github.com/eclipse/paho.mqtt.golang/README.md
generated
vendored
Normal file
62
vendor/github.com/eclipse/paho.mqtt.golang/README.md
generated
vendored
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
Eclipse Paho MQTT Go client
|
||||||
|
===========================
|
||||||
|
|
||||||
|
|
||||||
|
This repository contains the source code for the [Eclipse Paho](http://eclipse.org/paho) MQTT Go client library.
|
||||||
|
|
||||||
|
This code builds a library which enable applications to connect to an [MQTT](http://mqtt.org) broker to publish messages, and to subscribe to topics and receive published messages.
|
||||||
|
|
||||||
|
This library supports a fully asynchronous mode of operation.
|
||||||
|
|
||||||
|
|
||||||
|
Installation and Build
|
||||||
|
----------------------
|
||||||
|
|
||||||
|
This client is designed to work with the standard Go tools, so installation is as easy as:
|
||||||
|
|
||||||
|
```
|
||||||
|
go get github.com/eclipse/paho.mqtt.golang
|
||||||
|
```
|
||||||
|
|
||||||
|
The client depends on Google's [websockets](https://godoc.org/golang.org/x/net/websocket) package,
|
||||||
|
also easily installed with the command:
|
||||||
|
|
||||||
|
```
|
||||||
|
go get golang.org/x/net/websocket
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Usage and API
|
||||||
|
-------------
|
||||||
|
|
||||||
|
Detailed API documentation is available by using to godoc tool, or can be browsed online
|
||||||
|
using the [godoc.org](http://godoc.org/github.com/eclipse/paho.mqtt.golang) service.
|
||||||
|
|
||||||
|
Make use of the library by importing it in your Go client source code. For example,
|
||||||
|
```
|
||||||
|
import "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
```
|
||||||
|
|
||||||
|
Samples are available in the `cmd` directory for reference.
|
||||||
|
|
||||||
|
|
||||||
|
Runtime tracing
|
||||||
|
---------------
|
||||||
|
|
||||||
|
Tracing is enabled by assigning logs (from the Go log package) to the logging endpoints, ERROR, CRITICAL, WARN and DEBUG
|
||||||
|
|
||||||
|
|
||||||
|
Reporting bugs
|
||||||
|
--------------
|
||||||
|
|
||||||
|
Please report bugs by raising issues for this project in github https://github.com/eclipse/paho.mqtt.golang/issues
|
||||||
|
|
||||||
|
|
||||||
|
More information
|
||||||
|
----------------
|
||||||
|
|
||||||
|
Discussion of the Paho clients takes place on the [Eclipse paho-dev mailing list](https://dev.eclipse.org/mailman/listinfo/paho-dev).
|
||||||
|
|
||||||
|
General questions about the MQTT protocol are discussed in the [MQTT Google Group](https://groups.google.com/forum/?hl=en-US&fromgroups#!forum/mqtt).
|
||||||
|
|
||||||
|
There is much more information available via the [MQTT community site](http://mqtt.org).
|
41
vendor/github.com/eclipse/paho.mqtt.golang/about.html
generated
vendored
Normal file
41
vendor/github.com/eclipse/paho.mqtt.golang/about.html
generated
vendored
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml"><head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||||
|
<title>About</title>
|
||||||
|
</head>
|
||||||
|
<body lang="EN-US">
|
||||||
|
<h2>About This Content</h2>
|
||||||
|
|
||||||
|
<p><em>December 9, 2013</em></p>
|
||||||
|
<h3>License</h3>
|
||||||
|
|
||||||
|
<p>The Eclipse Foundation makes available all content in this plug-in ("Content"). Unless otherwise
|
||||||
|
indicated below, the Content is provided to you under the terms and conditions of the
|
||||||
|
Eclipse Public License Version 1.0 ("EPL") and Eclipse Distribution License Version 1.0 ("EDL").
|
||||||
|
A copy of the EPL is available at
|
||||||
|
<a href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a>
|
||||||
|
and a copy of the EDL is available at
|
||||||
|
<a href="http://www.eclipse.org/org/documents/edl-v10.php">http://www.eclipse.org/org/documents/edl-v10.php</a>.
|
||||||
|
For purposes of the EPL, "Program" will mean the Content.</p>
|
||||||
|
|
||||||
|
<p>If you did not receive this Content directly from the Eclipse Foundation, the Content is
|
||||||
|
being redistributed by another party ("Redistributor") and different terms and conditions may
|
||||||
|
apply to your use of any object code in the Content. Check the Redistributor's license that was
|
||||||
|
provided with the Content. If no such license exists, contact the Redistributor. Unless otherwise
|
||||||
|
indicated below, the terms and conditions of the EPL still apply to any source code in the Content
|
||||||
|
and such source code may be obtained at <a href="http://www.eclipse.org/">http://www.eclipse.org</a>.</p>
|
||||||
|
|
||||||
|
|
||||||
|
<h3>Third Party Content</h3>
|
||||||
|
<p>The Content includes items that have been sourced from third parties as set out below. If you
|
||||||
|
did not receive this Content directly from the Eclipse Foundation, the following is provided
|
||||||
|
for informational purposes only, and you should look to the Redistributor's license for
|
||||||
|
terms and conditions of use.</p>
|
||||||
|
<p><em>
|
||||||
|
<strong>None</strong> <br><br>
|
||||||
|
<br><br>
|
||||||
|
</em></p>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</body></html>
|
611
vendor/github.com/eclipse/paho.mqtt.golang/client.go
generated
vendored
Normal file
611
vendor/github.com/eclipse/paho.mqtt.golang/client.go
generated
vendored
Normal file
@ -0,0 +1,611 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Package mqtt provides an MQTT v3.1.1 client library.
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/eclipse/paho.mqtt.golang/packets"
|
||||||
|
)
|
||||||
|
|
||||||
|
type connStatus uint
|
||||||
|
|
||||||
|
const (
|
||||||
|
disconnected connStatus = iota
|
||||||
|
connecting
|
||||||
|
reconnecting
|
||||||
|
connected
|
||||||
|
)
|
||||||
|
|
||||||
|
// Client is the interface definition for a Client as used by this
|
||||||
|
// library, the interface is primarily to allow mocking tests.
|
||||||
|
//
|
||||||
|
// It is an MQTT v3.1.1 client for communicating
|
||||||
|
// with an MQTT server using non-blocking methods that allow work
|
||||||
|
// to be done in the background.
|
||||||
|
// An application may connect to an MQTT server using:
|
||||||
|
// A plain TCP socket
|
||||||
|
// A secure SSL/TLS socket
|
||||||
|
// A websocket
|
||||||
|
// To enable ensured message delivery at Quality of Service (QoS) levels
|
||||||
|
// described in the MQTT spec, a message persistence mechanism must be
|
||||||
|
// used. This is done by providing a type which implements the Store
|
||||||
|
// interface. For convenience, FileStore and MemoryStore are provided
|
||||||
|
// implementations that should be sufficient for most use cases. More
|
||||||
|
// information can be found in their respective documentation.
|
||||||
|
// Numerous connection options may be specified by configuring a
|
||||||
|
// and then supplying a ClientOptions type.
|
||||||
|
type Client interface {
|
||||||
|
IsConnected() bool
|
||||||
|
Connect() Token
|
||||||
|
Disconnect(quiesce uint)
|
||||||
|
Publish(topic string, qos byte, retained bool, payload interface{}) Token
|
||||||
|
Subscribe(topic string, qos byte, callback MessageHandler) Token
|
||||||
|
SubscribeMultiple(filters map[string]byte, callback MessageHandler) Token
|
||||||
|
Unsubscribe(topics ...string) Token
|
||||||
|
AddRoute(topic string, callback MessageHandler)
|
||||||
|
}
|
||||||
|
|
||||||
|
// client implements the Client interface
|
||||||
|
type client struct {
|
||||||
|
sync.RWMutex
|
||||||
|
messageIds
|
||||||
|
conn net.Conn
|
||||||
|
ibound chan packets.ControlPacket
|
||||||
|
obound chan *PacketAndToken
|
||||||
|
oboundP chan *PacketAndToken
|
||||||
|
msgRouter *router
|
||||||
|
stopRouter chan bool
|
||||||
|
incomingPubChan chan *packets.PublishPacket
|
||||||
|
errors chan error
|
||||||
|
stop chan struct{}
|
||||||
|
persist Store
|
||||||
|
options ClientOptions
|
||||||
|
pingResp chan struct{}
|
||||||
|
packetResp chan struct{}
|
||||||
|
keepaliveReset chan struct{}
|
||||||
|
status connStatus
|
||||||
|
workers sync.WaitGroup
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClient will create an MQTT v3.1.1 client with all of the options specified
|
||||||
|
// in the provided ClientOptions. The client must have the Connect method called
|
||||||
|
// on it before it may be used. This is to make sure resources (such as a net
|
||||||
|
// connection) are created before the application is actually ready.
|
||||||
|
func NewClient(o *ClientOptions) Client {
|
||||||
|
c := &client{}
|
||||||
|
c.options = *o
|
||||||
|
|
||||||
|
if c.options.Store == nil {
|
||||||
|
c.options.Store = NewMemoryStore()
|
||||||
|
}
|
||||||
|
switch c.options.ProtocolVersion {
|
||||||
|
case 3, 4:
|
||||||
|
c.options.protocolVersionExplicit = true
|
||||||
|
default:
|
||||||
|
c.options.ProtocolVersion = 4
|
||||||
|
c.options.protocolVersionExplicit = false
|
||||||
|
}
|
||||||
|
c.persist = c.options.Store
|
||||||
|
c.status = disconnected
|
||||||
|
c.messageIds = messageIds{index: make(map[uint16]Token)}
|
||||||
|
c.msgRouter, c.stopRouter = newRouter()
|
||||||
|
c.msgRouter.setDefaultHandler(c.options.DefaultPublishHander)
|
||||||
|
if !c.options.AutoReconnect {
|
||||||
|
c.options.MessageChannelDepth = 0
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *client) AddRoute(topic string, callback MessageHandler) {
|
||||||
|
if callback != nil {
|
||||||
|
c.msgRouter.addRoute(topic, callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsConnected returns a bool signifying whether
|
||||||
|
// the client is connected or not.
|
||||||
|
func (c *client) IsConnected() bool {
|
||||||
|
c.RLock()
|
||||||
|
defer c.RUnlock()
|
||||||
|
switch {
|
||||||
|
case c.status == connected:
|
||||||
|
return true
|
||||||
|
case c.options.AutoReconnect && c.status > disconnected:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *client) connectionStatus() connStatus {
|
||||||
|
c.RLock()
|
||||||
|
defer c.RUnlock()
|
||||||
|
return c.status
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *client) setConnected(status connStatus) {
|
||||||
|
c.Lock()
|
||||||
|
defer c.Unlock()
|
||||||
|
c.status = status
|
||||||
|
}
|
||||||
|
|
||||||
|
//ErrNotConnected is the error returned from function calls that are
|
||||||
|
//made when the client is not connected to a broker
|
||||||
|
var ErrNotConnected = errors.New("Not Connected")
|
||||||
|
|
||||||
|
// Connect will create a connection to the message broker
|
||||||
|
// If clean session is false, then a slice will
|
||||||
|
// be returned containing Receipts for all messages
|
||||||
|
// that were in-flight at the last disconnect.
|
||||||
|
// If clean session is true, then any existing client
|
||||||
|
// state will be removed.
|
||||||
|
func (c *client) Connect() Token {
|
||||||
|
var err error
|
||||||
|
t := newToken(packets.Connect).(*ConnectToken)
|
||||||
|
DEBUG.Println(CLI, "Connect()")
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
c.persist.Open()
|
||||||
|
|
||||||
|
c.setConnected(connecting)
|
||||||
|
var rc byte
|
||||||
|
cm := newConnectMsgFromOptions(&c.options)
|
||||||
|
|
||||||
|
for _, broker := range c.options.Servers {
|
||||||
|
CONN:
|
||||||
|
DEBUG.Println(CLI, "about to write new connect msg")
|
||||||
|
c.conn, err = openConnection(broker, &c.options.TLSConfig, c.options.ConnectTimeout)
|
||||||
|
if err == nil {
|
||||||
|
DEBUG.Println(CLI, "socket connected to broker")
|
||||||
|
switch c.options.ProtocolVersion {
|
||||||
|
case 3:
|
||||||
|
DEBUG.Println(CLI, "Using MQTT 3.1 protocol")
|
||||||
|
cm.ProtocolName = "MQIsdp"
|
||||||
|
cm.ProtocolVersion = 3
|
||||||
|
default:
|
||||||
|
DEBUG.Println(CLI, "Using MQTT 3.1.1 protocol")
|
||||||
|
c.options.ProtocolVersion = 4
|
||||||
|
cm.ProtocolName = "MQTT"
|
||||||
|
cm.ProtocolVersion = 4
|
||||||
|
}
|
||||||
|
cm.Write(c.conn)
|
||||||
|
|
||||||
|
rc = c.connect()
|
||||||
|
if rc != packets.Accepted {
|
||||||
|
if c.conn != nil {
|
||||||
|
c.conn.Close()
|
||||||
|
c.conn = nil
|
||||||
|
}
|
||||||
|
//if the protocol version was explicitly set don't do any fallback
|
||||||
|
if c.options.protocolVersionExplicit {
|
||||||
|
ERROR.Println(CLI, "Connecting to", broker, "CONNACK was not CONN_ACCEPTED, but rather", packets.ConnackReturnCodes[rc])
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if c.options.ProtocolVersion == 4 {
|
||||||
|
DEBUG.Println(CLI, "Trying reconnect using MQTT 3.1 protocol")
|
||||||
|
c.options.ProtocolVersion = 3
|
||||||
|
goto CONN
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
ERROR.Println(CLI, err.Error())
|
||||||
|
WARN.Println(CLI, "failed to connect to broker, trying next")
|
||||||
|
rc = packets.ErrNetworkError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.conn == nil {
|
||||||
|
ERROR.Println(CLI, "Failed to connect to a broker")
|
||||||
|
t.returnCode = rc
|
||||||
|
if rc != packets.ErrNetworkError {
|
||||||
|
t.err = packets.ConnErrors[rc]
|
||||||
|
} else {
|
||||||
|
t.err = fmt.Errorf("%s : %s", packets.ConnErrors[rc], err)
|
||||||
|
}
|
||||||
|
c.setConnected(disconnected)
|
||||||
|
c.persist.Close()
|
||||||
|
t.flowComplete()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.options.KeepAlive != 0 {
|
||||||
|
c.workers.Add(1)
|
||||||
|
go keepalive(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.obound = make(chan *PacketAndToken, c.options.MessageChannelDepth)
|
||||||
|
c.oboundP = make(chan *PacketAndToken, c.options.MessageChannelDepth)
|
||||||
|
c.ibound = make(chan packets.ControlPacket)
|
||||||
|
c.errors = make(chan error, 1)
|
||||||
|
c.stop = make(chan struct{})
|
||||||
|
c.pingResp = make(chan struct{}, 1)
|
||||||
|
c.packetResp = make(chan struct{}, 1)
|
||||||
|
c.keepaliveReset = make(chan struct{}, 1)
|
||||||
|
|
||||||
|
c.incomingPubChan = make(chan *packets.PublishPacket, c.options.MessageChannelDepth)
|
||||||
|
c.msgRouter.matchAndDispatch(c.incomingPubChan, c.options.Order, c)
|
||||||
|
|
||||||
|
c.setConnected(connected)
|
||||||
|
DEBUG.Println(CLI, "client is connected")
|
||||||
|
if c.options.OnConnect != nil {
|
||||||
|
go c.options.OnConnect(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Take care of any messages in the store
|
||||||
|
//var leftovers []Receipt
|
||||||
|
if c.options.CleanSession == false {
|
||||||
|
//leftovers = c.resume()
|
||||||
|
} else {
|
||||||
|
c.persist.Reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
go errorWatch(c)
|
||||||
|
|
||||||
|
// Do not start incoming until resume has completed
|
||||||
|
c.workers.Add(3)
|
||||||
|
go alllogic(c)
|
||||||
|
go outgoing(c)
|
||||||
|
go incoming(c)
|
||||||
|
|
||||||
|
DEBUG.Println(CLI, "exit startClient")
|
||||||
|
t.flowComplete()
|
||||||
|
}()
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
// internal function used to reconnect the client when it loses its connection
|
||||||
|
func (c *client) reconnect() {
|
||||||
|
DEBUG.Println(CLI, "enter reconnect")
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
|
||||||
|
rc = byte(1)
|
||||||
|
sleep = time.Duration(1 * time.Second)
|
||||||
|
)
|
||||||
|
|
||||||
|
for rc != 0 && c.status != disconnected {
|
||||||
|
cm := newConnectMsgFromOptions(&c.options)
|
||||||
|
|
||||||
|
for _, broker := range c.options.Servers {
|
||||||
|
CONN:
|
||||||
|
DEBUG.Println(CLI, "about to write new connect msg")
|
||||||
|
c.conn, err = openConnection(broker, &c.options.TLSConfig, c.options.ConnectTimeout)
|
||||||
|
if err == nil {
|
||||||
|
DEBUG.Println(CLI, "socket connected to broker")
|
||||||
|
switch c.options.ProtocolVersion {
|
||||||
|
case 3:
|
||||||
|
DEBUG.Println(CLI, "Using MQTT 3.1 protocol")
|
||||||
|
cm.ProtocolName = "MQIsdp"
|
||||||
|
cm.ProtocolVersion = 3
|
||||||
|
default:
|
||||||
|
DEBUG.Println(CLI, "Using MQTT 3.1.1 protocol")
|
||||||
|
c.options.ProtocolVersion = 4
|
||||||
|
cm.ProtocolName = "MQTT"
|
||||||
|
cm.ProtocolVersion = 4
|
||||||
|
}
|
||||||
|
cm.Write(c.conn)
|
||||||
|
|
||||||
|
rc = c.connect()
|
||||||
|
if rc != packets.Accepted {
|
||||||
|
c.conn.Close()
|
||||||
|
c.conn = nil
|
||||||
|
//if the protocol version was explicitly set don't do any fallback
|
||||||
|
if c.options.protocolVersionExplicit {
|
||||||
|
ERROR.Println(CLI, "Connecting to", broker, "CONNACK was not Accepted, but rather", packets.ConnackReturnCodes[rc])
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if c.options.ProtocolVersion == 4 {
|
||||||
|
DEBUG.Println(CLI, "Trying reconnect using MQTT 3.1 protocol")
|
||||||
|
c.options.ProtocolVersion = 3
|
||||||
|
goto CONN
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
ERROR.Println(CLI, err.Error())
|
||||||
|
WARN.Println(CLI, "failed to connect to broker, trying next")
|
||||||
|
rc = packets.ErrNetworkError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if rc != 0 {
|
||||||
|
DEBUG.Println(CLI, "Reconnect failed, sleeping for", int(sleep.Seconds()), "seconds")
|
||||||
|
time.Sleep(sleep)
|
||||||
|
if sleep < c.options.MaxReconnectInterval {
|
||||||
|
sleep *= 2
|
||||||
|
}
|
||||||
|
|
||||||
|
if sleep > c.options.MaxReconnectInterval {
|
||||||
|
sleep = c.options.MaxReconnectInterval
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Disconnect() must have been called while we were trying to reconnect.
|
||||||
|
if c.status == disconnected {
|
||||||
|
DEBUG.Println(CLI, "Client moved to disconnected state while reconnecting, abandoning reconnect")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.options.KeepAlive != 0 {
|
||||||
|
c.workers.Add(1)
|
||||||
|
go keepalive(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.stop = make(chan struct{})
|
||||||
|
|
||||||
|
c.setConnected(connected)
|
||||||
|
DEBUG.Println(CLI, "client is reconnected")
|
||||||
|
if c.options.OnConnect != nil {
|
||||||
|
go c.options.OnConnect(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
go errorWatch(c)
|
||||||
|
|
||||||
|
c.workers.Add(3)
|
||||||
|
go alllogic(c)
|
||||||
|
go outgoing(c)
|
||||||
|
go incoming(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function is only used for receiving a connack
|
||||||
|
// when the connection is first started.
|
||||||
|
// This prevents receiving incoming data while resume
|
||||||
|
// is in progress if clean session is false.
|
||||||
|
func (c *client) connect() byte {
|
||||||
|
DEBUG.Println(NET, "connect started")
|
||||||
|
|
||||||
|
ca, err := packets.ReadPacket(c.conn)
|
||||||
|
if err != nil {
|
||||||
|
ERROR.Println(NET, "connect got error", err)
|
||||||
|
return packets.ErrNetworkError
|
||||||
|
}
|
||||||
|
if ca == nil {
|
||||||
|
ERROR.Println(NET, "received nil packet")
|
||||||
|
return packets.ErrNetworkError
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, ok := ca.(*packets.ConnackPacket)
|
||||||
|
if !ok {
|
||||||
|
ERROR.Println(NET, "received msg that was not CONNACK")
|
||||||
|
return packets.ErrNetworkError
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG.Println(NET, "received connack")
|
||||||
|
return msg.ReturnCode
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disconnect will end the connection with the server, but not before waiting
|
||||||
|
// the specified number of milliseconds to wait for existing work to be
|
||||||
|
// completed.
|
||||||
|
func (c *client) Disconnect(quiesce uint) {
|
||||||
|
if c.status == connected {
|
||||||
|
DEBUG.Println(CLI, "disconnecting")
|
||||||
|
c.setConnected(disconnected)
|
||||||
|
|
||||||
|
dm := packets.NewControlPacket(packets.Disconnect).(*packets.DisconnectPacket)
|
||||||
|
dt := newToken(packets.Disconnect)
|
||||||
|
c.oboundP <- &PacketAndToken{p: dm, t: dt}
|
||||||
|
|
||||||
|
// wait for work to finish, or quiesce time consumed
|
||||||
|
dt.WaitTimeout(time.Duration(quiesce) * time.Millisecond)
|
||||||
|
} else {
|
||||||
|
WARN.Println(CLI, "Disconnect() called but not connected (disconnected/reconnecting)")
|
||||||
|
c.setConnected(disconnected)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.disconnect()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForceDisconnect will end the connection with the mqtt broker immediately.
|
||||||
|
func (c *client) forceDisconnect() {
|
||||||
|
if !c.IsConnected() {
|
||||||
|
WARN.Println(CLI, "already disconnected")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.setConnected(disconnected)
|
||||||
|
c.conn.Close()
|
||||||
|
DEBUG.Println(CLI, "forcefully disconnecting")
|
||||||
|
c.disconnect()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *client) internalConnLost(err error) {
|
||||||
|
// Only do anything if this was called and we are still "connected"
|
||||||
|
// forceDisconnect can cause incoming/outgoing/alllogic to end with
|
||||||
|
// error from closing the socket but state will be "disconnected"
|
||||||
|
if c.IsConnected() {
|
||||||
|
c.closeStop()
|
||||||
|
c.conn.Close()
|
||||||
|
c.workers.Wait()
|
||||||
|
if c.options.CleanSession {
|
||||||
|
c.messageIds.cleanUp()
|
||||||
|
}
|
||||||
|
if c.options.AutoReconnect {
|
||||||
|
c.setConnected(reconnecting)
|
||||||
|
go c.reconnect()
|
||||||
|
} else {
|
||||||
|
c.setConnected(disconnected)
|
||||||
|
}
|
||||||
|
if c.options.OnConnectionLost != nil {
|
||||||
|
go c.options.OnConnectionLost(c, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *client) closeStop() {
|
||||||
|
c.Lock()
|
||||||
|
defer c.Unlock()
|
||||||
|
select {
|
||||||
|
case <-c.stop:
|
||||||
|
DEBUG.Println("In disconnect and stop channel is already closed")
|
||||||
|
default:
|
||||||
|
close(c.stop)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *client) closeConn() {
|
||||||
|
c.Lock()
|
||||||
|
defer c.Unlock()
|
||||||
|
if c.conn != nil {
|
||||||
|
c.conn.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *client) disconnect() {
|
||||||
|
c.closeStop()
|
||||||
|
c.closeConn()
|
||||||
|
c.workers.Wait()
|
||||||
|
close(c.stopRouter)
|
||||||
|
DEBUG.Println(CLI, "disconnected")
|
||||||
|
c.persist.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Publish will publish a message with the specified QoS and content
|
||||||
|
// to the specified topic.
|
||||||
|
// Returns a token to track delivery of the message to the broker
|
||||||
|
func (c *client) Publish(topic string, qos byte, retained bool, payload interface{}) Token {
|
||||||
|
token := newToken(packets.Publish).(*PublishToken)
|
||||||
|
DEBUG.Println(CLI, "enter Publish")
|
||||||
|
switch {
|
||||||
|
case !c.IsConnected():
|
||||||
|
token.err = ErrNotConnected
|
||||||
|
token.flowComplete()
|
||||||
|
return token
|
||||||
|
case c.connectionStatus() == reconnecting && qos == 0:
|
||||||
|
token.flowComplete()
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
pub := packets.NewControlPacket(packets.Publish).(*packets.PublishPacket)
|
||||||
|
pub.Qos = qos
|
||||||
|
pub.TopicName = topic
|
||||||
|
pub.Retain = retained
|
||||||
|
switch payload.(type) {
|
||||||
|
case string:
|
||||||
|
pub.Payload = []byte(payload.(string))
|
||||||
|
case []byte:
|
||||||
|
pub.Payload = payload.([]byte)
|
||||||
|
default:
|
||||||
|
token.err = errors.New("Unknown payload type")
|
||||||
|
token.flowComplete()
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG.Println(CLI, "sending publish message, topic:", topic)
|
||||||
|
if pub.Qos != 0 && pub.MessageID == 0 {
|
||||||
|
pub.MessageID = c.getID(token)
|
||||||
|
token.messageID = pub.MessageID
|
||||||
|
}
|
||||||
|
persistOutbound(c.persist, pub)
|
||||||
|
c.obound <- &PacketAndToken{p: pub, t: token}
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subscribe starts a new subscription. Provide a MessageHandler to be executed when
|
||||||
|
// a message is published on the topic provided.
|
||||||
|
func (c *client) Subscribe(topic string, qos byte, callback MessageHandler) Token {
|
||||||
|
token := newToken(packets.Subscribe).(*SubscribeToken)
|
||||||
|
DEBUG.Println(CLI, "enter Subscribe")
|
||||||
|
if !c.IsConnected() {
|
||||||
|
token.err = ErrNotConnected
|
||||||
|
token.flowComplete()
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
sub := packets.NewControlPacket(packets.Subscribe).(*packets.SubscribePacket)
|
||||||
|
if err := validateTopicAndQos(topic, qos); err != nil {
|
||||||
|
token.err = err
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
sub.Topics = append(sub.Topics, topic)
|
||||||
|
sub.Qoss = append(sub.Qoss, qos)
|
||||||
|
DEBUG.Println(CLI, sub.String())
|
||||||
|
|
||||||
|
if callback != nil {
|
||||||
|
c.msgRouter.addRoute(topic, callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
token.subs = append(token.subs, topic)
|
||||||
|
c.oboundP <- &PacketAndToken{p: sub, t: token}
|
||||||
|
DEBUG.Println(CLI, "exit Subscribe")
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
|
// SubscribeMultiple starts a new subscription for multiple topics. Provide a MessageHandler to
|
||||||
|
// be executed when a message is published on one of the topics provided.
|
||||||
|
func (c *client) SubscribeMultiple(filters map[string]byte, callback MessageHandler) Token {
|
||||||
|
var err error
|
||||||
|
token := newToken(packets.Subscribe).(*SubscribeToken)
|
||||||
|
DEBUG.Println(CLI, "enter SubscribeMultiple")
|
||||||
|
if !c.IsConnected() {
|
||||||
|
token.err = ErrNotConnected
|
||||||
|
token.flowComplete()
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
sub := packets.NewControlPacket(packets.Subscribe).(*packets.SubscribePacket)
|
||||||
|
if sub.Topics, sub.Qoss, err = validateSubscribeMap(filters); err != nil {
|
||||||
|
token.err = err
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
|
if callback != nil {
|
||||||
|
for topic := range filters {
|
||||||
|
c.msgRouter.addRoute(topic, callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
token.subs = make([]string, len(sub.Topics))
|
||||||
|
copy(token.subs, sub.Topics)
|
||||||
|
c.oboundP <- &PacketAndToken{p: sub, t: token}
|
||||||
|
DEBUG.Println(CLI, "exit SubscribeMultiple")
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unsubscribe will end the subscription from each of the topics provided.
|
||||||
|
// Messages published to those topics from other clients will no longer be
|
||||||
|
// received.
|
||||||
|
func (c *client) Unsubscribe(topics ...string) Token {
|
||||||
|
token := newToken(packets.Unsubscribe).(*UnsubscribeToken)
|
||||||
|
DEBUG.Println(CLI, "enter Unsubscribe")
|
||||||
|
if !c.IsConnected() {
|
||||||
|
token.err = ErrNotConnected
|
||||||
|
token.flowComplete()
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
unsub := packets.NewControlPacket(packets.Unsubscribe).(*packets.UnsubscribePacket)
|
||||||
|
unsub.Topics = make([]string, len(topics))
|
||||||
|
copy(unsub.Topics, topics)
|
||||||
|
|
||||||
|
c.oboundP <- &PacketAndToken{p: unsub, t: token}
|
||||||
|
for _, topic := range topics {
|
||||||
|
c.msgRouter.deleteRoute(topic)
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG.Println(CLI, "exit Unsubscribe")
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *client) OptionsReader() ClientOptionsReader {
|
||||||
|
r := ClientOptionsReader{options: &c.options}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
//DefaultConnectionLostHandler is a definition of a function that simply
|
||||||
|
//reports to the DEBUG log the reason for the client losing a connection.
|
||||||
|
func DefaultConnectionLostHandler(client Client, reason error) {
|
||||||
|
DEBUG.Println("Connection lost:", reason.Error())
|
||||||
|
}
|
31
vendor/github.com/eclipse/paho.mqtt.golang/components.go
generated
vendored
Normal file
31
vendor/github.com/eclipse/paho.mqtt.golang/components.go
generated
vendored
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
type component string
|
||||||
|
|
||||||
|
// Component names for debug output
|
||||||
|
const (
|
||||||
|
NET component = "[net] "
|
||||||
|
PNG component = "[pinger] "
|
||||||
|
CLI component = "[client] "
|
||||||
|
DEC component = "[decode] "
|
||||||
|
MES component = "[message] "
|
||||||
|
STR component = "[store] "
|
||||||
|
MID component = "[msgids] "
|
||||||
|
TST component = "[test] "
|
||||||
|
STA component = "[state] "
|
||||||
|
ERR component = "[error] "
|
||||||
|
)
|
15
vendor/github.com/eclipse/paho.mqtt.golang/edl-v10
generated
vendored
Normal file
15
vendor/github.com/eclipse/paho.mqtt.golang/edl-v10
generated
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
Eclipse Distribution License - v 1.0
|
||||||
|
|
||||||
|
Copyright (c) 2007, Eclipse Foundation, Inc. and its licensors.
|
||||||
|
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||||
|
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||||
|
Neither the name of the Eclipse Foundation, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
70
vendor/github.com/eclipse/paho.mqtt.golang/epl-v10
generated
vendored
Normal file
70
vendor/github.com/eclipse/paho.mqtt.golang/epl-v10
generated
vendored
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
Eclipse Public License - v 1.0
|
||||||
|
|
||||||
|
THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
|
||||||
|
|
||||||
|
1. DEFINITIONS
|
||||||
|
|
||||||
|
"Contribution" means:
|
||||||
|
|
||||||
|
a) in the case of the initial Contributor, the initial code and documentation distributed under this Agreement, and
|
||||||
|
b) in the case of each subsequent Contributor:
|
||||||
|
i) changes to the Program, and
|
||||||
|
ii) additions to the Program;
|
||||||
|
where such changes and/or additions to the Program originate from and are distributed by that particular Contributor. A Contribution 'originates' from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor's behalf. Contributions do not include additions to the Program which: (i) are separate modules of software distributed in conjunction with the Program under their own license agreement, and (ii) are not derivative works of the Program.
|
||||||
|
"Contributor" means any person or entity that distributes the Program.
|
||||||
|
|
||||||
|
"Licensed Patents" mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program.
|
||||||
|
|
||||||
|
"Program" means the Contributions distributed in accordance with this Agreement.
|
||||||
|
|
||||||
|
"Recipient" means anyone who receives the Program under this Agreement, including all Contributors.
|
||||||
|
|
||||||
|
2. GRANT OF RIGHTS
|
||||||
|
|
||||||
|
a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, distribute and sublicense the Contribution of such Contributor, if any, and such derivative works, in source code and object code form.
|
||||||
|
b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in source code and object code form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder.
|
||||||
|
c) Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to distribute the Program, it is Recipient's responsibility to acquire that license before distributing the Program.
|
||||||
|
d) Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement.
|
||||||
|
3. REQUIREMENTS
|
||||||
|
|
||||||
|
A Contributor may choose to distribute the Program in object code form under its own license agreement, provided that:
|
||||||
|
|
||||||
|
a) it complies with the terms and conditions of this Agreement; and
|
||||||
|
b) its license agreement:
|
||||||
|
i) effectively disclaims on behalf of all Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose;
|
||||||
|
ii) effectively excludes on behalf of all Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits;
|
||||||
|
iii) states that any provisions which differ from this Agreement are offered by that Contributor alone and not by any other party; and
|
||||||
|
iv) states that source code for the Program is available from such Contributor, and informs licensees how to obtain it in a reasonable manner on or through a medium customarily used for software exchange.
|
||||||
|
When the Program is made available in source code form:
|
||||||
|
|
||||||
|
a) it must be made available under this Agreement; and
|
||||||
|
b) a copy of this Agreement must be included with each copy of the Program.
|
||||||
|
Contributors may not remove or alter any copyright notices contained within the Program.
|
||||||
|
|
||||||
|
Each Contributor must identify itself as the originator of its Contribution, if any, in a manner that reasonably allows subsequent Recipients to identify the originator of the Contribution.
|
||||||
|
|
||||||
|
4. COMMERCIAL DISTRIBUTION
|
||||||
|
|
||||||
|
Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor ("Commercial Contributor") hereby agrees to defend and indemnify every other Contributor ("Indemnified Contributor") against any losses, damages and costs (collectively "Losses") arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense.
|
||||||
|
|
||||||
|
For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages.
|
||||||
|
|
||||||
|
5. NO WARRANTY
|
||||||
|
|
||||||
|
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement , including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations.
|
||||||
|
|
||||||
|
6. DISCLAIMER OF LIABILITY
|
||||||
|
|
||||||
|
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||||
|
|
||||||
|
7. GENERAL
|
||||||
|
|
||||||
|
If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable.
|
||||||
|
|
||||||
|
If Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 2(b) shall terminate as of the date such litigation is filed.
|
||||||
|
|
||||||
|
All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive.
|
||||||
|
|
||||||
|
Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. The Eclipse Foundation is the initial Agreement Steward. The Eclipse Foundation may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to distribute the Program (including its Contributions) under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved.
|
||||||
|
|
||||||
|
This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation.
|
235
vendor/github.com/eclipse/paho.mqtt.golang/filestore.go
generated
vendored
Normal file
235
vendor/github.com/eclipse/paho.mqtt.golang/filestore.go
generated
vendored
Normal file
@ -0,0 +1,235 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/eclipse/paho.mqtt.golang/packets"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
msgExt = ".msg"
|
||||||
|
tmpExt = ".tmp"
|
||||||
|
corruptExt = ".CORRUPT"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FileStore implements the store interface using the filesystem to provide
|
||||||
|
// true persistence, even across client failure. This is designed to use a
|
||||||
|
// single directory per running client. If you are running multiple clients
|
||||||
|
// on the same filesystem, you will need to be careful to specify unique
|
||||||
|
// store directories for each.
|
||||||
|
type FileStore struct {
|
||||||
|
sync.RWMutex
|
||||||
|
directory string
|
||||||
|
opened bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFileStore will create a new FileStore which stores its messages in the
|
||||||
|
// directory provided.
|
||||||
|
func NewFileStore(directory string) *FileStore {
|
||||||
|
store := &FileStore{
|
||||||
|
directory: directory,
|
||||||
|
opened: false,
|
||||||
|
}
|
||||||
|
return store
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open will allow the FileStore to be used.
|
||||||
|
func (store *FileStore) Open() {
|
||||||
|
store.Lock()
|
||||||
|
defer store.Unlock()
|
||||||
|
// if no store directory was specified in ClientOpts, by default use the
|
||||||
|
// current working directory
|
||||||
|
if store.directory == "" {
|
||||||
|
store.directory, _ = os.Getwd()
|
||||||
|
}
|
||||||
|
|
||||||
|
// if store dir exists, great, otherwise, create it
|
||||||
|
if !exists(store.directory) {
|
||||||
|
perms := os.FileMode(0770)
|
||||||
|
merr := os.MkdirAll(store.directory, perms)
|
||||||
|
chkerr(merr)
|
||||||
|
}
|
||||||
|
store.opened = true
|
||||||
|
DEBUG.Println(STR, "store is opened at", store.directory)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close will disallow the FileStore from being used.
|
||||||
|
func (store *FileStore) Close() {
|
||||||
|
store.Lock()
|
||||||
|
defer store.Unlock()
|
||||||
|
store.opened = false
|
||||||
|
DEBUG.Println(STR, "store is closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put will put a message into the store, associated with the provided
|
||||||
|
// key value.
|
||||||
|
func (store *FileStore) Put(key string, m packets.ControlPacket) {
|
||||||
|
store.Lock()
|
||||||
|
defer store.Unlock()
|
||||||
|
if !store.opened {
|
||||||
|
ERROR.Println(STR, "Trying to use file store, but not open")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
full := fullpath(store.directory, key)
|
||||||
|
write(store.directory, key, m)
|
||||||
|
if !exists(full) {
|
||||||
|
ERROR.Println(STR, "file not created:", full)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get will retrieve a message from the store, the one associated with
|
||||||
|
// the provided key value.
|
||||||
|
func (store *FileStore) Get(key string) packets.ControlPacket {
|
||||||
|
store.RLock()
|
||||||
|
defer store.RUnlock()
|
||||||
|
if !store.opened {
|
||||||
|
ERROR.Println(STR, "Trying to use file store, but not open")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
filepath := fullpath(store.directory, key)
|
||||||
|
if !exists(filepath) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
mfile, oerr := os.Open(filepath)
|
||||||
|
chkerr(oerr)
|
||||||
|
msg, rerr := packets.ReadPacket(mfile)
|
||||||
|
chkerr(mfile.Close())
|
||||||
|
|
||||||
|
// Message was unreadable, return nil
|
||||||
|
if rerr != nil {
|
||||||
|
newpath := corruptpath(store.directory, key)
|
||||||
|
WARN.Println(STR, "corrupted file detected:", rerr.Error(), "archived at:", newpath)
|
||||||
|
os.Rename(filepath, newpath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
|
||||||
|
// All will provide a list of all of the keys associated with messages
|
||||||
|
// currenly residing in the FileStore.
|
||||||
|
func (store *FileStore) All() []string {
|
||||||
|
store.RLock()
|
||||||
|
defer store.RUnlock()
|
||||||
|
return store.all()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Del will remove the persisted message associated with the provided
|
||||||
|
// key from the FileStore.
|
||||||
|
func (store *FileStore) Del(key string) {
|
||||||
|
store.Lock()
|
||||||
|
defer store.Unlock()
|
||||||
|
store.del(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset will remove all persisted messages from the FileStore.
|
||||||
|
func (store *FileStore) Reset() {
|
||||||
|
store.Lock()
|
||||||
|
defer store.Unlock()
|
||||||
|
WARN.Println(STR, "FileStore Reset")
|
||||||
|
for _, key := range store.all() {
|
||||||
|
store.del(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// lockless
|
||||||
|
func (store *FileStore) all() []string {
|
||||||
|
if !store.opened {
|
||||||
|
ERROR.Println(STR, "Trying to use file store, but not open")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
keys := []string{}
|
||||||
|
files, rderr := ioutil.ReadDir(store.directory)
|
||||||
|
chkerr(rderr)
|
||||||
|
for _, f := range files {
|
||||||
|
DEBUG.Println(STR, "file in All():", f.Name())
|
||||||
|
name := f.Name()
|
||||||
|
if name[len(name)-4:len(name)] != msgExt {
|
||||||
|
DEBUG.Println(STR, "skipping file, doesn't have right extension: ", name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
key := name[0 : len(name)-4] // remove file extension
|
||||||
|
keys = append(keys, key)
|
||||||
|
}
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
// lockless
|
||||||
|
func (store *FileStore) del(key string) {
|
||||||
|
if !store.opened {
|
||||||
|
ERROR.Println(STR, "Trying to use file store, but not open")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
DEBUG.Println(STR, "store del filepath:", store.directory)
|
||||||
|
DEBUG.Println(STR, "store delete key:", key)
|
||||||
|
filepath := fullpath(store.directory, key)
|
||||||
|
DEBUG.Println(STR, "path of deletion:", filepath)
|
||||||
|
if !exists(filepath) {
|
||||||
|
WARN.Println(STR, "store could not delete key:", key)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rerr := os.Remove(filepath)
|
||||||
|
chkerr(rerr)
|
||||||
|
DEBUG.Println(STR, "del msg:", key)
|
||||||
|
if exists(filepath) {
|
||||||
|
ERROR.Println(STR, "file not deleted:", filepath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func fullpath(store string, key string) string {
|
||||||
|
p := path.Join(store, key+msgExt)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func tmppath(store string, key string) string {
|
||||||
|
p := path.Join(store, key+tmpExt)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func corruptpath(store string, key string) string {
|
||||||
|
p := path.Join(store, key+corruptExt)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// create file called "X.[messageid].tmp" located in the store
|
||||||
|
// the contents of the file is the bytes of the message, then
|
||||||
|
// rename it to "X.[messageid].msg", overwriting any existing
|
||||||
|
// message with the same id
|
||||||
|
// X will be 'i' for inbound messages, and O for outbound messages
|
||||||
|
func write(store, key string, m packets.ControlPacket) {
|
||||||
|
temppath := tmppath(store, key)
|
||||||
|
f, err := os.Create(temppath)
|
||||||
|
chkerr(err)
|
||||||
|
werr := m.Write(f)
|
||||||
|
chkerr(werr)
|
||||||
|
cerr := f.Close()
|
||||||
|
chkerr(cerr)
|
||||||
|
rerr := os.Rename(temppath, fullpath(store, key))
|
||||||
|
chkerr(rerr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func exists(file string) bool {
|
||||||
|
if _, err := os.Stat(file); err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
chkerr(err)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
138
vendor/github.com/eclipse/paho.mqtt.golang/memstore.go
generated
vendored
Normal file
138
vendor/github.com/eclipse/paho.mqtt.golang/memstore.go
generated
vendored
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/eclipse/paho.mqtt.golang/packets"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MemoryStore implements the store interface to provide a "persistence"
|
||||||
|
// mechanism wholly stored in memory. This is only useful for
|
||||||
|
// as long as the client instance exists.
|
||||||
|
type MemoryStore struct {
|
||||||
|
sync.RWMutex
|
||||||
|
messages map[string]packets.ControlPacket
|
||||||
|
opened bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMemoryStore returns a pointer to a new instance of
|
||||||
|
// MemoryStore, the instance is not initialized and ready to
|
||||||
|
// use until Open() has been called on it.
|
||||||
|
func NewMemoryStore() *MemoryStore {
|
||||||
|
store := &MemoryStore{
|
||||||
|
messages: make(map[string]packets.ControlPacket),
|
||||||
|
opened: false,
|
||||||
|
}
|
||||||
|
return store
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open initializes a MemoryStore instance.
|
||||||
|
func (store *MemoryStore) Open() {
|
||||||
|
store.Lock()
|
||||||
|
defer store.Unlock()
|
||||||
|
store.opened = true
|
||||||
|
DEBUG.Println(STR, "memorystore initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put takes a key and a pointer to a Message and stores the
|
||||||
|
// message.
|
||||||
|
func (store *MemoryStore) Put(key string, message packets.ControlPacket) {
|
||||||
|
store.Lock()
|
||||||
|
defer store.Unlock()
|
||||||
|
if !store.opened {
|
||||||
|
ERROR.Println(STR, "Trying to use memory store, but not open")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
store.messages[key] = message
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get takes a key and looks in the store for a matching Message
|
||||||
|
// returning either the Message pointer or nil.
|
||||||
|
func (store *MemoryStore) Get(key string) packets.ControlPacket {
|
||||||
|
store.RLock()
|
||||||
|
defer store.RUnlock()
|
||||||
|
if !store.opened {
|
||||||
|
ERROR.Println(STR, "Trying to use memory store, but not open")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
mid := mIDFromKey(key)
|
||||||
|
m := store.messages[key]
|
||||||
|
if m == nil {
|
||||||
|
CRITICAL.Println(STR, "memorystore get: message", mid, "not found")
|
||||||
|
} else {
|
||||||
|
DEBUG.Println(STR, "memorystore get: message", mid, "found")
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
// All returns a slice of strings containing all the keys currently
|
||||||
|
// in the MemoryStore.
|
||||||
|
func (store *MemoryStore) All() []string {
|
||||||
|
store.RLock()
|
||||||
|
defer store.RUnlock()
|
||||||
|
if !store.opened {
|
||||||
|
ERROR.Println(STR, "Trying to use memory store, but not open")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
keys := []string{}
|
||||||
|
for k := range store.messages {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
// Del takes a key, searches the MemoryStore and if the key is found
|
||||||
|
// deletes the Message pointer associated with it.
|
||||||
|
func (store *MemoryStore) Del(key string) {
|
||||||
|
store.Lock()
|
||||||
|
defer store.Unlock()
|
||||||
|
if !store.opened {
|
||||||
|
ERROR.Println(STR, "Trying to use memory store, but not open")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mid := mIDFromKey(key)
|
||||||
|
m := store.messages[key]
|
||||||
|
if m == nil {
|
||||||
|
WARN.Println(STR, "memorystore del: message", mid, "not found")
|
||||||
|
} else {
|
||||||
|
store.messages[key] = nil
|
||||||
|
DEBUG.Println(STR, "memorystore del: message", mid, "was deleted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close will disallow modifications to the state of the store.
|
||||||
|
func (store *MemoryStore) Close() {
|
||||||
|
store.Lock()
|
||||||
|
defer store.Unlock()
|
||||||
|
if !store.opened {
|
||||||
|
ERROR.Println(STR, "Trying to close memory store, but not open")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
store.opened = false
|
||||||
|
DEBUG.Println(STR, "memorystore closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset eliminates all persisted message data in the store.
|
||||||
|
func (store *MemoryStore) Reset() {
|
||||||
|
store.Lock()
|
||||||
|
defer store.Unlock()
|
||||||
|
if !store.opened {
|
||||||
|
ERROR.Println(STR, "Trying to reset memory store, but not open")
|
||||||
|
}
|
||||||
|
store.messages = make(map[string]packets.ControlPacket)
|
||||||
|
WARN.Println(STR, "memorystore wiped")
|
||||||
|
}
|
104
vendor/github.com/eclipse/paho.mqtt.golang/message.go
generated
vendored
Normal file
104
vendor/github.com/eclipse/paho.mqtt.golang/message.go
generated
vendored
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/eclipse/paho.mqtt.golang/packets"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Message defines the externals that a message implementation must support
|
||||||
|
// these are received messages that are passed to the callbacks, not internal
|
||||||
|
// messages
|
||||||
|
type Message interface {
|
||||||
|
Duplicate() bool
|
||||||
|
Qos() byte
|
||||||
|
Retained() bool
|
||||||
|
Topic() string
|
||||||
|
MessageID() uint16
|
||||||
|
Payload() []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type message struct {
|
||||||
|
duplicate bool
|
||||||
|
qos byte
|
||||||
|
retained bool
|
||||||
|
topic string
|
||||||
|
messageID uint16
|
||||||
|
payload []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *message) Duplicate() bool {
|
||||||
|
return m.duplicate
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *message) Qos() byte {
|
||||||
|
return m.qos
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *message) Retained() bool {
|
||||||
|
return m.retained
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *message) Topic() string {
|
||||||
|
return m.topic
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *message) MessageID() uint16 {
|
||||||
|
return m.messageID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *message) Payload() []byte {
|
||||||
|
return m.payload
|
||||||
|
}
|
||||||
|
|
||||||
|
func messageFromPublish(p *packets.PublishPacket) Message {
|
||||||
|
return &message{
|
||||||
|
duplicate: p.Dup,
|
||||||
|
qos: p.Qos,
|
||||||
|
retained: p.Retain,
|
||||||
|
topic: p.TopicName,
|
||||||
|
messageID: p.MessageID,
|
||||||
|
payload: p.Payload,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newConnectMsgFromOptions(options *ClientOptions) *packets.ConnectPacket {
|
||||||
|
m := packets.NewControlPacket(packets.Connect).(*packets.ConnectPacket)
|
||||||
|
|
||||||
|
m.CleanSession = options.CleanSession
|
||||||
|
m.WillFlag = options.WillEnabled
|
||||||
|
m.WillRetain = options.WillRetained
|
||||||
|
m.ClientIdentifier = options.ClientID
|
||||||
|
|
||||||
|
if options.WillEnabled {
|
||||||
|
m.WillQos = options.WillQos
|
||||||
|
m.WillTopic = options.WillTopic
|
||||||
|
m.WillMessage = options.WillPayload
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.Username != "" {
|
||||||
|
m.UsernameFlag = true
|
||||||
|
m.Username = options.Username
|
||||||
|
//mustn't have password without user as well
|
||||||
|
if options.Password != "" {
|
||||||
|
m.PasswordFlag = true
|
||||||
|
m.Password = []byte(options.Password)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Keepalive = uint16(options.KeepAlive.Seconds())
|
||||||
|
|
||||||
|
return m
|
||||||
|
}
|
79
vendor/github.com/eclipse/paho.mqtt.golang/messageids.go
generated
vendored
Normal file
79
vendor/github.com/eclipse/paho.mqtt.golang/messageids.go
generated
vendored
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MId is 16 bit message id as specified by the MQTT spec.
|
||||||
|
// In general, these values should not be depended upon by
|
||||||
|
// the client application.
|
||||||
|
type MId uint16
|
||||||
|
|
||||||
|
type messageIds struct {
|
||||||
|
sync.RWMutex
|
||||||
|
index map[uint16]Token
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
midMin uint16 = 1
|
||||||
|
midMax uint16 = 65535
|
||||||
|
)
|
||||||
|
|
||||||
|
func (mids *messageIds) cleanUp() {
|
||||||
|
mids.Lock()
|
||||||
|
for _, token := range mids.index {
|
||||||
|
switch t := token.(type) {
|
||||||
|
case *PublishToken:
|
||||||
|
t.err = fmt.Errorf("Connection lost before Publish completed")
|
||||||
|
case *SubscribeToken:
|
||||||
|
t.err = fmt.Errorf("Connection lost before Subscribe completed")
|
||||||
|
case *UnsubscribeToken:
|
||||||
|
t.err = fmt.Errorf("Connection lost before Unsubscribe completed")
|
||||||
|
}
|
||||||
|
token.flowComplete()
|
||||||
|
}
|
||||||
|
mids.index = make(map[uint16]Token)
|
||||||
|
mids.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mids *messageIds) freeID(id uint16) {
|
||||||
|
mids.Lock()
|
||||||
|
delete(mids.index, id)
|
||||||
|
mids.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mids *messageIds) getID(t Token) uint16 {
|
||||||
|
mids.Lock()
|
||||||
|
defer mids.Unlock()
|
||||||
|
for i := midMin; i < midMax; i++ {
|
||||||
|
if _, ok := mids.index[i]; !ok {
|
||||||
|
mids.index[i] = t
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mids *messageIds) getToken(id uint16) Token {
|
||||||
|
mids.RLock()
|
||||||
|
defer mids.RUnlock()
|
||||||
|
if token, ok := mids.index[id]; ok {
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
285
vendor/github.com/eclipse/paho.mqtt.golang/net.go
generated
vendored
Normal file
285
vendor/github.com/eclipse/paho.mqtt.golang/net.go
generated
vendored
Normal file
@ -0,0 +1,285 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/url"
|
||||||
|
"reflect"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/eclipse/paho.mqtt.golang/packets"
|
||||||
|
"golang.org/x/net/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
func signalError(c chan<- error, err error) {
|
||||||
|
select {
|
||||||
|
case c <- err:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func openConnection(uri *url.URL, tlsc *tls.Config, timeout time.Duration) (net.Conn, error) {
|
||||||
|
switch uri.Scheme {
|
||||||
|
case "ws":
|
||||||
|
conn, err := websocket.Dial(uri.String(), "mqtt", fmt.Sprintf("http://%s", uri.Host))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
conn.PayloadType = websocket.BinaryFrame
|
||||||
|
return conn, err
|
||||||
|
case "wss":
|
||||||
|
config, _ := websocket.NewConfig(uri.String(), fmt.Sprintf("https://%s", uri.Host))
|
||||||
|
config.Protocol = []string{"mqtt"}
|
||||||
|
config.TlsConfig = tlsc
|
||||||
|
conn, err := websocket.DialConfig(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
conn.PayloadType = websocket.BinaryFrame
|
||||||
|
return conn, err
|
||||||
|
case "tcp":
|
||||||
|
conn, err := net.DialTimeout("tcp", uri.Host, timeout)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return conn, nil
|
||||||
|
case "ssl":
|
||||||
|
fallthrough
|
||||||
|
case "tls":
|
||||||
|
fallthrough
|
||||||
|
case "tcps":
|
||||||
|
conn, err := tls.DialWithDialer(&net.Dialer{Timeout: timeout}, "tcp", uri.Host, tlsc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
return nil, errors.New("Unknown protocol")
|
||||||
|
}
|
||||||
|
|
||||||
|
// actually read incoming messages off the wire
|
||||||
|
// send Message object into ibound channel
|
||||||
|
func incoming(c *client) {
|
||||||
|
var err error
|
||||||
|
var cp packets.ControlPacket
|
||||||
|
|
||||||
|
defer c.workers.Done()
|
||||||
|
|
||||||
|
DEBUG.Println(NET, "incoming started")
|
||||||
|
|
||||||
|
for {
|
||||||
|
if cp, err = packets.ReadPacket(c.conn); err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
DEBUG.Println(NET, "Received Message")
|
||||||
|
select {
|
||||||
|
case c.ibound <- cp:
|
||||||
|
// Notify keepalive logic that we recently received a packet
|
||||||
|
if c.options.KeepAlive != 0 {
|
||||||
|
c.packetResp <- struct{}{}
|
||||||
|
}
|
||||||
|
case <-c.stop:
|
||||||
|
// This avoids a deadlock should a message arrive while shutting down.
|
||||||
|
// In that case the "reader" of c.ibound might already be gone
|
||||||
|
WARN.Println(NET, "incoming dropped a received message during shutdown")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// We received an error on read.
|
||||||
|
// If disconnect is in progress, swallow error and return
|
||||||
|
select {
|
||||||
|
case <-c.stop:
|
||||||
|
DEBUG.Println(NET, "incoming stopped")
|
||||||
|
return
|
||||||
|
// Not trying to disconnect, send the error to the errors channel
|
||||||
|
default:
|
||||||
|
ERROR.Println(NET, "incoming stopped with error", err)
|
||||||
|
signalError(c.errors, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// receive a Message object on obound, and then
|
||||||
|
// actually send outgoing message to the wire
|
||||||
|
func outgoing(c *client) {
|
||||||
|
defer c.workers.Done()
|
||||||
|
DEBUG.Println(NET, "outgoing started")
|
||||||
|
|
||||||
|
for {
|
||||||
|
DEBUG.Println(NET, "outgoing waiting for an outbound message")
|
||||||
|
select {
|
||||||
|
case <-c.stop:
|
||||||
|
DEBUG.Println(NET, "outgoing stopped")
|
||||||
|
return
|
||||||
|
case pub := <-c.obound:
|
||||||
|
msg := pub.p.(*packets.PublishPacket)
|
||||||
|
|
||||||
|
if c.options.WriteTimeout > 0 {
|
||||||
|
c.conn.SetWriteDeadline(time.Now().Add(c.options.WriteTimeout))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := msg.Write(c.conn); err != nil {
|
||||||
|
ERROR.Println(NET, "outgoing stopped with error", err)
|
||||||
|
signalError(c.errors, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.options.WriteTimeout > 0 {
|
||||||
|
// If we successfully wrote, we don't want the timeout to happen during an idle period
|
||||||
|
// so we reset it to infinite.
|
||||||
|
c.conn.SetWriteDeadline(time.Time{})
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg.Qos == 0 {
|
||||||
|
pub.t.flowComplete()
|
||||||
|
}
|
||||||
|
DEBUG.Println(NET, "obound wrote msg, id:", msg.MessageID)
|
||||||
|
case msg := <-c.oboundP:
|
||||||
|
switch msg.p.(type) {
|
||||||
|
case *packets.SubscribePacket:
|
||||||
|
msg.p.(*packets.SubscribePacket).MessageID = c.getID(msg.t)
|
||||||
|
case *packets.UnsubscribePacket:
|
||||||
|
msg.p.(*packets.UnsubscribePacket).MessageID = c.getID(msg.t)
|
||||||
|
}
|
||||||
|
DEBUG.Println(NET, "obound priority msg to write, type", reflect.TypeOf(msg.p))
|
||||||
|
if err := msg.p.Write(c.conn); err != nil {
|
||||||
|
ERROR.Println(NET, "outgoing stopped with error", err)
|
||||||
|
signalError(c.errors, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch msg.p.(type) {
|
||||||
|
case *packets.DisconnectPacket:
|
||||||
|
msg.t.(*DisconnectToken).flowComplete()
|
||||||
|
DEBUG.Println(NET, "outbound wrote disconnect, stopping")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Reset ping timer after sending control packet.
|
||||||
|
if c.options.KeepAlive != 0 {
|
||||||
|
select {
|
||||||
|
case c.keepaliveReset <- struct{}{}:
|
||||||
|
default:
|
||||||
|
DEBUG.Println(NET, "couldn't send keepalive signal in outbound as channel full")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// receive Message objects on ibound
|
||||||
|
// store messages if necessary
|
||||||
|
// send replies on obound
|
||||||
|
// delete messages from store if necessary
|
||||||
|
func alllogic(c *client) {
|
||||||
|
defer c.workers.Done()
|
||||||
|
DEBUG.Println(NET, "logic started")
|
||||||
|
|
||||||
|
for {
|
||||||
|
DEBUG.Println(NET, "logic waiting for msg on ibound")
|
||||||
|
|
||||||
|
select {
|
||||||
|
case msg := <-c.ibound:
|
||||||
|
DEBUG.Println(NET, "logic got msg on ibound")
|
||||||
|
persistInbound(c.persist, msg)
|
||||||
|
switch m := msg.(type) {
|
||||||
|
case *packets.PingrespPacket:
|
||||||
|
DEBUG.Println(NET, "received pingresp")
|
||||||
|
c.pingResp <- struct{}{}
|
||||||
|
case *packets.SubackPacket:
|
||||||
|
DEBUG.Println(NET, "received suback, id:", m.MessageID)
|
||||||
|
token := c.getToken(m.MessageID).(*SubscribeToken)
|
||||||
|
DEBUG.Println(NET, "granted qoss", m.ReturnCodes)
|
||||||
|
for i, qos := range m.ReturnCodes {
|
||||||
|
token.subResult[token.subs[i]] = qos
|
||||||
|
}
|
||||||
|
token.flowComplete()
|
||||||
|
c.freeID(m.MessageID)
|
||||||
|
case *packets.UnsubackPacket:
|
||||||
|
DEBUG.Println(NET, "received unsuback, id:", m.MessageID)
|
||||||
|
token := c.getToken(m.MessageID).(*UnsubscribeToken)
|
||||||
|
token.flowComplete()
|
||||||
|
c.freeID(m.MessageID)
|
||||||
|
case *packets.PublishPacket:
|
||||||
|
DEBUG.Println(NET, "received publish, msgId:", m.MessageID)
|
||||||
|
DEBUG.Println(NET, "putting msg on onPubChan")
|
||||||
|
switch m.Qos {
|
||||||
|
case 2:
|
||||||
|
c.incomingPubChan <- m
|
||||||
|
DEBUG.Println(NET, "done putting msg on incomingPubChan")
|
||||||
|
pr := packets.NewControlPacket(packets.Pubrec).(*packets.PubrecPacket)
|
||||||
|
pr.MessageID = m.MessageID
|
||||||
|
DEBUG.Println(NET, "putting pubrec msg on obound")
|
||||||
|
c.oboundP <- &PacketAndToken{p: pr, t: nil}
|
||||||
|
DEBUG.Println(NET, "done putting pubrec msg on obound")
|
||||||
|
case 1:
|
||||||
|
c.incomingPubChan <- m
|
||||||
|
DEBUG.Println(NET, "done putting msg on incomingPubChan")
|
||||||
|
pa := packets.NewControlPacket(packets.Puback).(*packets.PubackPacket)
|
||||||
|
pa.MessageID = m.MessageID
|
||||||
|
DEBUG.Println(NET, "putting puback msg on obound")
|
||||||
|
c.oboundP <- &PacketAndToken{p: pa, t: nil}
|
||||||
|
DEBUG.Println(NET, "done putting puback msg on obound")
|
||||||
|
case 0:
|
||||||
|
c.incomingPubChan <- m
|
||||||
|
DEBUG.Println(NET, "done putting msg on incomingPubChan")
|
||||||
|
}
|
||||||
|
case *packets.PubackPacket:
|
||||||
|
DEBUG.Println(NET, "received puback, id:", m.MessageID)
|
||||||
|
// c.receipts.get(msg.MsgId()) <- Receipt{}
|
||||||
|
// c.receipts.end(msg.MsgId())
|
||||||
|
c.getToken(m.MessageID).flowComplete()
|
||||||
|
c.freeID(m.MessageID)
|
||||||
|
case *packets.PubrecPacket:
|
||||||
|
DEBUG.Println(NET, "received pubrec, id:", m.MessageID)
|
||||||
|
prel := packets.NewControlPacket(packets.Pubrel).(*packets.PubrelPacket)
|
||||||
|
prel.MessageID = m.MessageID
|
||||||
|
select {
|
||||||
|
case c.oboundP <- &PacketAndToken{p: prel, t: nil}:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
}
|
||||||
|
case *packets.PubrelPacket:
|
||||||
|
DEBUG.Println(NET, "received pubrel, id:", m.MessageID)
|
||||||
|
pc := packets.NewControlPacket(packets.Pubcomp).(*packets.PubcompPacket)
|
||||||
|
pc.MessageID = m.MessageID
|
||||||
|
select {
|
||||||
|
case c.oboundP <- &PacketAndToken{p: pc, t: nil}:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
}
|
||||||
|
case *packets.PubcompPacket:
|
||||||
|
DEBUG.Println(NET, "received pubcomp, id:", m.MessageID)
|
||||||
|
c.getToken(m.MessageID).flowComplete()
|
||||||
|
c.freeID(m.MessageID)
|
||||||
|
}
|
||||||
|
case <-c.stop:
|
||||||
|
WARN.Println(NET, "logic stopped")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func errorWatch(c *client) {
|
||||||
|
select {
|
||||||
|
case <-c.stop:
|
||||||
|
WARN.Println(NET, "errorWatch stopped")
|
||||||
|
return
|
||||||
|
case err := <-c.errors:
|
||||||
|
ERROR.Println(NET, "error triggered, stopping")
|
||||||
|
c.internalConnLost(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
108
vendor/github.com/eclipse/paho.mqtt.golang/notice.html
generated
vendored
Normal file
108
vendor/github.com/eclipse/paho.mqtt.golang/notice.html
generated
vendored
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
|
||||||
|
<title>Eclipse Foundation Software User Agreement</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body lang="EN-US">
|
||||||
|
<h2>Eclipse Foundation Software User Agreement</h2>
|
||||||
|
<p>February 1, 2011</p>
|
||||||
|
|
||||||
|
<h3>Usage Of Content</h3>
|
||||||
|
|
||||||
|
<p>THE ECLIPSE FOUNDATION MAKES AVAILABLE SOFTWARE, DOCUMENTATION, INFORMATION AND/OR OTHER MATERIALS FOR OPEN SOURCE PROJECTS
|
||||||
|
(COLLECTIVELY "CONTENT"). USE OF THE CONTENT IS GOVERNED BY THE TERMS AND CONDITIONS OF THIS AGREEMENT AND/OR THE TERMS AND
|
||||||
|
CONDITIONS OF LICENSE AGREEMENTS OR NOTICES INDICATED OR REFERENCED BELOW. BY USING THE CONTENT, YOU AGREE THAT YOUR USE
|
||||||
|
OF THE CONTENT IS GOVERNED BY THIS AGREEMENT AND/OR THE TERMS AND CONDITIONS OF ANY APPLICABLE LICENSE AGREEMENTS OR
|
||||||
|
NOTICES INDICATED OR REFERENCED BELOW. IF YOU DO NOT AGREE TO THE TERMS AND CONDITIONS OF THIS AGREEMENT AND THE TERMS AND
|
||||||
|
CONDITIONS OF ANY APPLICABLE LICENSE AGREEMENTS OR NOTICES INDICATED OR REFERENCED BELOW, THEN YOU MAY NOT USE THE CONTENT.</p>
|
||||||
|
|
||||||
|
<h3>Applicable Licenses</h3>
|
||||||
|
|
||||||
|
<p>Unless otherwise indicated, all Content made available by the Eclipse Foundation is provided to you under the terms and conditions of the Eclipse Public License Version 1.0
|
||||||
|
("EPL"). A copy of the EPL is provided with this Content and is also available at <a href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a>.
|
||||||
|
For purposes of the EPL, "Program" will mean the Content.</p>
|
||||||
|
|
||||||
|
<p>Content includes, but is not limited to, source code, object code, documentation and other files maintained in the Eclipse Foundation source code
|
||||||
|
repository ("Repository") in software modules ("Modules") and made available as downloadable archives ("Downloads").</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>Content may be structured and packaged into modules to facilitate delivering, extending, and upgrading the Content. Typical modules may include plug-ins ("Plug-ins"), plug-in fragments ("Fragments"), and features ("Features").</li>
|
||||||
|
<li>Each Plug-in or Fragment may be packaged as a sub-directory or JAR (Java™ ARchive) in a directory named "plugins".</li>
|
||||||
|
<li>A Feature is a bundle of one or more Plug-ins and/or Fragments and associated material. Each Feature may be packaged as a sub-directory in a directory named "features". Within a Feature, files named "feature.xml" may contain a list of the names and version numbers of the Plug-ins
|
||||||
|
and/or Fragments associated with that Feature.</li>
|
||||||
|
<li>Features may also include other Features ("Included Features"). Within a Feature, files named "feature.xml" may contain a list of the names and version numbers of Included Features.</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>The terms and conditions governing Plug-ins and Fragments should be contained in files named "about.html" ("Abouts"). The terms and conditions governing Features and
|
||||||
|
Included Features should be contained in files named "license.html" ("Feature Licenses"). Abouts and Feature Licenses may be located in any directory of a Download or Module
|
||||||
|
including, but not limited to the following locations:</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>The top-level (root) directory</li>
|
||||||
|
<li>Plug-in and Fragment directories</li>
|
||||||
|
<li>Inside Plug-ins and Fragments packaged as JARs</li>
|
||||||
|
<li>Sub-directories of the directory named "src" of certain Plug-ins</li>
|
||||||
|
<li>Feature directories</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>Note: if a Feature made available by the Eclipse Foundation is installed using the Provisioning Technology (as defined below), you must agree to a license ("Feature Update License") during the
|
||||||
|
installation process. If the Feature contains Included Features, the Feature Update License should either provide you with the terms and conditions governing the Included Features or
|
||||||
|
inform you where you can locate them. Feature Update Licenses may be found in the "license" property of files named "feature.properties" found within a Feature.
|
||||||
|
Such Abouts, Feature Licenses, and Feature Update Licenses contain the terms and conditions (or references to such terms and conditions) that govern your use of the associated Content in
|
||||||
|
that directory.</p>
|
||||||
|
|
||||||
|
<p>THE ABOUTS, FEATURE LICENSES, AND FEATURE UPDATE LICENSES MAY REFER TO THE EPL OR OTHER LICENSE AGREEMENTS, NOTICES OR TERMS AND CONDITIONS. SOME OF THESE
|
||||||
|
OTHER LICENSE AGREEMENTS MAY INCLUDE (BUT ARE NOT LIMITED TO):</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>Eclipse Distribution License Version 1.0 (available at <a href="http://www.eclipse.org/licenses/edl-v10.html">http://www.eclipse.org/licenses/edl-v1.0.html</a>)</li>
|
||||||
|
<li>Common Public License Version 1.0 (available at <a href="http://www.eclipse.org/legal/cpl-v10.html">http://www.eclipse.org/legal/cpl-v10.html</a>)</li>
|
||||||
|
<li>Apache Software License 1.1 (available at <a href="http://www.apache.org/licenses/LICENSE">http://www.apache.org/licenses/LICENSE</a>)</li>
|
||||||
|
<li>Apache Software License 2.0 (available at <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>)</li>
|
||||||
|
<li>Metro Link Public License 1.00 (available at <a href="http://www.opengroup.org/openmotif/supporters/metrolink/license.html">http://www.opengroup.org/openmotif/supporters/metrolink/license.html</a>)</li>
|
||||||
|
<li>Mozilla Public License Version 1.1 (available at <a href="http://www.mozilla.org/MPL/MPL-1.1.html">http://www.mozilla.org/MPL/MPL-1.1.html</a>)</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>IT IS YOUR OBLIGATION TO READ AND ACCEPT ALL SUCH TERMS AND CONDITIONS PRIOR TO USE OF THE CONTENT. If no About, Feature License, or Feature Update License is provided, please
|
||||||
|
contact the Eclipse Foundation to determine what terms and conditions govern that particular Content.</p>
|
||||||
|
|
||||||
|
|
||||||
|
<h3>Use of Provisioning Technology</h3>
|
||||||
|
|
||||||
|
<p>The Eclipse Foundation makes available provisioning software, examples of which include, but are not limited to, p2 and the Eclipse
|
||||||
|
Update Manager ("Provisioning Technology") for the purpose of allowing users to install software, documentation, information and/or
|
||||||
|
other materials (collectively "Installable Software"). This capability is provided with the intent of allowing such users to
|
||||||
|
install, extend and update Eclipse-based products. Information about packaging Installable Software is available at <a
|
||||||
|
href="http://eclipse.org/equinox/p2/repository_packaging.html">http://eclipse.org/equinox/p2/repository_packaging.html</a>
|
||||||
|
("Specification").</p>
|
||||||
|
|
||||||
|
<p>You may use Provisioning Technology to allow other parties to install Installable Software. You shall be responsible for enabling the
|
||||||
|
applicable license agreements relating to the Installable Software to be presented to, and accepted by, the users of the Provisioning Technology
|
||||||
|
in accordance with the Specification. By using Provisioning Technology in such a manner and making it available in accordance with the
|
||||||
|
Specification, you further acknowledge your agreement to, and the acquisition of all necessary rights to permit the following:</p>
|
||||||
|
|
||||||
|
<ol>
|
||||||
|
<li>A series of actions may occur ("Provisioning Process") in which a user may execute the Provisioning Technology
|
||||||
|
on a machine ("Target Machine") with the intent of installing, extending or updating the functionality of an Eclipse-based
|
||||||
|
product.</li>
|
||||||
|
<li>During the Provisioning Process, the Provisioning Technology may cause third party Installable Software or a portion thereof to be
|
||||||
|
accessed and copied to the Target Machine.</li>
|
||||||
|
<li>Pursuant to the Specification, you will provide to the user the terms and conditions that govern the use of the Installable
|
||||||
|
Software ("Installable Software Agreement") and such Installable Software Agreement shall be accessed from the Target
|
||||||
|
Machine in accordance with the Specification. Such Installable Software Agreement must inform the user of the terms and conditions that govern
|
||||||
|
the Installable Software and must solicit acceptance by the end user in the manner prescribed in such Installable Software Agreement. Upon such
|
||||||
|
indication of agreement by the user, the provisioning Technology will complete installation of the Installable Software.</li>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
<h3>Cryptography</h3>
|
||||||
|
|
||||||
|
<p>Content may contain encryption software. The country in which you are currently may have restrictions on the import, possession, and use, and/or re-export to
|
||||||
|
another country, of encryption software. BEFORE using any encryption software, please check the country's laws, regulations and policies concerning the import,
|
||||||
|
possession, or use, and re-export of encryption software, to see if this is permitted.</p>
|
||||||
|
|
||||||
|
<p><small>Java and all Java-based trademarks are trademarks of Oracle Corporation in the United States, other countries, or both.</small></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
21
vendor/github.com/eclipse/paho.mqtt.golang/oops.go
generated
vendored
Normal file
21
vendor/github.com/eclipse/paho.mqtt.golang/oops.go
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
func chkerr(e error) {
|
||||||
|
if e != nil {
|
||||||
|
panic(e)
|
||||||
|
}
|
||||||
|
}
|
293
vendor/github.com/eclipse/paho.mqtt.golang/options.go
generated
vendored
Normal file
293
vendor/github.com/eclipse/paho.mqtt.golang/options.go
generated
vendored
Normal file
@ -0,0 +1,293 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MessageHandler is a callback type which can be set to be
|
||||||
|
// executed upon the arrival of messages published to topics
|
||||||
|
// to which the client is subscribed.
|
||||||
|
type MessageHandler func(Client, Message)
|
||||||
|
|
||||||
|
// ConnectionLostHandler is a callback type which can be set to be
|
||||||
|
// executed upon an unintended disconnection from the MQTT broker.
|
||||||
|
// Disconnects caused by calling Disconnect or ForceDisconnect will
|
||||||
|
// not cause an OnConnectionLost callback to execute.
|
||||||
|
type ConnectionLostHandler func(Client, error)
|
||||||
|
|
||||||
|
// OnConnectHandler is a callback that is called when the client
|
||||||
|
// state changes from unconnected/disconnected to connected. Both
|
||||||
|
// at initial connection and on reconnection
|
||||||
|
type OnConnectHandler func(Client)
|
||||||
|
|
||||||
|
// ClientOptions contains configurable options for an Client.
|
||||||
|
type ClientOptions struct {
|
||||||
|
Servers []*url.URL
|
||||||
|
ClientID string
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
CleanSession bool
|
||||||
|
Order bool
|
||||||
|
WillEnabled bool
|
||||||
|
WillTopic string
|
||||||
|
WillPayload []byte
|
||||||
|
WillQos byte
|
||||||
|
WillRetained bool
|
||||||
|
ProtocolVersion uint
|
||||||
|
protocolVersionExplicit bool
|
||||||
|
TLSConfig tls.Config
|
||||||
|
KeepAlive time.Duration
|
||||||
|
PingTimeout time.Duration
|
||||||
|
ConnectTimeout time.Duration
|
||||||
|
MaxReconnectInterval time.Duration
|
||||||
|
AutoReconnect bool
|
||||||
|
Store Store
|
||||||
|
DefaultPublishHander MessageHandler
|
||||||
|
OnConnect OnConnectHandler
|
||||||
|
OnConnectionLost ConnectionLostHandler
|
||||||
|
WriteTimeout time.Duration
|
||||||
|
MessageChannelDepth uint
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClientOptions will create a new ClientClientOptions type with some
|
||||||
|
// default values.
|
||||||
|
// Port: 1883
|
||||||
|
// CleanSession: True
|
||||||
|
// Order: True
|
||||||
|
// KeepAlive: 30 (seconds)
|
||||||
|
// ConnectTimeout: 30 (seconds)
|
||||||
|
// MaxReconnectInterval 10 (minutes)
|
||||||
|
// AutoReconnect: True
|
||||||
|
func NewClientOptions() *ClientOptions {
|
||||||
|
o := &ClientOptions{
|
||||||
|
Servers: nil,
|
||||||
|
ClientID: "",
|
||||||
|
Username: "",
|
||||||
|
Password: "",
|
||||||
|
CleanSession: true,
|
||||||
|
Order: true,
|
||||||
|
WillEnabled: false,
|
||||||
|
WillTopic: "",
|
||||||
|
WillPayload: nil,
|
||||||
|
WillQos: 0,
|
||||||
|
WillRetained: false,
|
||||||
|
ProtocolVersion: 0,
|
||||||
|
protocolVersionExplicit: false,
|
||||||
|
TLSConfig: tls.Config{},
|
||||||
|
KeepAlive: 30 * time.Second,
|
||||||
|
PingTimeout: 10 * time.Second,
|
||||||
|
ConnectTimeout: 30 * time.Second,
|
||||||
|
MaxReconnectInterval: 10 * time.Minute,
|
||||||
|
AutoReconnect: true,
|
||||||
|
Store: nil,
|
||||||
|
OnConnect: nil,
|
||||||
|
OnConnectionLost: DefaultConnectionLostHandler,
|
||||||
|
WriteTimeout: 0, // 0 represents timeout disabled
|
||||||
|
MessageChannelDepth: 100,
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddBroker adds a broker URI to the list of brokers to be used. The format should be
|
||||||
|
// scheme://host:port
|
||||||
|
// Where "scheme" is one of "tcp", "ssl", or "ws", "host" is the ip-address (or hostname)
|
||||||
|
// and "port" is the port on which the broker is accepting connections.
|
||||||
|
func (o *ClientOptions) AddBroker(server string) *ClientOptions {
|
||||||
|
brokerURI, err := url.Parse(server)
|
||||||
|
if err == nil {
|
||||||
|
o.Servers = append(o.Servers, brokerURI)
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetClientID will set the client id to be used by this client when
|
||||||
|
// connecting to the MQTT broker. According to the MQTT v3.1 specification,
|
||||||
|
// a client id mus be no longer than 23 characters.
|
||||||
|
func (o *ClientOptions) SetClientID(id string) *ClientOptions {
|
||||||
|
o.ClientID = id
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetUsername will set the username to be used by this client when connecting
|
||||||
|
// to the MQTT broker. Note: without the use of SSL/TLS, this information will
|
||||||
|
// be sent in plaintext accross the wire.
|
||||||
|
func (o *ClientOptions) SetUsername(u string) *ClientOptions {
|
||||||
|
o.Username = u
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPassword will set the password to be used by this client when connecting
|
||||||
|
// to the MQTT broker. Note: without the use of SSL/TLS, this information will
|
||||||
|
// be sent in plaintext accross the wire.
|
||||||
|
func (o *ClientOptions) SetPassword(p string) *ClientOptions {
|
||||||
|
o.Password = p
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCleanSession will set the "clean session" flag in the connect message
|
||||||
|
// when this client connects to an MQTT broker. By setting this flag, you are
|
||||||
|
// indicating that no messages saved by the broker for this client should be
|
||||||
|
// delivered. Any messages that were going to be sent by this client before
|
||||||
|
// diconnecting previously but didn't will not be sent upon connecting to the
|
||||||
|
// broker.
|
||||||
|
func (o *ClientOptions) SetCleanSession(clean bool) *ClientOptions {
|
||||||
|
o.CleanSession = clean
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetOrderMatters will set the message routing to guarantee order within
|
||||||
|
// each QoS level. By default, this value is true. If set to false,
|
||||||
|
// this flag indicates that messages can be delivered asynchronously
|
||||||
|
// from the client to the application and possibly arrive out of order.
|
||||||
|
func (o *ClientOptions) SetOrderMatters(order bool) *ClientOptions {
|
||||||
|
o.Order = order
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTLSConfig will set an SSL/TLS configuration to be used when connecting
|
||||||
|
// to an MQTT broker. Please read the official Go documentation for more
|
||||||
|
// information.
|
||||||
|
func (o *ClientOptions) SetTLSConfig(t *tls.Config) *ClientOptions {
|
||||||
|
o.TLSConfig = *t
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetStore will set the implementation of the Store interface
|
||||||
|
// used to provide message persistence in cases where QoS levels
|
||||||
|
// QoS_ONE or QoS_TWO are used. If no store is provided, then the
|
||||||
|
// client will use MemoryStore by default.
|
||||||
|
func (o *ClientOptions) SetStore(s Store) *ClientOptions {
|
||||||
|
o.Store = s
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetKeepAlive will set the amount of time (in seconds) that the client
|
||||||
|
// should wait before sending a PING request to the broker. This will
|
||||||
|
// allow the client to know that a connection has not been lost with the
|
||||||
|
// server.
|
||||||
|
func (o *ClientOptions) SetKeepAlive(k time.Duration) *ClientOptions {
|
||||||
|
o.KeepAlive = k
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPingTimeout will set the amount of time (in seconds) that the client
|
||||||
|
// will wait after sending a PING request to the broker, before deciding
|
||||||
|
// that the connection has been lost. Default is 10 seconds.
|
||||||
|
func (o *ClientOptions) SetPingTimeout(k time.Duration) *ClientOptions {
|
||||||
|
o.PingTimeout = k
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetProtocolVersion sets the MQTT version to be used to connect to the
|
||||||
|
// broker. Legitimate values are currently 3 - MQTT 3.1 or 4 - MQTT 3.1.1
|
||||||
|
func (o *ClientOptions) SetProtocolVersion(pv uint) *ClientOptions {
|
||||||
|
if pv >= 3 && pv <= 4 {
|
||||||
|
o.ProtocolVersion = pv
|
||||||
|
o.protocolVersionExplicit = true
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnsetWill will cause any set will message to be disregarded.
|
||||||
|
func (o *ClientOptions) UnsetWill() *ClientOptions {
|
||||||
|
o.WillEnabled = false
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetWill accepts a string will message to be set. When the client connects,
|
||||||
|
// it will give this will message to the broker, which will then publish the
|
||||||
|
// provided payload (the will) to any clients that are subscribed to the provided
|
||||||
|
// topic.
|
||||||
|
func (o *ClientOptions) SetWill(topic string, payload string, qos byte, retained bool) *ClientOptions {
|
||||||
|
o.SetBinaryWill(topic, []byte(payload), qos, retained)
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetBinaryWill accepts a []byte will message to be set. When the client connects,
|
||||||
|
// it will give this will message to the broker, which will then publish the
|
||||||
|
// provided payload (the will) to any clients that are subscribed to the provided
|
||||||
|
// topic.
|
||||||
|
func (o *ClientOptions) SetBinaryWill(topic string, payload []byte, qos byte, retained bool) *ClientOptions {
|
||||||
|
o.WillEnabled = true
|
||||||
|
o.WillTopic = topic
|
||||||
|
o.WillPayload = payload
|
||||||
|
o.WillQos = qos
|
||||||
|
o.WillRetained = retained
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDefaultPublishHandler sets the MessageHandler that will be called when a message
|
||||||
|
// is received that does not match any known subscriptions.
|
||||||
|
func (o *ClientOptions) SetDefaultPublishHandler(defaultHandler MessageHandler) *ClientOptions {
|
||||||
|
o.DefaultPublishHander = defaultHandler
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetOnConnectHandler sets the function to be called when the client is connected. Both
|
||||||
|
// at initial connection time and upon automatic reconnect.
|
||||||
|
func (o *ClientOptions) SetOnConnectHandler(onConn OnConnectHandler) *ClientOptions {
|
||||||
|
o.OnConnect = onConn
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetConnectionLostHandler will set the OnConnectionLost callback to be executed
|
||||||
|
// in the case where the client unexpectedly loses connection with the MQTT broker.
|
||||||
|
func (o *ClientOptions) SetConnectionLostHandler(onLost ConnectionLostHandler) *ClientOptions {
|
||||||
|
o.OnConnectionLost = onLost
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetWriteTimeout puts a limit on how long a mqtt publish should block until it unblocks with a
|
||||||
|
// timeout error. A duration of 0 never times out. Default 30 seconds
|
||||||
|
func (o *ClientOptions) SetWriteTimeout(t time.Duration) *ClientOptions {
|
||||||
|
o.WriteTimeout = t
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetConnectTimeout limits how long the client will wait when trying to open a connection
|
||||||
|
// to an MQTT server before timeing out and erroring the attempt. A duration of 0 never times out.
|
||||||
|
// Default 30 seconds. Currently only operational on TCP/TLS connections.
|
||||||
|
func (o *ClientOptions) SetConnectTimeout(t time.Duration) *ClientOptions {
|
||||||
|
o.ConnectTimeout = t
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMaxReconnectInterval sets the maximum time that will be waited between reconnection attempts
|
||||||
|
// when connection is lost
|
||||||
|
func (o *ClientOptions) SetMaxReconnectInterval(t time.Duration) *ClientOptions {
|
||||||
|
o.MaxReconnectInterval = t
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAutoReconnect sets whether the automatic reconnection logic should be used
|
||||||
|
// when the connection is lost, even if disabled the ConnectionLostHandler is still
|
||||||
|
// called
|
||||||
|
func (o *ClientOptions) SetAutoReconnect(a bool) *ClientOptions {
|
||||||
|
o.AutoReconnect = a
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMessageChannelDepth sets the size of the internal queue that holds messages while the
|
||||||
|
// client is temporairily offline, allowing the application to publish when the client is
|
||||||
|
// reconnecting. This setting is only valid if AutoReconnect is set to true, it is otherwise
|
||||||
|
// ignored.
|
||||||
|
func (o *ClientOptions) SetMessageChannelDepth(s uint) *ClientOptions {
|
||||||
|
o.MessageChannelDepth = s
|
||||||
|
return o
|
||||||
|
}
|
132
vendor/github.com/eclipse/paho.mqtt.golang/options_reader.go
generated
vendored
Normal file
132
vendor/github.com/eclipse/paho.mqtt.golang/options_reader.go
generated
vendored
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ClientOptionsReader provides an interface for reading ClientOptions after the client has been initialized.
|
||||||
|
type ClientOptionsReader struct {
|
||||||
|
options *ClientOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) Servers() []*url.URL {
|
||||||
|
s := make([]*url.URL, len(r.options.Servers))
|
||||||
|
|
||||||
|
for i, u := range r.options.Servers {
|
||||||
|
nu := *u
|
||||||
|
s[i] = &nu
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) ClientID() string {
|
||||||
|
s := r.options.ClientID
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) Username() string {
|
||||||
|
s := r.options.Username
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) Password() string {
|
||||||
|
s := r.options.Password
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) CleanSession() bool {
|
||||||
|
s := r.options.CleanSession
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) Order() bool {
|
||||||
|
s := r.options.Order
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) WillEnabled() bool {
|
||||||
|
s := r.options.WillEnabled
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) WillTopic() string {
|
||||||
|
s := r.options.WillTopic
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) WillPayload() []byte {
|
||||||
|
s := r.options.WillPayload
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) WillQos() byte {
|
||||||
|
s := r.options.WillQos
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) WillRetained() bool {
|
||||||
|
s := r.options.WillRetained
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) ProtocolVersion() uint {
|
||||||
|
s := r.options.ProtocolVersion
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) TLSConfig() tls.Config {
|
||||||
|
s := r.options.TLSConfig
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) KeepAlive() time.Duration {
|
||||||
|
s := r.options.KeepAlive
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) PingTimeout() time.Duration {
|
||||||
|
s := r.options.PingTimeout
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) ConnectTimeout() time.Duration {
|
||||||
|
s := r.options.ConnectTimeout
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) MaxReconnectInterval() time.Duration {
|
||||||
|
s := r.options.MaxReconnectInterval
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) AutoReconnect() bool {
|
||||||
|
s := r.options.AutoReconnect
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) WriteTimeout() time.Duration {
|
||||||
|
s := r.options.WriteTimeout
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClientOptionsReader) MessageChannelDepth() uint {
|
||||||
|
s := r.options.MessageChannelDepth
|
||||||
|
return s
|
||||||
|
}
|
51
vendor/github.com/eclipse/paho.mqtt.golang/packets/connack.go
generated
vendored
Normal file
51
vendor/github.com/eclipse/paho.mqtt.golang/packets/connack.go
generated
vendored
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//ConnackPacket is an internal representation of the fields of the
|
||||||
|
//Connack MQTT packet
|
||||||
|
type ConnackPacket struct {
|
||||||
|
FixedHeader
|
||||||
|
SessionPresent bool
|
||||||
|
ReturnCode byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ca *ConnackPacket) String() string {
|
||||||
|
str := fmt.Sprintf("%s", ca.FixedHeader)
|
||||||
|
str += " "
|
||||||
|
str += fmt.Sprintf("sessionpresent: %t returncode: %d", ca.SessionPresent, ca.ReturnCode)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ca *ConnackPacket) Write(w io.Writer) error {
|
||||||
|
var body bytes.Buffer
|
||||||
|
var err error
|
||||||
|
|
||||||
|
body.WriteByte(boolToByte(ca.SessionPresent))
|
||||||
|
body.WriteByte(ca.ReturnCode)
|
||||||
|
ca.FixedHeader.RemainingLength = 2
|
||||||
|
packet := ca.FixedHeader.pack()
|
||||||
|
packet.Write(body.Bytes())
|
||||||
|
_, err = packet.WriteTo(w)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unpack decodes the details of a ControlPacket after the fixed
|
||||||
|
//header has been read
|
||||||
|
func (ca *ConnackPacket) Unpack(b io.Reader) error {
|
||||||
|
ca.SessionPresent = 1&decodeByte(b) > 0
|
||||||
|
ca.ReturnCode = decodeByte(b)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details returns a Details struct containing the Qos and
|
||||||
|
//MessageID of this ControlPacket
|
||||||
|
func (ca *ConnackPacket) Details() Details {
|
||||||
|
return Details{Qos: 0, MessageID: 0}
|
||||||
|
}
|
122
vendor/github.com/eclipse/paho.mqtt.golang/packets/connect.go
generated
vendored
Normal file
122
vendor/github.com/eclipse/paho.mqtt.golang/packets/connect.go
generated
vendored
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//ConnectPacket is an internal representation of the fields of the
|
||||||
|
//Connect MQTT packet
|
||||||
|
type ConnectPacket struct {
|
||||||
|
FixedHeader
|
||||||
|
ProtocolName string
|
||||||
|
ProtocolVersion byte
|
||||||
|
CleanSession bool
|
||||||
|
WillFlag bool
|
||||||
|
WillQos byte
|
||||||
|
WillRetain bool
|
||||||
|
UsernameFlag bool
|
||||||
|
PasswordFlag bool
|
||||||
|
ReservedBit byte
|
||||||
|
Keepalive uint16
|
||||||
|
|
||||||
|
ClientIdentifier string
|
||||||
|
WillTopic string
|
||||||
|
WillMessage []byte
|
||||||
|
Username string
|
||||||
|
Password []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ConnectPacket) String() string {
|
||||||
|
str := fmt.Sprintf("%s", c.FixedHeader)
|
||||||
|
str += " "
|
||||||
|
str += fmt.Sprintf("protocolversion: %d protocolname: %s cleansession: %t willflag: %t WillQos: %d WillRetain: %t Usernameflag: %t Passwordflag: %t keepalive: %d clientId: %s willtopic: %s willmessage: %s Username: %s Password: %s", c.ProtocolVersion, c.ProtocolName, c.CleanSession, c.WillFlag, c.WillQos, c.WillRetain, c.UsernameFlag, c.PasswordFlag, c.Keepalive, c.ClientIdentifier, c.WillTopic, c.WillMessage, c.Username, c.Password)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ConnectPacket) Write(w io.Writer) error {
|
||||||
|
var body bytes.Buffer
|
||||||
|
var err error
|
||||||
|
|
||||||
|
body.Write(encodeString(c.ProtocolName))
|
||||||
|
body.WriteByte(c.ProtocolVersion)
|
||||||
|
body.WriteByte(boolToByte(c.CleanSession)<<1 | boolToByte(c.WillFlag)<<2 | c.WillQos<<3 | boolToByte(c.WillRetain)<<5 | boolToByte(c.PasswordFlag)<<6 | boolToByte(c.UsernameFlag)<<7)
|
||||||
|
body.Write(encodeUint16(c.Keepalive))
|
||||||
|
body.Write(encodeString(c.ClientIdentifier))
|
||||||
|
if c.WillFlag {
|
||||||
|
body.Write(encodeString(c.WillTopic))
|
||||||
|
body.Write(encodeBytes(c.WillMessage))
|
||||||
|
}
|
||||||
|
if c.UsernameFlag {
|
||||||
|
body.Write(encodeString(c.Username))
|
||||||
|
}
|
||||||
|
if c.PasswordFlag {
|
||||||
|
body.Write(encodeBytes(c.Password))
|
||||||
|
}
|
||||||
|
c.FixedHeader.RemainingLength = body.Len()
|
||||||
|
packet := c.FixedHeader.pack()
|
||||||
|
packet.Write(body.Bytes())
|
||||||
|
_, err = packet.WriteTo(w)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unpack decodes the details of a ControlPacket after the fixed
|
||||||
|
//header has been read
|
||||||
|
func (c *ConnectPacket) Unpack(b io.Reader) error {
|
||||||
|
c.ProtocolName = decodeString(b)
|
||||||
|
c.ProtocolVersion = decodeByte(b)
|
||||||
|
options := decodeByte(b)
|
||||||
|
c.ReservedBit = 1 & options
|
||||||
|
c.CleanSession = 1&(options>>1) > 0
|
||||||
|
c.WillFlag = 1&(options>>2) > 0
|
||||||
|
c.WillQos = 3 & (options >> 3)
|
||||||
|
c.WillRetain = 1&(options>>5) > 0
|
||||||
|
c.PasswordFlag = 1&(options>>6) > 0
|
||||||
|
c.UsernameFlag = 1&(options>>7) > 0
|
||||||
|
c.Keepalive = decodeUint16(b)
|
||||||
|
c.ClientIdentifier = decodeString(b)
|
||||||
|
if c.WillFlag {
|
||||||
|
c.WillTopic = decodeString(b)
|
||||||
|
c.WillMessage = decodeBytes(b)
|
||||||
|
}
|
||||||
|
if c.UsernameFlag {
|
||||||
|
c.Username = decodeString(b)
|
||||||
|
}
|
||||||
|
if c.PasswordFlag {
|
||||||
|
c.Password = decodeBytes(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Validate performs validation of the fields of a Connect packet
|
||||||
|
func (c *ConnectPacket) Validate() byte {
|
||||||
|
if c.PasswordFlag && !c.UsernameFlag {
|
||||||
|
return ErrRefusedBadUsernameOrPassword
|
||||||
|
}
|
||||||
|
if c.ReservedBit != 0 {
|
||||||
|
//Bad reserved bit
|
||||||
|
return ErrProtocolViolation
|
||||||
|
}
|
||||||
|
if (c.ProtocolName == "MQIsdp" && c.ProtocolVersion != 3) || (c.ProtocolName == "MQTT" && c.ProtocolVersion != 4) {
|
||||||
|
//Mismatched or unsupported protocol version
|
||||||
|
return ErrRefusedBadProtocolVersion
|
||||||
|
}
|
||||||
|
if c.ProtocolName != "MQIsdp" && c.ProtocolName != "MQTT" {
|
||||||
|
//Bad protocol name
|
||||||
|
return ErrProtocolViolation
|
||||||
|
}
|
||||||
|
if len(c.ClientIdentifier) > 65535 || len(c.Username) > 65535 || len(c.Password) > 65535 {
|
||||||
|
//Bad size field
|
||||||
|
return ErrProtocolViolation
|
||||||
|
}
|
||||||
|
return Accepted
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details returns a Details struct containing the Qos and
|
||||||
|
//MessageID of this ControlPacket
|
||||||
|
func (c *ConnectPacket) Details() Details {
|
||||||
|
return Details{Qos: 0, MessageID: 0}
|
||||||
|
}
|
36
vendor/github.com/eclipse/paho.mqtt.golang/packets/disconnect.go
generated
vendored
Normal file
36
vendor/github.com/eclipse/paho.mqtt.golang/packets/disconnect.go
generated
vendored
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//DisconnectPacket is an internal representation of the fields of the
|
||||||
|
//Disconnect MQTT packet
|
||||||
|
type DisconnectPacket struct {
|
||||||
|
FixedHeader
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DisconnectPacket) String() string {
|
||||||
|
str := fmt.Sprintf("%s", d.FixedHeader)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DisconnectPacket) Write(w io.Writer) error {
|
||||||
|
packet := d.FixedHeader.pack()
|
||||||
|
_, err := packet.WriteTo(w)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unpack decodes the details of a ControlPacket after the fixed
|
||||||
|
//header has been read
|
||||||
|
func (d *DisconnectPacket) Unpack(b io.Reader) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details returns a Details struct containing the Qos and
|
||||||
|
//MessageID of this ControlPacket
|
||||||
|
func (d *DisconnectPacket) Details() Details {
|
||||||
|
return Details{Qos: 0, MessageID: 0}
|
||||||
|
}
|
322
vendor/github.com/eclipse/paho.mqtt.golang/packets/packets.go
generated
vendored
Normal file
322
vendor/github.com/eclipse/paho.mqtt.golang/packets/packets.go
generated
vendored
Normal file
@ -0,0 +1,322 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//ControlPacket defines the interface for structs intended to hold
|
||||||
|
//decoded MQTT packets, either from being read or before being
|
||||||
|
//written
|
||||||
|
type ControlPacket interface {
|
||||||
|
Write(io.Writer) error
|
||||||
|
Unpack(io.Reader) error
|
||||||
|
String() string
|
||||||
|
Details() Details
|
||||||
|
}
|
||||||
|
|
||||||
|
//PacketNames maps the constants for each of the MQTT packet types
|
||||||
|
//to a string representation of their name.
|
||||||
|
var PacketNames = map[uint8]string{
|
||||||
|
1: "CONNECT",
|
||||||
|
2: "CONNACK",
|
||||||
|
3: "PUBLISH",
|
||||||
|
4: "PUBACK",
|
||||||
|
5: "PUBREC",
|
||||||
|
6: "PUBREL",
|
||||||
|
7: "PUBCOMP",
|
||||||
|
8: "SUBSCRIBE",
|
||||||
|
9: "SUBACK",
|
||||||
|
10: "UNSUBSCRIBE",
|
||||||
|
11: "UNSUBACK",
|
||||||
|
12: "PINGREQ",
|
||||||
|
13: "PINGRESP",
|
||||||
|
14: "DISCONNECT",
|
||||||
|
}
|
||||||
|
|
||||||
|
//Below are the constants assigned to each of the MQTT packet types
|
||||||
|
const (
|
||||||
|
Connect = 1
|
||||||
|
Connack = 2
|
||||||
|
Publish = 3
|
||||||
|
Puback = 4
|
||||||
|
Pubrec = 5
|
||||||
|
Pubrel = 6
|
||||||
|
Pubcomp = 7
|
||||||
|
Subscribe = 8
|
||||||
|
Suback = 9
|
||||||
|
Unsubscribe = 10
|
||||||
|
Unsuback = 11
|
||||||
|
Pingreq = 12
|
||||||
|
Pingresp = 13
|
||||||
|
Disconnect = 14
|
||||||
|
)
|
||||||
|
|
||||||
|
//Below are the const definitions for error codes returned by
|
||||||
|
//Connect()
|
||||||
|
const (
|
||||||
|
Accepted = 0x00
|
||||||
|
ErrRefusedBadProtocolVersion = 0x01
|
||||||
|
ErrRefusedIDRejected = 0x02
|
||||||
|
ErrRefusedServerUnavailable = 0x03
|
||||||
|
ErrRefusedBadUsernameOrPassword = 0x04
|
||||||
|
ErrRefusedNotAuthorised = 0x05
|
||||||
|
ErrNetworkError = 0xFE
|
||||||
|
ErrProtocolViolation = 0xFF
|
||||||
|
)
|
||||||
|
|
||||||
|
//ConnackReturnCodes is a map of the error codes constants for Connect()
|
||||||
|
//to a string representation of the error
|
||||||
|
var ConnackReturnCodes = map[uint8]string{
|
||||||
|
0: "Connection Accepted",
|
||||||
|
1: "Connection Refused: Bad Protocol Version",
|
||||||
|
2: "Connection Refused: Client Identifier Rejected",
|
||||||
|
3: "Connection Refused: Server Unavailable",
|
||||||
|
4: "Connection Refused: Username or Password in unknown format",
|
||||||
|
5: "Connection Refused: Not Authorised",
|
||||||
|
254: "Connection Error",
|
||||||
|
255: "Connection Refused: Protocol Violation",
|
||||||
|
}
|
||||||
|
|
||||||
|
//ConnErrors is a map of the errors codes constants for Connect()
|
||||||
|
//to a Go error
|
||||||
|
var ConnErrors = map[byte]error{
|
||||||
|
Accepted: nil,
|
||||||
|
ErrRefusedBadProtocolVersion: errors.New("Unnacceptable protocol version"),
|
||||||
|
ErrRefusedIDRejected: errors.New("Identifier rejected"),
|
||||||
|
ErrRefusedServerUnavailable: errors.New("Server Unavailable"),
|
||||||
|
ErrRefusedBadUsernameOrPassword: errors.New("Bad user name or password"),
|
||||||
|
ErrRefusedNotAuthorised: errors.New("Not Authorized"),
|
||||||
|
ErrNetworkError: errors.New("Network Error"),
|
||||||
|
ErrProtocolViolation: errors.New("Protocol Violation"),
|
||||||
|
}
|
||||||
|
|
||||||
|
//ReadPacket takes an instance of an io.Reader (such as net.Conn) and attempts
|
||||||
|
//to read an MQTT packet from the stream. It returns a ControlPacket
|
||||||
|
//representing the decoded MQTT packet and an error. One of these returns will
|
||||||
|
//always be nil, a nil ControlPacket indicating an error occurred.
|
||||||
|
func ReadPacket(r io.Reader) (cp ControlPacket, err error) {
|
||||||
|
var fh FixedHeader
|
||||||
|
b := make([]byte, 1)
|
||||||
|
|
||||||
|
_, err = io.ReadFull(r, b)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
fh.unpack(b[0], r)
|
||||||
|
cp = NewControlPacketWithHeader(fh)
|
||||||
|
if cp == nil {
|
||||||
|
return nil, errors.New("Bad data from client")
|
||||||
|
}
|
||||||
|
packetBytes := make([]byte, fh.RemainingLength)
|
||||||
|
_, err = io.ReadFull(r, packetBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = cp.Unpack(bytes.NewBuffer(packetBytes))
|
||||||
|
return cp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewControlPacket is used to create a new ControlPacket of the type specified
|
||||||
|
//by packetType, this is usually done by reference to the packet type constants
|
||||||
|
//defined in packets.go. The newly created ControlPacket is empty and a pointer
|
||||||
|
//is returned.
|
||||||
|
func NewControlPacket(packetType byte) (cp ControlPacket) {
|
||||||
|
switch packetType {
|
||||||
|
case Connect:
|
||||||
|
cp = &ConnectPacket{FixedHeader: FixedHeader{MessageType: Connect}}
|
||||||
|
case Connack:
|
||||||
|
cp = &ConnackPacket{FixedHeader: FixedHeader{MessageType: Connack}}
|
||||||
|
case Disconnect:
|
||||||
|
cp = &DisconnectPacket{FixedHeader: FixedHeader{MessageType: Disconnect}}
|
||||||
|
case Publish:
|
||||||
|
cp = &PublishPacket{FixedHeader: FixedHeader{MessageType: Publish}}
|
||||||
|
case Puback:
|
||||||
|
cp = &PubackPacket{FixedHeader: FixedHeader{MessageType: Puback}}
|
||||||
|
case Pubrec:
|
||||||
|
cp = &PubrecPacket{FixedHeader: FixedHeader{MessageType: Pubrec}}
|
||||||
|
case Pubrel:
|
||||||
|
cp = &PubrelPacket{FixedHeader: FixedHeader{MessageType: Pubrel, Qos: 1}}
|
||||||
|
case Pubcomp:
|
||||||
|
cp = &PubcompPacket{FixedHeader: FixedHeader{MessageType: Pubcomp}}
|
||||||
|
case Subscribe:
|
||||||
|
cp = &SubscribePacket{FixedHeader: FixedHeader{MessageType: Subscribe, Qos: 1}}
|
||||||
|
case Suback:
|
||||||
|
cp = &SubackPacket{FixedHeader: FixedHeader{MessageType: Suback}}
|
||||||
|
case Unsubscribe:
|
||||||
|
cp = &UnsubscribePacket{FixedHeader: FixedHeader{MessageType: Unsubscribe, Qos: 1}}
|
||||||
|
case Unsuback:
|
||||||
|
cp = &UnsubackPacket{FixedHeader: FixedHeader{MessageType: Unsuback}}
|
||||||
|
case Pingreq:
|
||||||
|
cp = &PingreqPacket{FixedHeader: FixedHeader{MessageType: Pingreq}}
|
||||||
|
case Pingresp:
|
||||||
|
cp = &PingrespPacket{FixedHeader: FixedHeader{MessageType: Pingresp}}
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return cp
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewControlPacketWithHeader is used to create a new ControlPacket of the type
|
||||||
|
//specified within the FixedHeader that is passed to the function.
|
||||||
|
//The newly created ControlPacket is empty and a pointer is returned.
|
||||||
|
func NewControlPacketWithHeader(fh FixedHeader) (cp ControlPacket) {
|
||||||
|
switch fh.MessageType {
|
||||||
|
case Connect:
|
||||||
|
cp = &ConnectPacket{FixedHeader: fh}
|
||||||
|
case Connack:
|
||||||
|
cp = &ConnackPacket{FixedHeader: fh}
|
||||||
|
case Disconnect:
|
||||||
|
cp = &DisconnectPacket{FixedHeader: fh}
|
||||||
|
case Publish:
|
||||||
|
cp = &PublishPacket{FixedHeader: fh}
|
||||||
|
case Puback:
|
||||||
|
cp = &PubackPacket{FixedHeader: fh}
|
||||||
|
case Pubrec:
|
||||||
|
cp = &PubrecPacket{FixedHeader: fh}
|
||||||
|
case Pubrel:
|
||||||
|
cp = &PubrelPacket{FixedHeader: fh}
|
||||||
|
case Pubcomp:
|
||||||
|
cp = &PubcompPacket{FixedHeader: fh}
|
||||||
|
case Subscribe:
|
||||||
|
cp = &SubscribePacket{FixedHeader: fh}
|
||||||
|
case Suback:
|
||||||
|
cp = &SubackPacket{FixedHeader: fh}
|
||||||
|
case Unsubscribe:
|
||||||
|
cp = &UnsubscribePacket{FixedHeader: fh}
|
||||||
|
case Unsuback:
|
||||||
|
cp = &UnsubackPacket{FixedHeader: fh}
|
||||||
|
case Pingreq:
|
||||||
|
cp = &PingreqPacket{FixedHeader: fh}
|
||||||
|
case Pingresp:
|
||||||
|
cp = &PingrespPacket{FixedHeader: fh}
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return cp
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details struct returned by the Details() function called on
|
||||||
|
//ControlPackets to present details of the Qos and MessageID
|
||||||
|
//of the ControlPacket
|
||||||
|
type Details struct {
|
||||||
|
Qos byte
|
||||||
|
MessageID uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
//FixedHeader is a struct to hold the decoded information from
|
||||||
|
//the fixed header of an MQTT ControlPacket
|
||||||
|
type FixedHeader struct {
|
||||||
|
MessageType byte
|
||||||
|
Dup bool
|
||||||
|
Qos byte
|
||||||
|
Retain bool
|
||||||
|
RemainingLength int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fh FixedHeader) String() string {
|
||||||
|
return fmt.Sprintf("%s: dup: %t qos: %d retain: %t rLength: %d", PacketNames[fh.MessageType], fh.Dup, fh.Qos, fh.Retain, fh.RemainingLength)
|
||||||
|
}
|
||||||
|
|
||||||
|
func boolToByte(b bool) byte {
|
||||||
|
switch b {
|
||||||
|
case true:
|
||||||
|
return 1
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fh *FixedHeader) pack() bytes.Buffer {
|
||||||
|
var header bytes.Buffer
|
||||||
|
header.WriteByte(fh.MessageType<<4 | boolToByte(fh.Dup)<<3 | fh.Qos<<1 | boolToByte(fh.Retain))
|
||||||
|
header.Write(encodeLength(fh.RemainingLength))
|
||||||
|
return header
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fh *FixedHeader) unpack(typeAndFlags byte, r io.Reader) {
|
||||||
|
fh.MessageType = typeAndFlags >> 4
|
||||||
|
fh.Dup = (typeAndFlags>>3)&0x01 > 0
|
||||||
|
fh.Qos = (typeAndFlags >> 1) & 0x03
|
||||||
|
fh.Retain = typeAndFlags&0x01 > 0
|
||||||
|
fh.RemainingLength = decodeLength(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeByte(b io.Reader) byte {
|
||||||
|
num := make([]byte, 1)
|
||||||
|
b.Read(num)
|
||||||
|
return num[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeUint16(b io.Reader) uint16 {
|
||||||
|
num := make([]byte, 2)
|
||||||
|
b.Read(num)
|
||||||
|
return binary.BigEndian.Uint16(num)
|
||||||
|
}
|
||||||
|
|
||||||
|
func encodeUint16(num uint16) []byte {
|
||||||
|
bytes := make([]byte, 2)
|
||||||
|
binary.BigEndian.PutUint16(bytes, num)
|
||||||
|
return bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
func encodeString(field string) []byte {
|
||||||
|
fieldLength := make([]byte, 2)
|
||||||
|
binary.BigEndian.PutUint16(fieldLength, uint16(len(field)))
|
||||||
|
return append(fieldLength, []byte(field)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeString(b io.Reader) string {
|
||||||
|
fieldLength := decodeUint16(b)
|
||||||
|
field := make([]byte, fieldLength)
|
||||||
|
b.Read(field)
|
||||||
|
return string(field)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeBytes(b io.Reader) []byte {
|
||||||
|
fieldLength := decodeUint16(b)
|
||||||
|
field := make([]byte, fieldLength)
|
||||||
|
b.Read(field)
|
||||||
|
return field
|
||||||
|
}
|
||||||
|
|
||||||
|
func encodeBytes(field []byte) []byte {
|
||||||
|
fieldLength := make([]byte, 2)
|
||||||
|
binary.BigEndian.PutUint16(fieldLength, uint16(len(field)))
|
||||||
|
return append(fieldLength, field...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func encodeLength(length int) []byte {
|
||||||
|
var encLength []byte
|
||||||
|
for {
|
||||||
|
digit := byte(length % 128)
|
||||||
|
length /= 128
|
||||||
|
if length > 0 {
|
||||||
|
digit |= 0x80
|
||||||
|
}
|
||||||
|
encLength = append(encLength, digit)
|
||||||
|
if length == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return encLength
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeLength(r io.Reader) int {
|
||||||
|
var rLength uint32
|
||||||
|
var multiplier uint32
|
||||||
|
b := make([]byte, 1)
|
||||||
|
for multiplier < 27 { //fix: Infinite '(digit & 128) == 1' will cause the dead loop
|
||||||
|
io.ReadFull(r, b)
|
||||||
|
digit := b[0]
|
||||||
|
rLength |= uint32(digit&127) << multiplier
|
||||||
|
if (digit & 128) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
multiplier += 7
|
||||||
|
}
|
||||||
|
return int(rLength)
|
||||||
|
}
|
36
vendor/github.com/eclipse/paho.mqtt.golang/packets/pingreq.go
generated
vendored
Normal file
36
vendor/github.com/eclipse/paho.mqtt.golang/packets/pingreq.go
generated
vendored
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//PingreqPacket is an internal representation of the fields of the
|
||||||
|
//Pingreq MQTT packet
|
||||||
|
type PingreqPacket struct {
|
||||||
|
FixedHeader
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pr *PingreqPacket) String() string {
|
||||||
|
str := fmt.Sprintf("%s", pr.FixedHeader)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pr *PingreqPacket) Write(w io.Writer) error {
|
||||||
|
packet := pr.FixedHeader.pack()
|
||||||
|
_, err := packet.WriteTo(w)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unpack decodes the details of a ControlPacket after the fixed
|
||||||
|
//header has been read
|
||||||
|
func (pr *PingreqPacket) Unpack(b io.Reader) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details returns a Details struct containing the Qos and
|
||||||
|
//MessageID of this ControlPacket
|
||||||
|
func (pr *PingreqPacket) Details() Details {
|
||||||
|
return Details{Qos: 0, MessageID: 0}
|
||||||
|
}
|
36
vendor/github.com/eclipse/paho.mqtt.golang/packets/pingresp.go
generated
vendored
Normal file
36
vendor/github.com/eclipse/paho.mqtt.golang/packets/pingresp.go
generated
vendored
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//PingrespPacket is an internal representation of the fields of the
|
||||||
|
//Pingresp MQTT packet
|
||||||
|
type PingrespPacket struct {
|
||||||
|
FixedHeader
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pr *PingrespPacket) String() string {
|
||||||
|
str := fmt.Sprintf("%s", pr.FixedHeader)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pr *PingrespPacket) Write(w io.Writer) error {
|
||||||
|
packet := pr.FixedHeader.pack()
|
||||||
|
_, err := packet.WriteTo(w)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unpack decodes the details of a ControlPacket after the fixed
|
||||||
|
//header has been read
|
||||||
|
func (pr *PingrespPacket) Unpack(b io.Reader) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details returns a Details struct containing the Qos and
|
||||||
|
//MessageID of this ControlPacket
|
||||||
|
func (pr *PingrespPacket) Details() Details {
|
||||||
|
return Details{Qos: 0, MessageID: 0}
|
||||||
|
}
|
44
vendor/github.com/eclipse/paho.mqtt.golang/packets/puback.go
generated
vendored
Normal file
44
vendor/github.com/eclipse/paho.mqtt.golang/packets/puback.go
generated
vendored
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//PubackPacket is an internal representation of the fields of the
|
||||||
|
//Puback MQTT packet
|
||||||
|
type PubackPacket struct {
|
||||||
|
FixedHeader
|
||||||
|
MessageID uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pa *PubackPacket) String() string {
|
||||||
|
str := fmt.Sprintf("%s", pa.FixedHeader)
|
||||||
|
str += " "
|
||||||
|
str += fmt.Sprintf("MessageID: %d", pa.MessageID)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pa *PubackPacket) Write(w io.Writer) error {
|
||||||
|
var err error
|
||||||
|
pa.FixedHeader.RemainingLength = 2
|
||||||
|
packet := pa.FixedHeader.pack()
|
||||||
|
packet.Write(encodeUint16(pa.MessageID))
|
||||||
|
_, err = packet.WriteTo(w)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unpack decodes the details of a ControlPacket after the fixed
|
||||||
|
//header has been read
|
||||||
|
func (pa *PubackPacket) Unpack(b io.Reader) error {
|
||||||
|
pa.MessageID = decodeUint16(b)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details returns a Details struct containing the Qos and
|
||||||
|
//MessageID of this ControlPacket
|
||||||
|
func (pa *PubackPacket) Details() Details {
|
||||||
|
return Details{Qos: pa.Qos, MessageID: pa.MessageID}
|
||||||
|
}
|
44
vendor/github.com/eclipse/paho.mqtt.golang/packets/pubcomp.go
generated
vendored
Normal file
44
vendor/github.com/eclipse/paho.mqtt.golang/packets/pubcomp.go
generated
vendored
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//PubcompPacket is an internal representation of the fields of the
|
||||||
|
//Pubcomp MQTT packet
|
||||||
|
type PubcompPacket struct {
|
||||||
|
FixedHeader
|
||||||
|
MessageID uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *PubcompPacket) String() string {
|
||||||
|
str := fmt.Sprintf("%s", pc.FixedHeader)
|
||||||
|
str += " "
|
||||||
|
str += fmt.Sprintf("MessageID: %d", pc.MessageID)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *PubcompPacket) Write(w io.Writer) error {
|
||||||
|
var err error
|
||||||
|
pc.FixedHeader.RemainingLength = 2
|
||||||
|
packet := pc.FixedHeader.pack()
|
||||||
|
packet.Write(encodeUint16(pc.MessageID))
|
||||||
|
_, err = packet.WriteTo(w)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unpack decodes the details of a ControlPacket after the fixed
|
||||||
|
//header has been read
|
||||||
|
func (pc *PubcompPacket) Unpack(b io.Reader) error {
|
||||||
|
pc.MessageID = decodeUint16(b)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details returns a Details struct containing the Qos and
|
||||||
|
//MessageID of this ControlPacket
|
||||||
|
func (pc *PubcompPacket) Details() Details {
|
||||||
|
return Details{Qos: pc.Qos, MessageID: pc.MessageID}
|
||||||
|
}
|
80
vendor/github.com/eclipse/paho.mqtt.golang/packets/publish.go
generated
vendored
Normal file
80
vendor/github.com/eclipse/paho.mqtt.golang/packets/publish.go
generated
vendored
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//PublishPacket is an internal representation of the fields of the
|
||||||
|
//Publish MQTT packet
|
||||||
|
type PublishPacket struct {
|
||||||
|
FixedHeader
|
||||||
|
TopicName string
|
||||||
|
MessageID uint16
|
||||||
|
Payload []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PublishPacket) String() string {
|
||||||
|
str := fmt.Sprintf("%s", p.FixedHeader)
|
||||||
|
str += " "
|
||||||
|
str += fmt.Sprintf("topicName: %s MessageID: %d", p.TopicName, p.MessageID)
|
||||||
|
str += " "
|
||||||
|
str += fmt.Sprintf("payload: %s", string(p.Payload))
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PublishPacket) Write(w io.Writer) error {
|
||||||
|
var body bytes.Buffer
|
||||||
|
var err error
|
||||||
|
|
||||||
|
body.Write(encodeString(p.TopicName))
|
||||||
|
if p.Qos > 0 {
|
||||||
|
body.Write(encodeUint16(p.MessageID))
|
||||||
|
}
|
||||||
|
p.FixedHeader.RemainingLength = body.Len() + len(p.Payload)
|
||||||
|
packet := p.FixedHeader.pack()
|
||||||
|
packet.Write(body.Bytes())
|
||||||
|
packet.Write(p.Payload)
|
||||||
|
_, err = w.Write(packet.Bytes())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unpack decodes the details of a ControlPacket after the fixed
|
||||||
|
//header has been read
|
||||||
|
func (p *PublishPacket) Unpack(b io.Reader) error {
|
||||||
|
var payloadLength = p.FixedHeader.RemainingLength
|
||||||
|
p.TopicName = decodeString(b)
|
||||||
|
if p.Qos > 0 {
|
||||||
|
p.MessageID = decodeUint16(b)
|
||||||
|
payloadLength -= len(p.TopicName) + 4
|
||||||
|
} else {
|
||||||
|
payloadLength -= len(p.TopicName) + 2
|
||||||
|
}
|
||||||
|
if payloadLength < 0 {
|
||||||
|
return fmt.Errorf("Error upacking publish, payload length < 0")
|
||||||
|
}
|
||||||
|
p.Payload = make([]byte, payloadLength)
|
||||||
|
_, err := b.Read(p.Payload)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Copy creates a new PublishPacket with the same topic and payload
|
||||||
|
//but an empty fixed header, useful for when you want to deliver
|
||||||
|
//a message with different properties such as Qos but the same
|
||||||
|
//content
|
||||||
|
func (p *PublishPacket) Copy() *PublishPacket {
|
||||||
|
newP := NewControlPacket(Publish).(*PublishPacket)
|
||||||
|
newP.TopicName = p.TopicName
|
||||||
|
newP.Payload = p.Payload
|
||||||
|
|
||||||
|
return newP
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details returns a Details struct containing the Qos and
|
||||||
|
//MessageID of this ControlPacket
|
||||||
|
func (p *PublishPacket) Details() Details {
|
||||||
|
return Details{Qos: p.Qos, MessageID: p.MessageID}
|
||||||
|
}
|
44
vendor/github.com/eclipse/paho.mqtt.golang/packets/pubrec.go
generated
vendored
Normal file
44
vendor/github.com/eclipse/paho.mqtt.golang/packets/pubrec.go
generated
vendored
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//PubrecPacket is an internal representation of the fields of the
|
||||||
|
//Pubrec MQTT packet
|
||||||
|
type PubrecPacket struct {
|
||||||
|
FixedHeader
|
||||||
|
MessageID uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pr *PubrecPacket) String() string {
|
||||||
|
str := fmt.Sprintf("%s", pr.FixedHeader)
|
||||||
|
str += " "
|
||||||
|
str += fmt.Sprintf("MessageID: %d", pr.MessageID)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pr *PubrecPacket) Write(w io.Writer) error {
|
||||||
|
var err error
|
||||||
|
pr.FixedHeader.RemainingLength = 2
|
||||||
|
packet := pr.FixedHeader.pack()
|
||||||
|
packet.Write(encodeUint16(pr.MessageID))
|
||||||
|
_, err = packet.WriteTo(w)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unpack decodes the details of a ControlPacket after the fixed
|
||||||
|
//header has been read
|
||||||
|
func (pr *PubrecPacket) Unpack(b io.Reader) error {
|
||||||
|
pr.MessageID = decodeUint16(b)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details returns a Details struct containing the Qos and
|
||||||
|
//MessageID of this ControlPacket
|
||||||
|
func (pr *PubrecPacket) Details() Details {
|
||||||
|
return Details{Qos: pr.Qos, MessageID: pr.MessageID}
|
||||||
|
}
|
44
vendor/github.com/eclipse/paho.mqtt.golang/packets/pubrel.go
generated
vendored
Normal file
44
vendor/github.com/eclipse/paho.mqtt.golang/packets/pubrel.go
generated
vendored
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//PubrelPacket is an internal representation of the fields of the
|
||||||
|
//Pubrel MQTT packet
|
||||||
|
type PubrelPacket struct {
|
||||||
|
FixedHeader
|
||||||
|
MessageID uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pr *PubrelPacket) String() string {
|
||||||
|
str := fmt.Sprintf("%s", pr.FixedHeader)
|
||||||
|
str += " "
|
||||||
|
str += fmt.Sprintf("MessageID: %d", pr.MessageID)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pr *PubrelPacket) Write(w io.Writer) error {
|
||||||
|
var err error
|
||||||
|
pr.FixedHeader.RemainingLength = 2
|
||||||
|
packet := pr.FixedHeader.pack()
|
||||||
|
packet.Write(encodeUint16(pr.MessageID))
|
||||||
|
_, err = packet.WriteTo(w)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unpack decodes the details of a ControlPacket after the fixed
|
||||||
|
//header has been read
|
||||||
|
func (pr *PubrelPacket) Unpack(b io.Reader) error {
|
||||||
|
pr.MessageID = decodeUint16(b)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details returns a Details struct containing the Qos and
|
||||||
|
//MessageID of this ControlPacket
|
||||||
|
func (pr *PubrelPacket) Details() Details {
|
||||||
|
return Details{Qos: pr.Qos, MessageID: pr.MessageID}
|
||||||
|
}
|
52
vendor/github.com/eclipse/paho.mqtt.golang/packets/suback.go
generated
vendored
Normal file
52
vendor/github.com/eclipse/paho.mqtt.golang/packets/suback.go
generated
vendored
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//SubackPacket is an internal representation of the fields of the
|
||||||
|
//Suback MQTT packet
|
||||||
|
type SubackPacket struct {
|
||||||
|
FixedHeader
|
||||||
|
MessageID uint16
|
||||||
|
ReturnCodes []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sa *SubackPacket) String() string {
|
||||||
|
str := fmt.Sprintf("%s", sa.FixedHeader)
|
||||||
|
str += " "
|
||||||
|
str += fmt.Sprintf("MessageID: %d", sa.MessageID)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sa *SubackPacket) Write(w io.Writer) error {
|
||||||
|
var body bytes.Buffer
|
||||||
|
var err error
|
||||||
|
body.Write(encodeUint16(sa.MessageID))
|
||||||
|
body.Write(sa.ReturnCodes)
|
||||||
|
sa.FixedHeader.RemainingLength = body.Len()
|
||||||
|
packet := sa.FixedHeader.pack()
|
||||||
|
packet.Write(body.Bytes())
|
||||||
|
_, err = packet.WriteTo(w)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unpack decodes the details of a ControlPacket after the fixed
|
||||||
|
//header has been read
|
||||||
|
func (sa *SubackPacket) Unpack(b io.Reader) error {
|
||||||
|
var qosBuffer bytes.Buffer
|
||||||
|
sa.MessageID = decodeUint16(b)
|
||||||
|
qosBuffer.ReadFrom(b)
|
||||||
|
sa.ReturnCodes = qosBuffer.Bytes()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details returns a Details struct containing the Qos and
|
||||||
|
//MessageID of this ControlPacket
|
||||||
|
func (sa *SubackPacket) Details() Details {
|
||||||
|
return Details{Qos: 0, MessageID: sa.MessageID}
|
||||||
|
}
|
62
vendor/github.com/eclipse/paho.mqtt.golang/packets/subscribe.go
generated
vendored
Normal file
62
vendor/github.com/eclipse/paho.mqtt.golang/packets/subscribe.go
generated
vendored
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//SubscribePacket is an internal representation of the fields of the
|
||||||
|
//Subscribe MQTT packet
|
||||||
|
type SubscribePacket struct {
|
||||||
|
FixedHeader
|
||||||
|
MessageID uint16
|
||||||
|
Topics []string
|
||||||
|
Qoss []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SubscribePacket) String() string {
|
||||||
|
str := fmt.Sprintf("%s", s.FixedHeader)
|
||||||
|
str += " "
|
||||||
|
str += fmt.Sprintf("MessageID: %d topics: %s", s.MessageID, s.Topics)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SubscribePacket) Write(w io.Writer) error {
|
||||||
|
var body bytes.Buffer
|
||||||
|
var err error
|
||||||
|
|
||||||
|
body.Write(encodeUint16(s.MessageID))
|
||||||
|
for i, topic := range s.Topics {
|
||||||
|
body.Write(encodeString(topic))
|
||||||
|
body.WriteByte(s.Qoss[i])
|
||||||
|
}
|
||||||
|
s.FixedHeader.RemainingLength = body.Len()
|
||||||
|
packet := s.FixedHeader.pack()
|
||||||
|
packet.Write(body.Bytes())
|
||||||
|
_, err = packet.WriteTo(w)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unpack decodes the details of a ControlPacket after the fixed
|
||||||
|
//header has been read
|
||||||
|
func (s *SubscribePacket) Unpack(b io.Reader) error {
|
||||||
|
s.MessageID = decodeUint16(b)
|
||||||
|
payloadLength := s.FixedHeader.RemainingLength - 2
|
||||||
|
for payloadLength > 0 {
|
||||||
|
topic := decodeString(b)
|
||||||
|
s.Topics = append(s.Topics, topic)
|
||||||
|
qos := decodeByte(b)
|
||||||
|
s.Qoss = append(s.Qoss, qos)
|
||||||
|
payloadLength -= 2 + len(topic) + 1 //2 bytes of string length, plus string, plus 1 byte for Qos
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details returns a Details struct containing the Qos and
|
||||||
|
//MessageID of this ControlPacket
|
||||||
|
func (s *SubscribePacket) Details() Details {
|
||||||
|
return Details{Qos: 1, MessageID: s.MessageID}
|
||||||
|
}
|
44
vendor/github.com/eclipse/paho.mqtt.golang/packets/unsuback.go
generated
vendored
Normal file
44
vendor/github.com/eclipse/paho.mqtt.golang/packets/unsuback.go
generated
vendored
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//UnsubackPacket is an internal representation of the fields of the
|
||||||
|
//Unsuback MQTT packet
|
||||||
|
type UnsubackPacket struct {
|
||||||
|
FixedHeader
|
||||||
|
MessageID uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ua *UnsubackPacket) String() string {
|
||||||
|
str := fmt.Sprintf("%s", ua.FixedHeader)
|
||||||
|
str += " "
|
||||||
|
str += fmt.Sprintf("MessageID: %d", ua.MessageID)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ua *UnsubackPacket) Write(w io.Writer) error {
|
||||||
|
var err error
|
||||||
|
ua.FixedHeader.RemainingLength = 2
|
||||||
|
packet := ua.FixedHeader.pack()
|
||||||
|
packet.Write(encodeUint16(ua.MessageID))
|
||||||
|
_, err = packet.WriteTo(w)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unpack decodes the details of a ControlPacket after the fixed
|
||||||
|
//header has been read
|
||||||
|
func (ua *UnsubackPacket) Unpack(b io.Reader) error {
|
||||||
|
ua.MessageID = decodeUint16(b)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details returns a Details struct containing the Qos and
|
||||||
|
//MessageID of this ControlPacket
|
||||||
|
func (ua *UnsubackPacket) Details() Details {
|
||||||
|
return Details{Qos: 0, MessageID: ua.MessageID}
|
||||||
|
}
|
55
vendor/github.com/eclipse/paho.mqtt.golang/packets/unsubscribe.go
generated
vendored
Normal file
55
vendor/github.com/eclipse/paho.mqtt.golang/packets/unsubscribe.go
generated
vendored
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package packets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
//UnsubscribePacket is an internal representation of the fields of the
|
||||||
|
//Unsubscribe MQTT packet
|
||||||
|
type UnsubscribePacket struct {
|
||||||
|
FixedHeader
|
||||||
|
MessageID uint16
|
||||||
|
Topics []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UnsubscribePacket) String() string {
|
||||||
|
str := fmt.Sprintf("%s", u.FixedHeader)
|
||||||
|
str += " "
|
||||||
|
str += fmt.Sprintf("MessageID: %d", u.MessageID)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UnsubscribePacket) Write(w io.Writer) error {
|
||||||
|
var body bytes.Buffer
|
||||||
|
var err error
|
||||||
|
body.Write(encodeUint16(u.MessageID))
|
||||||
|
for _, topic := range u.Topics {
|
||||||
|
body.Write(encodeString(topic))
|
||||||
|
}
|
||||||
|
u.FixedHeader.RemainingLength = body.Len()
|
||||||
|
packet := u.FixedHeader.pack()
|
||||||
|
packet.Write(body.Bytes())
|
||||||
|
_, err = packet.WriteTo(w)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unpack decodes the details of a ControlPacket after the fixed
|
||||||
|
//header has been read
|
||||||
|
func (u *UnsubscribePacket) Unpack(b io.Reader) error {
|
||||||
|
u.MessageID = decodeUint16(b)
|
||||||
|
var topic string
|
||||||
|
for topic = decodeString(b); topic != ""; topic = decodeString(b) {
|
||||||
|
u.Topics = append(u.Topics, topic)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Details returns a Details struct containing the Qos and
|
||||||
|
//MessageID of this ControlPacket
|
||||||
|
func (u *UnsubscribePacket) Details() Details {
|
||||||
|
return Details{Qos: 1, MessageID: u.MessageID}
|
||||||
|
}
|
102
vendor/github.com/eclipse/paho.mqtt.golang/ping.go
generated
vendored
Normal file
102
vendor/github.com/eclipse/paho.mqtt.golang/ping.go
generated
vendored
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/eclipse/paho.mqtt.golang/packets"
|
||||||
|
)
|
||||||
|
|
||||||
|
func keepalive(c *client) {
|
||||||
|
DEBUG.Println(PNG, "keepalive starting")
|
||||||
|
|
||||||
|
receiveInterval := c.options.KeepAlive + (1 * time.Second)
|
||||||
|
pingTimer := timer{Timer: time.NewTimer(c.options.KeepAlive)}
|
||||||
|
receiveTimer := timer{Timer: time.NewTimer(receiveInterval)}
|
||||||
|
pingRespTimer := timer{Timer: time.NewTimer(c.options.PingTimeout)}
|
||||||
|
|
||||||
|
pingRespTimer.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-c.stop:
|
||||||
|
DEBUG.Println(PNG, "keepalive stopped")
|
||||||
|
c.workers.Done()
|
||||||
|
return
|
||||||
|
case <-pingTimer.C:
|
||||||
|
sendPing(&pingTimer, &pingRespTimer, c)
|
||||||
|
case <-c.keepaliveReset:
|
||||||
|
DEBUG.Println(NET, "resetting ping timer")
|
||||||
|
pingTimer.Reset(c.options.KeepAlive)
|
||||||
|
case <-c.pingResp:
|
||||||
|
DEBUG.Println(NET, "resetting ping timeout timer")
|
||||||
|
pingRespTimer.Stop()
|
||||||
|
pingTimer.Reset(c.options.KeepAlive)
|
||||||
|
receiveTimer.Reset(receiveInterval)
|
||||||
|
case <-c.packetResp:
|
||||||
|
DEBUG.Println(NET, "resetting receive timer")
|
||||||
|
receiveTimer.Reset(receiveInterval)
|
||||||
|
case <-receiveTimer.C:
|
||||||
|
receiveTimer.SetRead(true)
|
||||||
|
receiveTimer.Reset(receiveInterval)
|
||||||
|
sendPing(&pingTimer, &pingRespTimer, c)
|
||||||
|
case <-pingRespTimer.C:
|
||||||
|
pingRespTimer.SetRead(true)
|
||||||
|
CRITICAL.Println(PNG, "pingresp not received, disconnecting")
|
||||||
|
c.workers.Done()
|
||||||
|
c.internalConnLost(errors.New("pingresp not received, disconnecting"))
|
||||||
|
pingTimer.Stop()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type timer struct {
|
||||||
|
*time.Timer
|
||||||
|
readFrom bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *timer) SetRead(v bool) {
|
||||||
|
t.readFrom = v
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *timer) Stop() bool {
|
||||||
|
defer t.SetRead(true)
|
||||||
|
|
||||||
|
if !t.Timer.Stop() && !t.readFrom {
|
||||||
|
<-t.C
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *timer) Reset(d time.Duration) bool {
|
||||||
|
defer t.SetRead(false)
|
||||||
|
t.Stop()
|
||||||
|
return t.Timer.Reset(d)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendPing(pt *timer, rt *timer, c *client) {
|
||||||
|
pt.SetRead(true)
|
||||||
|
DEBUG.Println(PNG, "keepalive sending ping")
|
||||||
|
ping := packets.NewControlPacket(packets.Pingreq).(*packets.PingreqPacket)
|
||||||
|
//We don't want to wait behind large messages being sent, the Write call
|
||||||
|
//will block until it it able to send the packet.
|
||||||
|
ping.Write(c.conn)
|
||||||
|
|
||||||
|
rt.Reset(c.options.PingTimeout)
|
||||||
|
}
|
161
vendor/github.com/eclipse/paho.mqtt.golang/router.go
generated
vendored
Normal file
161
vendor/github.com/eclipse/paho.mqtt.golang/router.go
generated
vendored
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"container/list"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/eclipse/paho.mqtt.golang/packets"
|
||||||
|
)
|
||||||
|
|
||||||
|
// route is a type which associates MQTT Topic strings with a
|
||||||
|
// callback to be executed upon the arrival of a message associated
|
||||||
|
// with a subscription to that topic.
|
||||||
|
type route struct {
|
||||||
|
topic string
|
||||||
|
callback MessageHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
// match takes a slice of strings which represent the route being tested having been split on '/'
|
||||||
|
// separators, and a slice of strings representing the topic string in the published message, similarly
|
||||||
|
// split.
|
||||||
|
// The function determines if the topic string matches the route according to the MQTT topic rules
|
||||||
|
// and returns a boolean of the outcome
|
||||||
|
func match(route []string, topic []string) bool {
|
||||||
|
if len(route) == 0 {
|
||||||
|
if len(topic) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(topic) == 0 {
|
||||||
|
if route[0] == "#" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if route[0] == "#" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if (route[0] == "+") || (route[0] == topic[0]) {
|
||||||
|
return match(route[1:], topic[1:])
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func routeIncludesTopic(route, topic string) bool {
|
||||||
|
return match(strings.Split(route, "/"), strings.Split(topic, "/"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// match takes the topic string of the published message and does a basic compare to the
|
||||||
|
// string of the current Route, if they match it returns true
|
||||||
|
func (r *route) match(topic string) bool {
|
||||||
|
return r.topic == topic || routeIncludesTopic(r.topic, topic)
|
||||||
|
}
|
||||||
|
|
||||||
|
type router struct {
|
||||||
|
sync.RWMutex
|
||||||
|
routes *list.List
|
||||||
|
defaultHandler MessageHandler
|
||||||
|
messages chan *packets.PublishPacket
|
||||||
|
stop chan bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// newRouter returns a new instance of a Router and channel which can be used to tell the Router
|
||||||
|
// to stop
|
||||||
|
func newRouter() (*router, chan bool) {
|
||||||
|
router := &router{routes: list.New(), messages: make(chan *packets.PublishPacket), stop: make(chan bool)}
|
||||||
|
stop := router.stop
|
||||||
|
return router, stop
|
||||||
|
}
|
||||||
|
|
||||||
|
// addRoute takes a topic string and MessageHandler callback. It looks in the current list of
|
||||||
|
// routes to see if there is already a matching Route. If there is it replaces the current
|
||||||
|
// callback with the new one. If not it add a new entry to the list of Routes.
|
||||||
|
func (r *router) addRoute(topic string, callback MessageHandler) {
|
||||||
|
r.Lock()
|
||||||
|
defer r.Unlock()
|
||||||
|
for e := r.routes.Front(); e != nil; e = e.Next() {
|
||||||
|
if e.Value.(*route).match(topic) {
|
||||||
|
r := e.Value.(*route)
|
||||||
|
r.callback = callback
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r.routes.PushBack(&route{topic: topic, callback: callback})
|
||||||
|
}
|
||||||
|
|
||||||
|
// deleteRoute takes a route string, looks for a matching Route in the list of Routes. If
|
||||||
|
// found it removes the Route from the list.
|
||||||
|
func (r *router) deleteRoute(topic string) {
|
||||||
|
r.Lock()
|
||||||
|
defer r.Unlock()
|
||||||
|
for e := r.routes.Front(); e != nil; e = e.Next() {
|
||||||
|
if e.Value.(*route).match(topic) {
|
||||||
|
r.routes.Remove(e)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// setDefaultHandler assigns a default callback that will be called if no matching Route
|
||||||
|
// is found for an incoming Publish.
|
||||||
|
func (r *router) setDefaultHandler(handler MessageHandler) {
|
||||||
|
r.Lock()
|
||||||
|
defer r.Unlock()
|
||||||
|
r.defaultHandler = handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// matchAndDispatch takes a channel of Message pointers as input and starts a go routine that
|
||||||
|
// takes messages off the channel, matches them against the internal route list and calls the
|
||||||
|
// associated callback (or the defaultHandler, if one exists and no other route matched). If
|
||||||
|
// anything is sent down the stop channel the function will end.
|
||||||
|
func (r *router) matchAndDispatch(messages <-chan *packets.PublishPacket, order bool, client *client) {
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case message := <-messages:
|
||||||
|
sent := false
|
||||||
|
r.RLock()
|
||||||
|
for e := r.routes.Front(); e != nil; e = e.Next() {
|
||||||
|
if e.Value.(*route).match(message.TopicName) {
|
||||||
|
if order {
|
||||||
|
e.Value.(*route).callback(client, messageFromPublish(message))
|
||||||
|
} else {
|
||||||
|
go e.Value.(*route).callback(client, messageFromPublish(message))
|
||||||
|
}
|
||||||
|
sent = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !sent && r.defaultHandler != nil {
|
||||||
|
if order {
|
||||||
|
r.defaultHandler(client, messageFromPublish(message))
|
||||||
|
} else {
|
||||||
|
go r.defaultHandler(client, messageFromPublish(message))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r.RUnlock()
|
||||||
|
case <-r.stop:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
126
vendor/github.com/eclipse/paho.mqtt.golang/store.go
generated
vendored
Normal file
126
vendor/github.com/eclipse/paho.mqtt.golang/store.go
generated
vendored
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/eclipse/paho.mqtt.golang/packets"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
inboundPrefix = "i."
|
||||||
|
outboundPrefix = "o."
|
||||||
|
)
|
||||||
|
|
||||||
|
// Store is an interface which can be used to provide implementations
|
||||||
|
// for message persistence.
|
||||||
|
// Because we may have to store distinct messages with the same
|
||||||
|
// message ID, we need a unique key for each message. This is
|
||||||
|
// possible by prepending "i." or "o." to each message id
|
||||||
|
type Store interface {
|
||||||
|
Open()
|
||||||
|
Put(key string, message packets.ControlPacket)
|
||||||
|
Get(key string) packets.ControlPacket
|
||||||
|
All() []string
|
||||||
|
Del(key string)
|
||||||
|
Close()
|
||||||
|
Reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
// A key MUST have the form "X.[messageid]"
|
||||||
|
// where X is 'i' or 'o'
|
||||||
|
func mIDFromKey(key string) uint16 {
|
||||||
|
s := key[2:]
|
||||||
|
i, err := strconv.Atoi(s)
|
||||||
|
chkerr(err)
|
||||||
|
return uint16(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a string of the form "i.[id]"
|
||||||
|
func inboundKeyFromMID(id uint16) string {
|
||||||
|
return fmt.Sprintf("%s%d", inboundPrefix, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a string of the form "o.[id]"
|
||||||
|
func outboundKeyFromMID(id uint16) string {
|
||||||
|
return fmt.Sprintf("%s%d", outboundPrefix, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// govern which outgoing messages are persisted
|
||||||
|
func persistOutbound(s Store, m packets.ControlPacket) {
|
||||||
|
switch m.Details().Qos {
|
||||||
|
case 0:
|
||||||
|
switch m.(type) {
|
||||||
|
case *packets.PubackPacket, *packets.PubcompPacket:
|
||||||
|
// Sending puback. delete matching publish
|
||||||
|
// from ibound
|
||||||
|
s.Del(inboundKeyFromMID(m.Details().MessageID))
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
switch m.(type) {
|
||||||
|
case *packets.PublishPacket, *packets.PubrelPacket, *packets.SubscribePacket, *packets.UnsubscribePacket:
|
||||||
|
// Sending publish. store in obound
|
||||||
|
// until puback received
|
||||||
|
s.Put(outboundKeyFromMID(m.Details().MessageID), m)
|
||||||
|
default:
|
||||||
|
ERROR.Println(STR, "Asked to persist an invalid message type")
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
switch m.(type) {
|
||||||
|
case *packets.PublishPacket:
|
||||||
|
// Sending publish. store in obound
|
||||||
|
// until pubrel received
|
||||||
|
s.Put(outboundKeyFromMID(m.Details().MessageID), m)
|
||||||
|
default:
|
||||||
|
ERROR.Println(STR, "Asked to persist an invalid message type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// govern which incoming messages are persisted
|
||||||
|
func persistInbound(s Store, m packets.ControlPacket) {
|
||||||
|
switch m.Details().Qos {
|
||||||
|
case 0:
|
||||||
|
switch m.(type) {
|
||||||
|
case *packets.PubackPacket, *packets.SubackPacket, *packets.UnsubackPacket, *packets.PubcompPacket:
|
||||||
|
// Received a puback. delete matching publish
|
||||||
|
// from obound
|
||||||
|
s.Del(outboundKeyFromMID(m.Details().MessageID))
|
||||||
|
case *packets.PublishPacket, *packets.PubrecPacket, *packets.PingrespPacket, *packets.ConnackPacket:
|
||||||
|
default:
|
||||||
|
ERROR.Println(STR, "Asked to persist an invalid messages type")
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
switch m.(type) {
|
||||||
|
case *packets.PublishPacket, *packets.PubrelPacket:
|
||||||
|
// Received a publish. store it in ibound
|
||||||
|
// until puback sent
|
||||||
|
s.Put(inboundKeyFromMID(m.Details().MessageID), m)
|
||||||
|
default:
|
||||||
|
ERROR.Println(STR, "Asked to persist an invalid messages type")
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
switch m.(type) {
|
||||||
|
case *packets.PublishPacket:
|
||||||
|
// Received a publish. store it in ibound
|
||||||
|
// until pubrel received
|
||||||
|
s.Put(inboundKeyFromMID(m.Details().MessageID), m)
|
||||||
|
default:
|
||||||
|
ERROR.Println(STR, "Asked to persist an invalid messages type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
157
vendor/github.com/eclipse/paho.mqtt.golang/token.go
generated
vendored
Normal file
157
vendor/github.com/eclipse/paho.mqtt.golang/token.go
generated
vendored
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/eclipse/paho.mqtt.golang/packets"
|
||||||
|
)
|
||||||
|
|
||||||
|
//PacketAndToken is a struct that contains both a ControlPacket and a
|
||||||
|
//Token. This struct is passed via channels between the client interface
|
||||||
|
//code and the underlying code responsible for sending and receiving
|
||||||
|
//MQTT messages.
|
||||||
|
type PacketAndToken struct {
|
||||||
|
p packets.ControlPacket
|
||||||
|
t Token
|
||||||
|
}
|
||||||
|
|
||||||
|
//Token defines the interface for the tokens used to indicate when
|
||||||
|
//actions have completed.
|
||||||
|
type Token interface {
|
||||||
|
Wait() bool
|
||||||
|
WaitTimeout(time.Duration) bool
|
||||||
|
flowComplete()
|
||||||
|
Error() error
|
||||||
|
}
|
||||||
|
|
||||||
|
type baseToken struct {
|
||||||
|
m sync.RWMutex
|
||||||
|
complete chan struct{}
|
||||||
|
ready bool
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait will wait indefinitely for the Token to complete, ie the Publish
|
||||||
|
// to be sent and confirmed receipt from the broker
|
||||||
|
func (b *baseToken) Wait() bool {
|
||||||
|
b.m.Lock()
|
||||||
|
defer b.m.Unlock()
|
||||||
|
if !b.ready {
|
||||||
|
<-b.complete
|
||||||
|
b.ready = true
|
||||||
|
}
|
||||||
|
return b.ready
|
||||||
|
}
|
||||||
|
|
||||||
|
// WaitTimeout takes a time in ms to wait for the flow associated with the
|
||||||
|
// Token to complete, returns true if it returned before the timeout or
|
||||||
|
// returns false if the timeout occurred. In the case of a timeout the Token
|
||||||
|
// does not have an error set in case the caller wishes to wait again
|
||||||
|
func (b *baseToken) WaitTimeout(d time.Duration) bool {
|
||||||
|
b.m.Lock()
|
||||||
|
defer b.m.Unlock()
|
||||||
|
if !b.ready {
|
||||||
|
select {
|
||||||
|
case <-b.complete:
|
||||||
|
b.ready = true
|
||||||
|
case <-time.After(d):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b.ready
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *baseToken) flowComplete() {
|
||||||
|
close(b.complete)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *baseToken) Error() error {
|
||||||
|
b.m.RLock()
|
||||||
|
defer b.m.RUnlock()
|
||||||
|
return b.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func newToken(tType byte) Token {
|
||||||
|
switch tType {
|
||||||
|
case packets.Connect:
|
||||||
|
return &ConnectToken{baseToken: baseToken{complete: make(chan struct{})}}
|
||||||
|
case packets.Subscribe:
|
||||||
|
return &SubscribeToken{baseToken: baseToken{complete: make(chan struct{})}, subResult: make(map[string]byte)}
|
||||||
|
case packets.Publish:
|
||||||
|
return &PublishToken{baseToken: baseToken{complete: make(chan struct{})}}
|
||||||
|
case packets.Unsubscribe:
|
||||||
|
return &UnsubscribeToken{baseToken: baseToken{complete: make(chan struct{})}}
|
||||||
|
case packets.Disconnect:
|
||||||
|
return &DisconnectToken{baseToken: baseToken{complete: make(chan struct{})}}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//ConnectToken is an extension of Token containing the extra fields
|
||||||
|
//required to provide information about calls to Connect()
|
||||||
|
type ConnectToken struct {
|
||||||
|
baseToken
|
||||||
|
returnCode byte
|
||||||
|
}
|
||||||
|
|
||||||
|
//ReturnCode returns the acknowlegement code in the connack sent
|
||||||
|
//in response to a Connect()
|
||||||
|
func (c *ConnectToken) ReturnCode() byte {
|
||||||
|
c.m.RLock()
|
||||||
|
defer c.m.RUnlock()
|
||||||
|
return c.returnCode
|
||||||
|
}
|
||||||
|
|
||||||
|
//PublishToken is an extension of Token containing the extra fields
|
||||||
|
//required to provide information about calls to Publish()
|
||||||
|
type PublishToken struct {
|
||||||
|
baseToken
|
||||||
|
messageID uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
//MessageID returns the MQTT message ID that was assigned to the
|
||||||
|
//Publish packet when it was sent to the broker
|
||||||
|
func (p *PublishToken) MessageID() uint16 {
|
||||||
|
return p.messageID
|
||||||
|
}
|
||||||
|
|
||||||
|
//SubscribeToken is an extension of Token containing the extra fields
|
||||||
|
//required to provide information about calls to Subscribe()
|
||||||
|
type SubscribeToken struct {
|
||||||
|
baseToken
|
||||||
|
subs []string
|
||||||
|
subResult map[string]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
//Result returns a map of topics that were subscribed to along with
|
||||||
|
//the matching return code from the broker. This is either the Qos
|
||||||
|
//value of the subscription or an error code.
|
||||||
|
func (s *SubscribeToken) Result() map[string]byte {
|
||||||
|
s.m.RLock()
|
||||||
|
defer s.m.RUnlock()
|
||||||
|
return s.subResult
|
||||||
|
}
|
||||||
|
|
||||||
|
//UnsubscribeToken is an extension of Token containing the extra fields
|
||||||
|
//required to provide information about calls to Unsubscribe()
|
||||||
|
type UnsubscribeToken struct {
|
||||||
|
baseToken
|
||||||
|
}
|
||||||
|
|
||||||
|
//DisconnectToken is an extension of Token containing the extra fields
|
||||||
|
//required to provide information about calls to Disconnect()
|
||||||
|
type DisconnectToken struct {
|
||||||
|
baseToken
|
||||||
|
}
|
82
vendor/github.com/eclipse/paho.mqtt.golang/topic.go
generated
vendored
Normal file
82
vendor/github.com/eclipse/paho.mqtt.golang/topic.go
generated
vendored
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
//ErrInvalidQos is the error returned when an packet is to be sent
|
||||||
|
//with an invalid Qos value
|
||||||
|
var ErrInvalidQos = errors.New("Invalid QoS")
|
||||||
|
|
||||||
|
//ErrInvalidTopicEmptyString is the error returned when a topic string
|
||||||
|
//is passed in that is 0 length
|
||||||
|
var ErrInvalidTopicEmptyString = errors.New("Invalid Topic; empty string")
|
||||||
|
|
||||||
|
//ErrInvalidTopicMultilevel is the error returned when a topic string
|
||||||
|
//is passed in that has the multi level wildcard in any position but
|
||||||
|
//the last
|
||||||
|
var ErrInvalidTopicMultilevel = errors.New("Invalid Topic; multi-level wildcard must be last level")
|
||||||
|
|
||||||
|
// Topic Names and Topic Filters
|
||||||
|
// The MQTT v3.1.1 spec clarifies a number of ambiguities with regard
|
||||||
|
// to the validity of Topic strings.
|
||||||
|
// - A Topic must be between 1 and 65535 bytes.
|
||||||
|
// - A Topic is case sensitive.
|
||||||
|
// - A Topic may contain whitespace.
|
||||||
|
// - A Topic containing a leading forward slash is different than a Topic without.
|
||||||
|
// - A Topic may be "/" (two levels, both empty string).
|
||||||
|
// - A Topic must be UTF-8 encoded.
|
||||||
|
// - A Topic may contain any number of levels.
|
||||||
|
// - A Topic may contain an empty level (two forward slashes in a row).
|
||||||
|
// - A TopicName may not contain a wildcard.
|
||||||
|
// - A TopicFilter may only have a # (multi-level) wildcard as the last level.
|
||||||
|
// - A TopicFilter may contain any number of + (single-level) wildcards.
|
||||||
|
// - A TopicFilter with a # will match the absense of a level
|
||||||
|
// Example: a subscription to "foo/#" will match messages published to "foo".
|
||||||
|
|
||||||
|
func validateSubscribeMap(subs map[string]byte) ([]string, []byte, error) {
|
||||||
|
var topics []string
|
||||||
|
var qoss []byte
|
||||||
|
for topic, qos := range subs {
|
||||||
|
if err := validateTopicAndQos(topic, qos); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
topics = append(topics, topic)
|
||||||
|
qoss = append(qoss, qos)
|
||||||
|
}
|
||||||
|
|
||||||
|
return topics, qoss, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateTopicAndQos(topic string, qos byte) error {
|
||||||
|
if len(topic) == 0 {
|
||||||
|
return ErrInvalidTopicEmptyString
|
||||||
|
}
|
||||||
|
|
||||||
|
levels := strings.Split(topic, "/")
|
||||||
|
for i, level := range levels {
|
||||||
|
if level == "#" && i != len(levels)-1 {
|
||||||
|
return ErrInvalidTopicMultilevel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if qos < 0 || qos > 2 {
|
||||||
|
return ErrInvalidQos
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
36
vendor/github.com/eclipse/paho.mqtt.golang/trace.go
generated
vendored
Normal file
36
vendor/github.com/eclipse/paho.mqtt.golang/trace.go
generated
vendored
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 IBM Corp.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Seth Hoenig
|
||||||
|
* Allan Stockdill-Mander
|
||||||
|
* Mike Robertson
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mqtt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Internal levels of library output that are initialised to not print
|
||||||
|
// anything but can be overridden by programmer
|
||||||
|
var (
|
||||||
|
ERROR *log.Logger
|
||||||
|
CRITICAL *log.Logger
|
||||||
|
WARN *log.Logger
|
||||||
|
DEBUG *log.Logger
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
ERROR = log.New(ioutil.Discard, "", 0)
|
||||||
|
CRITICAL = log.New(ioutil.Discard, "", 0)
|
||||||
|
WARN = log.New(ioutil.Discard, "", 0)
|
||||||
|
DEBUG = log.New(ioutil.Discard, "", 0)
|
||||||
|
}
|
106
vendor/golang.org/x/net/websocket/client.go
generated
vendored
Normal file
106
vendor/golang.org/x/net/websocket/client.go
generated
vendored
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DialError is an error that occurs while dialling a websocket server.
|
||||||
|
type DialError struct {
|
||||||
|
*Config
|
||||||
|
Err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *DialError) Error() string {
|
||||||
|
return "websocket.Dial " + e.Config.Location.String() + ": " + e.Err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewConfig creates a new WebSocket config for client connection.
|
||||||
|
func NewConfig(server, origin string) (config *Config, err error) {
|
||||||
|
config = new(Config)
|
||||||
|
config.Version = ProtocolVersionHybi13
|
||||||
|
config.Location, err = url.ParseRequestURI(server)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
config.Origin, err = url.ParseRequestURI(origin)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
config.Header = http.Header(make(map[string][]string))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClient creates a new WebSocket client connection over rwc.
|
||||||
|
func NewClient(config *Config, rwc io.ReadWriteCloser) (ws *Conn, err error) {
|
||||||
|
br := bufio.NewReader(rwc)
|
||||||
|
bw := bufio.NewWriter(rwc)
|
||||||
|
err = hybiClientHandshake(config, br, bw)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
buf := bufio.NewReadWriter(br, bw)
|
||||||
|
ws = newHybiClientConn(config, buf, rwc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dial opens a new client connection to a WebSocket.
|
||||||
|
func Dial(url_, protocol, origin string) (ws *Conn, err error) {
|
||||||
|
config, err := NewConfig(url_, origin)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if protocol != "" {
|
||||||
|
config.Protocol = []string{protocol}
|
||||||
|
}
|
||||||
|
return DialConfig(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
var portMap = map[string]string{
|
||||||
|
"ws": "80",
|
||||||
|
"wss": "443",
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseAuthority(location *url.URL) string {
|
||||||
|
if _, ok := portMap[location.Scheme]; ok {
|
||||||
|
if _, _, err := net.SplitHostPort(location.Host); err != nil {
|
||||||
|
return net.JoinHostPort(location.Host, portMap[location.Scheme])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return location.Host
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialConfig opens a new client connection to a WebSocket with a config.
|
||||||
|
func DialConfig(config *Config) (ws *Conn, err error) {
|
||||||
|
var client net.Conn
|
||||||
|
if config.Location == nil {
|
||||||
|
return nil, &DialError{config, ErrBadWebSocketLocation}
|
||||||
|
}
|
||||||
|
if config.Origin == nil {
|
||||||
|
return nil, &DialError{config, ErrBadWebSocketOrigin}
|
||||||
|
}
|
||||||
|
dialer := config.Dialer
|
||||||
|
if dialer == nil {
|
||||||
|
dialer = &net.Dialer{}
|
||||||
|
}
|
||||||
|
client, err = dialWithDialer(dialer, config)
|
||||||
|
if err != nil {
|
||||||
|
goto Error
|
||||||
|
}
|
||||||
|
ws, err = NewClient(config, client)
|
||||||
|
if err != nil {
|
||||||
|
client.Close()
|
||||||
|
goto Error
|
||||||
|
}
|
||||||
|
return
|
||||||
|
|
||||||
|
Error:
|
||||||
|
return nil, &DialError{config, err}
|
||||||
|
}
|
24
vendor/golang.org/x/net/websocket/dial.go
generated
vendored
Normal file
24
vendor/golang.org/x/net/websocket/dial.go
generated
vendored
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
func dialWithDialer(dialer *net.Dialer, config *Config) (conn net.Conn, err error) {
|
||||||
|
switch config.Location.Scheme {
|
||||||
|
case "ws":
|
||||||
|
conn, err = dialer.Dial("tcp", parseAuthority(config.Location))
|
||||||
|
|
||||||
|
case "wss":
|
||||||
|
conn, err = tls.DialWithDialer(dialer, "tcp", parseAuthority(config.Location), config.TlsConfig)
|
||||||
|
|
||||||
|
default:
|
||||||
|
err = ErrBadScheme
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
583
vendor/golang.org/x/net/websocket/hybi.go
generated
vendored
Normal file
583
vendor/golang.org/x/net/websocket/hybi.go
generated
vendored
Normal file
@ -0,0 +1,583 @@
|
|||||||
|
// Copyright 2011 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
// This file implements a protocol of hybi draft.
|
||||||
|
// http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
websocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
||||||
|
|
||||||
|
closeStatusNormal = 1000
|
||||||
|
closeStatusGoingAway = 1001
|
||||||
|
closeStatusProtocolError = 1002
|
||||||
|
closeStatusUnsupportedData = 1003
|
||||||
|
closeStatusFrameTooLarge = 1004
|
||||||
|
closeStatusNoStatusRcvd = 1005
|
||||||
|
closeStatusAbnormalClosure = 1006
|
||||||
|
closeStatusBadMessageData = 1007
|
||||||
|
closeStatusPolicyViolation = 1008
|
||||||
|
closeStatusTooBigData = 1009
|
||||||
|
closeStatusExtensionMismatch = 1010
|
||||||
|
|
||||||
|
maxControlFramePayloadLength = 125
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrBadMaskingKey = &ProtocolError{"bad masking key"}
|
||||||
|
ErrBadPongMessage = &ProtocolError{"bad pong message"}
|
||||||
|
ErrBadClosingStatus = &ProtocolError{"bad closing status"}
|
||||||
|
ErrUnsupportedExtensions = &ProtocolError{"unsupported extensions"}
|
||||||
|
ErrNotImplemented = &ProtocolError{"not implemented"}
|
||||||
|
|
||||||
|
handshakeHeader = map[string]bool{
|
||||||
|
"Host": true,
|
||||||
|
"Upgrade": true,
|
||||||
|
"Connection": true,
|
||||||
|
"Sec-Websocket-Key": true,
|
||||||
|
"Sec-Websocket-Origin": true,
|
||||||
|
"Sec-Websocket-Version": true,
|
||||||
|
"Sec-Websocket-Protocol": true,
|
||||||
|
"Sec-Websocket-Accept": true,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// A hybiFrameHeader is a frame header as defined in hybi draft.
|
||||||
|
type hybiFrameHeader struct {
|
||||||
|
Fin bool
|
||||||
|
Rsv [3]bool
|
||||||
|
OpCode byte
|
||||||
|
Length int64
|
||||||
|
MaskingKey []byte
|
||||||
|
|
||||||
|
data *bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
// A hybiFrameReader is a reader for hybi frame.
|
||||||
|
type hybiFrameReader struct {
|
||||||
|
reader io.Reader
|
||||||
|
|
||||||
|
header hybiFrameHeader
|
||||||
|
pos int64
|
||||||
|
length int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (frame *hybiFrameReader) Read(msg []byte) (n int, err error) {
|
||||||
|
n, err = frame.reader.Read(msg)
|
||||||
|
if frame.header.MaskingKey != nil {
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
msg[i] = msg[i] ^ frame.header.MaskingKey[frame.pos%4]
|
||||||
|
frame.pos++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (frame *hybiFrameReader) PayloadType() byte { return frame.header.OpCode }
|
||||||
|
|
||||||
|
func (frame *hybiFrameReader) HeaderReader() io.Reader {
|
||||||
|
if frame.header.data == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if frame.header.data.Len() == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return frame.header.data
|
||||||
|
}
|
||||||
|
|
||||||
|
func (frame *hybiFrameReader) TrailerReader() io.Reader { return nil }
|
||||||
|
|
||||||
|
func (frame *hybiFrameReader) Len() (n int) { return frame.length }
|
||||||
|
|
||||||
|
// A hybiFrameReaderFactory creates new frame reader based on its frame type.
|
||||||
|
type hybiFrameReaderFactory struct {
|
||||||
|
*bufio.Reader
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFrameReader reads a frame header from the connection, and creates new reader for the frame.
|
||||||
|
// See Section 5.2 Base Framing protocol for detail.
|
||||||
|
// http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17#section-5.2
|
||||||
|
func (buf hybiFrameReaderFactory) NewFrameReader() (frame frameReader, err error) {
|
||||||
|
hybiFrame := new(hybiFrameReader)
|
||||||
|
frame = hybiFrame
|
||||||
|
var header []byte
|
||||||
|
var b byte
|
||||||
|
// First byte. FIN/RSV1/RSV2/RSV3/OpCode(4bits)
|
||||||
|
b, err = buf.ReadByte()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
header = append(header, b)
|
||||||
|
hybiFrame.header.Fin = ((header[0] >> 7) & 1) != 0
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
j := uint(6 - i)
|
||||||
|
hybiFrame.header.Rsv[i] = ((header[0] >> j) & 1) != 0
|
||||||
|
}
|
||||||
|
hybiFrame.header.OpCode = header[0] & 0x0f
|
||||||
|
|
||||||
|
// Second byte. Mask/Payload len(7bits)
|
||||||
|
b, err = buf.ReadByte()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
header = append(header, b)
|
||||||
|
mask := (b & 0x80) != 0
|
||||||
|
b &= 0x7f
|
||||||
|
lengthFields := 0
|
||||||
|
switch {
|
||||||
|
case b <= 125: // Payload length 7bits.
|
||||||
|
hybiFrame.header.Length = int64(b)
|
||||||
|
case b == 126: // Payload length 7+16bits
|
||||||
|
lengthFields = 2
|
||||||
|
case b == 127: // Payload length 7+64bits
|
||||||
|
lengthFields = 8
|
||||||
|
}
|
||||||
|
for i := 0; i < lengthFields; i++ {
|
||||||
|
b, err = buf.ReadByte()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if lengthFields == 8 && i == 0 { // MSB must be zero when 7+64 bits
|
||||||
|
b &= 0x7f
|
||||||
|
}
|
||||||
|
header = append(header, b)
|
||||||
|
hybiFrame.header.Length = hybiFrame.header.Length*256 + int64(b)
|
||||||
|
}
|
||||||
|
if mask {
|
||||||
|
// Masking key. 4 bytes.
|
||||||
|
for i := 0; i < 4; i++ {
|
||||||
|
b, err = buf.ReadByte()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
header = append(header, b)
|
||||||
|
hybiFrame.header.MaskingKey = append(hybiFrame.header.MaskingKey, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hybiFrame.reader = io.LimitReader(buf.Reader, hybiFrame.header.Length)
|
||||||
|
hybiFrame.header.data = bytes.NewBuffer(header)
|
||||||
|
hybiFrame.length = len(header) + int(hybiFrame.header.Length)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// A HybiFrameWriter is a writer for hybi frame.
|
||||||
|
type hybiFrameWriter struct {
|
||||||
|
writer *bufio.Writer
|
||||||
|
|
||||||
|
header *hybiFrameHeader
|
||||||
|
}
|
||||||
|
|
||||||
|
func (frame *hybiFrameWriter) Write(msg []byte) (n int, err error) {
|
||||||
|
var header []byte
|
||||||
|
var b byte
|
||||||
|
if frame.header.Fin {
|
||||||
|
b |= 0x80
|
||||||
|
}
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
if frame.header.Rsv[i] {
|
||||||
|
j := uint(6 - i)
|
||||||
|
b |= 1 << j
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b |= frame.header.OpCode
|
||||||
|
header = append(header, b)
|
||||||
|
if frame.header.MaskingKey != nil {
|
||||||
|
b = 0x80
|
||||||
|
} else {
|
||||||
|
b = 0
|
||||||
|
}
|
||||||
|
lengthFields := 0
|
||||||
|
length := len(msg)
|
||||||
|
switch {
|
||||||
|
case length <= 125:
|
||||||
|
b |= byte(length)
|
||||||
|
case length < 65536:
|
||||||
|
b |= 126
|
||||||
|
lengthFields = 2
|
||||||
|
default:
|
||||||
|
b |= 127
|
||||||
|
lengthFields = 8
|
||||||
|
}
|
||||||
|
header = append(header, b)
|
||||||
|
for i := 0; i < lengthFields; i++ {
|
||||||
|
j := uint((lengthFields - i - 1) * 8)
|
||||||
|
b = byte((length >> j) & 0xff)
|
||||||
|
header = append(header, b)
|
||||||
|
}
|
||||||
|
if frame.header.MaskingKey != nil {
|
||||||
|
if len(frame.header.MaskingKey) != 4 {
|
||||||
|
return 0, ErrBadMaskingKey
|
||||||
|
}
|
||||||
|
header = append(header, frame.header.MaskingKey...)
|
||||||
|
frame.writer.Write(header)
|
||||||
|
data := make([]byte, length)
|
||||||
|
for i := range data {
|
||||||
|
data[i] = msg[i] ^ frame.header.MaskingKey[i%4]
|
||||||
|
}
|
||||||
|
frame.writer.Write(data)
|
||||||
|
err = frame.writer.Flush()
|
||||||
|
return length, err
|
||||||
|
}
|
||||||
|
frame.writer.Write(header)
|
||||||
|
frame.writer.Write(msg)
|
||||||
|
err = frame.writer.Flush()
|
||||||
|
return length, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (frame *hybiFrameWriter) Close() error { return nil }
|
||||||
|
|
||||||
|
type hybiFrameWriterFactory struct {
|
||||||
|
*bufio.Writer
|
||||||
|
needMaskingKey bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (buf hybiFrameWriterFactory) NewFrameWriter(payloadType byte) (frame frameWriter, err error) {
|
||||||
|
frameHeader := &hybiFrameHeader{Fin: true, OpCode: payloadType}
|
||||||
|
if buf.needMaskingKey {
|
||||||
|
frameHeader.MaskingKey, err = generateMaskingKey()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &hybiFrameWriter{writer: buf.Writer, header: frameHeader}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type hybiFrameHandler struct {
|
||||||
|
conn *Conn
|
||||||
|
payloadType byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (handler *hybiFrameHandler) HandleFrame(frame frameReader) (frameReader, error) {
|
||||||
|
if handler.conn.IsServerConn() {
|
||||||
|
// The client MUST mask all frames sent to the server.
|
||||||
|
if frame.(*hybiFrameReader).header.MaskingKey == nil {
|
||||||
|
handler.WriteClose(closeStatusProtocolError)
|
||||||
|
return nil, io.EOF
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// The server MUST NOT mask all frames.
|
||||||
|
if frame.(*hybiFrameReader).header.MaskingKey != nil {
|
||||||
|
handler.WriteClose(closeStatusProtocolError)
|
||||||
|
return nil, io.EOF
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if header := frame.HeaderReader(); header != nil {
|
||||||
|
io.Copy(ioutil.Discard, header)
|
||||||
|
}
|
||||||
|
switch frame.PayloadType() {
|
||||||
|
case ContinuationFrame:
|
||||||
|
frame.(*hybiFrameReader).header.OpCode = handler.payloadType
|
||||||
|
case TextFrame, BinaryFrame:
|
||||||
|
handler.payloadType = frame.PayloadType()
|
||||||
|
case CloseFrame:
|
||||||
|
return nil, io.EOF
|
||||||
|
case PingFrame, PongFrame:
|
||||||
|
b := make([]byte, maxControlFramePayloadLength)
|
||||||
|
n, err := io.ReadFull(frame, b)
|
||||||
|
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
io.Copy(ioutil.Discard, frame)
|
||||||
|
if frame.PayloadType() == PingFrame {
|
||||||
|
if _, err := handler.WritePong(b[:n]); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return frame, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (handler *hybiFrameHandler) WriteClose(status int) (err error) {
|
||||||
|
handler.conn.wio.Lock()
|
||||||
|
defer handler.conn.wio.Unlock()
|
||||||
|
w, err := handler.conn.frameWriterFactory.NewFrameWriter(CloseFrame)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
msg := make([]byte, 2)
|
||||||
|
binary.BigEndian.PutUint16(msg, uint16(status))
|
||||||
|
_, err = w.Write(msg)
|
||||||
|
w.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (handler *hybiFrameHandler) WritePong(msg []byte) (n int, err error) {
|
||||||
|
handler.conn.wio.Lock()
|
||||||
|
defer handler.conn.wio.Unlock()
|
||||||
|
w, err := handler.conn.frameWriterFactory.NewFrameWriter(PongFrame)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
n, err = w.Write(msg)
|
||||||
|
w.Close()
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// newHybiConn creates a new WebSocket connection speaking hybi draft protocol.
|
||||||
|
func newHybiConn(config *Config, buf *bufio.ReadWriter, rwc io.ReadWriteCloser, request *http.Request) *Conn {
|
||||||
|
if buf == nil {
|
||||||
|
br := bufio.NewReader(rwc)
|
||||||
|
bw := bufio.NewWriter(rwc)
|
||||||
|
buf = bufio.NewReadWriter(br, bw)
|
||||||
|
}
|
||||||
|
ws := &Conn{config: config, request: request, buf: buf, rwc: rwc,
|
||||||
|
frameReaderFactory: hybiFrameReaderFactory{buf.Reader},
|
||||||
|
frameWriterFactory: hybiFrameWriterFactory{
|
||||||
|
buf.Writer, request == nil},
|
||||||
|
PayloadType: TextFrame,
|
||||||
|
defaultCloseStatus: closeStatusNormal}
|
||||||
|
ws.frameHandler = &hybiFrameHandler{conn: ws}
|
||||||
|
return ws
|
||||||
|
}
|
||||||
|
|
||||||
|
// generateMaskingKey generates a masking key for a frame.
|
||||||
|
func generateMaskingKey() (maskingKey []byte, err error) {
|
||||||
|
maskingKey = make([]byte, 4)
|
||||||
|
if _, err = io.ReadFull(rand.Reader, maskingKey); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// generateNonce generates a nonce consisting of a randomly selected 16-byte
|
||||||
|
// value that has been base64-encoded.
|
||||||
|
func generateNonce() (nonce []byte) {
|
||||||
|
key := make([]byte, 16)
|
||||||
|
if _, err := io.ReadFull(rand.Reader, key); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
nonce = make([]byte, 24)
|
||||||
|
base64.StdEncoding.Encode(nonce, key)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// removeZone removes IPv6 zone identifer from host.
|
||||||
|
// E.g., "[fe80::1%en0]:8080" to "[fe80::1]:8080"
|
||||||
|
func removeZone(host string) string {
|
||||||
|
if !strings.HasPrefix(host, "[") {
|
||||||
|
return host
|
||||||
|
}
|
||||||
|
i := strings.LastIndex(host, "]")
|
||||||
|
if i < 0 {
|
||||||
|
return host
|
||||||
|
}
|
||||||
|
j := strings.LastIndex(host[:i], "%")
|
||||||
|
if j < 0 {
|
||||||
|
return host
|
||||||
|
}
|
||||||
|
return host[:j] + host[i:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// getNonceAccept computes the base64-encoded SHA-1 of the concatenation of
|
||||||
|
// the nonce ("Sec-WebSocket-Key" value) with the websocket GUID string.
|
||||||
|
func getNonceAccept(nonce []byte) (expected []byte, err error) {
|
||||||
|
h := sha1.New()
|
||||||
|
if _, err = h.Write(nonce); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err = h.Write([]byte(websocketGUID)); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
expected = make([]byte, 28)
|
||||||
|
base64.StdEncoding.Encode(expected, h.Sum(nil))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Client handshake described in draft-ietf-hybi-thewebsocket-protocol-17
|
||||||
|
func hybiClientHandshake(config *Config, br *bufio.Reader, bw *bufio.Writer) (err error) {
|
||||||
|
bw.WriteString("GET " + config.Location.RequestURI() + " HTTP/1.1\r\n")
|
||||||
|
|
||||||
|
// According to RFC 6874, an HTTP client, proxy, or other
|
||||||
|
// intermediary must remove any IPv6 zone identifier attached
|
||||||
|
// to an outgoing URI.
|
||||||
|
bw.WriteString("Host: " + removeZone(config.Location.Host) + "\r\n")
|
||||||
|
bw.WriteString("Upgrade: websocket\r\n")
|
||||||
|
bw.WriteString("Connection: Upgrade\r\n")
|
||||||
|
nonce := generateNonce()
|
||||||
|
if config.handshakeData != nil {
|
||||||
|
nonce = []byte(config.handshakeData["key"])
|
||||||
|
}
|
||||||
|
bw.WriteString("Sec-WebSocket-Key: " + string(nonce) + "\r\n")
|
||||||
|
bw.WriteString("Origin: " + strings.ToLower(config.Origin.String()) + "\r\n")
|
||||||
|
|
||||||
|
if config.Version != ProtocolVersionHybi13 {
|
||||||
|
return ErrBadProtocolVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
bw.WriteString("Sec-WebSocket-Version: " + fmt.Sprintf("%d", config.Version) + "\r\n")
|
||||||
|
if len(config.Protocol) > 0 {
|
||||||
|
bw.WriteString("Sec-WebSocket-Protocol: " + strings.Join(config.Protocol, ", ") + "\r\n")
|
||||||
|
}
|
||||||
|
// TODO(ukai): send Sec-WebSocket-Extensions.
|
||||||
|
err = config.Header.WriteSubset(bw, handshakeHeader)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
bw.WriteString("\r\n")
|
||||||
|
if err = bw.Flush(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.ReadResponse(br, &http.Request{Method: "GET"})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if resp.StatusCode != 101 {
|
||||||
|
return ErrBadStatus
|
||||||
|
}
|
||||||
|
if strings.ToLower(resp.Header.Get("Upgrade")) != "websocket" ||
|
||||||
|
strings.ToLower(resp.Header.Get("Connection")) != "upgrade" {
|
||||||
|
return ErrBadUpgrade
|
||||||
|
}
|
||||||
|
expectedAccept, err := getNonceAccept(nonce)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if resp.Header.Get("Sec-WebSocket-Accept") != string(expectedAccept) {
|
||||||
|
return ErrChallengeResponse
|
||||||
|
}
|
||||||
|
if resp.Header.Get("Sec-WebSocket-Extensions") != "" {
|
||||||
|
return ErrUnsupportedExtensions
|
||||||
|
}
|
||||||
|
offeredProtocol := resp.Header.Get("Sec-WebSocket-Protocol")
|
||||||
|
if offeredProtocol != "" {
|
||||||
|
protocolMatched := false
|
||||||
|
for i := 0; i < len(config.Protocol); i++ {
|
||||||
|
if config.Protocol[i] == offeredProtocol {
|
||||||
|
protocolMatched = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !protocolMatched {
|
||||||
|
return ErrBadWebSocketProtocol
|
||||||
|
}
|
||||||
|
config.Protocol = []string{offeredProtocol}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// newHybiClientConn creates a client WebSocket connection after handshake.
|
||||||
|
func newHybiClientConn(config *Config, buf *bufio.ReadWriter, rwc io.ReadWriteCloser) *Conn {
|
||||||
|
return newHybiConn(config, buf, rwc, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A HybiServerHandshaker performs a server handshake using hybi draft protocol.
|
||||||
|
type hybiServerHandshaker struct {
|
||||||
|
*Config
|
||||||
|
accept []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *hybiServerHandshaker) ReadHandshake(buf *bufio.Reader, req *http.Request) (code int, err error) {
|
||||||
|
c.Version = ProtocolVersionHybi13
|
||||||
|
if req.Method != "GET" {
|
||||||
|
return http.StatusMethodNotAllowed, ErrBadRequestMethod
|
||||||
|
}
|
||||||
|
// HTTP version can be safely ignored.
|
||||||
|
|
||||||
|
if strings.ToLower(req.Header.Get("Upgrade")) != "websocket" ||
|
||||||
|
!strings.Contains(strings.ToLower(req.Header.Get("Connection")), "upgrade") {
|
||||||
|
return http.StatusBadRequest, ErrNotWebSocket
|
||||||
|
}
|
||||||
|
|
||||||
|
key := req.Header.Get("Sec-Websocket-Key")
|
||||||
|
if key == "" {
|
||||||
|
return http.StatusBadRequest, ErrChallengeResponse
|
||||||
|
}
|
||||||
|
version := req.Header.Get("Sec-Websocket-Version")
|
||||||
|
switch version {
|
||||||
|
case "13":
|
||||||
|
c.Version = ProtocolVersionHybi13
|
||||||
|
default:
|
||||||
|
return http.StatusBadRequest, ErrBadWebSocketVersion
|
||||||
|
}
|
||||||
|
var scheme string
|
||||||
|
if req.TLS != nil {
|
||||||
|
scheme = "wss"
|
||||||
|
} else {
|
||||||
|
scheme = "ws"
|
||||||
|
}
|
||||||
|
c.Location, err = url.ParseRequestURI(scheme + "://" + req.Host + req.URL.RequestURI())
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusBadRequest, err
|
||||||
|
}
|
||||||
|
protocol := strings.TrimSpace(req.Header.Get("Sec-Websocket-Protocol"))
|
||||||
|
if protocol != "" {
|
||||||
|
protocols := strings.Split(protocol, ",")
|
||||||
|
for i := 0; i < len(protocols); i++ {
|
||||||
|
c.Protocol = append(c.Protocol, strings.TrimSpace(protocols[i]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.accept, err = getNonceAccept([]byte(key))
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusInternalServerError, err
|
||||||
|
}
|
||||||
|
return http.StatusSwitchingProtocols, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Origin parses the Origin header in req.
|
||||||
|
// If the Origin header is not set, it returns nil and nil.
|
||||||
|
func Origin(config *Config, req *http.Request) (*url.URL, error) {
|
||||||
|
var origin string
|
||||||
|
switch config.Version {
|
||||||
|
case ProtocolVersionHybi13:
|
||||||
|
origin = req.Header.Get("Origin")
|
||||||
|
}
|
||||||
|
if origin == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return url.ParseRequestURI(origin)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *hybiServerHandshaker) AcceptHandshake(buf *bufio.Writer) (err error) {
|
||||||
|
if len(c.Protocol) > 0 {
|
||||||
|
if len(c.Protocol) != 1 {
|
||||||
|
// You need choose a Protocol in Handshake func in Server.
|
||||||
|
return ErrBadWebSocketProtocol
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buf.WriteString("HTTP/1.1 101 Switching Protocols\r\n")
|
||||||
|
buf.WriteString("Upgrade: websocket\r\n")
|
||||||
|
buf.WriteString("Connection: Upgrade\r\n")
|
||||||
|
buf.WriteString("Sec-WebSocket-Accept: " + string(c.accept) + "\r\n")
|
||||||
|
if len(c.Protocol) > 0 {
|
||||||
|
buf.WriteString("Sec-WebSocket-Protocol: " + c.Protocol[0] + "\r\n")
|
||||||
|
}
|
||||||
|
// TODO(ukai): send Sec-WebSocket-Extensions.
|
||||||
|
if c.Header != nil {
|
||||||
|
err := c.Header.WriteSubset(buf, handshakeHeader)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buf.WriteString("\r\n")
|
||||||
|
return buf.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *hybiServerHandshaker) NewServerConn(buf *bufio.ReadWriter, rwc io.ReadWriteCloser, request *http.Request) *Conn {
|
||||||
|
return newHybiServerConn(c.Config, buf, rwc, request)
|
||||||
|
}
|
||||||
|
|
||||||
|
// newHybiServerConn returns a new WebSocket connection speaking hybi draft protocol.
|
||||||
|
func newHybiServerConn(config *Config, buf *bufio.ReadWriter, rwc io.ReadWriteCloser, request *http.Request) *Conn {
|
||||||
|
return newHybiConn(config, buf, rwc, request)
|
||||||
|
}
|
113
vendor/golang.org/x/net/websocket/server.go
generated
vendored
Normal file
113
vendor/golang.org/x/net/websocket/server.go
generated
vendored
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newServerConn(rwc io.ReadWriteCloser, buf *bufio.ReadWriter, req *http.Request, config *Config, handshake func(*Config, *http.Request) error) (conn *Conn, err error) {
|
||||||
|
var hs serverHandshaker = &hybiServerHandshaker{Config: config}
|
||||||
|
code, err := hs.ReadHandshake(buf.Reader, req)
|
||||||
|
if err == ErrBadWebSocketVersion {
|
||||||
|
fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
|
||||||
|
fmt.Fprintf(buf, "Sec-WebSocket-Version: %s\r\n", SupportedProtocolVersion)
|
||||||
|
buf.WriteString("\r\n")
|
||||||
|
buf.WriteString(err.Error())
|
||||||
|
buf.Flush()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
|
||||||
|
buf.WriteString("\r\n")
|
||||||
|
buf.WriteString(err.Error())
|
||||||
|
buf.Flush()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if handshake != nil {
|
||||||
|
err = handshake(config, req)
|
||||||
|
if err != nil {
|
||||||
|
code = http.StatusForbidden
|
||||||
|
fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
|
||||||
|
buf.WriteString("\r\n")
|
||||||
|
buf.Flush()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = hs.AcceptHandshake(buf.Writer)
|
||||||
|
if err != nil {
|
||||||
|
code = http.StatusBadRequest
|
||||||
|
fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
|
||||||
|
buf.WriteString("\r\n")
|
||||||
|
buf.Flush()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
conn = hs.NewServerConn(buf, rwc, req)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server represents a server of a WebSocket.
|
||||||
|
type Server struct {
|
||||||
|
// Config is a WebSocket configuration for new WebSocket connection.
|
||||||
|
Config
|
||||||
|
|
||||||
|
// Handshake is an optional function in WebSocket handshake.
|
||||||
|
// For example, you can check, or don't check Origin header.
|
||||||
|
// Another example, you can select config.Protocol.
|
||||||
|
Handshake func(*Config, *http.Request) error
|
||||||
|
|
||||||
|
// Handler handles a WebSocket connection.
|
||||||
|
Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeHTTP implements the http.Handler interface for a WebSocket
|
||||||
|
func (s Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
|
s.serveWebSocket(w, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Server) serveWebSocket(w http.ResponseWriter, req *http.Request) {
|
||||||
|
rwc, buf, err := w.(http.Hijacker).Hijack()
|
||||||
|
if err != nil {
|
||||||
|
panic("Hijack failed: " + err.Error())
|
||||||
|
}
|
||||||
|
// The server should abort the WebSocket connection if it finds
|
||||||
|
// the client did not send a handshake that matches with protocol
|
||||||
|
// specification.
|
||||||
|
defer rwc.Close()
|
||||||
|
conn, err := newServerConn(rwc, buf, req, &s.Config, s.Handshake)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if conn == nil {
|
||||||
|
panic("unexpected nil conn")
|
||||||
|
}
|
||||||
|
s.Handler(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler is a simple interface to a WebSocket browser client.
|
||||||
|
// It checks if Origin header is valid URL by default.
|
||||||
|
// You might want to verify websocket.Conn.Config().Origin in the func.
|
||||||
|
// If you use Server instead of Handler, you could call websocket.Origin and
|
||||||
|
// check the origin in your Handshake func. So, if you want to accept
|
||||||
|
// non-browser clients, which do not send an Origin header, set a
|
||||||
|
// Server.Handshake that does not check the origin.
|
||||||
|
type Handler func(*Conn)
|
||||||
|
|
||||||
|
func checkOrigin(config *Config, req *http.Request) (err error) {
|
||||||
|
config.Origin, err = Origin(config, req)
|
||||||
|
if err == nil && config.Origin == nil {
|
||||||
|
return fmt.Errorf("null origin")
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeHTTP implements the http.Handler interface for a WebSocket
|
||||||
|
func (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
|
s := Server{Handler: h, Handshake: checkOrigin}
|
||||||
|
s.serveWebSocket(w, req)
|
||||||
|
}
|
448
vendor/golang.org/x/net/websocket/websocket.go
generated
vendored
Normal file
448
vendor/golang.org/x/net/websocket/websocket.go
generated
vendored
Normal file
@ -0,0 +1,448 @@
|
|||||||
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package websocket implements a client and server for the WebSocket protocol
|
||||||
|
// as specified in RFC 6455.
|
||||||
|
//
|
||||||
|
// This package currently lacks some features found in an alternative
|
||||||
|
// and more actively maintained WebSocket package:
|
||||||
|
//
|
||||||
|
// https://godoc.org/github.com/gorilla/websocket
|
||||||
|
//
|
||||||
|
package websocket // import "golang.org/x/net/websocket"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ProtocolVersionHybi13 = 13
|
||||||
|
ProtocolVersionHybi = ProtocolVersionHybi13
|
||||||
|
SupportedProtocolVersion = "13"
|
||||||
|
|
||||||
|
ContinuationFrame = 0
|
||||||
|
TextFrame = 1
|
||||||
|
BinaryFrame = 2
|
||||||
|
CloseFrame = 8
|
||||||
|
PingFrame = 9
|
||||||
|
PongFrame = 10
|
||||||
|
UnknownFrame = 255
|
||||||
|
|
||||||
|
DefaultMaxPayloadBytes = 32 << 20 // 32MB
|
||||||
|
)
|
||||||
|
|
||||||
|
// ProtocolError represents WebSocket protocol errors.
|
||||||
|
type ProtocolError struct {
|
||||||
|
ErrorString string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err *ProtocolError) Error() string { return err.ErrorString }
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrBadProtocolVersion = &ProtocolError{"bad protocol version"}
|
||||||
|
ErrBadScheme = &ProtocolError{"bad scheme"}
|
||||||
|
ErrBadStatus = &ProtocolError{"bad status"}
|
||||||
|
ErrBadUpgrade = &ProtocolError{"missing or bad upgrade"}
|
||||||
|
ErrBadWebSocketOrigin = &ProtocolError{"missing or bad WebSocket-Origin"}
|
||||||
|
ErrBadWebSocketLocation = &ProtocolError{"missing or bad WebSocket-Location"}
|
||||||
|
ErrBadWebSocketProtocol = &ProtocolError{"missing or bad WebSocket-Protocol"}
|
||||||
|
ErrBadWebSocketVersion = &ProtocolError{"missing or bad WebSocket Version"}
|
||||||
|
ErrChallengeResponse = &ProtocolError{"mismatch challenge/response"}
|
||||||
|
ErrBadFrame = &ProtocolError{"bad frame"}
|
||||||
|
ErrBadFrameBoundary = &ProtocolError{"not on frame boundary"}
|
||||||
|
ErrNotWebSocket = &ProtocolError{"not websocket protocol"}
|
||||||
|
ErrBadRequestMethod = &ProtocolError{"bad method"}
|
||||||
|
ErrNotSupported = &ProtocolError{"not supported"}
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrFrameTooLarge is returned by Codec's Receive method if payload size
|
||||||
|
// exceeds limit set by Conn.MaxPayloadBytes
|
||||||
|
var ErrFrameTooLarge = errors.New("websocket: frame payload size exceeds limit")
|
||||||
|
|
||||||
|
// Addr is an implementation of net.Addr for WebSocket.
|
||||||
|
type Addr struct {
|
||||||
|
*url.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
// Network returns the network type for a WebSocket, "websocket".
|
||||||
|
func (addr *Addr) Network() string { return "websocket" }
|
||||||
|
|
||||||
|
// Config is a WebSocket configuration
|
||||||
|
type Config struct {
|
||||||
|
// A WebSocket server address.
|
||||||
|
Location *url.URL
|
||||||
|
|
||||||
|
// A Websocket client origin.
|
||||||
|
Origin *url.URL
|
||||||
|
|
||||||
|
// WebSocket subprotocols.
|
||||||
|
Protocol []string
|
||||||
|
|
||||||
|
// WebSocket protocol version.
|
||||||
|
Version int
|
||||||
|
|
||||||
|
// TLS config for secure WebSocket (wss).
|
||||||
|
TlsConfig *tls.Config
|
||||||
|
|
||||||
|
// Additional header fields to be sent in WebSocket opening handshake.
|
||||||
|
Header http.Header
|
||||||
|
|
||||||
|
// Dialer used when opening websocket connections.
|
||||||
|
Dialer *net.Dialer
|
||||||
|
|
||||||
|
handshakeData map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
// serverHandshaker is an interface to handle WebSocket server side handshake.
|
||||||
|
type serverHandshaker interface {
|
||||||
|
// ReadHandshake reads handshake request message from client.
|
||||||
|
// Returns http response code and error if any.
|
||||||
|
ReadHandshake(buf *bufio.Reader, req *http.Request) (code int, err error)
|
||||||
|
|
||||||
|
// AcceptHandshake accepts the client handshake request and sends
|
||||||
|
// handshake response back to client.
|
||||||
|
AcceptHandshake(buf *bufio.Writer) (err error)
|
||||||
|
|
||||||
|
// NewServerConn creates a new WebSocket connection.
|
||||||
|
NewServerConn(buf *bufio.ReadWriter, rwc io.ReadWriteCloser, request *http.Request) (conn *Conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// frameReader is an interface to read a WebSocket frame.
|
||||||
|
type frameReader interface {
|
||||||
|
// Reader is to read payload of the frame.
|
||||||
|
io.Reader
|
||||||
|
|
||||||
|
// PayloadType returns payload type.
|
||||||
|
PayloadType() byte
|
||||||
|
|
||||||
|
// HeaderReader returns a reader to read header of the frame.
|
||||||
|
HeaderReader() io.Reader
|
||||||
|
|
||||||
|
// TrailerReader returns a reader to read trailer of the frame.
|
||||||
|
// If it returns nil, there is no trailer in the frame.
|
||||||
|
TrailerReader() io.Reader
|
||||||
|
|
||||||
|
// Len returns total length of the frame, including header and trailer.
|
||||||
|
Len() int
|
||||||
|
}
|
||||||
|
|
||||||
|
// frameReaderFactory is an interface to creates new frame reader.
|
||||||
|
type frameReaderFactory interface {
|
||||||
|
NewFrameReader() (r frameReader, err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// frameWriter is an interface to write a WebSocket frame.
|
||||||
|
type frameWriter interface {
|
||||||
|
// Writer is to write payload of the frame.
|
||||||
|
io.WriteCloser
|
||||||
|
}
|
||||||
|
|
||||||
|
// frameWriterFactory is an interface to create new frame writer.
|
||||||
|
type frameWriterFactory interface {
|
||||||
|
NewFrameWriter(payloadType byte) (w frameWriter, err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type frameHandler interface {
|
||||||
|
HandleFrame(frame frameReader) (r frameReader, err error)
|
||||||
|
WriteClose(status int) (err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Conn represents a WebSocket connection.
|
||||||
|
//
|
||||||
|
// Multiple goroutines may invoke methods on a Conn simultaneously.
|
||||||
|
type Conn struct {
|
||||||
|
config *Config
|
||||||
|
request *http.Request
|
||||||
|
|
||||||
|
buf *bufio.ReadWriter
|
||||||
|
rwc io.ReadWriteCloser
|
||||||
|
|
||||||
|
rio sync.Mutex
|
||||||
|
frameReaderFactory
|
||||||
|
frameReader
|
||||||
|
|
||||||
|
wio sync.Mutex
|
||||||
|
frameWriterFactory
|
||||||
|
|
||||||
|
frameHandler
|
||||||
|
PayloadType byte
|
||||||
|
defaultCloseStatus int
|
||||||
|
|
||||||
|
// MaxPayloadBytes limits the size of frame payload received over Conn
|
||||||
|
// by Codec's Receive method. If zero, DefaultMaxPayloadBytes is used.
|
||||||
|
MaxPayloadBytes int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read implements the io.Reader interface:
|
||||||
|
// it reads data of a frame from the WebSocket connection.
|
||||||
|
// if msg is not large enough for the frame data, it fills the msg and next Read
|
||||||
|
// will read the rest of the frame data.
|
||||||
|
// it reads Text frame or Binary frame.
|
||||||
|
func (ws *Conn) Read(msg []byte) (n int, err error) {
|
||||||
|
ws.rio.Lock()
|
||||||
|
defer ws.rio.Unlock()
|
||||||
|
again:
|
||||||
|
if ws.frameReader == nil {
|
||||||
|
frame, err := ws.frameReaderFactory.NewFrameReader()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
ws.frameReader, err = ws.frameHandler.HandleFrame(frame)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if ws.frameReader == nil {
|
||||||
|
goto again
|
||||||
|
}
|
||||||
|
}
|
||||||
|
n, err = ws.frameReader.Read(msg)
|
||||||
|
if err == io.EOF {
|
||||||
|
if trailer := ws.frameReader.TrailerReader(); trailer != nil {
|
||||||
|
io.Copy(ioutil.Discard, trailer)
|
||||||
|
}
|
||||||
|
ws.frameReader = nil
|
||||||
|
goto again
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write implements the io.Writer interface:
|
||||||
|
// it writes data as a frame to the WebSocket connection.
|
||||||
|
func (ws *Conn) Write(msg []byte) (n int, err error) {
|
||||||
|
ws.wio.Lock()
|
||||||
|
defer ws.wio.Unlock()
|
||||||
|
w, err := ws.frameWriterFactory.NewFrameWriter(ws.PayloadType)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
n, err = w.Write(msg)
|
||||||
|
w.Close()
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close implements the io.Closer interface.
|
||||||
|
func (ws *Conn) Close() error {
|
||||||
|
err := ws.frameHandler.WriteClose(ws.defaultCloseStatus)
|
||||||
|
err1 := ws.rwc.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return err1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *Conn) IsClientConn() bool { return ws.request == nil }
|
||||||
|
func (ws *Conn) IsServerConn() bool { return ws.request != nil }
|
||||||
|
|
||||||
|
// LocalAddr returns the WebSocket Origin for the connection for client, or
|
||||||
|
// the WebSocket location for server.
|
||||||
|
func (ws *Conn) LocalAddr() net.Addr {
|
||||||
|
if ws.IsClientConn() {
|
||||||
|
return &Addr{ws.config.Origin}
|
||||||
|
}
|
||||||
|
return &Addr{ws.config.Location}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoteAddr returns the WebSocket location for the connection for client, or
|
||||||
|
// the Websocket Origin for server.
|
||||||
|
func (ws *Conn) RemoteAddr() net.Addr {
|
||||||
|
if ws.IsClientConn() {
|
||||||
|
return &Addr{ws.config.Location}
|
||||||
|
}
|
||||||
|
return &Addr{ws.config.Origin}
|
||||||
|
}
|
||||||
|
|
||||||
|
var errSetDeadline = errors.New("websocket: cannot set deadline: not using a net.Conn")
|
||||||
|
|
||||||
|
// SetDeadline sets the connection's network read & write deadlines.
|
||||||
|
func (ws *Conn) SetDeadline(t time.Time) error {
|
||||||
|
if conn, ok := ws.rwc.(net.Conn); ok {
|
||||||
|
return conn.SetDeadline(t)
|
||||||
|
}
|
||||||
|
return errSetDeadline
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetReadDeadline sets the connection's network read deadline.
|
||||||
|
func (ws *Conn) SetReadDeadline(t time.Time) error {
|
||||||
|
if conn, ok := ws.rwc.(net.Conn); ok {
|
||||||
|
return conn.SetReadDeadline(t)
|
||||||
|
}
|
||||||
|
return errSetDeadline
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetWriteDeadline sets the connection's network write deadline.
|
||||||
|
func (ws *Conn) SetWriteDeadline(t time.Time) error {
|
||||||
|
if conn, ok := ws.rwc.(net.Conn); ok {
|
||||||
|
return conn.SetWriteDeadline(t)
|
||||||
|
}
|
||||||
|
return errSetDeadline
|
||||||
|
}
|
||||||
|
|
||||||
|
// Config returns the WebSocket config.
|
||||||
|
func (ws *Conn) Config() *Config { return ws.config }
|
||||||
|
|
||||||
|
// Request returns the http request upgraded to the WebSocket.
|
||||||
|
// It is nil for client side.
|
||||||
|
func (ws *Conn) Request() *http.Request { return ws.request }
|
||||||
|
|
||||||
|
// Codec represents a symmetric pair of functions that implement a codec.
|
||||||
|
type Codec struct {
|
||||||
|
Marshal func(v interface{}) (data []byte, payloadType byte, err error)
|
||||||
|
Unmarshal func(data []byte, payloadType byte, v interface{}) (err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send sends v marshaled by cd.Marshal as single frame to ws.
|
||||||
|
func (cd Codec) Send(ws *Conn, v interface{}) (err error) {
|
||||||
|
data, payloadType, err := cd.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ws.wio.Lock()
|
||||||
|
defer ws.wio.Unlock()
|
||||||
|
w, err := ws.frameWriterFactory.NewFrameWriter(payloadType)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = w.Write(data)
|
||||||
|
w.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Receive receives single frame from ws, unmarshaled by cd.Unmarshal and stores
|
||||||
|
// in v. The whole frame payload is read to an in-memory buffer; max size of
|
||||||
|
// payload is defined by ws.MaxPayloadBytes. If frame payload size exceeds
|
||||||
|
// limit, ErrFrameTooLarge is returned; in this case frame is not read off wire
|
||||||
|
// completely. The next call to Receive would read and discard leftover data of
|
||||||
|
// previous oversized frame before processing next frame.
|
||||||
|
func (cd Codec) Receive(ws *Conn, v interface{}) (err error) {
|
||||||
|
ws.rio.Lock()
|
||||||
|
defer ws.rio.Unlock()
|
||||||
|
if ws.frameReader != nil {
|
||||||
|
_, err = io.Copy(ioutil.Discard, ws.frameReader)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ws.frameReader = nil
|
||||||
|
}
|
||||||
|
again:
|
||||||
|
frame, err := ws.frameReaderFactory.NewFrameReader()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
frame, err = ws.frameHandler.HandleFrame(frame)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if frame == nil {
|
||||||
|
goto again
|
||||||
|
}
|
||||||
|
maxPayloadBytes := ws.MaxPayloadBytes
|
||||||
|
if maxPayloadBytes == 0 {
|
||||||
|
maxPayloadBytes = DefaultMaxPayloadBytes
|
||||||
|
}
|
||||||
|
if hf, ok := frame.(*hybiFrameReader); ok && hf.header.Length > int64(maxPayloadBytes) {
|
||||||
|
// payload size exceeds limit, no need to call Unmarshal
|
||||||
|
//
|
||||||
|
// set frameReader to current oversized frame so that
|
||||||
|
// the next call to this function can drain leftover
|
||||||
|
// data before processing the next frame
|
||||||
|
ws.frameReader = frame
|
||||||
|
return ErrFrameTooLarge
|
||||||
|
}
|
||||||
|
payloadType := frame.PayloadType()
|
||||||
|
data, err := ioutil.ReadAll(frame)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return cd.Unmarshal(data, payloadType, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func marshal(v interface{}) (msg []byte, payloadType byte, err error) {
|
||||||
|
switch data := v.(type) {
|
||||||
|
case string:
|
||||||
|
return []byte(data), TextFrame, nil
|
||||||
|
case []byte:
|
||||||
|
return data, BinaryFrame, nil
|
||||||
|
}
|
||||||
|
return nil, UnknownFrame, ErrNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmarshal(msg []byte, payloadType byte, v interface{}) (err error) {
|
||||||
|
switch data := v.(type) {
|
||||||
|
case *string:
|
||||||
|
*data = string(msg)
|
||||||
|
return nil
|
||||||
|
case *[]byte:
|
||||||
|
*data = msg
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return ErrNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Message is a codec to send/receive text/binary data in a frame on WebSocket connection.
|
||||||
|
To send/receive text frame, use string type.
|
||||||
|
To send/receive binary frame, use []byte type.
|
||||||
|
|
||||||
|
Trivial usage:
|
||||||
|
|
||||||
|
import "websocket"
|
||||||
|
|
||||||
|
// receive text frame
|
||||||
|
var message string
|
||||||
|
websocket.Message.Receive(ws, &message)
|
||||||
|
|
||||||
|
// send text frame
|
||||||
|
message = "hello"
|
||||||
|
websocket.Message.Send(ws, message)
|
||||||
|
|
||||||
|
// receive binary frame
|
||||||
|
var data []byte
|
||||||
|
websocket.Message.Receive(ws, &data)
|
||||||
|
|
||||||
|
// send binary frame
|
||||||
|
data = []byte{0, 1, 2}
|
||||||
|
websocket.Message.Send(ws, data)
|
||||||
|
|
||||||
|
*/
|
||||||
|
var Message = Codec{marshal, unmarshal}
|
||||||
|
|
||||||
|
func jsonMarshal(v interface{}) (msg []byte, payloadType byte, err error) {
|
||||||
|
msg, err = json.Marshal(v)
|
||||||
|
return msg, TextFrame, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func jsonUnmarshal(msg []byte, payloadType byte, v interface{}) (err error) {
|
||||||
|
return json.Unmarshal(msg, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
JSON is a codec to send/receive JSON data in a frame from a WebSocket connection.
|
||||||
|
|
||||||
|
Trivial usage:
|
||||||
|
|
||||||
|
import "websocket"
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
Msg string
|
||||||
|
Count int
|
||||||
|
}
|
||||||
|
|
||||||
|
// receive JSON type T
|
||||||
|
var data T
|
||||||
|
websocket.JSON.Receive(ws, &data)
|
||||||
|
|
||||||
|
// send JSON type T
|
||||||
|
websocket.JSON.Send(ws, data)
|
||||||
|
*/
|
||||||
|
var JSON = Codec{jsonMarshal, jsonUnmarshal}
|
18
vendor/vendor.json
vendored
18
vendor/vendor.json
vendored
@ -58,6 +58,18 @@
|
|||||||
"revision": "44cc805cf13205b55f69e14bcb69867d1ae92f98",
|
"revision": "44cc805cf13205b55f69e14bcb69867d1ae92f98",
|
||||||
"revisionTime": "2016-08-05T00:47:13Z"
|
"revisionTime": "2016-08-05T00:47:13Z"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"checksumSHA1": "LHwm1G0lFyQ3X0iHZ/MOYnAtjd4=",
|
||||||
|
"path": "github.com/eclipse/paho.mqtt.golang",
|
||||||
|
"revision": "d06cc70ac43d625e602946b5ff2346ddebb768e4",
|
||||||
|
"revisionTime": "2017-06-02T16:30:32Z"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"checksumSHA1": "g2AaO9VMaxzFQZkrCfxBUV3yAyM=",
|
||||||
|
"path": "github.com/eclipse/paho.mqtt.golang/packets",
|
||||||
|
"revision": "d06cc70ac43d625e602946b5ff2346ddebb768e4",
|
||||||
|
"revisionTime": "2017-06-02T16:30:32Z"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"path": "github.com/elazarl/go-bindata-assetfs",
|
"path": "github.com/elazarl/go-bindata-assetfs",
|
||||||
"revision": "57eb5e1fc594ad4b0b1dbea7b286d299e0cb43c2",
|
"revision": "57eb5e1fc594ad4b0b1dbea7b286d299e0cb43c2",
|
||||||
@ -377,6 +389,12 @@
|
|||||||
"revision": "f2499483f923065a842d38eb4c7f1927e6fc6e6d",
|
"revision": "f2499483f923065a842d38eb4c7f1927e6fc6e6d",
|
||||||
"revisionTime": "2017-01-14T04:22:49Z"
|
"revisionTime": "2017-01-14T04:22:49Z"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"checksumSHA1": "7EZyXN0EmZLgGxZxK01IJua4c8o=",
|
||||||
|
"path": "golang.org/x/net/websocket",
|
||||||
|
"revision": "e4fa1c5465ad6111f206fc92186b8c83d64adbe1",
|
||||||
|
"revisionTime": "2017-06-02T19:04:32Z"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "ZVZBntvdoK/qUq/6uF8YMWmuMC4=",
|
"checksumSHA1": "ZVZBntvdoK/qUq/6uF8YMWmuMC4=",
|
||||||
"path": "golang.org/x/sys/unix",
|
"path": "golang.org/x/sys/unix",
|
||||||
|
Loading…
Reference in New Issue
Block a user