2023-02-03 15:13:57 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
Importer Class
|
|
|
|
================
|
|
|
|
|
2023-10-11 10:06:01 +00:00
|
|
|
ZMQ Importer
|
2023-02-03 15:13:57 +00: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 10:06:01 +00:00
|
|
|
from lib.objects.Items import Item
|
|
|
|
|
2023-02-03 15:13:57 +00: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 14:24:28 +00:00
|
|
|
self.default_feeder_name = config_loader.get_config_str("Module_Mixer", "default_unnamed_feed_name")
|
|
|
|
|
2023-03-31 12:53:20 +00:00
|
|
|
addresses = config_loader.get_config_str('ZMQ_Global', 'address')
|
2023-04-04 12:12:23 +00:00
|
|
|
addresses = addresses.split(',')
|
2023-02-03 15:13:57 +00:00
|
|
|
channel = config_loader.get_config_str('ZMQ_Global', 'channel')
|
|
|
|
self.zmq_importer = ZMQImporters()
|
2023-03-31 12:53:20 +00:00
|
|
|
for address in addresses:
|
2023-04-04 12:12:23 +00:00
|
|
|
self.zmq_importer.add(address.strip(), channel)
|
2023-02-03 15:13:57 +00: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 14:24:28 +00:00
|
|
|
|
2023-10-11 10:06:01 +00:00
|
|
|
obj_id, gzip64encoded = message.split(' ', 1) # TODO ADD LOGS
|
2023-10-05 14:24:28 +00:00
|
|
|
splitted = obj_id.split('>>', 1)
|
2023-12-12 09:14:59 +00:00
|
|
|
if len(splitted) == 2:
|
2023-10-05 14:24:28 +00:00
|
|
|
feeder_name, obj_id = splitted
|
|
|
|
else:
|
|
|
|
feeder_name = self.default_feeder_name
|
|
|
|
|
2023-10-11 10:06:01 +00:00
|
|
|
obj = Item(obj_id)
|
|
|
|
# f'{source} {content}'
|
|
|
|
relay_message = f'{feeder_name} {gzip64encoded}'
|
2023-10-05 14:24:28 +00:00
|
|
|
|
|
|
|
print(f'feeder_name item::{obj_id}')
|
2023-10-11 10:06:01 +00:00
|
|
|
self.add_message_to_queue(obj=obj, message=relay_message)
|
2023-02-03 15:13:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
module = ZMQModuleImporter()
|
|
|
|
module.run()
|