コード例 #1
0
func (this *HookSwitchInspector) Start() error {
	log.Debugf("Initializing Ethernet Inspector %#v", this)
	var err error

	if this.EnableTCPWatcher {
		this.tcpWatcher = tcpwatcher.New()
	}

	this.trans, err = transceiver.NewTransceiver(this.OrchestratorURL, this.EntityID)
	if err != nil {
		return err
	}
	this.trans.Start()

	zmqSocket, err := zmq.NewSocket(zmq.Pair)
	if err != nil {
		return err
	}
	zmqSocket.Bind(this.HookSwitchZMQAddr)
	defer zmqSocket.Close()
	this.zmqChannels = zmqSocket.Channels()
	for {
		select {
		case msgBytes := <-this.zmqChannels.In():
			meta, ethBytes, err := this.decodeZMQMessageBytes(msgBytes)
			if err != nil {
				log.Error(err)
				continue
			}
			eth, ip, tcp := parseEthernetBytes(ethBytes)
			// note: tcpwatcher is not thread-safe
			if this.EnableTCPWatcher && this.tcpWatcher.IsTCPRetrans(ip, tcp) {
				meta.Op = hookswitch.Drop
				err = this.sendZMQMessage(*meta, nil)
				if err != nil {
					log.Error(err)
				}
				continue
			}
			go func() {
				if err := this.onHookSwitchMessage(*meta, eth, ip, tcp); err != nil {
					log.Error(err)
				}
			}()
		case err := <-this.zmqChannels.Errors():
			return err
		}
	}
	// NOTREACHED
}
コード例 #2
0
ファイル: hwserver.go プロジェクト: golang-alex/go-zmq
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)
	}
}