本文整理汇总了Golang中github.com/kildevaeld/projects/Godeps/_workspace/src/github.com/pebbe/zmq4.NewSocket函数的典型用法代码示例。如果您正苦于以下问题:Golang NewSocket函数的具体用法?Golang NewSocket怎么用?Golang NewSocket使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewSocket函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
lbbroker := &lbbroker_t{}
lbbroker.frontend, _ = zmq.NewSocket(zmq.ROUTER)
lbbroker.backend, _ = zmq.NewSocket(zmq.ROUTER)
defer lbbroker.frontend.Close()
defer lbbroker.backend.Close()
lbbroker.frontend.Bind("ipc://frontend.ipc")
lbbroker.backend.Bind("ipc://backend.ipc")
for client_nbr := 0; client_nbr < NBR_CLIENTS; client_nbr++ {
go client_task()
}
for worker_nbr := 0; worker_nbr < NBR_WORKERS; worker_nbr++ {
go worker_task()
}
// Queue of available workers
lbbroker.workers = make([]string, 0, 10)
// Prepare reactor and fire it up
lbbroker.reactor = zmq.NewReactor()
lbbroker.reactor.AddSocket(lbbroker.backend, zmq.POLLIN,
func(e zmq.State) error { return handle_backend(lbbroker) })
lbbroker.reactor.Run(-1)
}
示例2: main
func main() {
// Connect to task ventilator
receiver, _ := zmq.NewSocket(zmq.PULL)
defer receiver.Close()
receiver.Connect("tcp://localhost:5557")
// Connect to weather server
subscriber, _ := zmq.NewSocket(zmq.SUB)
defer subscriber.Close()
subscriber.Connect("tcp://localhost:5556")
subscriber.SetSubscribe("10001 ")
// Initialize poll set
poller := zmq.NewPoller()
poller.Add(receiver, zmq.POLLIN)
poller.Add(subscriber, zmq.POLLIN)
// Process messages from both sockets
for {
sockets, _ := poller.Poll(-1)
for _, socket := range sockets {
switch s := socket.Socket; s {
case receiver:
task, _ := s.Recv(0)
// Process task
fmt.Println("Got task:", task)
case subscriber:
update, _ := s.Recv(0)
// Process weather update
fmt.Println("Got weather update:", update)
}
}
}
}
示例3: main
func main() {
// Socket to receive messages on
receiver, _ := zmq.NewSocket(zmq.PULL)
defer receiver.Close()
receiver.Connect("tcp://localhost:5557")
// Socket to send messages to
sender, _ := zmq.NewSocket(zmq.PUSH)
defer sender.Close()
sender.Connect("tcp://localhost:5558")
// Process tasks forever
for {
s, _ := receiver.Recv(0)
// Simple progress indicator for the viewer
fmt.Print(s + ".")
// Do the work
msec, _ := strconv.Atoi(s)
time.Sleep(time.Duration(msec) * time.Millisecond)
// Send results to sink
sender.Send("", 0)
}
}
示例4: main
func main() {
// Prepare our context and sockets
publisher, _ := zmq.NewSocket(zmq.PUB)
publisher.Bind("tcp://*:5557")
sequence := int64(0)
rand.Seed(time.Now().UnixNano())
// Start state manager and wait for synchronization signal
updates, _ := zmq.NewSocket(zmq.PAIR)
updates.Bind("inproc://pipe")
go state_manager()
updates.RecvMessage(0) // "READY"
for {
// Distribute as key-value message
sequence++
kvmsg := kvsimple.NewKvmsg(sequence)
kvmsg.SetKey(fmt.Sprint(rand.Intn(10000)))
kvmsg.SetBody(fmt.Sprint(rand.Intn(1000000)))
if kvmsg.Send(publisher) != nil {
break
}
if kvmsg.Send(updates) != nil {
break
}
}
fmt.Printf("Interrupted\n%d messages out\n", sequence)
}
示例5: main
func main() {
// Start authentication engine
zmq.AuthSetVerbose(true)
zmq.AuthStart()
zmq.AuthAllow("domain1", "127.0.0.1")
// Tell the authenticator how to handle PLAIN requests
zmq.AuthPlainAdd("domain1", "admin", "secret")
// Create and bind server socket
server, _ := zmq.NewSocket(zmq.PUSH)
server.ServerAuthPlain("domain1")
server.Bind("tcp://*:9000")
// Create and connect client socket
client, _ := zmq.NewSocket(zmq.PULL)
client.SetPlainUsername("admin")
client.SetPlainPassword("secret")
client.Connect("tcp://127.0.0.1:9000")
// Send a single message from server to client
_, err := server.Send("Hello", 0)
checkErr(err)
message, err := client.Recv(0)
checkErr(err)
if message != "Hello" {
log.Fatalln(message, "!= Hello")
}
zmq.AuthStop()
fmt.Println("Woodhouse test OK")
}
示例6: main
func main() {
// Socket to receive messages on
receiver, _ := zmq.NewSocket(zmq.PULL)
defer receiver.Close()
receiver.Bind("tcp://*:5558")
// Socket for worker control
controller, _ := zmq.NewSocket(zmq.PUB)
defer controller.Close()
controller.Bind("tcp://*:5559")
// Wait for start of batch
receiver.Recv(0)
// Start our clock now
start_time := time.Now()
// Process 100 confirmations
for task_nbr := 0; task_nbr < 100; task_nbr++ {
receiver.Recv(0)
if task_nbr%10 == 0 {
fmt.Print(":")
} else {
fmt.Print(".")
}
}
fmt.Println("\nTotal elapsed time:", time.Since(start_time))
// Send kill signal to workers
controller.Send("KILL", 0)
// Finished
time.Sleep(time.Second) // Give 0MQ time to deliver
}
示例7: New
func New(primary bool, local, remote string) (bstar *Bstar, err error) {
bstar = &Bstar{}
// Initialize the Binary Star
bstar.Reactor = zmq.NewReactor()
if primary {
bstar.state = state_PRIMARY
} else {
bstar.state = state_BACKUP
}
// Create publisher for state going to peer
bstar.statepub, err = zmq.NewSocket(zmq.PUB)
bstar.statepub.Bind(local)
// Create subscriber for state coming from peer
bstar.statesub, err = zmq.NewSocket(zmq.SUB)
bstar.statesub.SetSubscribe("")
bstar.statesub.Connect(remote)
// Set-up basic reactor events
bstar.Reactor.AddChannelTime(time.Tick(bstar_HEARTBEAT), 1,
func(i interface{}) error { return bstar.send_state() })
bstar.Reactor.AddSocket(bstar.statesub, zmq.POLLIN,
func(e zmq.State) error { return bstar.recv_state() })
return
}
示例8: main
func main() {
srv := &clonesrv_t{
port: 5556,
kvmap: make(map[string]*kvmsg.Kvmsg),
}
// Set up our clone server sockets
srv.snapshot, _ = zmq.NewSocket(zmq.ROUTER)
srv.snapshot.Bind(fmt.Sprint("tcp://*:", srv.port))
srv.publisher, _ = zmq.NewSocket(zmq.PUB)
srv.publisher.Bind(fmt.Sprint("tcp://*:", srv.port+1))
srv.collector, _ = zmq.NewSocket(zmq.PULL)
srv.collector.Bind(fmt.Sprint("tcp://*:", srv.port+2))
// Register our handlers with reactor
reactor := zmq.NewReactor()
reactor.AddSocket(srv.snapshot, zmq.POLLIN,
func(e zmq.State) error { return snapshots(srv) })
reactor.AddSocket(srv.collector, zmq.POLLIN,
func(e zmq.State) error { return collector(srv) })
reactor.AddChannelTime(time.Tick(1000*time.Millisecond), 1,
func(v interface{}) error { return flush_ttl(srv) })
log.Println(reactor.Run(100 * time.Millisecond)) // precision: .1 seconds
}
示例9: main
func main() {
// Socket to send messages on
sender, _ := zmq.NewSocket(zmq.PUSH)
defer sender.Close()
sender.Bind("tcp://*:5557")
// Socket to send start of batch message on
sink, _ := zmq.NewSocket(zmq.PUSH)
defer sink.Close()
sink.Connect("tcp://localhost:5558")
fmt.Print("Press Enter when the workers are ready: ")
var line string
fmt.Scanln(&line)
fmt.Println("Sending tasks to workers...")
// The first message is "0" and signals start of batch
sink.Send("0", 0)
// Initialize random number generator
rand.Seed(time.Now().UnixNano())
// Send 100 tasks
total_msec := 0
for task_nbr := 0; task_nbr < 100; task_nbr++ {
// Random workload from 1 to 100msecs
workload := rand.Intn(100) + 1
total_msec += workload
s := fmt.Sprintf("%d", workload)
sender.Send(s, 0)
}
fmt.Println("Total expected cost:", time.Duration(total_msec)*time.Millisecond)
time.Sleep(time.Second) // Give 0MQ time to deliver
}
示例10: BrokerTask
func BrokerTask() {
// Prepare our sockets
frontend, _ := zmq.NewSocket(zmq.DEALER)
frontend.Bind("tcp://*:5555")
backend, _ := zmq.NewSocket(zmq.DEALER)
backend.Bind("tcp://*:5556")
zmq.Proxy(frontend, backend, nil)
}
示例11: main
func main() {
frontend, _ := zmq.NewSocket(zmq.SUB)
frontend.Bind("tcp://*:5557")
backend, _ := zmq.NewSocket(zmq.XPUB)
backend.Bind("tcp://*:5558")
// Subscribe to every single topic from publisher
frontend.SetSubscribe("")
// Store last instance of each topic in a cache
cache := make(map[string]string)
// We route topic updates from frontend to backend, and
// we handle subscriptions by sending whatever we cached,
// if anything:
poller := zmq.NewPoller()
poller.Add(frontend, zmq.POLLIN)
poller.Add(backend, zmq.POLLIN)
LOOP:
for {
polled, err := poller.Poll(1000 * time.Millisecond)
if err != nil {
break // Interrupted
}
for _, item := range polled {
switch socket := item.Socket; socket {
case frontend:
// Any new topic data we cache and then forward
msg, err := frontend.RecvMessage(0)
if err != nil {
break LOOP
}
cache[msg[0]] = msg[1]
backend.SendMessage(msg)
case backend:
// When we get a new subscription we pull data from the cache:
msg, err := backend.RecvMessage(0)
if err != nil {
break LOOP
}
frame := msg[0]
// Event is one byte 0=unsub or 1=sub, followed by topic
if frame[0] == 1 {
topic := frame[1:]
fmt.Println("Sending cached topic", topic)
previous, ok := cache[topic]
if ok {
backend.SendMessage(topic, previous)
}
}
}
}
}
}
示例12: TestKvmsg
func TestKvmsg(t *testing.T) {
// Prepare our context and sockets
output, err := zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Error(err)
}
err = output.Bind("ipc://kvmsg_selftest.ipc")
if err != nil {
t.Error(err)
}
input, err := zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Error(err)
}
err = input.Connect("ipc://kvmsg_selftest.ipc")
if err != nil {
t.Error(err)
}
kvmap := make(map[string]*Kvmsg)
// Test send and receive of simple message
kvmsg := NewKvmsg(1)
kvmsg.SetKey("key")
kvmsg.SetBody("body")
kvmsg.Dump()
err = kvmsg.Send(output)
kvmsg.Store(kvmap)
if err != nil {
t.Error(err)
}
kvmsg, err = RecvKvmsg(input)
if err != nil {
t.Error(err)
}
kvmsg.Dump()
key, err := kvmsg.GetKey()
if err != nil {
t.Error(err)
}
if key != "key" {
t.Error("Expected \"key\", got \"" + key + "\"")
}
kvmsg.Store(kvmap)
input.Close()
output.Close()
os.Remove("kvmsg_selftest.ipc")
}
示例13: Example_test_abstract_ipc
func Example_test_abstract_ipc() {
addr := "ipc://@/tmp/tester"
// This is Linux only
if runtime.GOOS != "linux" {
fmt.Printf("%q\n", addr)
fmt.Println("Done")
return
}
sb, err := zmq.NewSocket(zmq.PAIR)
if checkErr(err) {
return
}
err = sb.Bind(addr)
if checkErr(err) {
return
}
endpoint, err := sb.GetLastEndpoint()
if checkErr(err) {
return
}
fmt.Printf("%q\n", endpoint)
sc, err := zmq.NewSocket(zmq.PAIR)
if checkErr(err) {
return
}
err = sc.Connect(addr)
if checkErr(err) {
return
}
bounce(sb, sc, false)
err = sc.Close()
if checkErr(err) {
return
}
err = sb.Close()
if checkErr(err) {
return
}
fmt.Println("Done")
// Output:
// "ipc://@/tmp/tester"
// Done
}
示例14: agent_new
func agent_new() (agent *agent_t) {
agent = &agent_t{
servers: make(map[string]*server_t),
actives: make([]*server_t, 0),
request: make([]string, 0),
reply: make([]string, 0),
}
agent.pipe, _ = zmq.NewSocket(zmq.PAIR)
agent.pipe.Connect("inproc://pipe")
agent.router, _ = zmq.NewSocket(zmq.ROUTER)
return
}
示例15: main
func main() {
snapshot, _ := zmq.NewSocket(zmq.DEALER)
snapshot.Connect("tcp://localhost:5556")
subscriber, _ := zmq.NewSocket(zmq.SUB)
subscriber.SetRcvhwm(100000) // or messages between snapshot and next are lost
subscriber.SetSubscribe("")
subscriber.Connect("tcp://localhost:5557")
time.Sleep(time.Second) // or messages between snapshot and next are lost
kvmap := make(map[string]*kvsimple.Kvmsg)
// Get state snapshot
sequence := int64(0)
snapshot.SendMessage("ICANHAZ?")
for {
kvmsg, err := kvsimple.RecvKvmsg(snapshot)
if err != nil {
fmt.Println(err)
break // Interrupted
}
if key, _ := kvmsg.GetKey(); key == "KTHXBAI" {
sequence, _ = kvmsg.GetSequence()
fmt.Printf("Received snapshot=%d\n", sequence)
break // Done
}
kvmsg.Store(kvmap)
}
snapshot.Close()
first := true
// Now apply pending updates, discard out-of-sequence messages
for {
kvmsg, err := kvsimple.RecvKvmsg(subscriber)
if err != nil {
fmt.Println(err)
break // Interrupted
}
if seq, _ := kvmsg.GetSequence(); seq > sequence {
sequence, _ = kvmsg.GetSequence()
kvmsg.Store(kvmap)
if first {
// Show what the first regular update is after the snapshot,
// to see if we missed updates.
first = false
fmt.Println("Next:", sequence)
}
}
}
}