From: Frédéric Péters Date: Mon, 6 Jan 2020 11:12:59 +0000 (+0100) Subject: use low-level socket module to manipulate soma X-Git-Tag: v2021~267 X-Git-Url: https://git.0d.be/?p=django-panik-nonstop.git;a=commitdiff_plain;h=bda13f3e66c782efd5e5c53b91794f2c7c644af0 use low-level socket module to manipulate soma (required as pysoma won't ever be ported to python 3) --- diff --git a/nonstop/utils.py b/nonstop/utils.py index d3d71db..c6067b9 100644 --- a/nonstop/utils.py +++ b/nonstop/utils.py @@ -1,19 +1,19 @@ import datetime import os import shutil +import socket import time from django.template import loader import xml.etree.ElementTree as ET -try: - import pysoma -except ImportError: - pysoma = None - from .models import Track, SomaLogLine, LOCAL_BASE_PATH +class SomaException(Exception): + pass + + def get_current_nonstop_track(): try: soma_log_line = SomaLogLine.objects.select_related().order_by('-play_timestamp')[0] @@ -78,19 +78,38 @@ def add_diffusion(diffusion): palinsesti = palinsesti_template.render(context) palinsesti_xml = ET.fromstring(palinsesti.encode('utf-8')) - # append to soma - if pysoma: - connection = pysoma.open_tcp('soma.panik', 12521, '', 0) - palinsesto_xml = ET.fromstring(connection.get_palinsesto()) - palinsesto_xml.append(palinsesti_xml) - with open('/tmp/soma.pl', 'w') as fd: - fd.write(ET.tostring(palinsesto_xml)) - - connection.new_palinsesto('/tmp/soma.pl') - del connection - - # give it some time (...) - time.sleep(3) - connection = pysoma.open_tcp('soma.panik', 12521, '', 0) - connection.set_default_palinsesto() - del connection + # add to soma + def soma_connection(): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect(('soma', 12521)) + s.recv(1024) # -> b'soma 2.5 NO_SSL\n' + s.sendall(b'100 - Ok\n') + s.recv(1024) # -> b'100 - Welcome to soma daemon\n' + s.sendall(b'\n') # (empty password) + if s.recv(1024) == b'100 - Ok\n': + raise SomaException('failed to initialize soma connection') + return s + + with soma_connection() as s: + s.sendall(b'109 - Get the current palinsesto\n') + palinsesto_bytes = b'' + while True: + new_bytes = s.recv(200000) + if not new_bytes: + break + palinsesto_bytes += new_bytes + + palinsesto_xml = ET.fromstring(palinsesto_bytes) + palinsesto_xml.append(palinsesti_xml) + with soma_connection() as s: + s.sendall(b'106 - Switch to a New Palinsesto Request\n') + if s.recv(1024) == b'100 - Ok\n': + raise SomaException('failed to switch palinsesto') + s.sendall(ET.tostring(palinsesto_xml).encode('utf-8')) + + # give it some time (...) + time.sleep(3) + with soma_connection() as s: + s.sendall(b'122 - Set the current Palinsesto as Default\n') + if s.recv(1024) == b'100 - Ok\n': + raise SomaException('failed to set current palinsesto as default') diff --git a/nonstop/views.py b/nonstop/views.py index 5981eab..1d86142 100644 --- a/nonstop/views.py +++ b/nonstop/views.py @@ -359,6 +359,8 @@ class AddDiffusionView(DetailView): utils.add_diffusion(diffusion) except utils.DuplicateDiffusionSlot: messages.error(self.request, _('soma slot already in use, the diffusion could not be added.')) + except utils.SomaException as e: + messages.error(self.request, _('technical soma error (%s)') % e) else: messages.info(self.request, _('%s added to soma') % episode.emission.title) return HttpResponseRedirect(reverse('episode-view', kwargs={