コード例 #1
0
ファイル: private.go プロジェクト: xeb/backq
// Subscribe will automatically wire up processing of messages for a request address
func Subscribe(reqaddy string, repaddy string) {
	reqsock, _ = zmq.NewSocket(zmq.SUB)
	defer reqsock.Close()
	reqsock.Connect(reqaddy)
	reqsock.SetSubscribe("")

	repsock, _ = zmq.NewSocket(zmq.REQ)
	repsock.Connect(repaddy)
	defer repsock.Close()

	fmt.Println("[PRIVATE] Connected to PUBLIC")

	for subscribed {
		fmt.Println("[PRIVATE] Receiving")
		r, _ := reqsock.Recv(0)
		fmt.Printf("[PRIVATE] Received %d\n", len(r))

		fmt.Printf("[PRIVATE] Executing inner HTTP request\n")

		req, e := messages.NewRequest(r)

		if e != nil {
			fmt.Printf("Terrible error trying to recreate request")
			panic(e)
		}
		reply, body, e := MakeRequest(req)
		SendReply(reply, body, repsock)
	}

	fmt.Println("[PRIVATE] Unsubscribing")
}
コード例 #2
0
ファイル: zmqPubSubProxy.go プロジェクト: yuhuichen/gocode
func main() {

	go listener_thread()
	time.Sleep(time.Millisecond * 5)

	//  This is where the weather server sits
	frontend, _ := zmq.NewSocket(zmq.XSUB)
	defer frontend.Close()
	frontend.Bind(proxy_frontend_url_port)

	//  This is our public endpoint for subscribers
	backend, _ := zmq.NewSocket(zmq.XPUB)
	defer backend.Close()
	backend.Bind(proxy_backend_url_port)

	listener, _ := zmq.NewSocket(zmq.PAIR)
	listener.Connect("inproc://pipe")

	log.Println("0MQ proxy started!")
	log.Println("Frontend protocl/url/port:", proxy_frontend_url_port)
	log.Println("Backend protocol/url/port:", proxy_backend_url_port)

	//  Run the proxy until the user interrupts us
	err := zmq.Proxy(frontend, backend, listener)
	log.Fatalln("Proxy interrupted:", err)
}
コード例 #3
0
ファイル: clonesrv5.go プロジェクト: gallir/zmq4
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
}
コード例 #4
0
ファイル: main.go プロジェクト: FoxelBox/ChatLinkRouter
func main() {
	file := new(ZMQConfigFile)

	fileReader, err := os.Open("config.json")
	if err != nil {
		log.Panicf("Error: %v", err)
	}
	jsonReader := json.NewDecoder(fileReader)
	err = jsonReader.Decode(&file)
	fileReader.Close()
	if err != nil {
		log.Panicf("Error: %v", err)
	}

	serverToMe, _ := zmq.NewSocket(zmq.PULL)
	defer serverToMe.Close()
	applyZMQConfig(serverToMe, file.ServerToMe)
	meToLink, _ := zmq.NewSocket(zmq.PUSH)
	defer meToLink.Close()
	applyZMQConfig(meToLink, file.MeToLink)

	linkToMe, _ := zmq.NewSocket(zmq.XSUB)
	defer linkToMe.Close()
	applyZMQConfig(linkToMe, file.LinkToMe)
	meToServer, _ := zmq.NewSocket(zmq.XPUB)
	defer meToServer.Close()
	applyZMQConfig(meToServer, file.MeToServer)

	go zmq.Proxy(serverToMe, meToLink, nil)
	zmq.Proxy(linkToMe, meToServer, nil)
}
コード例 #5
0
ファイル: bstar.go プロジェクト: gallir/zmq4
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
}
コード例 #6
0
ファイル: woodhouse.go プロジェクト: JeremyOT/zmq4
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")

}
コード例 #7
0
ファイル: tasksink2.go プロジェクト: JeremyOT/zmq4
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
}
コード例 #8
0
func StartZeromqJsonRouterDealerServer(port string) {
	frontend, _ := zmq.NewSocket(zmq.ROUTER)
	defer frontend.Close()

	backend, _ := zmq.NewSocket(zmq.DEALER)
	defer backend.Close()

	frontend.Bind("tcp://0.0.0.0:" + port)
	backend.Bind("inproc://backend")

	// start num cpu request processors
	for i := 0; i < runtime.NumCPU(); i++ {
		go func() {
			responder, _ := zmq.NewSocket(zmq.REP)
			defer responder.Close()

			responder.Connect("inproc://backend")

			for {
				request, _ := responder.RecvBytes(0)

				var body Request

				json.Unmarshal(request, body)

				response, _ := json.Marshal(Response{Method: body.Method, PayloadLength: len(body.Payload)})

				responder.Send(string(response), 0)
			}
		}()
	}

	err := zmq.Proxy(frontend, backend, nil)
	log.Fatalln(err)
}
コード例 #9
0
ファイル: mspoller.go プロジェクト: JeremyOT/zmq4
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)
			}
		}
	}
}
コード例 #10
0
ファイル: msg_brokers.go プロジェクト: alext234/code-grep
// see explanation here http://api.zeromq.org/4-0:zmq-proxy#toc2
func RunForwarderProxy(pubPort string, subPort string) {
	var err error

	glog.V(1).Info("pub port :", pubPort, " sub port :", subPort)
	//  Socket facing clients

	frontend, _ := zmq.NewSocket(zmq.XSUB)
	defer frontend.Close()
	err = frontend.Bind("tcp://*:" + pubPort)
	if err != nil {
		glog.Error("Error Binding frontend:", err)
	}

	//  Socket facing services

	backend, _ := zmq.NewSocket(zmq.XPUB)
	defer backend.Close()
	err = backend.Bind("tcp://*:" + subPort)
	if err != nil {
		glog.Error("Error Binding backend:", err)
	}

	//  Start the proxy
	err = zmq.Proxy(frontend, backend, nil)

	glog.Error("Proxy interrupted:", err)

}
コード例 #11
0
ファイル: ping_test.go プロジェクト: hintjens/zccp
// Yay! Test function.
func TestPing(t *testing.T) {

	// Create pair of sockets we can send through

	// Output socket
	output, err := zmq.NewSocket(zmq.DEALER)
	if err != nil {
		t.Fatal(err)
	}
	defer output.Close()

	routingId := "Shout"
	output.SetIdentity(routingId)
	err = output.Bind("inproc://selftest-ping")
	if err != nil {
		t.Fatal(err)
	}
	defer output.Unbind("inproc://selftest-ping")

	// Input socket
	input, err := zmq.NewSocket(zmq.ROUTER)
	if err != nil {
		t.Fatal(err)
	}
	defer input.Close()

	err = input.Connect("inproc://selftest-ping")
	if err != nil {
		t.Fatal(err)
	}
	defer input.Disconnect("inproc://selftest-ping")

	// Create a Ping message and send it through the wire
	ping := NewPing()

	err = ping.Send(output)
	if err != nil {
		t.Fatal(err)
	}
	transit, err := Recv(input)
	if err != nil {
		t.Fatal(err)
	}

	tr := transit.(*Ping)

	err = tr.Send(input)
	if err != nil {
		t.Fatal(err)
	}

	transit, err = Recv(output)
	if err != nil {
		t.Fatal(err)
	}

	if routingId != string(tr.RoutingId()) {
		t.Fatalf("expected %s, got %s", routingId, string(tr.RoutingId()))
	}
}
コード例 #12
0
ファイル: taskwork.go プロジェクト: JeremyOT/zmq4
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)
	}
}
コード例 #13
0
// ZmqPubSubProxy Implements a many-to-many device on a zmq PUB-SUB connection
func ZmqPubSubProxy(host string, portIn int, portOut int, logger *log.Logger) {
	xsub, _ := zmq.NewSocket(zmq.SUB)
	xpub, _ := zmq.NewSocket(zmq.PUB)

	defer xsub.Close()
	defer xpub.Close()

	addrIn := fmt.Sprintf("tcp://*:%d", portIn)
	addrOut := fmt.Sprintf("tcp://*:%d", portOut)

	logger.Println("ZMQ XSUB on", addrIn)
	xsub.Bind(addrIn)
	xsub.SetSubscribe("")

	logger.Println("ZMQ XPUB on", addrOut)
	xpub.Bind(addrOut)

	poller := zmq.NewPoller()
	poller.Add(xsub, zmq.POLLIN)

	for {
		// keep looping
		sockets, _ := poller.Poll(5 * time.Second)
		for _, socket := range sockets {
			switch s := socket.Socket; s {
			case xsub:
				ZmqSendMulti(xpub, ZmqRecvMulti(s))
			}
		}
	}
}
コード例 #14
0
func router(log func(level int, format string, a ...interface{})) {
	// inproc socket for receiving requests from frontend
	frontend, error := zmq.NewSocket(zmq.ROUTER)
	if error != nil {
		log(3, "error creating frontend socket: %s\n", error)
	} else {
		defer frontend.Close()
		log(0, "created frontend socket\n")
	}
	error = frontend.Bind("ipc:///tmp/feeds/upstream")
	if error != nil {
		log(3, "error binding [ipc:///tmp/feeds/upstream] frontend socket: %s\n", error)
	} else {
		log(0, "bound frontend socket to [ipc:///tmp/feeds/upstream]\n")
	}

	//  inproc socket for sending requests to downstream
	backend, error := zmq.NewSocket(zmq.DEALER)
	if error != nil {
		log(3, "error creating backend socket: %s\n", error)
	} else {
		defer backend.Close()
		log(0, "created backend socket\n")
	}
	error = backend.Bind("ipc:///tmp/feeds/downstream")
	if error != nil {
		log(3, "error binding [ipc:///tmp/feeds/downstream] backend socket: %s\n", error)
	} else {
		log(0, "bound backend socket to [ipc:///tmp/feeds/downstream]\n")
	}

	//  Connect backend to frontend via a proxy
	err := zmq.Proxy(frontend, backend, nil)
	log(0, "Proxy interrupted: %s\n", err)
}
コード例 #15
0
func New(id int, f string) Cluster {

	var myid int
	var peer [MAX_SERVER]int
	var cluster [MAX_SERVER]*zmq.Socket
	var mycluster *zmq.Socket
	var no_of_p int
	file, _ := ioutil.ReadFile(f)
	var jsontype jsonobject
	json.Unmarshal(file, &jsontype)
	myid = id
	no_of_p = jsontype.Object.Buffer_size

	for i := 0; i < jsontype.Object.Buffer_size; i++ {
		if jsontype.Object.Peers[i].Id != myid {
			peer[i] = jsontype.Object.Peers[i].Id
			cluster[i], _ = zmq.NewSocket(zmq.PUSH)
			cluster[i].Connect("tcp://" + jsontype.Object.Peers[i].Host)
		} else {
			mycluster, _ = zmq.NewSocket(zmq.PULL)
			mycluster.SetIdentity(string(id))
			a := strings.Split(jsontype.Object.Peers[i].Host, ":")
			mycluster.Bind("tcp://*:" + a[1])
		}
	}

	return Cluster{myid: id, peer: peer, no_of_peer: no_of_p, my_cluster: mycluster, cluster: cluster, inbox: make(chan *Envelope, BUFFER_LENGTH_IN), outbox: make(chan *Envelope, BUFFER_LENGTH_OUT)}
}
コード例 #16
0
ファイル: lbbroker3.go プロジェクト: JeremyOT/zmq4
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)
}
コード例 #17
0
/************************************************************
	Creates Sockets for Sending and Receiving purpose
*************************************************************/
func createSendAndReceivingSocket(configFile string, selfPort int) ([]*zmq.Socket, *zmq.Socket) {

	file, e := ioutil.ReadFile("./" + configFile)

	if e != nil {
		fmt.Printf("Raft Test File error: %v\n", e)
		os.Exit(1)
	}

	var jsontype jsonobject
	json.Unmarshal(file, &jsontype)

	elementCount := len(jsontype.Server)

	sendConnections := make([]*zmq.Socket, elementCount)

	tempId := 0

	for _, value := range jsontype.Server {

		sendConnections[tempId], _ = zmq.NewSocket(zmq.PUSH)
		sendConnections[tempId].Connect("tcp://localhost:" + strconv.Itoa(value.Port_Num+1))
		tempId++
	}

	var receiveConnection *zmq.Socket

	receiveConnection, _ = zmq.NewSocket(zmq.PULL)
	receiveConnection.Bind("tcp://*:" + strconv.Itoa(selfPort))

	return sendConnections, receiveConnection

}
コード例 #18
0
ファイル: clonesrv2.go プロジェクト: JeremyOT/zmq4
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)
}
コード例 #19
0
ファイル: taskvent.go プロジェクト: gallir/zmq4
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

}
コード例 #20
0
ファイル: Cluster.go プロジェクト: kalloldey/assignment
func sendRoutine(msg chan *Envelope, r *Raftserver) {
	//	fmt.Println("[send Routine] start")
	//Broadcast or Unicast
	om := <-msg
	//	fmt.Println("[send:] to pid: ", om.Pid)
	sendmsg := string(strconv.Itoa(om.Pid) + "#" + strconv.Itoa(om.MsgId) + "#" + om.Msg + "#") //msg to send out
	if om.Pid == -1 {                                                                           //Do a Broadcast
		for i := 0; i < r.TotalPeer; i++ { //Improve it by calling simultaneous goroutines instead of this ordered flow
			endpoint := string(r.PeerHandle + strconv.Itoa(r.StartAddr+r.PeersPid[i]))
			client, _ := zmq.NewSocket(zmq.DEALER)
			client.Connect(endpoint)
			//			fmt.Println("[Server ]", r.MyPid,"sending msg to: ",r.PeersPid[i])
			client.SendMessage(sendmsg)
			client.Close()
		}
	} else { //Do a unicast
		endpoint := string(r.PeerHandle + strconv.Itoa(r.StartAddr+om.Pid))
		client, _ := zmq.NewSocket(zmq.DEALER)
		//                fmt.Println("[send endpoint]:  ",endpoint)
		client.Connect(endpoint)
		client.SendMessage(sendmsg)
		client.Close()
	}
	r.Fin.Done() //telling I am over ..
}
コード例 #21
0
ファイル: msg_brokers.go プロジェクト: alext234/code-grep
// see explanation here http://api.zeromq.org/4-0:zmq-proxy#toc2
func RunStreamerProxy(pushPort string, pullPort string) {
	var err error

	glog.V(1).Info("push port :", pushPort, " pull port :", pullPort)
	//  Socket facing web servers (front end)

	frontend, _ := zmq.NewSocket(zmq.PULL)
	defer frontend.Close()
	err = frontend.Bind("tcp://*:" + pushPort)
	if err != nil {
		glog.Error("Binding frontend:", err)
	}

	//  Socket facing backend workers

	backend, _ := zmq.NewSocket(zmq.PUSH)
	defer backend.Close()
	err = backend.Bind("tcp://*:" + pullPort)
	if err != nil {
		glog.Error("Binding backend:", err)
	}

	//  Start the proxy
	err = zmq.Proxy(frontend, backend, nil)

	glog.Error("Proxy interrupted:", err)

}
コード例 #22
0
ファイル: spqueue.go プロジェクト: JeremyOT/zmq4
func main() {
	frontend, _ := zmq.NewSocket(zmq.ROUTER)
	backend, _ := zmq.NewSocket(zmq.ROUTER)
	defer frontend.Close()
	defer backend.Close()
	frontend.Bind("tcp://*:5555") //  For clients
	backend.Bind("tcp://*:5556")  //  For workers

	//  Queue of available workers
	workers := make([]string, 0)

	poller1 := zmq.NewPoller()
	poller1.Add(backend, zmq.POLLIN)
	poller2 := zmq.NewPoller()
	poller2.Add(backend, zmq.POLLIN)
	poller2.Add(frontend, zmq.POLLIN)

	//  The body of this example is exactly the same as lbbroker2.
LOOP:
	for {
		//  Poll frontend only if we have available workers
		var sockets []zmq.Polled
		var err error
		if len(workers) > 0 {
			sockets, err = poller2.Poll(-1)
		} else {
			sockets, err = poller1.Poll(-1)
		}
		if err != nil {
			break //  Interrupted
		}
		for _, socket := range sockets {
			switch s := socket.Socket; s {
			case backend: //  Handle worker activity on backend
				//  Use worker identity for load-balancing
				msg, err := s.RecvMessage(0)
				if err != nil {
					break LOOP //  Interrupted
				}
				var identity string
				identity, msg = unwrap(msg)
				workers = append(workers, identity)

				//  Forward message to client if it's not a READY
				if msg[0] != WORKER_READY {
					frontend.SendMessage(msg)
				}

			case frontend:
				//  Get client request, route to first available worker
				msg, err := s.RecvMessage(0)
				if err == nil {
					backend.SendMessage(workers[0], "", msg)
					workers = workers[1:]
				}
			}
		}
	}
}
コード例 #23
0
ファイル: tripping.go プロジェクト: JeremyOT/zmq4
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)
}
コード例 #24
0
ファイル: public.go プロジェクト: xeb/backq
// BindBackQ will bind the 0MQ backend endpoints specifid
func BindBackQ(reqaddy string, repaddy string) {
	reqsock, _ = zmq.NewSocket(zmq.PUB)
	reqsock.Bind(reqaddy)

	repsock, _ = zmq.NewSocket(zmq.REP)
	repsock.Bind(repaddy)
	// repsock.SetSubscribe("")
}
コード例 #25
0
ファイル: zmqRRBroker.go プロジェクト: yuhuichen/gocode
func main() {

	//config := ReadConfig("")
	//  Prepare our sockets
	frontend, _ := zmq.NewSocket(zmq.ROUTER)
	defer frontend.Close()
	backend, _ := zmq.NewSocket(zmq.DEALER)
	defer backend.Close()
	frontend.Bind(frontend_url_port)
	backend.Bind(backend_url_port)

	//  Initialize poll set
	poller := zmq.NewPoller()
	poller.Add(frontend, zmq.POLLIN)
	poller.Add(backend, zmq.POLLIN)

	log.Println("0MQ broker started!")
	log.Println("Frontend protocl/url/port:", frontend_url_port)
	log.Println("Backend protocol/url/port:", backend_url_port)

	//  Switch messages between sockets
	for {
		sockets, _ := poller.Poll(-1)
		for _, socket := range sockets {
			switch s := socket.Socket; s {
			case frontend:
				for {
					msg, _ := s.Recv(0)
					if more, _ := s.GetRcvmore(); more {
						backend.Send(msg, zmq.SNDMORE)
					} else {
						backend.Send(msg, 0)
						break
					}
					if debug {
						log.Printf("relayed request: [%s]\n", msg)
					}

				}
			case backend:
				for {
					msg, _ := s.Recv(0)
					if more, _ := s.GetRcvmore(); more {
						frontend.Send(msg, zmq.SNDMORE)
					} else {
						frontend.Send(msg, 0)
						break
					}
					if debug {
						log.Printf("relayed reply: [%s]\n", msg)
					}
				}

			}
		}
	}
	//log.Fatalln("Proxy interrupted:", err)
}
コード例 #26
0
ファイル: cluster.go プロジェクト: rpraveenverma/cluster
func New(serverid int, filename string) Servernode {

	var data serverlist
	content, err := ioutil.ReadFile(filename)
	if err != nil {
		panic(err)
	}

	err = json.Unmarshal(content, &data)
	if err != nil {
		panic("Error parsing json File")
	}

	mapofservers = make(map[int]int)
	for i := 0; i < len(data.RunningServer); i++ {

		mapofservers[data.RunningServer[i].Serverid] = data.RunningServer[i].Portno
	}

	var s Servernode
	s.peers = make(map[int]int)
	s.peersocks = make(map[int]*zmq.Socket)
	_, exist := mapofservers[serverid]
	if exist != true {
		panic("Error in creation of new server instance")
	}
	s.sid = serverid
	s.pid = mapofservers[serverid]
	s.outbox = make(chan *Envelope)
	s.inbox = make(chan *Envelope)
	var responder *zmq.Socket
	var requester *zmq.Socket

	for id, port := range mapofservers {
		if id == serverid {
			responder, err = zmq.NewSocket(zmq.PULL)
			s.Ownsocks = responder
			responder.Bind("tcp://*:" + strconv.Itoa(port))

			if err != nil {
				panic("Problem Binding Server")
			}
		} else {
			requester, err = zmq.NewSocket(zmq.PUSH)
			if err != nil {
				panic("Problem Binding Server")
			}
			requester.Connect("tcp://localhost:" + strconv.Itoa(port))
			s.peers[id] = port
			s.peersocks[port] = requester
		}
	}
	go s.SendMessage()
	go s.RecieveMessage(responder)
	return s
}
コード例 #27
0
ファイル: kvsimple_test.go プロジェクト: JeremyOT/zmq4
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")
}
コード例 #28
0
ファイル: lvcache.go プロジェクト: JeremyOT/zmq4
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)
					}
				}
			}
		}
	}
}
コード例 #29
0
func main() {
	println("=====================[ emdr-relay-go ]=====================")
	println("Starting emdr-relay-go 1.1...")
	cache := cache.NewLRUCache(cache_size_limit)

	receiver, _ := zmq.NewSocket(zmq.SUB)
	receiver.Connect("tcp://master.eve-emdr.com:8050")
	receiver.Connect("tcp://secondary.eve-emdr.com:8050")
	receiver.SetSubscribe("")
	defer receiver.Close()

	sender, _ := zmq.NewSocket(zmq.PUB)
	sender.Bind("tcp://*:8050")
	defer sender.Close()

	println("Listening on port 8050.")
	println("===========================================================")
	//  Ensure subscriber connection has time to complete
	time.Sleep(time.Second)

	// We auto-exit every number of hours so we can recover from some
	// weird edge case conditions that disrupt the network. They're not common,
	// but we'll do this to be absolutely sure.
	go periodic_exiter()

	for {
		msg, zmq_err := receiver.Recv(0)
		if zmq_err != nil {
			println("RECV ERROR:", zmq_err.Error())
		}

		var h hash.Hash = fnv.New32()
		h.Write([]byte(msg))

		checksum := h.Sum([]byte{})
		cache_key := fmt.Sprintf("%x", checksum)

		cache_item, cache_hit := cache.Get(cache_key)
		if cache_hit {
			// We've already seen this before, ignore it.
			continue
		}

		// At this point, we know we've encountered a message we haven't
		// seen in the recent past.
		cache_item = &CacheValue{found: true}
		// Insert the cache entry to prevent future re-sends of this message.
		cache.Set(cache_key, cache_item)

		// A cache miss means that the incoming message is not a dupe.
		// Send the message to subscribers.
		sender.Send(msg, 0)
	}
}
コード例 #30
0
ファイル: zmq4_test.go プロジェクト: rdterner/zmq4
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
}