Passed
Push — master ( 412f03...3b6ffe )
by Ian
05:40
created

build.rsudp.c_forward.Forward.run()   F

Complexity

Conditions 15

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 39
rs 2.9998
c 0
b 0
f 0
cc 15
nop 1

How to fix   Complexity   

Complexity

Complex classes like build.rsudp.c_forward.Forward.run() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import os, sys
2
import socket as s
3
from rsudp import printM, printW, printE
4
import rsudp.raspberryshake as rs
5
6
class Forward(rs.ConsumerThread):
7
	"""
8
	Single-destination data forwarding. This consumer reads
9
	queue messages from the :class:`rsudp.c_consumer.Consumer`
10
	and forwards those messages to a specified address and port.
11
12
	.. versionadded:: 1.0.2
13
14
		The option to choose whether to forward either data or alarms or both
15
		(find boolean settings :code:`"fwd_data"` and :code:`"fwd_alarms"` in
16
		settings json files built by this version and later).
17
18
	:param str addr: IP address to pass UDP data to
19
	:param str port: network port to pass UDP data to (at specified address)
20
	:param bool fwd_data: whether or not to forward raw data packets
21
	:param bool fwd_alarms: whether or not to forward :code:`ALARM` and :code:`RESET` messages
22
	:param cha: channel(s) to forward. others will be ignored.
23
	:type cha: str or list
24
	:param queue.Queue q: queue of data and messages sent by :class:`rsudp.c_consumer.Consumer`
25
	"""
26
27
	def __init__(self, addr, port, fwd_data, fwd_alarms, cha, q):
28
		"""
29
		Initializes data forwarding module.
30
		
31
		"""
32
		super().__init__()
33
34
		self.sender = 'Forward'
35
		self.queue = q
36
		self.addr = addr
37
		self.port = port
38
		self.fwd_data = fwd_data
39
		self.fwd_alarms = fwd_alarms
40
		self.chans = []
41
		cha = rs.chns if (cha == 'all') else cha
42
		cha = list(cha) if isinstance(cha, str) else cha
43
		l = rs.chns
44
		for c in l:
45
			n = 0
46
			for uch in cha:
47
				if (uch.upper() in c) and (c not in str(self.chans)):
48
					self.chans.append(c)
49
				n += 1
50
		if len(self.chans) < 1:
51
			self.chans = rs.chns
52
		self.running = True
53
		self.alive = True
54
55
		printM('Starting.', self.sender)
56
57
58
	def _exit(self):
59
		"""
60
		Exits the thread.
61
		"""
62
		self.alive = False
63
		printM('Exiting.', self.sender)
64
		sys.exit()
65
66
67
	def run(self):
68
		"""
69
		Gets and distributes queue objects to another address and port on the network.
70
		"""
71
		printM('Opening socket...', sender=self.sender)
72
		socket_type = s.SOCK_DGRAM if os.name in 'nt' else s.SOCK_DGRAM | s.SO_REUSEADDR
73
		sock = s.socket(s.AF_INET, socket_type)
74
75
		msg_data = '%s data' % (self.chans) if self.fwd_data else ''
76
		msg_and = ' and ' if (self.fwd_data and self.fwd_alarms) else ''
77
		msg_alarms = 'ALARM / RESET messages' if self.fwd_alarms else ''
78
79
		printM('Forwarding %s%s%s to %s:%s' % (msg_data, msg_and, msg_alarms, self.addr,
80
											   self.port), sender=self.sender)
81
82
		try:
83
			while self.running:
84
				p = self.queue.get()	# get a packet
85
				self.queue.task_done()	# close the queue
86
87
				if 'TERM' in str(p):	# shutdown if there's a TERM message on the queue
88
					self._exit()
89
90
				if 'IMGPATH' in str(p):
91
					continue
92
93
				if ('ALARM' in str(p)) or ('RESET' in str(p)):
94
					if self.fwd_alarms:
95
						sock.sendto(p, (self.addr, self.port))
96
					continue
97
98
				if "{'" in str(p):
99
					if (self.fwd_data) and (rs.getCHN(p) in self.chans):
100
						sock.sendto(p, (self.addr, self.port))
101
102
		except Exception as e:
103
			self.alive = False
104
			printE('%s' % e, sender=self.sender)
105
			sys.exit(2)
106
107