示例#1
0
def clientthread(conn):
    qe = QueryEngine()
    #Sending message to connected client
    #conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string

    #infinite loop so that function do not terminate and thread do not end.
    while True:
        #Receiving from client
        reply = ''
        data = conn.recv(1024)
        #print data
        
        params = qe.deserialize(data)
        param_type = params[0]
        print param_type
        print 'PARAMS', params
        if param_type == QueryType.SELECT:
            reply = db.select(msToSec(params[1]), params[2])

        elif param_type == QueryType.INSERT:
            if params[2]=='':
                pass
            else:
                db.insert(msToSec(params[1]),params[2], params[3])
                reply = "inserting " + data
            
            #reply = db.insert(params[1:])
        elif param_type == QueryType.UPDATE:
            reply = "updating"
            #reply = db.update(params[1:])
        elif param_type == QueryType.SELECTRANGE:
            #dbResponse = list(db.selectRangeForDisplay(msToSec(params[1]), msToSec(params[2]), params[3]))
            dbResponse = list(db.selectRangeAndInterval(msToSec(params[1]), msToSec(params[2]), params[3],params[4]))
            dbResponse.insert(0, QueryType.SERVER_REPLY)
            reply = qe.serialize(dbResponse)
        elif param_type == QueryType.INC_AVG:
            reply = db.getRunningAverage(params[1]);
        elif param_type == QueryType.INC_COUNT:
            reply = db.getAggregateCountInRange(msToSec(params[1]), msToSec(params[2]), params[3])
        elif param_type == QueryType.INC_VAR:
	    reply = db.getRunningVariance(params[1]);
	else:
            # throw exception
            reply = "Invalid arguments, should be start with SELECT, INSERT, or UPDATE"
        print reply
        conn.sendall(reply)
    
        
            #conn.close()
    conn.close()