2 // Copyright © 2011-2019 Guy M. Allard
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
27 "github.com/gmallard/stompngo/senv"
31 Wrapper for primary STOMP Connect function that returns an interface.
33 func NewConnector(n net
.Conn
, h Headers
) (STOMPConnector
, error
) {
38 Primary STOMP Connect.
40 For STOMP 1.1+ the Headers parameter MUST contain the headers required
41 by the specification. Those headers are not magically inferred.
44 // Obtain a network connection
45 n, e := net.Dial(NetProtoTCP, "localhost:61613")
47 // Do something sane ...
49 h := stompngo.Headers{} // A STOMP 1.0 connection request
50 c, e := stompngo.Connect(n, h)
52 // Do something sane ...
57 // Obtain a network connection
58 n, e := net.Dial(NetProtoTCP, "localhost:61613")
60 // Do something sane ...
62 h := stompngo.Headers{HK_ACCEPT_VERSION, "1.1",
63 HK_HOST, "localhost"} // A STOMP 1.1 connection
64 c, e := stompngo.Connect(n, h)
66 // Do something sane ...
70 func Connect(n net
.Conn
, h Headers
) (*Connection
, error
) {
74 if e
:= h
.Validate(); e
!= nil {
77 if _
, ok
:= h
.Contains(HK_RECEIPT
); ok
{
81 //fmt.Printf("CONDB01\n")
82 c
:= &Connection
{netconn
: n
,
83 input
: make(chan MessageData
, 1),
84 output
: make(chan wiredata
),
88 subs
: make(map[string]*subscription
),
89 DisconnectReceipt
: MessageData
{},
90 ssdc
: make(chan struct{}),
91 wtrsdc
: make(chan struct{}),
96 c
.mets
= &metrics
{st
: time
.Now()}
99 c
.MessageData
= c
.input
101 // Check that the client wants a version we support
102 if e
:= c
.checkClientVersions(h
); e
!= nil {
105 // Optional logging from connection start
106 ln
:= senv
.WantLogger()
108 c
.SetLogger(log
.New(os
.Stdout
, ln
+" ",
109 log
.Ldate|log
.Lmicroseconds|log
.Lshortfile
))
112 // OK, put a CONNECT on the wire
113 c
.wtr
= bufio
.NewWriter(n
) // Create the writer
114 go c
.writer() // Start it
115 f
:= Frame
{CONNECT
, ch
, NULLBUFF
} // Create actual CONNECT frame
116 r
:= make(chan error
) // Make the error channel for a write
117 if e
:= c
.writeWireData(wiredata
{f
, r
}); e
!= nil { // Send the CONNECT frame
120 e
:= <-r
// Retrieve any error
123 c
.sysAbort() // Shutdown, we are done with errors
126 //fmt.Printf("CONDB03\n")
128 e
= c
.connectHandler(ch
)
130 c
.sysAbort() // Shutdown , we are done with errors
133 //fmt.Printf("CONDB04\n")