options_reader.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2021 IBM Corp and others.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Seth Hoenig
  15. * Allan Stockdill-Mander
  16. * Mike Robertson
  17. */
  18. package mqtt
  19. import (
  20. "crypto/tls"
  21. "net/http"
  22. "net/url"
  23. "time"
  24. )
  25. // ClientOptionsReader provides an interface for reading ClientOptions after the client has been initialized.
  26. type ClientOptionsReader struct {
  27. options *ClientOptions
  28. }
  29. // Servers returns a slice of the servers defined in the clientoptions
  30. func (r *ClientOptionsReader) Servers() []*url.URL {
  31. s := make([]*url.URL, len(r.options.Servers))
  32. for i, u := range r.options.Servers {
  33. nu := *u
  34. s[i] = &nu
  35. }
  36. return s
  37. }
  38. // ResumeSubs returns true if resuming stored (un)sub is enabled
  39. func (r *ClientOptionsReader) ResumeSubs() bool {
  40. s := r.options.ResumeSubs
  41. return s
  42. }
  43. // ClientID returns the set client id
  44. func (r *ClientOptionsReader) ClientID() string {
  45. s := r.options.ClientID
  46. return s
  47. }
  48. // Username returns the set username
  49. func (r *ClientOptionsReader) Username() string {
  50. s := r.options.Username
  51. return s
  52. }
  53. // Password returns the set password
  54. func (r *ClientOptionsReader) Password() string {
  55. s := r.options.Password
  56. return s
  57. }
  58. // CleanSession returns whether Cleansession is set
  59. func (r *ClientOptionsReader) CleanSession() bool {
  60. s := r.options.CleanSession
  61. return s
  62. }
  63. func (r *ClientOptionsReader) Order() bool {
  64. s := r.options.Order
  65. return s
  66. }
  67. func (r *ClientOptionsReader) WillEnabled() bool {
  68. s := r.options.WillEnabled
  69. return s
  70. }
  71. func (r *ClientOptionsReader) WillTopic() string {
  72. s := r.options.WillTopic
  73. return s
  74. }
  75. func (r *ClientOptionsReader) WillPayload() []byte {
  76. s := r.options.WillPayload
  77. return s
  78. }
  79. func (r *ClientOptionsReader) WillQos() byte {
  80. s := r.options.WillQos
  81. return s
  82. }
  83. func (r *ClientOptionsReader) WillRetained() bool {
  84. s := r.options.WillRetained
  85. return s
  86. }
  87. func (r *ClientOptionsReader) ProtocolVersion() uint {
  88. s := r.options.ProtocolVersion
  89. return s
  90. }
  91. func (r *ClientOptionsReader) TLSConfig() *tls.Config {
  92. s := r.options.TLSConfig
  93. return s
  94. }
  95. func (r *ClientOptionsReader) KeepAlive() time.Duration {
  96. s := time.Duration(r.options.KeepAlive * int64(time.Second))
  97. return s
  98. }
  99. func (r *ClientOptionsReader) PingTimeout() time.Duration {
  100. s := r.options.PingTimeout
  101. return s
  102. }
  103. func (r *ClientOptionsReader) ConnectTimeout() time.Duration {
  104. s := r.options.ConnectTimeout
  105. return s
  106. }
  107. func (r *ClientOptionsReader) MaxReconnectInterval() time.Duration {
  108. s := r.options.MaxReconnectInterval
  109. return s
  110. }
  111. func (r *ClientOptionsReader) AutoReconnect() bool {
  112. s := r.options.AutoReconnect
  113. return s
  114. }
  115. // ConnectRetryInterval returns the delay between retries on the initial connection (if ConnectRetry true)
  116. func (r *ClientOptionsReader) ConnectRetryInterval() time.Duration {
  117. s := r.options.ConnectRetryInterval
  118. return s
  119. }
  120. // ConnectRetry returns whether the initial connection request will be retried until connection established
  121. func (r *ClientOptionsReader) ConnectRetry() bool {
  122. s := r.options.ConnectRetry
  123. return s
  124. }
  125. func (r *ClientOptionsReader) WriteTimeout() time.Duration {
  126. s := r.options.WriteTimeout
  127. return s
  128. }
  129. func (r *ClientOptionsReader) MessageChannelDepth() uint {
  130. s := r.options.MessageChannelDepth
  131. return s
  132. }
  133. func (r *ClientOptionsReader) HTTPHeaders() http.Header {
  134. h := r.options.HTTPHeaders
  135. return h
  136. }
  137. // WebsocketOptions returns the currently configured WebSocket options
  138. func (r *ClientOptionsReader) WebsocketOptions() *WebsocketOptions {
  139. s := r.options.WebsocketOptions
  140. return s
  141. }