Пример #1
0
func HandlePush(agent *Agent, pkt *packet.Pkt) {
	dataMsg := packet.PktDataMessage{}
	err := packet.Unpack(pkt, &dataMsg)
	if err != nil {
		log.Errorf("Error unpack push msg: %s", err.Error())
	}
	log.Infof("Received push message: MsgId: %d, Type: %d,  Appid: %s, Msg: %s\n",
		dataMsg.MsgId, dataMsg.MsgType, dataMsg.AppId, dataMsg.Msg)

	dataAck := packet.PktDataACK{
		MsgId: dataMsg.MsgId,
		AppId: dataMsg.AppId,
		RegId: agent.RegIds[dataMsg.AppId],
	}

	pktAck, err := packet.Pack(packet.PKT_ACK, 0, dataAck)
	if err != nil {
		log.Errorf("Pack error: %s", err.Error())
		return
	}

	agent.SendPkt(pktAck)

	if agent.OnReceiveMsg != nil {
		agent.OnReceiveMsg(dataMsg.AppId, dataMsg.MsgId, dataMsg.MsgType, dataMsg.Msg)
	}
}
Пример #2
0
func HandleUnregist(client *Client, pkt *packet.Pkt) {
	dataUnreg := packet.PktDataUnregist{}
	err := packet.Unpack(pkt, &dataUnreg)
	if err != nil {
		log.Printf("Unpack error: %s", err.Error())
		return
	}

	log.Printf("Received Unregist. AppId: %s, AppKey: %s, RegId: %s",
		dataUnreg.AppId, dataUnreg.AppKey, dataUnreg.RegId)

	dataUnregResp := packet.PktDataUnregResp{
		AppId:  dataUnreg.AppId,
		RegId:  dataUnreg.RegId,
		Result: 0,
	}
	pktUnregResp, err := packet.Pack(packet.PKT_Unregist_Resp, 0, dataUnregResp)
	if err != nil {
		log.Printf("Pack Unreg_Resp error: %s", err.Error())
		return
	}
	b, err := pktUnregResp.Serialize()
	if err != nil {
		log.Printf("Serialize error: %s", err.Error())
		return
	}
	client.Conn.Write(b)
}
Пример #3
0
func HandleRegist(client *Client, pkt *packet.Pkt) {
	var dataRegist = packet.PktDataRegist{}
	err := packet.Unpack(pkt, &dataRegist)
	if err != nil {
		log.Printf("Failed to Unpack: %s", err.Error())
	}
	log.Printf("Device [%s] regist AppID: %s", client.Id, dataRegist.AppId)

	dataRegResp := packet.PktDataRegResp{
		AppId:  dataRegist.AppId,
		RegId:  dataRegist.AppId + "_XXX",
		Result: 0,
	}
	pktRegResp, err := packet.Pack(packet.PKT_Regist_Resp, 0, &dataRegResp)
	if err != nil {
		log.Printf("Pack error: %s", err.Error())
		return
	}

	b, err := pktRegResp.Serialize()
	if err != nil {
		log.Printf("Serialize error: %s", err.Error())
		return
	}
	client.Conn.Write(b)

	client.AppIds = append(client.AppIds, dataRegist.AppId)
	SendOfflineMsg(client, dataRegist.AppId)
}
Пример #4
0
func (this *Client) SendMsg(msg string, appid string) error {
	pktDataMsg := packet.PktDataMessage{
		Msg:   msg,
		AppId: appid,
	}

	pkt, err := packet.Pack(packet.PKT_Push, 0, &pktDataMsg)
	if err != nil {
		return err
	}
	this.PktChan <- pkt
	return nil
}
Пример #5
0
func (this *Agent) Unregist(appid string, appkey string, regid string, token string) {
	log.Infof("Unregist AppId: %s, AppKey: %s, RegId: %s", appid, appkey, regid)
	dataUnregist := packet.PktDataUnregist{
		AppId:  appid,
		AppKey: appkey,
		RegId:  regid,
		Token:  token,
	}
	pktUnregist, err := packet.Pack(packet.PKT_Unregist, 0, dataUnregist)
	if err != nil {
		log.Errorf("Pack error: %s", err.Error())
		return
	}
	this.SendPkt(pktUnregist)
}
Пример #6
0
func (this *Agent) Run() {
	this.LoadRegIds()

	conn, err := net.DialTCP("tcp", nil, this.serverAddr)
	if err != nil {
		log.Errorf("Dial %v error: %s", this.serverAddr, err.Error())
		log.Flush()
		os.Exit(1)
		return
	}
	defer func() {
		conn.Close()
	}()

	dataInit := packet.PktDataInit{
		DevId: this.deviceId,
	}

	initPkt, err := packet.Pack(packet.PKT_Init, 0, dataInit)
	if err != nil {
		log.Error("Pack error: %s", err.Error())
		return
	}

	b, err := initPkt.Serialize()
	if err != nil {
		log.Error("Serialize error: %s", err.Error())
	}
	log.Debug(string(initPkt.Data))
	conn.Write(b)

	go func() {
		timer := time.NewTicker(20 * time.Second)
		hbPkt, _ := packet.Pack(packet.PKT_Heartbeat, 0, nil)
		heartbeat, _ := hbPkt.Serialize()
		for {
			select {
			//case <- done:
			//	break
			case pkt := <-this.outPkt:
				b, err := pkt.Serialize()
				if err != nil {
					log.Errorf("Serialize error: %s", err.Error())
				}
				log.Debugf("Write data: %s\n", b)
				conn.Write(b)
			case <-timer.C:
				conn.Write(heartbeat)
			}
		}
	}()

	var bufHeader = make([]byte, packet.PKT_HEADER_SIZE)
	for {
		//// check if we are exiting
		//select {
		//case <-this.exitChan:
		//	log.Printf("Closing connection from %s.\n", conn.RemoteAddr().String())
		//	return
		//default:
		//	// continue read
		//}

		const readTimeout = 300 * time.Second
		conn.SetReadDeadline(time.Now().Add(readTimeout))

		// read the packet header
		nbytes, err := io.ReadFull(conn, bufHeader)
		if err != nil {
			if err == io.EOF {
				log.Warn("read EOF, closing connection")
				return
			} else if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
				// just read timeout, not an error
				continue
			}
			log.Errorf("read error: %s\n", err.Error())
			return
		}
		log.Debugf("%d bytes packet header read\n", nbytes)

		var pkt = packet.Pkt{
			Data: nil,
		}
		pkt.Header.Deserialize(bufHeader)

		// read the packet data
		if pkt.Header.Len > 0 {
			log.Debugf("expecting data size: %d\n", pkt.Header.Len)
			var bufData = make([]byte, pkt.Header.Len)
			nbytes, err := io.ReadFull(conn, bufData)
			if err != nil {
				if err == io.EOF {
					log.Warn("read EOF, closing connection")
					return
				} else if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
					// read timeout
					//TODO: exit?
					log.Errorf("Timeout on receiving packet data. error: %s\n", err.Error())
					continue
				}
				log.Errorf("Error on receiving packet data. error: %s\n", err.Error())
				return
			}
			log.Debugf("%d bytes packet data read: %s\n", nbytes, bufData)
			pkt.Data = bufData
		}

		this.handlePacket(&pkt)
	}
}
Пример #7
0
func HandleInit(conn *net.TCPConn, pkt *packet.Pkt) *Client {
	log.Printf("Handling packet type: Regist")
	var dataInit = packet.PktDataInit{}
	err := packet.Unpack(pkt, &dataInit)
	if err != nil {
		log.Printf("Failed to Unpack: %s", err.Error())
		return nil
	}

	// TODO: check if the Id already exist

	client := Client{
		Conn:          conn,
		Id:            dataInit.DevId,
		PktChan:       make(chan *packet.Pkt),
		LastHeartbeat: time.Now(),
	}

	log.Printf("New Device is online, Id: %s", client.Id)

	go func() {
		for {
			select {
			case pkt := <-client.PktChan:
				log.Printf("pkt to send")
				b, err := pkt.Serialize()
				if err != nil {
					log.Printf("Error on serializing pkt: %s", err.Error())
					continue
				}
				var n int
				n, err = conn.Write(b)
				if err != nil {
					log.Printf("Error on sending pkt: %s", err.Error())
					continue
				}
				log.Printf("Write successfully: %d", n)
			}
		}
	}()

	// send Response for the Init packet
	dataInitResp := packet.PktDataInitResp{
		Result: 0,
	}

	initRespPkt, err := packet.Pack(packet.PKT_Init_Resp, 0, &dataInitResp)
	if err != nil {
		log.Printf("Pack error: %s", err.Error())
		return nil
	}

	b, err := initRespPkt.Serialize()
	if err != nil {
		log.Printf("Serialize error: %s", err.Error())
		return nil
	}
	conn.Write(b)

	ClientMapLock.Lock()
	ClientMap[client.Id] = &client
	ClientMapLock.Unlock()

	return &client
}