Add custom cipher suite demo to all TLS examples.
[stompngo_examples.git] / sngecomm / environment.go
blob447a8387a9feccf3c1c21645698a6ebd4bd94cd4
1 //
2 // Copyright © 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 Package sngecomm provides common functionality used in the stompngo_examples
19 project.
21 package sngecomm
23 import (
24 "bytes"
25 "log"
26 "os"
27 "strconv"
29 // "github.com/gmallard/stompngo"
32 var (
34 nqs = 1 // Default number of queues for multi-queue demo(s)
35 mdml = 1024 * 32 // Message data max length of variable message, 32K
36 md = make([]byte, 1) // Additional message data, primed during init()
37 pbc = 64 // Number of bytes to print (used in some
38 // // examples that receive).
40 sendFact float64 = 1.0 // Send sleep time factor
41 recvFact float64 = 1.0 // Receive sleep time factor
43 ackMode = "auto" // The default ack mode
45 pprof = false // Do not do profiling
47 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0xC0,0x2F
48 // TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 = 0xC0,0x2B
49 // TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 = 0xC0,0x30
50 cipherSuites = []uint16{
51 0xc02f,
52 0xc02b,
53 0xc030,
56 useCustomCiphers = false // Set Custom Cipher Suite list
60 // Initialization
61 func init() {
62 p := "_123456789ABCDEF"
63 c := mdml / len(p)
64 b := []byte(p)
65 md = bytes.Repeat(b, c) // A long string
69 // Number of queues
70 func Nqs() int {
72 if s := os.Getenv("STOMP_NQS"); s != "" {
73 i, e := strconv.ParseInt(s, 10, 32)
74 if nil != e {
75 log.Printf("v1:%v v2:%v\n", "NQS conversion error", e)
76 } else {
77 nqs = int(i)
80 return nqs
83 // Max Data Message Length
84 func Mdml() int {
85 if s := os.Getenv("STOMP_MDML"); s != "" {
86 i, e := strconv.ParseInt(s, 10, 32)
87 if nil != e {
88 log.Printf("v1:%v v2:%v\n", "MDML conversion error", e)
89 } else {
90 mdml = int(i)
91 p := "_123456789ABCDEF"
92 c := mdml / len(p)
93 b := []byte(p)
94 md = bytes.Repeat(b, c) // A long string
97 return mdml
100 // Use profiling or not
101 func Pprof() bool {
102 if am := os.Getenv("STOMP_PPROF"); am != "" {
103 pprof = true
105 return pprof
108 // ACK mode for those examples that use it.
109 func AckMode() string {
110 if am := os.Getenv("STOMP_ACKMODE"); am != "" {
111 if am == "auto" || am == "client" || am == "client-individual" {
112 ackMode = am
113 } else {
114 log.Printf("v1:%v v2:%v\n", "ACKMODE error", am)
117 return ackMode
120 // Get Send Sleep Factor
121 func SendFactor() float64 {
122 if s := os.Getenv("STOMP_SENDFACT"); s != "" {
123 f, e := strconv.ParseFloat(s, 64)
124 if nil != e {
125 log.Printf("v1:%v v2:%v\n", "SENDFACT conversion error", e)
126 } else {
127 sendFact = float64(f)
130 return sendFact
133 // Get Recv Sleep Factor
134 func RecvFactor() float64 {
135 if s := os.Getenv("STOMP_RECVFACT"); s != "" {
136 f, e := strconv.ParseFloat(s, 64)
137 if nil != e {
138 log.Printf("v1:%v v2:%v\n", "RECVFACT conversion error", e)
139 } else {
140 recvFact = float64(f)
143 return recvFact
146 // Get partial string, random length
147 func Partial() []byte {
148 r := int(ValueBetween(1, int64(mdml-1), 1.0))
149 return md[0:r]
152 // Print Byte Count
153 func Pbc() int {
154 if s := os.Getenv("STOMP_PBC"); s != "" {
155 i, e := strconv.ParseInt(s, 10, 32)
156 if nil != e {
157 log.Printf("v1:%v v2:%v\n", "PBC conversion error", e)
158 } else {
159 pbc = int(i)
162 return pbc
165 // Does receive wait to simulate message processing
166 func RecvWait() bool {
167 f := os.Getenv("STOMP_RECVWAIT")
168 if f == "" {
169 return true
171 return false
174 // Does send wait to simulate message building
175 func SendWait() bool {
176 f := os.Getenv("STOMP_SENDWAIT")
177 if f == "" {
178 return true
180 return false
183 // True if max procs are to be set
184 func SetMAXPROCS() bool {
185 f := os.Getenv("STOMP_SETMAXPROCS")
186 if f == "" {
187 return false
189 return true
192 // Use Custon Cipher List
193 func UseCustomCiphers() bool {
194 f := os.Getenv("STOMP_USECUSTOMCIPHERS")
195 if f == "" {
196 return useCustomCiphers
198 useCustomCiphers = true
199 return useCustomCiphers
202 // CustomCiphers()
203 func CustomCiphers() []uint16 {
204 if UseCustomCiphers() {
205 return cipherSuites
207 return []uint16{}