Beispiel #1
0
func handleUpdateDevice(pool *redis.Pool, zmqPUB *zmq.Socket, deviceInfoB []byte) {
	deviceInfo := string(deviceInfoB)
	deviceID := strings.Split(deviceInfo, "::")[0] //makes life
	serverID := strings.Split(deviceInfo, "::")[1] //easy

	conn := pool.Get() //greb a connection from the pool
	defer conn.Close()

	//get the length of the list
	listLength, err := redis.Int(conn.Do("LLEN", "ASMS::pending::deviceID:"+deviceID+"::messageID:"))
	if err != nil {
		panic(err)
	}

	//get the list of messageID that is missing for the device
	messageIDs, err := redis.Strings(conn.Do("LRANGE", "ASMS::pending::deviceID:"+deviceID+"::messageID:", 0, listLength))
	if err != nil {
		panic(err)
	}

	for _, messageID := range messageIDs { // for each pending message in the list of messages
		var m common.Message

		msg, err := redis.String(conn.Do("GET", "ASMS::pending::messageID:"+messageID+"::message:"))
		if err != nil {
			panic(err)
		}

		if err := json.Unmarshal([]byte(msg), &m); err != nil { //unpack the message into m, check for an error
			panic(err)
		} //end if

		m.DestinationDeviceID = deviceID //who we are sending it too

		MessageJSON, err := json.Marshal(m)
		if err != nil {
			panic(err)
		}

		//send the serverDesination through as the first part
		if err := zmqPUB.SendPart([]byte(serverID), true); err != nil {
			panic(err)
		}

		//identify the type of message being sent
		if err := zmqPUB.SendPart([]byte("Message"), true); err != nil {
			fmt.Println(err)
		} //end if

		//send the message as JSON through
		if err := zmqPUB.SendPart(MessageJSON, false); err != nil { //second part of the multipart message, this contains the message and all relevant information.
			panic(err)
		}

	} //end for
} //end handleUpdateDevice
Beispiel #2
0
func handleMessage(pool *redis.Pool, zmqPUB *zmq.Socket, msg []byte) {
	var m common.Message //create an empty Message
	var r common.Receipt //create an empty Receipt

	if err := json.Unmarshal(msg, &m); err != nil { //unpack the message into m, check for an error
		fmt.Println(err)
	} //end if

	//setup the receipt we send back to the sender letting them know that
	r.MessageID = m.MessageID                //it has this messageID
	r.Status = common.RECEIPT_SENDING_STATUS //the status is sending
	r.OriginDeviceID = "tracker"             //it is being reported by the tracker

	accountIDsToDeviceID := common.TranslateAccountIDtoDeviceID(m.AccountIDs, pool) //translate the accountIDs to the deviceIDs returns a map[string][]string
	conn := pool.Get()                                                              //get a new connection from the pool
	defer conn.Close()

	//save the message in the database, so we dont lose it
	if _, err := conn.Do("SET", "ASMS::pending::messageID:"+m.MessageID+"::message:", msg); err != nil {
		fmt.Println(err)
	}

	//format the reciept into JSON
	ReceiptJSON, err := json.Marshal(r)
	if err != nil {
		fmt.Println(err)
	}

	clientsServer, err := redis.String(conn.Do("GET", "ASMS::deviceID:"+m.OriginDeviceID+"::serverID:"))
	if err != nil {
		fmt.Println(err)
	}

	if err := zmqPUB.SendPart([]byte(clientsServer), true); err != nil { //send to the client
		fmt.Println(err)
	}

	//identify the type of message being sent
	if err := zmqPUB.SendPart([]byte("Receipt"), true); err != nil {
		fmt.Println(err)
	} //end if

	if err := zmqPUB.SendPart(ReceiptJSON, false); err != nil {
		fmt.Println(err)
	}

	for _, accountID := range accountIDsToDeviceID { //accountID level
		for _, deviceID := range accountID { //each device associated with the account ID
			deviceCurrentServer, err := redis.String(conn.Do("GET", "ASMS::connection::deviceID:"+deviceID+"::server:")) //get the current server that the device is on
			if err != nil {
				fmt.Println(err)
			} //end if

			m.DestinationDeviceID = deviceID //set the destination device id in the message to this deviceID

			MessageJSON, err := json.Marshal(m)
			if err != nil {
				fmt.Println(err)
			}

			//set the message as pending, always assume that it never went through
			if _, err := conn.Do("LPUSH", "ASMS::pending::messageID:"+m.MessageID+"::deviceID:", deviceID); err != nil {
				fmt.Println(err)
			}

			if _, err := conn.Do("LPUSH", "ASMS::pending::deviceID:"+deviceID+"::messageID:", m.MessageID); err != nil {
				fmt.Println(err)
			}

			//send the serverDesination through as the first part
			if err := zmqPUB.SendPart([]byte(deviceCurrentServer), true); err != nil {
				fmt.Println(err)
			}

			//identify the type of message being sent
			if err := zmqPUB.SendPart([]byte("Message"), true); err != nil {
				fmt.Println(err)
			} //end if

			//send the message as JSON through
			if err := zmqPUB.SendPart(MessageJSON, false); err != nil { //second part of the multipart message, this contains the message and all relevant information.
				fmt.Println(err)
			}

		} //end for deviceID
	} //end for accountID

} //end handleMessage