Demo some examples in *nix.
[stompngo_examples.git] / sngecomm / environment.go
blob85cc54b48b84425d43daafe0d7d9d910dc255706
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 examples that receive).
39 ngors = 1 // Number of go routines to use (publish)
40 gorsleep = "" // If non-empty, go routines will sleep (publish)
43 sendFact float64 = 1.0 // Send sleep time factor
44 recvFact float64 = 1.0 // Receive sleep time factor
46 ackMode = "auto" // The default ack mode
48 pprof = false // Do not do profiling
50 // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0xC0,0x2F
51 // TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 = 0xC0,0x2B
52 // TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 = 0xC0,0x30
53 cipherSuites = []uint16{
54 0xc02f,
55 0xc02b,
56 0xc030,
59 useCustomCiphers = false // Set Custom Cipher Suite list
61 memprof = "" // memory profile file
62 cpuprof = "" // cpu profile file
65 const (
66 EOF_MSG = "STOMP_EOF"
69 // Initialization
70 func init() {
71 p := "_123456789ABCDEF"
72 c := mdml / len(p)
73 b := []byte(p)
74 md = bytes.Repeat(b, c) // A long string
76 memprof = os.Getenv("STOMP_MEMPROF")
77 cpuprof = os.Getenv("STOMP_CPUPROF")
80 // Number of go routines
81 func Ngors() int {
83 if s := os.Getenv("STOMP_NGORS"); s != "" {
84 i, e := strconv.ParseInt(s, 10, 32)
85 if nil != e {
86 log.Printf("v1:%v v2:%v\n", "NGORS conversion error", e)
87 } else {
88 ngors = int(i)
91 return ngors
94 // Number of queues
95 func Nqs() int {
97 if s := os.Getenv("STOMP_NQS"); s != "" {
98 i, e := strconv.ParseInt(s, 10, 32)
99 if nil != e {
100 log.Printf("v1:%v v2:%v\n", "NQS conversion error", e)
101 } else {
102 nqs = int(i)
105 return nqs
108 // Max Data Message Length
109 func Mdml() int {
110 if s := os.Getenv("STOMP_MDML"); s != "" {
111 i, e := strconv.ParseInt(s, 10, 32)
112 if nil != e {
113 log.Printf("v1:%v v2:%v\n", "MDML conversion error", e)
114 } else {
115 mdml = int(i)
116 p := "_123456789ABCDEF"
117 c := mdml / len(p)
118 b := []byte(p)
119 md = bytes.Repeat(b, c) // A long string
122 return mdml
125 // Use profiling or not
126 func Pprof() bool {
127 if am := os.Getenv("STOMP_PPROF"); am != "" {
128 pprof = true
130 return pprof
133 // Memory profile file
134 func Memprof() string {
135 return memprof
138 // Cpu profile file
139 func Cpuprof() string {
140 return cpuprof
143 // ACK mode for those examples that use it.
144 func AckMode() string {
145 if am := os.Getenv("STOMP_ACKMODE"); am != "" {
146 if am == "auto" || am == "client" || am == "client-individual" {
147 ackMode = am
148 } else {
149 log.Printf("v1:%v v2:%v\n", "ACKMODE error", am)
152 return ackMode
155 // Get Send Sleep Factor
156 func SendFactor() float64 {
157 if s := os.Getenv("STOMP_SENDFACT"); s != "" {
158 f, e := strconv.ParseFloat(s, 64)
159 if nil != e {
160 log.Printf("v1:%v v2:%v\n", "SENDFACT conversion error", e)
161 } else {
162 sendFact = float64(f)
165 return sendFact
168 // Get Recv Sleep Factor
169 func RecvFactor() float64 {
170 if s := os.Getenv("STOMP_RECVFACT"); s != "" {
171 f, e := strconv.ParseFloat(s, 64)
172 if nil != e {
173 log.Printf("v1:%v v2:%v\n", "RECVFACT conversion error", e)
174 } else {
175 recvFact = float64(f)
178 return recvFact
181 // Get partial string, random length
182 func Partial() []byte {
183 r := int(ValueBetween(1, int64(mdml-1), 1.0))
184 return md[0:r]
187 // Get partial string, fixed length
188 func PartialSubstr(l int) []byte {
189 return md[0:l]
192 // Print Byte Count
193 func Pbc() int {
194 if s := os.Getenv("STOMP_PBC"); s != "" {
195 i, e := strconv.ParseInt(s, 10, 32)
196 if nil != e {
197 log.Printf("v1:%v v2:%v\n", "PBC conversion error", e)
198 } else {
199 pbc = int(i)
202 return pbc
205 // Whether go routines will sleep or not
206 func Gorsleep() string {
207 gorsleep = os.Getenv("STOMP_GORSLEEP")
208 return gorsleep
211 // Does receive wait to simulate message processing
212 func RecvWait() bool {
213 f := os.Getenv("STOMP_RECVWAIT")
214 if f == "" {
215 return true
217 return false
220 // Does send wait to simulate message building
221 func SendWait() bool {
222 f := os.Getenv("STOMP_SENDWAIT")
223 if f == "" {
224 return true
226 return false
229 // True if max procs are to be set
230 func SetMAXPROCS() bool {
231 f := os.Getenv("STOMP_SETMAXPROCS")
232 if f == "" {
233 return false
235 return true
238 // Use Custon Cipher List
239 func UseCustomCiphers() bool {
240 f := os.Getenv("STOMP_USECUSTOMCIPHERS")
241 if f == "" {
242 return useCustomCiphers
244 useCustomCiphers = true
245 return useCustomCiphers
248 // CustomCiphers()
249 func CustomCiphers() []uint16 {
250 if UseCustomCiphers() {
251 return cipherSuites
253 return []uint16{}
256 // Connection logger
257 func Logger() string {
258 return os.Getenv("STOMP_LOGGER")
261 // Use special EOF message
262 func UseEOF() bool {
263 if os.Getenv("STOMP_USEEOF") != "" {
264 return true
266 return false