use LATClientException not just Exception
[uccvend-vendserver.git] / sql-edition / servers / LATClient.py
index ead307a..b7818de 100644 (file)
@@ -1,4 +1,7 @@
 from socket import *
 from socket import *
+from select import select
+from os import popen
+from time import sleep
 
 LATCP_SOCKET = '/var/run/latlogin'
 
 
 LATCP_SOCKET = '/var/run/latlogin'
 
@@ -7,9 +10,17 @@ LATCP_CMD_VERSION = 8
 LATCP_CMD_TERMINALSESSION = 26
 LATCP_CMD_ERRORMSG = 99
 
 LATCP_CMD_TERMINALSESSION = 26
 LATCP_CMD_ERRORMSG = 99
 
+class LATClientException(Exception): pass
+
 class LATClient:
 class LATClient:
-       def __init__(self, service = None, node = None, port = None, \
-                       localport = None, password = None, is_queued = False):
+       def __init__(self, service = None, node = None, port = None,
+                    localport = None, password = None, is_queued = False,
+                    server_name = '', connect_password='', priv_password=''):
+
+               self.server_name = server_name
+               self.connect_password = connect_password
+               self.priv_password = priv_password
+               
                self.sock = socket(AF_UNIX, SOCK_STREAM, 0);
                self.sock.connect(LATCP_SOCKET)
                self.send_msg(LATCP_CMD_VERSION, LAT_VERSION+'\000')
                self.sock = socket(AF_UNIX, SOCK_STREAM, 0);
                self.sock.connect(LATCP_SOCKET)
                self.send_msg(LATCP_CMD_VERSION, LAT_VERSION+'\000')
@@ -33,7 +44,14 @@ class LATClient:
                         ))
                (cmd, msg) = self.read_reply()
                if ord(cmd) == LATCP_CMD_ERRORMSG:
                         ))
                (cmd, msg) = self.read_reply()
                if ord(cmd) == LATCP_CMD_ERRORMSG:
-                       raise Exception('LATClient: '+msg)
+                       raise LATClientException(msg)
+
+               r = select([self.sock], [], [], 2.0)[0]
+               if r:
+                       l = self.sock.readline()
+                       if l.find('Service in use') >= 0:
+                               print "Service in use, apparently: restarting DEC server"
+                               self.reboot_server()
 
        def __del__(self):
                try:
 
        def __del__(self):
                try:
@@ -46,6 +64,20 @@ class LATClient:
        def send_msg(self, cmd, msg):
                self.sock.send('%c%c%c%s'%(cmd, len(msg)/256, len(msg)%256, msg))
 
        def send_msg(self, cmd, msg):
                self.sock.send('%c%c%c%s'%(cmd, len(msg)/256, len(msg)%256, msg))
 
+       def reboot_server(self):
+               self.sock.shutdown(2)
+               self.sock.close()
+               mop = popen('/usr/sbin/moprc '+self.server_name, 'w')
+               mop.write('\n')
+               mop.write(self.connect_password+'\n')
+               mop.write('grim reaper\n')
+               mop.write('set priv\n')
+               mop.write(self.priv_password+'\n')
+               mop.write('init del 0\n')
+               mop.close()
+               print "Waiting 10 seconds for DEC server to come back to life..."
+               sleep(10)
+
        def read_reply(self):
                head = self.sock.recv(3)
                if len(head) != 3:
        def read_reply(self):
                head = self.sock.recv(3)
                if len(head) != 3:

UCC git Repository :: git.ucc.asn.au