]> git.0d.be Git - django-panik-nonstop.git/commitdiff
use low-level socket module to manipulate soma
authorFrédéric Péters <fpeters@0d.be>
Mon, 6 Jan 2020 11:12:59 +0000 (12:12 +0100)
committerFrédéric Péters <fpeters@0d.be>
Mon, 6 Jan 2020 11:12:59 +0000 (12:12 +0100)
(required as pysoma won't ever be ported to python 3)

nonstop/utils.py
nonstop/views.py

index d3d71dbef1e30310777313eaaf678540411d545d..c6067b90364f5e62eb5376786eb6f8452d6bab14 100644 (file)
@@ -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')
index 5981eab08f9bf0c91533ddf076ee27625e7e314c..1d86142b57d8ec6898e1e44066d8aac86cac768c 100644 (file)
@@ -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={