1 # -*- coding: utf-8 -*-
6 from __future__ import division
9 from ais.ntools import str_split_column_ipv6
10 from ais.common import strmmsi_to_mmsi
18 CONFIG_FILENAME = '/etc/ais/config'
19 HIDDENMMSI_FILENAME = '/etc/ais/hidden'
23 __source_normalized__ = False
24 def peers_get_config():
26 SOURCES normalization & checks
28 global __source_normalized__
29 if __source_normalized__:
32 config_file = file(CONFIG_FILENAME)
33 for lineno_e, line in enumerate(config_file.readlines()):
35 #logging.debug('considering line #%s: %s', lineno, repr(line))
36 line = line.strip(' \r\n')
37 if len(line)==0 or line[0]=='#':
38 logging.debug('ignoring line #%s: %s', lineno, repr(line))
40 spl = line.split(' ', 2)
41 assert len(spl) == 3, \
42 'line #%s: There should be at least 3 strings sepatated by spaces' % \
44 id4, io, definition = spl
45 assert len(id4)==4, 'source name (first parameter) must be 4 char long.'
46 logging.debug('%s %s %s', id4, io, definition)
47 if id4 not in SOURCES:
51 'line #%s: missing information after \'in\'' % \
53 SOURCES[id4]['in'] = definition
56 'line #%s: missing information after \'out\'' % \
58 if not SOURCES[id4].has_key('out'):
59 SOURCES[id4]['out'] = []
60 spl = definition.split(' ')
61 SOURCES[id4]['out'].append(spl)
63 SOURCES[id4]['name'] = definition
65 raise AssertionError( \
66 "line #%s: second parameter must be either 'in' or 'out'.\r%s" % \
69 logging.debug('SOURCES \n%s', pprint.pformat(SOURCES))
71 logging.debug('SOURCES (raw):\n%s', pprint.pformat(SOURCES))
72 for id4, settings in SOURCES.iteritems():
73 assert len(id4) == 4, \
74 'Invalid ID %s in SOURCES. It should be 4 characters long.'
76 if 'name' not in settings:
83 assert type(in_) == str, \
84 "SOURCES['%s']['in'] should be of type str, not %s'." % \
86 in_ = settings.get('in')
87 spl = str_split_column_ipv6(in_)
88 protocol, parameters = spl[0], spl[1:]
90 "SOURCES['%s']['in'] should contain parameters after %s" % \
94 assert len(parameters) == 4, \
95 "SOURCES['%s']['in'] udp: protocol requires 4 parameters." % \
97 host_local = parameters[0]
100 port_local = parameters[1]
101 assert port_local.isdigit(), \
102 "SOURCES['%s']['in'] udp: illegal local port" % \
104 port_local = int(port_local)
105 host_remote = parameters[2]
106 if host_remote == '*':
108 port_remote = parameters[3]
109 source_port_discreminate = False
110 if port_remote == '*':
112 elif port_remote == '!':
114 source_port_discreminate = True
116 assert port_remote.isdigit(), \
117 "SOURCES['%s']['in'] udp: illegal remote port" % \
119 port_remote = int(port_remote)
121 SOURCES[id4]['in'] = (protocol.title(), host_local, port_local, host_remote, port_remote, source_port_discreminate)
123 elif protocol == 'serial':
124 assert len(parameters) == 1, \
125 "SOURCES['%s']['in'] serial: protocol requires 1 parameter." % \
127 SOURCES[id4]['in'] = tuple(['Serial'] + parameters)
129 elif protocol == 'tcpout':
130 assert len(parameters) == 2, \
131 "SOURCES['%s']['in'] tcpout: protocol requires 2 parameters." % \
134 port = int(parameters[1])
135 SOURCES[id4]['in'] = ('TcpOut', host, port)
139 "Unsupported source protocol '%s' for SOURCES['%s']: %s",
141 __source_normalized__ = True
142 logging.debug('SOURCES (normalised):\n%s', pprint.pformat(SOURCES))
147 def source_get_infoin(*args):
149 Gets a list of matching info in SOURCES[*]['in']:
150 example: get_source_infoin('Udp', '', 1234)
151 might return [ ['SRC1', '12.23.34.45', '', False],
152 ['SRC2', '98.87.76.65', '', True]]
155 for id4, source_info in peers_get_config().iteritems():
156 rawin = source_info.get('in', None)
159 if args == rawin[:len(args)]:
160 newmatch = [id4] + [rawin[i] for i in range(len(args), len(rawin))]
161 result.append(newmatch)
164 __hidden_mmsi__ = None
165 def get_hidden_mmsi():
166 global __hidden_mmsi__
167 if __hidden_mmsi__ is None:
170 lines = file(HIDDENMMSI_FILENAME).read().split('\n')
172 if err.errno == 2: # No such file or directory
173 logging.info('No hidden ship')
174 return __hidden_mmsi__
179 if len(line)==0 or line[0] == '#':
180 continue # ignore empty lines & comments
181 mmsi = strmmsi_to_mmsi(line)
182 __hidden_mmsi__.append(mmsi)
183 return __hidden_mmsi__