func main() { context, _ := zmq.NewContext() defer context.Close() sink, _ := context.Socket(zmq.Router) defer sink.Close() sink.Bind("inproc://example") // First allow 0MQ to set the identity anonymous, _ := context.Socket(zmq.Req) defer anonymous.Close() anonymous.Connect("inproc://example") anonymous.SendPart([]byte("ROUTER uses a generated UUID"), false) helpers.Dump(sink) // Then set the identity ourselves identified, _ := context.Socket(zmq.Req) defer identified.Close() identified.SetIdentitiy([]byte("PEER2")) identified.Connect("inproc://example") identified.SendPart([]byte("ROUTER socket uses REQ's socket identity"), false) helpers.Dump(sink) }
func workerTask() { context, _ := zmq.NewContext() defer context.Close() worker, _ := context.Socket(zmq.Dealer) defer worker.Close() // Set a printable identity helpers.SetId(worker) worker.Connect("tcp://localhost:5671") var total int for { // Tell the broker we're ready for work worker.SendPart([]byte(""), true) worker.SendPart([]byte("Hi Boss"), false) // Get workload from broker, until finished worker.RecvPart() // Envelope delimiter workload, _, _ := worker.RecvPart() finished := bytes.Equal(workload, []byte("Fired!")) if finished { fmt.Printf("Completed: %d tasks\n", total) } total++ // Do some random work rand.Seed(time.Now().UnixNano()) r := rand.Intn(500) time.Sleep(time.Duration(r) * time.Millisecond) } }
func main() { fmt.Printf("Connecting to hello world server...") context, _ := zmq.NewContext() defer context.Close() requester, _ := context.Socket(zmq.Req) defer requester.Close() requester.Connect("tcp://localhost:5555") for request := 0; request != 10; request++ { fmt.Printf("Sending request %d...\n", request) requester.SendPart([]byte("Hello"), false) reply, _, _ := requester.RecvPart() fmt.Printf("Received reply %d: [%s]\n", request, string(reply)) } }
func main() { // Socket to talk to clients context, _ := zmq.NewContext() defer context.Close() responder, _ := zmq.NewSocket(zmq.Rep) defer responder.Close() responder.Bind("tcp://*:5555") for { // Wait for next request from client request, _, _ := responder.RecvPart() fmt.Printf("Received request: [%s]\n", string(request)) // Do some 'work' time.Sleep(1 * time.Second) // Send reply back to client responder.SendPart([]byte("World"), false) } }
// While this example runs in a single process, that is only to make // it easier to start and stop the example. Each go routine has its own // context and conceptually acts as a separate process. func main() { context, _ := zmq.NewContext() defer context.Close() broker, _ := context.Socket(zmq.Router) defer broker.Close() broker.Bind("tcp://*:5671") for w := 0; w < nbrWorkers; w++ { go workerTask() } // Run for five seconds and then tell workers to end endTime := time.Now().Unix() + 5 workersFired := 0 for { // Next message gives us least recently used worker identity, _, _ := broker.RecvPart() broker.SendPart(identity, true) broker.RecvPart() // Envelope delimiter broker.RecvPart() // Response from worker broker.SendPart([]byte(""), true) // Encourage workers until it's time to fire them if time.Now().Unix() < endTime { broker.SendPart([]byte("Work harder"), false) } else { broker.SendPart([]byte("Fired!"), false) workersFired++ if workersFired == nbrWorkers { break } } } }