コード例 #1
0
ファイル: helloservice.go プロジェクト: TomOgoma/GoZMQ
//  Main function is to serve clients
func main() {
	//  Socket to talk to clients
	responder, err := zmq.NewSocket(zmq.REP)
	if err != nil {
		panic(err)
	}
	defer responder.Close()
	responder.Bind(allowedbinders)

	for {
		var service_required msg.ProcessRequest
		var message string
		var reply string
		//  Wait for next request from client
		service_required, message, err = msg.RecieveClientRequest(responder, myservices)
		if err != nil {
			msg.SendToClient(mydescription.Reply, fmt.Sprintf("Error:Receive:%s", err), "Error Receiving message", responder)
			continue
		}
		fmt.Println("Received ", message)

		//  Do some 'work'
		time.Sleep(time.Second)
		reply, err = service_required(message)
		if err != nil {
			msg.SendToClient(mydescription.Reply, fmt.Sprintf("Error:Receive:%s", err), "Error Processing Request", responder)
			continue
		}

		//  Send reply back to client
		msg.SendToClient(mydescription.Reply, "", reply, responder)
	}
}
コード例 #2
0
ファイル: lookupservice.go プロジェクト: TomOgoma/GoZMQ
func main() {
	//  Socket to talk to clients
	responder, err := zmq.NewSocket(zmq.REP)
	if err != nil {
		panic(err)
	}
	defer responder.Close()
	responder.Bind(ALLOWED_BINDERS)
	fmt.Println("Directory Service at ", services["lookup"].Address, " waiting for connection...")

	//  Wait for next request from client
	for {
		var service_required msg.ProcessRequest
		var message string
		service_required, message, err = msg.RecieveClientRequest(responder, myservices)
		var reply string
		if err != nil {
			msg.SendToClient(services["lookup"].Reply, fmt.Sprintf("%s", err), "Error Receiving Message", responder)
			continue
		}
		reply, err := service_required(message)
		if err != nil {
			msg.SendToClient(services["lookup"].Reply, fmt.Sprintf("%s", err), "Error Processing Request", responder)
			continue
		}
		msg.SendToClient(services["lookup"].Reply, "", reply, responder)
	}
}