forked from pratiknarang/peershark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateFlows.py
77 lines (63 loc) · 2.15 KB
/
GenerateFlows.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from P2P_CONSTANTS import *
from Packet import *
from Flow import *
import multiprocessing as MP
import socket
## module to read all the files in the data folder of the
## project, build flow data and store it in a file
def generateFlow(filename):
sem.acquire()
inputfile = open(filename)
data = [line.strip() for line in inputfile]
inputfile.close()
packetlist = []
for eachline in data:
fields = eachline.split(',')
fields.pop(2)
packetlist.append(Packet(fields))
outflowlist = packetsToFlows(packetlist, FLOWGAP)
print 'flows in ' + filename + ' : ' + str(len(outflowlist))
outfilename = FLOWDATADIR + (filename.split('/')[-1])
writeFlowsToFile(outflowlist, outfilename)
print 'done writing to : ' + outfilename
sem.release()
csvfiles = getCSVFiles(PCAPDATADIR)
print csvfiles
#create a semaphore so as not to exceed threadlimit
sem = MP.Semaphore(THREADLIMIT)
#generate flowdata from each input packet file(not pcap) in parallel and store it in a file
#so we get as many output files as number of input files
for filename in csvfiles:
task = MP.Process(target = generateFlow, args = (filename,))
task.start()
# #execute commands in parallel
# for task in tasklist:
# sem.acquire()
# task.start()
# #wait for tasks to finish
# for task in tasklist:
# task.join()
# allflows = [flow for flowlist in alloutflowlists for flow in flowlist]
# finalflows = []
# combineFlows(allflows, FLOWGAP, finalflows)
# outfile = open(C_FLOWOUTFILE, 'w')
# for flow in finalflows:
# outfile.write(socket.inet_ntoa(flow.ip1) + ',' +
# socket.inet_ntoa(flow.ip2) + ',' +
# str(flow.getNoOfPackets()) + ',' +
# str(flow.getNoOfBytes()) + ',' +
# '%.6f'%flow.getInterArrivaltime() + ',' +
# '%.6f'%flow.getStart() + ',' +
# '%.6f'%flow.getEnd() + ',' +
# '%.6f'%flow.getDurationInSeconds() + ',' +
# str(flow.n_packet1) + ',' +
# str(flow.n_byte1) + ',' +
# '%.6f'%flow.t_start1 + ',' +
# '%.6f'%flow.t_end1 + ',' +
# '%.6f'%flow.getInterArrivaltime1() + ',' +
# str(flow.n_packet2) + ',' +
# str(flow.n_byte2) + ',' +
# '%.6f'%flow.t_start2 + ',' +
# '%.6f'%flow.t_end2 + ',' +
# '%.6f'%flow.getInterArrivaltime2() + '\n')
# outfile.close()