Fix merge conflict.
[stompngo_examples.git] / sngecomm / environment.go
blobf87038b9a32f25f2e9a121dc8137663603552922
1 //
2 // Copyright © 2016-2018 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"
28 "sync"
30 // "github.com/gmallard/stompngo"
33 var (
35 nqs = 1 // Default number of queues for multi-queue demo(s)
36 nqsLock sync.Mutex // nqs variable lock
37 mdml = 1024 * 32 // Message data max length of variable message, 32K
38 md = make([]byte, 1) // Additional message data, primed during init()
39 pbc = 64 // Number of bytes to print (used in some examples that receive).
41 ngors = 1 // Number of go routines to use (publish)
42 gorsleep = "" // If non-empty, go routines will sleep (publish)
45 sendFact = 1.0 // Send sleep time factor
46 recvFact = 1.0 // Receive sleep time factor
48 ackMode = "auto" // The default ack mode
50 pprof = false // Do not do profiling
52 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0xC0,0x2F
53 // TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 = 0xC0,0x2B
54 // TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 = 0xC0,0x30
55 cipherSuites = []uint16{
56 0xc02f,
57 0xc02b,
58 0xc030,
61 useCustomCiphers = false // Set Custom Cipher Suite list
63 memprof = "" // memory profile file
64 cpuprof = "" // cpu profile file
67 const (
68 // EOFMsg is the EOF message body
69 EOFMsg = "STOMP_EOF"
72 // Initialization
73 func init() {
74 p := "_123456789ABCDEF"
75 c := mdml / len(p)
76 b := []byte(p)
77 md = bytes.Repeat(b, c) // A long string
79 memprof = os.Getenv("STOMP_MEMPROF")
80 cpuprof = os.Getenv("STOMP_CPUPROF")
83 // Ngors sets the number of go routines
84 func Ngors() int {
86 if s := os.Getenv("STOMP_NGORS"); s != "" {
87 i, e := strconv.ParseInt(s, 10, 32)
88 if nil != e {
89 log.Printf("v1:%v v2:%v\n", "NGORS conversion error", e)
90 } else {
91 ngors = int(i)
94 return ngors
97 // Nqs sets the number of queues
98 func Nqs() int {
100 nqsLock.Lock()
101 defer nqsLock.Unlock()
102 if s := os.Getenv("STOMP_NQS"); s != "" {
103 i, e := strconv.ParseInt(s, 10, 32)
104 if nil != e {
105 log.Printf("v1:%v v2:%v\n", "NQS conversion error", e)
106 } else {
107 nqs = int(i)
110 return nqs
113 // Mdml sets the Max Data Message Length
114 func Mdml() int {
115 if s := os.Getenv("STOMP_MDML"); s != "" {
116 i, e := strconv.ParseInt(s, 10, 32)
117 if nil != e {
118 log.Printf("v1:%v v2:%v\n", "MDML conversion error", e)
119 } else {
120 mdml = int(i)
121 p := "_123456789ABCDEF"
122 c := mdml / len(p)
123 b := []byte(p)
124 md = bytes.Repeat(b, c) // A long string
127 return mdml
130 // Pprof indicates whether to use profiling or not
131 func Pprof() bool {
132 if am := os.Getenv("STOMP_PPROF"); am != "" {
133 pprof = true
135 return pprof
138 // Memprof returns the memory profile file name
139 func Memprof() string {
140 return memprof
143 // Cpuprof returns the CPU profile file name
144 func Cpuprof() string {
145 return cpuprof
148 // AckMode returns an ACK mode value for those examples that use it.
149 func AckMode() string {
150 if am := os.Getenv("STOMP_ACKMODE"); am != "" {
151 if am == "auto" || am == "client" || am == "client-individual" {
152 ackMode = am
153 } else {
154 log.Printf("v1:%v v2:%v\n", "ACKMODE error", am)
157 return ackMode
160 // SendFactor returns the send sleep factor
161 func SendFactor() float64 {
162 if s := os.Getenv("STOMP_SENDFACT"); s != "" {
163 f, e := strconv.ParseFloat(s, 64)
164 if nil != e {
165 log.Printf("v1:%v v2:%v\n", "SENDFACT conversion error", e)
166 } else {
167 sendFact = float64(f)
170 return sendFact
173 // RecvFactor returns the recv sleep factor
174 func RecvFactor() float64 {
175 if s := os.Getenv("STOMP_RECVFACT"); s != "" {
176 f, e := strconv.ParseFloat(s, 64)
177 if nil != e {
178 log.Printf("v1:%v v2:%v\n", "RECVFACT conversion error", e)
179 } else {
180 recvFact = float64(f)
183 return recvFact
186 // Partial returns the partial byte slice for logging, random length
187 func Partial() []byte {
188 r := int(ValueBetween(1, int64(mdml-1), 1.0))
189 return md[0:r]
192 // PartialSubstr returns the partial string for logging, fixed length
193 func PartialSubstr(l int) []byte {
194 return md[0:l]
197 // Pbc returns the byte count to log
198 func Pbc() int {
199 if s := os.Getenv("STOMP_PBC"); s != "" {
200 i, e := strconv.ParseInt(s, 10, 32)
201 if nil != e {
202 log.Printf("v1:%v v2:%v\n", "PBC conversion error", e)
203 } else {
204 pbc = int(i)
207 return pbc
210 // Gorsleep returns an indication of whether go routines will sleep or not
211 func Gorsleep() string {
212 gorsleep = os.Getenv("STOMP_GORSLEEP")
213 return gorsleep
216 // RecvWait indicates whether to wait in receives to simulate message processing
217 func RecvWait() bool {
218 f := os.Getenv("STOMP_RECVWAIT")
219 if f == "" {
220 return true
222 return false
225 // SendWait indicates whether to wait in sends to simulate message processing
226 func SendWait() bool {
227 f := os.Getenv("STOMP_SENDWAIT")
228 if f == "" {
229 return true
231 return false
234 // SetMAXPROCS returns true if max procs are to be set
235 func SetMAXPROCS() bool {
236 f := os.Getenv("STOMP_SETMAXPROCS")
237 if f == "" {
238 return false
240 return true
243 // UseCustomCiphers returns true if custom ciphers are to be used
244 func UseCustomCiphers() bool {
245 f := os.Getenv("STOMP_USECUSTOMCIPHERS")
246 if f == "" {
247 return useCustomCiphers
249 useCustomCiphers = true
250 return useCustomCiphers
253 // CustomCiphers returns a slice of custom ciphers
254 func CustomCiphers() []uint16 {
255 if UseCustomCiphers() {
256 return cipherSuites
258 return []uint16{}
261 // Logger returns an indication of whether to do logging
262 func Logger() string {
263 return os.Getenv("STOMP_LOGGER")
266 // UseEOF returns true if en EOF message is to be used.
267 func UseEOF() bool {
268 if os.Getenv("STOMP_USEEOF") != "" {
269 return true
271 return false