JMS interop examples updated for common code / logging.
[stompngo_examples.git] / tlsexamps / tlsuc1 / tlsuc1.go
blob4ecfc945b84019ebf08b2eccab5ba28d37c54056
1 //
2 // Copyright © 2013-2016 Guy M. Allard
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
18 Connect and Disconnect from a STOMP broker with a TLS connection, use case 1.
20 TLS Use Case 1 - client does *not* authenticate broker.
22 Subcase 1.A - Message broker configuration does *not* require client authentication
24 - Expect connection success
26 Subcase 1.B - Message broker configuration *does* require client authentication
28 - Expect connection failure (broker must be sent a valid client certificate)
30 Example use might be:
32 go build
33 ./tlsuc1
36 package main
38 import (
39 "crypto/tls"
40 "log"
41 "os"
42 "time"
43 // sngecomm methods are used specifically for these example clients.
44 "github.com/gmallard/stompngo_examples/sngecomm"
47 var (
48 exampid = "tlsuc1: "
49 tc *tls.Config
51 ll = log.New(os.Stdout, "TLSU1 ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
53 tag = "tuc1main"
56 // Connect to a STOMP broker using TLS and disconnect.
57 func main() {
59 st := time.Now()
61 ll.Printf("%stag:%s connsess:%s starts\n",
62 exampid, tag, sngecomm.Lcs)
64 // TLS Configuration.
65 tc = new(tls.Config)
66 tc.InsecureSkipVerify = true // Do *not* check the server's certificate
68 // Standard example TLS connect sequence
69 n, conn, e := sngecomm.CommonTLSConnect(exampid, tag, ll, tc)
70 if e != nil {
71 ll.Fatalf("%stag:%s connsess:%s main_on_connect error:%v",
72 exampid, tag, sngecomm.Lcs,
73 e.Error()) // Handle this ......
76 nc := n.(*tls.Conn)
77 sngecomm.DumpTLSConfig(exampid, tc, nc)
79 // *NOTE* application specific functionaltiy starts here!
80 // For you to add.
81 // *NOTE* application specific functionaltiy ends here!
83 // Standard example disconnect sequence
84 e = sngecomm.CommonDisconnect(n, conn, exampid, tag, ll)
85 if e != nil {
86 ll.Fatalf("%s %s\n", exampid, e.Error()) // Handle this ......
89 ll.Printf("%stag:%s connsess:%s main_elapsed:%v\n",
90 exampid, tag, conn.Session(),
91 time.Now().Sub(st))