Replace peer-based hidemmsi option by a global list of mmsi in /etc/ais/hidden
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Wed, 27 Apr 2011 16:29:24 +0000 (16:29 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Wed, 27 Apr 2011 16:29:24 +0000 (16:29 +0000)
Peer base option is now send_hidden to override.

bin/inputs/config.py
bin/inputs/outpeer.py

index e768fc32f70a46e1de7a09ff989cbc7e50ac9272..8c23e4343d33729605f2bfc1d11d2ceb8c1f748e 100644 (file)
@@ -7,12 +7,16 @@ from __future__ import division
 import logging
 import pprint
 from ais.ntools import str_split_column_ipv6
+from ais.common import strmmsi_to_mmsi
 
 __all__ = [
     'peers_get_config',
-    'source_get_infoin']
+    'source_get_infoin',
+    'get_hidden_mmsi',
+    ]
 
 CONFIG_FILENAME = '/etc/ais/config'
+HIDDENMMSI_FILENAME = '/etc/ais/hidden'
 
 SOURCES = {}
 
@@ -157,4 +161,25 @@ def source_get_infoin(*args):
             result.append(newmatch)
     return result
         
+__hidden_mmsi__ = None
+def get_hidden_mmsi():
+    global __hidden_mmsi__
+    if __hidden_mmsi__ is None:
+        __hidden_mmsi__ = []
+        try:
+            lines = file(HIDDENMMSI_FILENAME).read().split('\n')
+        except IOError, err:
+            if err.errno == 2: # No such file or directory
+                logging.info('No hidden ship')
+                return __hidden_mmsi__
+            else:
+                raise
+        for line in lines:
+            line = line.strip()
+            if len(line)==0 or line[0] == '#':
+                continue  # ignore empty lines & comments
+            mmsi = strmmsi_to_mmsi(line)
+            __hidden_mmsi__.append(mmsi)
+    return __hidden_mmsi__
+
 
index 0a479c559d8d83ba2352bd51bb000a93afd1007e..139fa76152d92300a98ceff76749a200f7f5a8e3 100644 (file)
@@ -10,7 +10,8 @@ import socket
 from threading import Thread, Lock
 
 from ais.ntools import formataddr
-from ais.common import strmmsi_to_mmsi
+from ais.common import strmmsi_to_mmsi, mmsi_to_strmmsi
+from ais.inputs.config import get_hidden_mmsi
 
 __all__ = [ 'OutPeers', 'outpeers_from_config', 'TcpInOutPeer', 'TcpInServiceOut', 'tcpin_outpeers' ]
 
@@ -34,13 +35,14 @@ class UdpOutPeer:
         self.timestamp_getaddrinfo = 0
         self._reinit_socket()
 
-        self.hidemmsi = []
+        self.send_hidden = False
         logging.warning('%s', options)
         for option in options:
-            if option.startswith('hidemmsi:'):
-                self.hidemmsi += option[len('hidemmsi:'):].split(',')
-        self.hidemmsi = [ strmmsi_to_mmsi(mmsi) for mmsi in self.hidemmsi ]
-        logging.warning('hidemmsi:%s', self.hidemmsi)
+            if option == 'send_hidden':
+                self.send_hidden = True
+            else:
+                loggin.critical('Unsupported option %s for UdpOutPeer' % option)
+                raise RuntimeError('Unsupported option %s for UdpOutPeer' % option)
 
     def __repr__(self):
         return '%s:%s' % (self.host, self.port)
@@ -217,15 +219,19 @@ class OutPeers:
         '''
         Send a line to all the output peers registered.
         '''
+        hidden_mmsi = get_hidden_mmsi()
+        #logging.info('hiddenmmsi: %s', get_hidden_mmsi())
         for outpeer in self.safe_iter():
-            if decoded_info and decoded_info != True:
-                try:
-                    hidemmsi = outpeer.hidemmsi
-                except AttributeError:
-                    hidemmsi = []
-                if decoded_info.mmsi in hidemmsi:
-                    logging.info('Hiding line for mmsi=%s', decoded_info.mmsi)
-                    continue
+            if not decoded_info:
+                continue
+            try:
+                send_hidden = outpeer.send_hidden
+            except AttributeError:
+                send_hidden = False
+            if decoded_info != True and decoded_info.mmsi in hidden_mmsi and not send_hidden:
+                logging.info('Hiding line for mmsi=%s', decoded_info.mmsi)
+                continue
+            
             logging.debug('OUT %s %s', outpeer, repr(line))
             outpeer.send_line(line, decoded_info)