2023-02-03 16:13:57 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
Importer Class
|
|
|
|
================
|
|
|
|
|
2023-10-11 12:06:01 +02:00
|
|
|
ZMQ Importer
|
2023-02-03 16:13:57 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import zmq
|
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from importer.abstract_importer import AbstractImporter
|
|
|
|
from modules.abstract_module import AbstractModule
|
|
|
|
from lib.ConfigLoader import ConfigLoader
|
|
|
|
|
2023-10-11 12:06:01 +02:00
|
|
|
from lib.objects.Items import Item
|
|
|
|
|
2023-02-03 16:13:57 +01:00
|
|
|
class ZMQImporters(AbstractImporter):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.subscribers = []
|
|
|
|
# Initialize poll set
|
|
|
|
self.poller = zmq.Poller()
|
|
|
|
|
|
|
|
def add(self, address, channel):
|
|
|
|
context = zmq.Context()
|
|
|
|
subscriber = context.socket(zmq.SUB)
|
|
|
|
r = subscriber.connect(address)
|
|
|
|
print(r)
|
|
|
|
subscriber.setsockopt_string(zmq.SUBSCRIBE, channel)
|
|
|
|
self.subscribers.append(subscriber)
|
|
|
|
|
|
|
|
self.poller.register(subscriber, zmq.POLLIN)
|
|
|
|
|
|
|
|
def importer(self, timeout=None): # -> FOR loop required
|
|
|
|
"""
|
|
|
|
:param timeout: The timeout (in milliseconds) to wait for an event.
|
|
|
|
If unspecified (or specified None), will wait forever for an event.
|
|
|
|
:returns: messages generator
|
|
|
|
"""
|
|
|
|
for event in self.poller.poll(timeout=timeout):
|
|
|
|
socket, event_mask = event
|
|
|
|
# DEBUG
|
|
|
|
print(socket, event_mask)
|
|
|
|
yield socket.recv()
|
|
|
|
|
|
|
|
|
|
|
|
class ZMQModuleImporter(AbstractModule):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
config_loader = ConfigLoader()
|
2023-10-05 16:24:28 +02:00
|
|
|
self.default_feeder_name = config_loader.get_config_str("Module_Mixer", "default_unnamed_feed_name")
|
|
|
|
|
2023-03-31 14:53:20 +02:00
|
|
|
addresses = config_loader.get_config_str('ZMQ_Global', 'address')
|
2023-04-04 14:12:23 +02:00
|
|
|
addresses = addresses.split(',')
|
2023-02-03 16:13:57 +01:00
|
|
|
channel = config_loader.get_config_str('ZMQ_Global', 'channel')
|
|
|
|
self.zmq_importer = ZMQImporters()
|
2023-03-31 14:53:20 +02:00
|
|
|
for address in addresses:
|
2023-04-04 14:12:23 +02:00
|
|
|
self.zmq_importer.add(address.strip(), channel)
|
2023-02-03 16:13:57 +01:00
|
|
|
|
|
|
|
def get_message(self):
|
|
|
|
for message in self.zmq_importer.importer():
|
|
|
|
# remove channel from message
|
|
|
|
yield message.split(b' ', 1)[1]
|
|
|
|
|
|
|
|
def compute(self, messages):
|
|
|
|
for message in messages:
|
|
|
|
message = message.decode()
|
2023-10-05 16:24:28 +02:00
|
|
|
|
2023-10-11 12:06:01 +02:00
|
|
|
obj_id, gzip64encoded = message.split(' ', 1) # TODO ADD LOGS
|
2023-10-05 16:24:28 +02:00
|
|
|
splitted = obj_id.split('>>', 1)
|
2023-12-12 10:14:59 +01:00
|
|
|
if len(splitted) == 2:
|
2023-10-05 16:24:28 +02:00
|
|
|
feeder_name, obj_id = splitted
|
|
|
|
else:
|
|
|
|
feeder_name = self.default_feeder_name
|
|
|
|
|
2023-10-11 12:06:01 +02:00
|
|
|
obj = Item(obj_id)
|
|
|
|
# f'{source} {content}'
|
|
|
|
relay_message = f'{feeder_name} {gzip64encoded}'
|
2023-10-05 16:24:28 +02:00
|
|
|
|
|
|
|
print(f'feeder_name item::{obj_id}')
|
2023-10-11 12:06:01 +02:00
|
|
|
self.add_message_to_queue(obj=obj, message=relay_message)
|
2023-02-03 16:13:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
module = ZMQModuleImporter()
|
|
|
|
module.run()
|