gyorgyf@48: #!/usr/bin/env python gyorgyf@48: # encoding: utf-8 gyorgyf@48: """ gyorgyf@48: simulator.py gyorgyf@48: gyorgyf@48: Created by George Fazekas on 2013-08-08. gyorgyf@48: Copyright (c) 2013 . All rights reserved. gyorgyf@48: """ gyorgyf@48: gyorgyf@48: import sys,os,time gyorgyf@48: import httplib as ht gyorgyf@48: import urllib gyorgyf@48: import random as r gyorgyf@48: import datetime as dt gyorgyf@48: gyorgyf@48: # IP = "127.0.0.1:8030" gyorgyf@48: IP = "138.37.95.215" # this is the IP of kakapo<=>golden gyorgyf@48: gyorgyf@48: HTTP_TIMEOUT = 3 gyorgyf@48: gyorgyf@48: class DataPoint(object): gyorgyf@48: gyorgyf@48: def __init__(self,x,y,ts): gyorgyf@48: self.x = x gyorgyf@48: self.y = y gyorgyf@48: self.ts = ts gyorgyf@48: gyorgyf@48: def __repr__(self): gyorgyf@48: return "DataPoint(%f,%f,%s)" %(self.x,self.y,str(self.ts)) gyorgyf@48: gyorgyf@48: gyorgyf@48: class Simulator(object): gyorgyf@48: gyorgyf@48: def __init__(self): gyorgyf@48: self.connect() gyorgyf@48: self.data = [] gyorgyf@48: self.first_date = None gyorgyf@48: pass gyorgyf@48: gyorgyf@48: def connect(self): gyorgyf@48: self.conn = ht.HTTPConnection(IP,timeout=HTTP_TIMEOUT) gyorgyf@48: gyorgyf@48: def read_logdata(self,file): gyorgyf@48: '''Read the log file''' gyorgyf@48: with open(file,'r') as fh: gyorgyf@48: lines = fh.readlines()[1000:] gyorgyf@48: for line in lines : gyorgyf@48: # x,y,ts = self.parse_logline(line) gyorgyf@48: x,y,ts = self.parse_logline_newformat(line) gyorgyf@48: self.data.append(DataPoint(x,y,ts)) gyorgyf@48: pass gyorgyf@48: gyorgyf@48: def parse_logline_newformat(self,line): gyorgyf@48: '''parse a single line of the logfile and convert it to the appropriate datatypes''' gyorgyf@48: x,y,ts = 0,0,0 gyorgyf@48: date,rest = line.split("]") gyorgyf@48: h,m,s = map(lambda x: int(x.strip()), date.split(":")[-3:]) gyorgyf@48: if self.first_date == None: gyorgyf@48: self.first_date = dt.datetime(1,1,1,h,m,s) gyorgyf@48: ts = dt.datetime(1,1,1,h,m,s) - self.first_date gyorgyf@48: ip,x,y,t = map(lambda x: x.strip(), rest.strip().split(",")) gyorgyf@48: # print ts,ip,x,y gyorgyf@48: return float(x),float(y),ts gyorgyf@48: gyorgyf@48: gyorgyf@48: def parse_logline(self,line): gyorgyf@48: '''parse a single line of the logfile and convert it to the appropriate datatypes''' gyorgyf@48: x,y,ts = 0,0,0 gyorgyf@48: date,rest = line.split("]") gyorgyf@48: h,m,s = map(lambda x: int(x.strip()), date.split(":")[-3:]) gyorgyf@48: if self.first_date == None: gyorgyf@48: self.first_date = dt.datetime(1,1,1,h,m,s) gyorgyf@48: ts = dt.datetime(1,1,1,h,m,s) - self.first_date gyorgyf@48: ip,x,y, = map(lambda x: x.strip(), rest.split("-")) gyorgyf@48: # print ts,ip,x,y gyorgyf@48: return float(x),float(y),ts gyorgyf@48: gyorgyf@48: def yield_data(self): gyorgyf@48: for d in self.data : gyorgyf@48: yield d gyorgyf@48: gyorgyf@48: def simulate(self): gyorgyf@48: deltas = map(lambda x:(x[1].ts-x[0].ts), zip(self.data,self.data[1:])) gyorgyf@48: # deltas = map(lambda x: x.seconds+(x.microseconds/1000000.0), deltas) gyorgyf@48: deltas = map(lambda x: x.seconds+r.random(), deltas) gyorgyf@48: # deltas = map(lambda x: x.seconds, deltas) gyorgyf@48: datagen = self.yield_data() gyorgyf@48: for d in deltas : gyorgyf@48: datapoint = datagen.next() gyorgyf@48: print datapoint gyorgyf@48: self.send_coordinate(datapoint.x, datapoint.y) gyorgyf@48: time.sleep(d) gyorgyf@48: pass gyorgyf@48: gyorgyf@48: def random(self): gyorgyf@48: '''Send random numbers''' gyorgyf@48: while True : gyorgyf@48: self.send_coordinate(r.random(),r.random()) gyorgyf@48: time.sleep(0.3) gyorgyf@48: gyorgyf@48: def send_coordinate(self,x,y): gyorgyf@48: self.conn.putrequest("GET","/moodconductor/mood?x=%(x)s&y=%(y)s" %locals(), skip_host=True) gyorgyf@48: self.conn.putheader("Host", "www.isophonics.net") gyorgyf@48: self.conn.endheaders() gyorgyf@48: res = self.conn.getresponse() gyorgyf@48: res.read() gyorgyf@48: print res.status, res.reason gyorgyf@48: print res.read() gyorgyf@48: gyorgyf@48: gyorgyf@48: def main(): gyorgyf@48: gyorgyf@48: s = Simulator() gyorgyf@48: # s.random() gyorgyf@48: # s.read_logdata("../performance2.log") gyorgyf@48: s.read_logdata("../performance-09Aug2013Barbican.log") gyorgyf@48: s.simulate() gyorgyf@48: gyorgyf@48: gyorgyf@48: gyorgyf@48: if __name__ == '__main__': gyorgyf@48: main() gyorgyf@48: