// the device point gateway start listen
func (this *DeviceGatewayServer) Start(host string) error {
	common.Assert((this.connManager != nil) && (this.devManager != nil), "check param nil")
	addr, err := net.ResolveTCPAddr("tcp4", host)
	if err != nil {
		log.Errorf("resolve tcp addr error:err[%v]", err)
		return err
	}
	ln, err := net.ListenTCP("tcp", addr)
	if err != nil {
		log.Errorf("listen error:err[%v]", err)
		return err
	}
	var waitGroup sync.WaitGroup
	for {
		socket, err := ln.AcceptTCP()
		if err != nil {
			log.Errorf("accept error:err[%v]", err)
			continue
		}
		log.Infof("device connect start:addr[%s]", socket.RemoteAddr())
		waitGroup.Add(1)
		go this.deviceRoutine(&waitGroup, socket, QueueLen)
	}
	waitGroup.Wait()
	return err
}
Пример #2
0
func loop() {
	body := make([]byte, 1024)
	// TODO using http client request the device
	var request, response common.Message
	for i := 0; i < 3600*1000; i++ {
		request.Header.PayloadLen = uint16(rand.Intn(30) + 1)
		request.Header.Version = 123
		request.Header.MsgCode = common.ZC_CODE_ZDESCRIBE
		request.Header.MsgId = uint8(i + 101)
		var j uint16
		for j = 0; j < request.Header.PayloadLen; j++ {
			body[j] = byte(j + 1)
		}
		old := time.Now().UTC()
		request.Payload = body[:request.Header.PayloadLen]
		fmt.Println("Write Request:", request.Header, body[:request.Header.PayloadLen])
		// check the message
		if response.Header.MsgCode != common.ZC_CODE_ERR {
			fmt.Println("Read Response:", response.Header, body[:response.Header.PayloadLen])
			for j = 0; j < response.Header.PayloadLen; j++ {
				common.Assert(response.Payload[j] == body[j], "check the response failed")
			}
			fmt.Println("request timeused:", request.Header.MsgId, old, time.Now().UTC())
		} else {
			fmt.Println("Send Request to Device Failed")
		}
		time.Sleep(time.Millisecond * 50)
	}
}
Пример #3
0
// async send request to device and wait the response on result
func (this *Connection) SendRequest(packet *common.Message, result *chan common.Message) (err error) {
	common.Assert(packet != nil && result != nil, "check input param failed")
	oldId := packet.Header.MsgId
	log.Infof("receive new Request from app:msgId[%d]", oldId)
	request := NewDeviceCtrlRequest(oldId, result)
	// rewrite the msg id as the connection new id
	packet.Header.MsgId = this.getNewRequestId()
	err = this.requestMap.Insert(packet.Header.MsgId, request)
	if err != nil {
		log.Warningf("check request insert failed:old[%d], new[%d]", oldId, packet.Header.MsgId)
		return err
	}
	log.Infof("insert the request mapping succ:mid[%d], code[%d], dest[%v], gid[%s]",
		packet.Header.MsgId, packet.Header.MsgCode, this.socket.RemoteAddr(), this.gid.String())
	// if the dev connection closed panic will occured
	defer func() {
		packet.Header.MsgId = oldId
		info := recover()
		if info != nil {
			log.Warningf("the request queue is closed:err[%v]", info)
			err = common.ErrDeviceConnClosed
		}
	}()
	this.requestQueue <- *packet
	return nil
}
func NewDeviceGatewayServer(conn *common.SafeMap, dev *DevicePKManager) *DeviceGatewayServer {
	common.Assert((conn != nil) && (dev != nil), "check param nil")
	return &DeviceGatewayServer{connManager: conn, devManager: dev}
}