-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblaster_srv.py
162 lines (145 loc) · 5.19 KB
/
blaster_srv.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
"""
Copyright (c) 2018, Masaru Natsu ([email protected])
"""
"""
https://github.com/Pandachan/blaster.git
"""
"""
License
this program is free software; you can redistribute it and/or modify
under the terms of the GNU General Public License as published by
the Free Sftware Fundation; either version 3 of the License, or
any later version.
This program is distributed in the hope it will bring me lots of food
and then we can become friends, but in the meantime, is stii useful
to use but WITHOUT ANY WARRANT; without even the implied warrant of
MERCHANTABILITY or FITNESS FOR PARTICULAR PURPOSE. Unless you give me
delicious food, then I can help you to debug, but I can't garantee
you that we will solve the problems nor solve any bugs, but at least
we will have a good meal.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Or, if you prefer, because this is the 21st Century, you can email
them at [email protected]
"""
##
# Python libraries to play with
import argparse
import os
import threading
import time
import struct
import socket
import sys
from queue import Queue
##
# Our own logger module.
from logger import *
##
# Get the file name used for the log and the pipe to share the data
fileName, extension = os.path.splitext(os.path.basename(__file__))
del(extension)
##
# Initialize the log, for the puspose of this program, no log no run.
try:
myLog = Logger(fileName)
except Exception as e:
print(str(e))
sys.exit(1)
# >>> End of try
class myFifo(object):
"""
This class will create and manage the Piped Fifo for the incoming data
"""
def __init__(self, fifoFile=None):
""" Class constructor, will use the provided file name to creat the FIFO pipe """
self.fifoFile = "/tmp/" + fifoFile
self.fifoHandler = None
self.thread = None
self.isRunning = False
self.__createFifo()
self.__start()
# >>> End of __init__
##
# Before the garbage collector kicks in, we stop teh thread and log the
# event.
def __del__(self):
""" This desctuctor method will make sure the threads are alldone before finishing """
self.__stop()
# >>> End of __del__
##
# This method will log the start of the FIFO process and create the
# main thread to keep the FIFO running
def __start(self):
""" Initiate the main thread that will ahdnle pushing data into the FIFO pipe """
myLog.Info("Start the FIFO Thread")
self.isRunning = True
self.thread = threading.Thread(target = self.__fifoRunner)
self.thread.daemon = True
self.thread.start()
# >>> End of __start
##
# Change the running flag and wait for the thread to finalize
def __stop(self):
""" Mark the thread as innactive and wait for it to finish all its processing """
myLog.Info("Stop the FIFO Thread")
self.isRunning = False
self.thread.join() ## Block until the thread stop running.
# >>> End of __stop
def __createFifo(self):
""" Open the FIFO pipe in the tmp file system """
try:
os.mkfifo(self.fifoFile)
myLog.Info("Make the file {0} for the fifo".format(self.fifFile))
except OSError as e:
if (e.errno == errno.EEXIST):
myLog.Warning("File {0} already exist".format(self.fifoFile))
else: raise
# >>> End of try-except
# >>> End of __createFifo
def __openFifo(self, keepAlive = False):
try:
fileDescriptor = os.open(self.fifoFile, os.O-NONBLOCK | os.O_WRONLY)
self.fifoHandler = os.fdopen(fileDescriptor, "w")
except OSError as e:
if (e.errno == errno.ENXIO):
self.fifoHandler = None
if not keepAlive:
myLog.Error("Pipe not read")
# >>> End of if not...
else: raise
# >>> End of if
# >>> End of try-except
# >>> End of __openFifo
def __fifoRunner(self):
while self.isRunning:
self.__openFifo(keepAlive = True)
time.sleep(0.05)
# >>> End of while...
# >>> End of __fifoRunner
def pipe(self, length = 0, data = None):
if not length:
myLog.Warning("Data length 0 is not allowed")
return False
if not data:
myLog.Error("Data mut be provided")
return False
if not self.fifoHandler:
self.__openFifo()
if not self.fifoHandler:
myLog.Error("Unable to open the FIFO Pipe")
return False
try:
self.fifoHandler.write(data)
self.fifoHanlder.flush()
myLog.Info("{0} bytes have been pushed to the FIFO Pipe".format(length))
except IOError as e:
if e.errno == errno.EPIPE:
myLog.Warning("Pipe not being read")
self.fifoHandler = None
else: raise
# >>> End of try-except
# >>> End of pipe
# >>> End of class myFifo