-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.py
More file actions
74 lines (62 loc) · 2.47 KB
/
buffer.py
File metadata and controls
74 lines (62 loc) · 2.47 KB
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
import time
import os
import errno
class Buffer(object):
_first_pipe_location = "/home/clyde/snmptrapd.log"
_second_pipe_location = "../buffer"
_first_pipe = None
#_second_pipe = None
#_second_pipe_fd = os.open("/dev/null",os.O_WRONLY)
_second_pipe_writable = False
def __init__(self):
try:
self._first_pipe = open(self._first_pipe_location, 'r')
except Exception as e:
print e
print "ERROR: Can\'t open first pipe file"
self._open_second_pipe()
def _open_second_pipe(self):
try:
self._second_pipe_fd = os.open(self._second_pipe_location, os.O_WRONLY|os.O_NONBLOCK)
except OSError as e:
print e
print "ERROR: Can\'t open second pipe file"
if e.errno == errno.ENXIO:
return
else:
self._second_pipe_writable = True
def run(self):
while(True):
try:
data_line = self._first_pipe.readline()
#with open(self._first_pipe_location, 'r') as first_pipe:
# data_line = first_pipe.readline()
except Exception as e:
print e
print "ERROR: Can\'t read line from first pipe file"
time.sleep(1)
continue
else:
if data_line.strip() != '':
if self._second_pipe_writable is True:
try:
#with os.fdopen(os.open(self._second_pipe_location, os.O_WRONLY|os.O_NONBLOCK)) as second_pipe:
#self._second_pipe.write(data_line)
os.write(self._second_pipe_fd, data_line)
except OSError as e:
print "ERROR: Can\'t Write line to second pipe file"
print e
if e.errno == errno.EPIPE:
self._second_pipe_writable = False
os.close(self._second_pipe_fd)
print "Missed Data: " + data_line
continue
else:
print "Transfer data: " + data_line
# if second pipe file is not writable
else:
print "Missed Data: " + data_line
self._open_second_pipe()
if __name__ == '__main__':
pbuffer = Buffer()
pbuffer.run()