The sngecomm package now builds cleanly.
[stompngo_examples.git] / sngecomm / environment.go
blobc22913b8bac103d1c761018d9f2fe1a6072d9d9d
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"
28 "time"
30 // "github.com/gmallard/stompngo"
33 var (
35 nqs = 1 // Default number of queues for multi-queue demo(s)
36 mdml = 1024 * 32 // Message data max length of variable message, 32K
37 md = make([]byte, 1) // Additional message data, primed during init()
38 rc = 1 // Receiver connection count, srmgor_1smrconn
39 pbc = 64 // Number of bytes to print (used in some
40 // // examples that receive).
42 sendFact float64 = 1.0 // Send sleep time factor
43 recvFact float64 = 1.0 // Receive sleep time factor
45 conn2Buffer int = -1 // 2 connection buffer. < 0 means use queue size.
47 ackMode = "auto" // The default ack mode
49 pprof = false // Do not do profiling
52 // Initialization
53 func init() {
54 p := "_123456789ABCDEF"
55 c := mdml / len(p)
56 b := []byte(p)
57 md = bytes.Repeat(b, c) // A long string
61 // Number of queues
62 func Nqs() (rc int) {
64 if s := os.Getenv("STOMP_NQS"); s != "" {
65 i, e := strconv.ParseInt(s, 10, 32)
66 if nil != e {
67 log.Println("NQS conversion error", e)
68 } else {
69 nqs = int(i)
72 return nqs
75 // Receiver connection count
76 func Recvconns() (rc int) {
78 if s := os.Getenv("STOMP_RECVCONNS"); s != "" {
79 i, e := strconv.ParseInt(s, 10, 32)
80 if nil != e {
81 log.Println("RECVCONNS conversion error", e)
82 } else {
83 rc = int(i)
86 return rc
89 // Max Data Message Length
90 func Mdml() int {
91 if s := os.Getenv("STOMP_MDML"); s != "" {
92 i, e := strconv.ParseInt(s, 10, 32)
93 if nil != e {
94 log.Println("MDML conversion error", e)
95 } else {
96 mdml = int(i)
97 p := "_123456789ABCDEF"
98 c := mdml / len(p)
99 b := []byte(p)
100 md = bytes.Repeat(b, c) // A long string
103 return mdml
106 // Use profiling or not
107 func Pprof() bool {
108 if am := os.Getenv("STOMP_PPROF"); am != "" {
109 pprof = true
111 return pprof
114 // ACK mode for those examples that use it.
115 func AckMode() string {
116 if am := os.Getenv("STOMP_ACKMODE"); am != "" {
117 if am == "auto" || am == "client" || am == "client-individual" {
118 ackMode = am
119 } else {
120 log.Println("ACKMODE error", am)
123 return ackMode
126 // 2 Connection Buffer Size
127 func Conn2Buffer() int {
128 if s := os.Getenv("STOMP_CONN2BUFFER"); s != "" {
129 i, e := strconv.ParseInt(s, 10, 32)
130 if nil != e {
131 log.Println("CONN2BUFFER conversion error", e)
132 } else {
133 conn2Buffer = int(i)
136 return conn2Buffer
139 // Timestamp example ids
140 func ExampIdNow(s string) string {
141 return time.Now().String() + " " + s
144 // Get Send Sleep Factor
145 func SendFactor() float64 {
146 if s := os.Getenv("STOMP_SENDFACT"); s != "" {
147 f, e := strconv.ParseFloat(s, 64)
148 if nil != e {
149 log.Println("SENDFACT conversion error", e)
150 } else {
151 sendFact = float64(f)
154 return sendFact
157 // Get Recv Sleep Factor
158 func RecvFactor() float64 {
159 if s := os.Getenv("STOMP_RECVFACT"); s != "" {
160 f, e := strconv.ParseFloat(s, 64)
161 if nil != e {
162 log.Println("RECVFACT conversion error", e)
163 } else {
164 recvFact = float64(f)
167 return recvFact
170 // Get partial string, random length
171 func Partial() []byte {
172 r := int(ValueBetween(1, int64(mdml-1), 1.0))
173 return md[0:r]
176 // Print Byte Count
177 func Pbc() int {
178 if s := os.Getenv("STOMP_PBC"); s != "" {
179 i, e := strconv.ParseInt(s, 10, 32)
180 if nil != e {
181 log.Println("PBC conversion error", e)
182 } else {
183 pbc = int(i)
186 return pbc
189 // Does receive wait to simulate message processing
190 func RecvWait() bool {
191 f := os.Getenv("STOMP_NORECVW")
192 if f == "" {
193 return true
195 return false
198 // Does send wait to simulate message building
199 func SendWait() bool {
200 f := os.Getenv("STOMP_NOSENDW")
201 if f == "" {
202 return true
204 return false
207 // True if persistent messages are desired.
208 func Persistent() bool {
209 f := os.Getenv("STOMP_PERSISTENT")
210 if f == "" {
211 return false
213 return true
216 // True if max procs are to be set
217 func SetMAXPROCS() bool {
218 f := os.Getenv("STOMP_SETMAXPROCS")
219 if f == "" {
220 return false
222 return true