Пример #1
0
func isValidAccountID(accountID string, pool *redis.Pool, sock *zmq.Socket) {
	conn := pool.Get()
	var valid string

	listLength, err := redis.Int(conn.Do("LLEN", "ASMS::accountID:"+accountID+"::deviceID:"))
	if err != nil {
		fmt.Println(err)
	}

	if listLength != 0 {
		valid = "true"
	} else {
		valid = "false"
	}
	go common.HandleSocketResponse([]byte(valid), sock)
} //end isValidAccountID
Пример #2
0
func main() {
	externalIP := findExternalIP()
	serverID := common.GenerateServerID() //create a serverID

	//setup redis pool
	var pool = &redis.Pool{
		MaxIdle:     common.REDIS_MAX_POOL_IDLE,
		IdleTimeout: 240 * time.Second,
		Dial: func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", common.REDIS_ADDRESS+":"+common.REDIS_PORT)
			if err != nil {
				return nil, err
			}
			return c, err
		}, //end dial
	} //end pool

	//setup zeromq sockets (XSUB(tracker to this), XPUB(this to device), REP(device to this))
	zmqCTX, err := zmq.NewContext()
	if err != nil {
		panic(err)
	}
	defer zmqCTX.Close()

	//setup the publisher sockets for zeromq
	userLayerZMQPUB, err := zmqCTX.Socket(zmq.Pub)
	if err != nil {
		panic(err)
	}
	defer userLayerZMQPUB.Close()

	//setup the sub sockets for zeromq
	userLayerZMQSUB, err := zmqCTX.Socket(zmq.Sub)
	if err != nil {
		panic(err)
	}
	defer userLayerZMQSUB.Close()

	userLayerZMQSUB.Subscribe([]byte(serverID)) //subscribe this server to its server address

	//setup the reply sockets for zeromq
	userLayerZMQREP, err := zmqCTX.Socket(zmq.Rep)
	if err != nil {
		panic(err)
	}
	defer userLayerZMQREP.Close()

	//bind the zeromq pub sockets for the device on the tracker.
	if err = userLayerZMQSUB.Bind(common.ZMQ_SOCKET_PROTOCOL + common.ZMQ_TRACKER_PUB_IP_ADDRESS + ":" + common.ZMQ_TRACKER_PUB_PORT); err != nil {
		panic(err)
	}

	//bind the publisher socket
	if err = userLayerZMQPUB.Bind(common.ZMQ_SOCKET_PROTOCOL + externalIP + ":" + common.ZMQ_USERVER_PUB_PORT); err != nil {
		panic(err)
	}

	//Filter for this servers ID on the subscriber socket.
	userLayerZMQSUB.Subscribe([]byte(serverID))

	//get a new connection from the pool
	conn := pool.Get()

	//register the server with redis. (write this servers: ID, IP, and current load)
	//current ip
	if _, err := conn.Do("SET", "ASMS::severID:"+serverID+"::IP:", findExternalIP()); err != nil {
		panic(err)
	}
	//current load, set as zero at startup
	if _, err := conn.Do("SET", "ASMS::server:"+serverID+"::load:", 0); err != nil {
		panic(err)
	}

	//set an expire for the key we just set!
	if _, err := conn.Do("PEXPIRE", "ASMS::server:"+serverID+"::load:", common.SERVER_UPDATE_LOAD_SECONDS*1000); err != nil {
		panic(err)
	} //end if

	//current ID
	if _, err := conn.Do("LPUSH", "ASMS::connected::serverID:", serverID); err != nil {
		panic(err)
	}

	go updateServerLoad(pool, serverID) //will update every so often

	var zmqPoll zmq.PollSet
	zmqPoll.Socket(userLayerZMQSUB, zmq.EventSet(zmq.In|zmq.Error)) //array 0
	zmqPoll.Socket(userLayerZMQREP, zmq.EventSet(zmq.In|zmq.Error)) //array 1

	for {
		_, _ = zmqPoll.Poll(-1 * time.Minute) //loop forever
		switch {
		case zmqPoll.Events(0) != 0: //sub socket
			//eventType := zmqPoll.Events(0)

			destination, more, err := userLayerZMQSUB.RecvPart() //first part contains destination
			if err != nil {
				panic(err)
			} //end if

			if more == true && string(destination) == serverID { //make sure this is the right server
				dataType, more, err := userLayerZMQSUB.RecvPart() //get the second part of the message, it contains the datatype
				if err != nil {
					panic(err)
				} //end if

				data, _, err := userLayerZMQSUB.RecvPart() //get the third part of the message, it contains the actual message.
				if err != nil {
					panic(err)
				} //end if

				if more != false { //just incase!
					switch string(dataType) {
					case "Message":
						go handleSubMessageEvent(data, userLayerZMQPUB)
					case "Receipt":
						go handleSubReceiptEvent(data, userLayerZMQPUB)
					} //end switch type
				} //end if more
			} //end if
		case zmqPoll.Events(1) != 0: //rep socket, its a message/receipt from the device

			request, more, err := userLayerZMQREP.RecvPart() //deviceID
			if err != nil {
				panic(err)
			} //end if

			if more == true {
				data, _, err := userLayerZMQREP.RecvPart() //contains the data
				if err != nil {
					panic(err)
				} //end if

				//deviceID::data
				info := strings.Split(string(data), "::") //0 is ALWAYS the deviceID. ALWAYS

				switch string(request) {
				case "isValidAccountID":
					go isValidAccountID(info[1], pool, userLayerZMQREP)
				case "translateEmailtoAccountID":
					go common.TranslateEmailtoAccountID(info[1], pool, userLayerZMQREP)
				case "translatePhonetoAccountID":
					go common.TranslatePhonetoAccountID(info[1], pool, userLayerZMQREP)
				case "updateDevice":
					go updateDevice(serverID, info[0], pool)
				default:
					msg := []byte("Unknown request.")
					go common.HandleSocketResponse(msg, userLayerZMQREP)
				} //end switch
			} //end if
		} //end switch
	} //end for
} //end main