// Test multiple go routines - SEND func BenchmarkMultipleGoRoutinesSend() { // SEND Phase // create a net.Conn, and pass that into Connect p := os.Getenv("STOMP_PORT") if p == "" { p = "61613" } nc, error := net.Dial("tcp", hap+p) if error != nil { log.Fatal(error) } // Connect ch := stompngo.Headers{"login", "guest", "passcode", "guest"} c, error := stompngo.Connect(nc, ch) if error != nil { log.Fatal(error) } for i := 1; i <= nq; i++ { qn := fmt.Sprintf("%d", i) wgsend.Add(1) // All sending go routines share the same connection in this example go sendMessages(c, qname+qn, nmsgs, i) } wgsend.Wait() // Disconnect nh := stompngo.Headers{} error = c.Disconnect(nh) if error != nil { log.Fatal(error) } log.Println("Done with SENDs ....") wgboth.Done() }
// Test multiple go routines - RECEIVE func BenchmarkMultipleGoRoutinesRecv() { // RECEIVE Phase p := os.Getenv("STOMP_PORT") if p == "" { p = "61613" } nc2, error := net.Dial("tcp", hap+p) if error != nil { // Handle error properly log.Fatal(error) } // Connect ch := stompngo.Headers{"login", "guest", "passcode", "guest"} c, error := stompngo.Connect(nc2, ch) if error != nil { log.Fatal(error) } for i := 1; i <= nq; i++ { qn := fmt.Sprintf("%d", i) wgrecv.Add(1) // All receiving go routines share the same connection in this example go recMessages(c, qname+qn, i) } wgrecv.Wait() // Disconnect nh := stompngo.Headers{} error = c.Disconnect(nh) if error != nil { log.Fatal(error) } wgboth.Done() }
// Test multiple go routines - SEND func BenchmarkMultipleGoRoutinesSend() { // SEND Phase // create a net.Conn, and pass that into Connect nc, error := net.Dial("tcp", handp) if error != nil { log.Fatal(error) } // Connect ch := stompngo.Headers{"login", "guest", "passcode", "guest"} c, error := stompngo.Connect(nc, ch) if error != nil { log.Fatal(error) } for i := 1; i <= nq; i++ { qn := fmt.Sprintf("%d", i) wgsend.Add(1) go sendMessages(c, qname+qn, nmsgs, i) } wgsend.Wait() // Disconnect nh := stompngo.Headers{} error = c.Disconnect(nh) if error != nil { log.Fatal(error) } log.Println("Done with SENDs ....") wgboth.Done() }
// Test multiple go routines - RECEIVE func BenchmarkMultipleGoRoutinesRecv() { // RECEIVE Phase nc2, error := net.Dial("tcp", handp) if error != nil { // Handle error properly log.Fatal(error) } // Connect ch := stompngo.Headers{"login", "guest", "passcode", "guest"} c, error := stompngo.Connect(nc2, ch) if error != nil { log.Fatal(error) } for i := 1; i <= nq; i++ { qn := fmt.Sprintf("%d", i) wgrecv.Add(1) go recMessages(c, qname+qn, i) } wgrecv.Wait() // Disconnect nh := stompngo.Headers{} error = c.Disconnect(nh) if error != nil { log.Fatal(error) } wgboth.Done() }
func main() { n, e := net.Dial("tcp", net.JoinHostPort(h, p)) if e != nil { log.Fatalln(e) } ch := stompngo.Headers{"accept-version", "1.2", "host", "localhost", "login", "gooduser", "passcode", "guest"} conn, e := stompngo.Connect(n, ch) if e != nil { log.Fatalln(e) } s := stompngo.Headers{"destination", dest} m := " message: " for i := 1; i <= 100; i++ { t := m + fmt.Sprintf("%d", i) e := conn.Send(s, t) if e != nil { log.Fatalln(e) } fmt.Println("send complete:", t) } e = conn.Disconnect(stompngo.Headers{}) if e != nil { log.Fatalln(e) } e = n.Close() if e != nil { log.Fatalln(e) } }
func main() { fmt.Println("Start...") p := os.Getenv("STOMP_PORT") if p == "" { p = "61613" } nc, error := net.Dial("tcp", hap+p) if error != nil { log.Fatal(error) } // Connect ch := stompngo.Headers{"login", "getter", "passcode", "recv1234"} c, error := stompngo.Connect(nc, ch) if error != nil { log.Fatal(error) } for i := 1; i <= mq; i++ { qn := fmt.Sprintf("%d", i) wg.Add(1) go recMessages(c, qname+qn) } wg.Wait() select { case v := <-c.MessageData: fmt.Printf("frame1: %v\n", v) default: fmt.Println("Nothing to show") } fmt.Println("unsubs: starts") for i := 1; i <= mq; i++ { qn := fmt.Sprintf("%d", i) h := stompngo.Headers{"destination", qname + qn} fmt.Printf("unsubhdr: %v\n", h) error = c.Unsubscribe(h) if error != nil { log.Fatal(error) } } fmt.Printf("Num received: %d\n", numRecv) // Disconnect nh := stompngo.Headers{} error = c.Disconnect(nh) if error != nil { log.Fatal(error) } fmt.Println("done nc.Close()") nc.Close() ngor := runtime.NumGoroutine() fmt.Printf("egor: %v\n", ngor) select { case v := <-c.MessageData: fmt.Printf("frame2: %v\n", v) default: fmt.Println("Nothing to show") } fmt.Println("End...") }
func main() { n, e := net.Dial("tcp", net.JoinHostPort(h, p)) if e != nil { log.Fatalln(e) } ch := stompngo.Headers{"accept-version", "1.2", "host", "localhost", "login", "gooduser", "passcode", "guest"} conn, e := stompngo.Connect(n, ch) if e != nil { log.Fatalln(e) } u := stompngo.Uuid() s := stompngo.Headers{"destination", dest, "id", u} r, e := conn.Subscribe(s) if e != nil { log.Fatalln(e) // Handle this ... } // Read data from the returned channel for i := 1; i <= 10000; i++ { m := <-r if m.Error != nil { log.Fatalln(m.Error) } // fmt.Printf("Frame Type: %s\n", m.Message.Command) if m.Message.Command != stompngo.MESSAGE { log.Fatalln(m) } h := m.Message.Headers for j := 0; j < len(h)-1; j += 2 { fmt.Printf("Header: %s:%s\n", h[j], h[j+1]) } fmt.Printf("Payload: %s\n", string(m.Message.Body)) if h.Value("subscription") != u { fmt.Printf("Error condition, expected [%s], received [%s]\n", u, h.Value("subscription")) log.Fatalln("Bad subscription header") } } e = conn.Unsubscribe(s) if e != nil { log.Fatalln(e) } e = conn.Disconnect(stompngo.Headers{}) if e != nil { log.Fatalln(e) } e = n.Close() if e != nil { log.Fatalln(e) } }
// Common example connect logic func CommonConnect(exampid, tag string, l *log.Logger) (net.Conn, *stompngo.Connection, error) { l.Printf("%stag:%s consess:%v common_connect_starts\n", exampid, tag, Lcs) // Set up the connection. h, p := senv.HostAndPort() hap := net.JoinHostPort(h, p) n, e := net.Dial("tcp", hap) if e != nil { return nil, nil, e } l.Printf("%stag:%s connsess:%s common_connect_host_and_port:%v\n", exampid, tag, Lcs, hap) // Create connect headers and connect to stompngo ch := ConnectHeaders() l.Printf("%stag:%s connsess:%s common_connect_headers headers:%v\n", exampid, tag, Lcs, ch) conn, e := stompngo.Connect(n, ch) if e != nil { return nil, conn, e } l.Printf("%stag:%s connsess:%s common_connect_complete host:%s port:%s vhost:%s protocol:%s server:%s\n", exampid, tag, conn.Session(), h, p, senv.Vhost(), conn.Protocol(), ServerIdent(conn)) // Show connect response l.Printf("%stag:%s connsess:%s common_connect_response connresp:%v\n", exampid, tag, conn.Session(), conn.ConnectResponse) // Heartbeat Data l.Printf("%stag:%s connsess:%s common_connect_heart_beat_send hbsend:%d\n", exampid, tag, conn.Session(), conn.SendTickerInterval()) l.Printf("%stag:%s connsess:%s common_connect_heart_beat_recv hbrecv:%d\n", exampid, tag, conn.Session(), conn.ReceiveTickerInterval()) l.Printf("%stag:%s connsess:%s common_connect_local_addr:%s\n", exampid, tag, conn.Session(), n.LocalAddr().String()) l.Printf("%stag:%s connsess:%s common_connect_remote_addr:%s\n", exampid, tag, conn.Session(), n.RemoteAddr().String()) // return n, conn, nil }
func main() { fmt.Println("Start...") // nc, error := net.Dial("tcp", hap+os.Getenv("STOMP_PORT")) if error != nil { log.Fatal(error) } // Connectionect ch := stompngo.Headers{"login", "putter", "passcode", "send1234", "accept-version", "1.1", "host", host} c, error := stompngo.Connect(nc, ch) if error != nil { log.Fatal(error) } for i := 1; i <= mq; i++ { qn := fmt.Sprintf("%d", i) wg.Add(1) go sendMessages(c, qname+qn, nmsgs, i) } wg.Wait() fmt.Println("done with wait") // Disconnect nh := stompngo.Headers{} error = c.Disconnect(nh) if error != nil { log.Fatal(error) } fmt.Println("done disconnect, start nc.Close()") nc.Close() fmt.Println("done nc.Close()") ngor := runtime.NumGoroutine() fmt.Printf("egor: %v\n", ngor) select { case v := <-c.MessageData: fmt.Printf("frame2: %v\n", v) default: fmt.Println("Nothing to show") } fmt.Println("End... mq:", mq, " nmsgs:", nmsgs) }
func main() { fmt.Println("Receiver Start...") // create a net.Conn, and pass that into Connect p := os.Getenv("STOMP_PORT") if p == "" { p = "61613" } nc, error := net.Dial("tcp", hap+p) if error != nil { log.Fatal(error) } // Connect ch := stompngo.Headers{"login", "getter", "passcode", "recv1234"} ch = ch.Add("accept-version", "1.0") // 1.0 only ch = ch.Add("host", host) c, error := stompngo.Connect(nc, ch) if error != nil { log.Fatal(error) } for i := 1; i <= mq; i++ { qn := fmt.Sprintf("%d", i) wg.Add(1) go recMessages(c, qname+qn) } wg.Wait() fmt.Println("Receiver done with wait") // Disconnect nh := stompngo.Headers{} error = c.Disconnect(nh) if error != nil { log.Fatalf("Receiver discerr %v\n", error) } // Sanity check for spurious errors select { case v := <-c.MessageData: fmt.Printf("Receiver frame2: %s\n", v.Message.Command) fmt.Printf("Receiver header2: %v\n", v.Message.Headers) fmt.Printf("Receiver data2: %s\n", string(v.Message.Body)) default: fmt.Println("Receiver Nothing to show") } // Network close nc.Close() fmt.Println("Receiver done nc.Close()") ngor := runtime.NumGoroutine() fmt.Printf("Receiver ngor: %v\n", ngor) fmt.Println("Receiver End... numq:", mq, "Num received:", numRecv) }
func main() { fmt.Println("Sender Start...") // p := os.Getenv("STOMP_PORT") if p == "" { p = "61613" } nc, error := net.Dial("tcp", hap+p) if error != nil { // Handle error properly } // Connect ch := stompngo.Headers{"login", "putter", "passcode", "send1234", "accept-version", "1.1", "host", host} c, error := stompngo.Connect(nc, ch) if error != nil { log.Fatal(error) } for i := 1; i <= mq; i++ { qn := fmt.Sprintf("%d", i) wg.Add(1) go sendMessages(c, qname+qn, nmsgs, i) } wg.Wait() fmt.Println("Sender done with wait") // Disconnect nh := stompngo.Headers{} error = c.Disconnect(nh) if error != nil { log.Fatalf("Sender discerr %v\n", error) } // Sanity check for spurious errors. select { case v := <-c.MessageData: fmt.Printf("frame2: %v\n", v) default: fmt.Println("Sender Nothing to show") } fmt.Println("Sender done disconnect, start nc.Close()") // Network close nc.Close() fmt.Println("Sender done nc.Close()") ngor := runtime.NumGoroutine() fmt.Printf("Sender ngor: %v\n", ngor) fmt.Println("Sender End... numq:", mq, "Num sent:", numSend) }
func Handle(handler func(xml []byte)) { n, err := net.Dial("tcp", net.JoinHostPort(JmsHost, JmsPort)) CheckErr(err) ch := stompngo.Headers{"accept-version", "1.1", "host", JmsVHost} conn, err := stompngo.Connect(n, ch) CheckErr(err) s := stompngo.Headers{"destination", JmsDest, "id", JmsId} r, e := conn.Subscribe(s) if e != nil { log.Fatalln(e) } func() { for { select { case m := <-r: if m.Error != nil { log.Fatalln(m.Error) } if m.Message.Command != stompngo.MESSAGE { log.Fatalln(m) } err := hand(handler, m.Message.Body) if err != nil { log.Fatalln(err) continue } case <-time.After(600 * time.Second): println("timeout") return } } }() err = conn.Unsubscribe(s) CheckErr(err) err = conn.Disconnect(stompngo.Headers{}) CheckErr(err) err = n.Close() CheckErr(err) }
func main() { fmt.Println("Start...") // create a net.Conn, and pass that into Connect nc, error := net.Dial("tcp", hap+os.Getenv("STOMP_PORT")) if error != nil { log.Fatal(error) } // Connect ch := stompngo.Headers{"login", "getter", "passcode", "recv1234"} ch = ch.Add("accept-version", "1.1") ch = ch.Add("host", host) c, error := stompngo.Connect(nc, ch) if error != nil { log.Fatal(error) } for i := 1; i <= mq; i++ { qn := fmt.Sprintf("%d", i) wg.Add(1) go recMessages(c, qname+qn) } wg.Wait() fmt.Printf("Num received: %d\n", numRecv) // Disconnect nh := stompngo.Headers{} error = c.Disconnect(nh) if error != nil { log.Fatalf("discerr %v\n", error) } fmt.Println("done nc.Close()") nc.Close() ngor := runtime.NumGoroutine() fmt.Printf("egor: %v\n", ngor) select { case v := <-c.MessageData: fmt.Printf("frame2: %s\n", v.Message.Command) fmt.Printf("header2: %v\n", v.Message.Headers) fmt.Printf("data2: %s\n", string(v.Message.Body)) default: fmt.Println("Nothing to show") } fmt.Println("End... mq:", mq) }
func main() { // create a net.Conn, and pass that into Connect nc, error := net.Dial("tcp", hap+os.Getenv("STOMP_PORT")) if error != nil { // Handle error properly } // Connect ch := stompngo.Headers{"login", "guest", "passcode", "guest", "accept-version", "1.1", "host", host} c, error := stompngo.Connect(nc, ch) if error != nil { log.Fatal(error) } sh := stompngo.Headers{"destination", qname} for i := 1; i <= nmsgs; i++ { msg := "subrecv message " + fmt.Sprintf("%d", i) error = c.Send(sh, msg) if error != nil { log.Fatal(error) } fmt.Printf("Sent message: %s\n", msg) } sc, error := c.Subscribe(sh) if error != nil { log.Fatal(error) } subid := "" i := 1 for { // Sanity check. Any unanticipated ERROR frames? select { case v := <-c.MessageData: log.Fatalf("frame1: %v\n", v) default: fmt.Println("Nothing to show - 1") } fmt.Println("Start receive ....") d := <-sc // fmt.Printf("d: %v\n", d) if d.Error != nil { log.Fatal(d.Error) } if i == 1 { subid = d.Message.Headers.Value("subscription") fmt.Printf("Subscription is: %s\n", subid) } // fmt.Printf("Received message: %s\n", d.Message.BodyString()) i++ if i > nmsgs { break } } uh := stompngo.Headers{"destination", qname, "id", subid} error = c.Unsubscribe(uh) if error != nil { log.Fatal(error) } // Disconnect nh := stompngo.Headers{} error = c.Disconnect(nh) if error != nil { log.Fatal(error) } // Sanity check. Any unanticipated ERROR frames? select { case v := <-c.MessageData: log.Fatalf("frame2: %v\n", v) default: fmt.Println("Nothing to show - 2") } fmt.Println("done disconnect, start nc.Close()") nc.Close() fmt.Println("done nc.Close()") }
err := m.conn.Disconnect(stompngo.Headers{}) if err != nil { return err } m.conn = nil } if m.netConn != nil { m.netConn.Close() } return nil } var getStompConn = func(n net.Conn, h stompngo.Headers) (connectioner, error) { return stompngo.Connect(n, h) } func (m *Mediator) Read() (stompngo.MessageData, error) { m.mu.Lock() defer m.mu.Unlock() if m.conn.Connected() { return <-m.sub, nil } return stompngo.MessageData{}, ErrNotConnected } func (m *Mediator) ReadAsync() <-chan stompngo.MessageData { out := make(chan stompngo.MessageData, 1)
func main() { // create a net.Conn, and pass that into Connect p := os.Getenv("STOMP_PORT") if p == "" { p = "61613" } nc, error := net.Dial("tcp", hap+p) if error != nil { // Handle error properly } // Connect ch := stompngo.Headers{"login", "guest", "passcode", "guest", "accept-version", "1.1", "host", host} c, error := stompngo.Connect(nc, ch) if error != nil { log.Fatal(error) } sh := stompngo.Headers{"destination", qname} for i := 1; i <= nmsgs; i++ { msg := "subrecv message " + fmt.Sprintf("%d", i) error = c.Send(sh, msg) if error != nil { log.Fatal(error) } fmt.Printf("Sent message: %s\n", msg) } // No 'id' header is present -> the stompngo client library creates one. // The assigned subscription id must be determined from subsequent traffic. sc, error := c.Subscribe(sh) if error != nil { log.Fatal(error) } subid := "" i := 1 for { // Sanity check. Any unanticipated ERROR frames? select { case v := <-c.MessageData: log.Fatalf("frame1: %v\n", v) default: fmt.Println("Nothing to show - 1") } fmt.Println("Start receive ....") d := <-sc // fmt.Printf("d: %v\n", d) if d.Error != nil { log.Fatal(d.Error) } // Save the subscription id in use for the eventual UNSUBSCRIBE. if i == 1 { subid = d.Message.Headers.Value("subscription") fmt.Printf("Subscription is: %s\n", subid) } // fmt.Printf("Received message: %s\n", d.Message.BodyString()) i++ if i > nmsgs { break } } uh := stompngo.Headers{"destination", qname, "id", subid} // Unsubscribe headers error = c.Unsubscribe(uh) if error != nil { log.Fatal(error) } // Disconnect nh := stompngo.Headers{} error = c.Disconnect(nh) if error != nil { log.Fatal(error) } // Sanity check. Any unanticipated ERROR frames? select { case v := <-c.MessageData: log.Fatalf("frame2: %v\n", v) default: fmt.Println("Nothing to show - 2") } fmt.Println("done disconnect, start nc.Close()") nc.Close() fmt.Println("done nc.Close()") }
// Common example TLS connect logic func CommonTLSConnect(exampid, tag string, l *log.Logger, c *tls.Config) (net.Conn, *stompngo.Connection, error) { l.Printf("%stag:%s consess:%s common_tls_connect_starts\n", exampid, tag, Lcs) // Set up the connection. h, p := senv.HostAndPort() hap := net.JoinHostPort(h, p) n, e := net.Dial("tcp", hap) if e != nil { return nil, nil, e } c.ServerName = h // SNI nc := tls.Client(n, c) // Returns: *tls.Conn : implements net.Conn e = nc.Handshake() if e != nil { if e.Error() == "EOF" { l.Printf("%stag:%s consess:%s common_tls_handshake_EOF_Is_the_broker_port_TLS_enabled? port:%s\n", exampid, tag, Lcs, p) } l.Fatalf("%stag:%s consess:%s common_tls_handshake_failed error:%v\n", exampid, tag, Lcs, e.Error()) } l.Printf("%stag:%s consess:%s common_tls_handshake_complete\n", exampid, tag, Lcs) l.Printf("%stag:%s connsess:%s common_tls_connect_host_and_port:%v\n", exampid, tag, Lcs, hap) // Create connect headers and connect to stompngo ch := ConnectHeaders() l.Printf("%stag:%s connsess:%s common_tls_connect_headers headers:%v\n", exampid, tag, Lcs, ch) conn, e := stompngo.Connect(nc, ch) if e != nil { return nil, nil, e } l.Printf("%stag:%s connsess:%s common_tls_connect_complete host:%s vhost:%s protocol:%s server:%s\n", exampid, tag, conn.Session(), h, senv.Vhost(), conn.Protocol(), ServerIdent(conn)) // Show connect response l.Printf("%stag:%s connsess:%s common_tls_connect_response connresp:%v\n", exampid, tag, conn.Session(), conn.ConnectResponse) // Show heartbeat data (if heart beats are being used) if senv.Heartbeats() != "" { l.Printf("%stag:%s connsess:%s common_tls_connect_heart_beat_send hbsend:%v\n", exampid, tag, conn.Session(), conn.SendTickerInterval()) l.Printf("%stag:%s connsess:%s common_tls_connect_heart_beat_recv hbrecv:%v\n", exampid, tag, conn.Session(), conn.ReceiveTickerInterval()) } l.Printf("%stag:%s connsess:%s common_tls_connect_local_addr:%s\n", exampid, tag, conn.Session(), n.LocalAddr().String()) l.Printf("%stag:%s connsess:%s common_tls_connect_remote_addr:%s\n", exampid, tag, conn.Session(), n.RemoteAddr().String()) // return nc, conn, nil }