Statistics
| Branch: | Revision:

peerstreamer-logs-analyzer / lib / experiment.py @ c0eac366

History | View | Annotate | Download (2.84 KB)

1
class Experiment:
2
        TIME_SENSIBILITY = 30
3

    
4
        def __init__(self):
5
                self.sessions = []
6

    
7
        def removeSessionByTime(self,session_time):
8
                for session in self.sessions:
9
                        if session.time < (session_time + 5) and session.time > (session_time-5):
10
                                self.sessions.remove(session)
11

    
12
        def getSession(self,time,creation=False):
13
                for session in self.sessions:
14
                        if ((session.time < (int(time)+Experiment.TIME_SENSIBILITY)) 
15
                        and (session.time > (int(time)-Experiment.TIME_SENSIBILITY))):
16
                                return session
17
                if creation:
18
                        session = Session(time)
19
                        self.sessions.append(session)
20
                        return session
21
                else:
22
                        return None
23

    
24
        def addr2hostname(self,addr,without_port=False):
25
                hostname = None        
26
                for session in self.sessions:
27
                        hostname = session.addr2hostname(addr.strip(),without_port)
28
                        if hostname:
29
                                break # I know it is horrible... stupid python!
30
                #print "hostname for addr "+addr+": "+str(hostname)
31
                return hostname
32

    
33
        def getPeer(self,hostname):
34
                for session in self.sessions:
35
                                peer = session.getPeer(hostname)
36
                                if peer:
37
                                                return peer
38
                return None
39

    
40
class Session:
41
        def __init__(self,time):
42
                self.time=int(time)
43
                self.peers = []
44

    
45
        def addPeer(self,peer):
46
                self.peers.append(peer)
47

    
48
        def getSource(self):
49
                for peer in self.peers:
50
                        if peer.is_source:
51
                                return peer
52
                return None
53

    
54
        def getPeer(self,hostname):
55
                for peer in self.peers:
56
                        if peer.hostname == hostname:
57
                                return peer
58
                return None
59

    
60
        def addr2hostname(self,addr,without_port=False):
61
                hostname = None
62
                for peer in self.peers :
63
                        if without_port and peer.address.split(':')[0] == addr:
64
                                hostname = peer.hostname
65
                        if peer.address == addr:
66
                                hostname = peer.hostname
67
                        if hostname:
68
                                break # I know it is horrible... stupid python!
69
                return hostname
70

    
71
class Peer:
72
        def __init__(self,hostname,source,dataframe):
73
                self.hostname = hostname
74
                self.is_source = source
75
                self.published = dataframe
76
                self.neighbours = None
77
                received = dataframe[dataframe['action'] == 'RECEIVED']
78
                sent = dataframe[dataframe['action'] == 'SENT']
79
                if len(received) > 0:
80
                        i = received.first_valid_index()
81
                        self.address = ''.join(received['receiver'][i])
82
                else:
83
                        if len(sent) > 0:
84
                                i = sent.first_valid_index()
85
                                self.address = ''.join(sent['sender'][i])
86
                        else:
87
                                self.address = 'unkwnon'
88

    
89
        def setNeighbours(self,neighs):
90
                self.neighbours = neighs
91

    
92
        def published_interval(self,begin_time,end_time):
93
                return self.published[(self.published['logTime'] > begin_time) & \
94
                                                                                       (self.published['logTime'] < end_time)]
95

    
96
        def published_interval_sec(self,begin_time,end_time):
97
                return self.published[(self.published['logTime'] > (begin_time*1000000)) & \
98
                                                                                       (self.published['logTime'] < (end_time*1000000))]
99

    
100
        def neighbours_interval_sec(self,begin_time,end_time):
101
                return self.neighbours[(self.neighbours['logTime'] > (begin_time*1000000)) & \
102
                        (self.neighbours['logTime'] < (end_time*1000000))]