Esempio n. 1
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)
}
Esempio n. 2
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)
	}
}
Esempio n. 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)
}
Esempio n. 4
0
// Received response for the unregist packet
func HandleUnregist_Resp(agent *Agent, pkt *packet.Pkt) {
	dataUnregResp := packet.PktDataUnregResp{}
	err := packet.Unpack(pkt, &dataUnregResp)
	if err != nil {
		log.Errorf("Error unpack pkt: %s", err.Error())
	}

	log.Infof("Received Unregist_Resp AppId: [%s] RegId: [%s]", dataUnregResp.AppId, dataUnregResp.RegId)
	delete(agent.RegIds, dataUnregResp.AppId)
	agent.SaveRegIds()
}
Esempio n. 5
0
// Received response for the init packet
func HandleInit_Resp(agent *Agent, pkt *packet.Pkt) {
	dataInitResp := packet.PktDataInitResp{}
	err := packet.Unpack(pkt, &dataInitResp)
	if err != nil {
		log.Errorf("Error unpack pkt: %s", err.Error())
		return
	}
	log.Infof("Received Init_Resp. Result: %d", dataInitResp.Result)
	if dataInitResp.Result != 0 {
		log.Errorf("Device Init failed!")
	}
}
Esempio n. 6
0
// Received response for the regist packet
func HandleRegist_Resp(agent *Agent, pkt *packet.Pkt) {
	dataRegResp := packet.PktDataRegResp{}
	err := packet.Unpack(pkt, &dataRegResp)
	if err != nil {
		log.Errorf("Error unpack pkt: %s", err.Error())
	}

	log.Infof("Received Regist_Resp AppId: [%s] RegId: [%s] Result: %d",
		dataRegResp.AppId, dataRegResp.RegId, dataRegResp.Result)
	agent.RegIds[dataRegResp.AppId] = dataRegResp.RegId
	agent.SaveRegIds()
	if agent.OnRegResponse != nil {
		agent.OnRegResponse(dataRegResp.AppId, dataRegResp.RegId, dataRegResp.Result)
	}
}
Esempio n. 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
}