Module AIPSTV
[hide private]
[frames] | no frames]

Source Code for Module AIPSTV

  1  # Copyright (C) 2005 Joint Institute for VLBI in Europe 
  2  # 
  3  # This program is free software; you can redistribute it and/or modify 
  4  # it under the terms of the GNU General Public License as published by 
  5  # the Free Software Foundation; either version 2 of the License, or 
  6  # (at your option) any later version. 
  7  # 
  8  # This program is distributed in the hope that it will be useful, 
  9  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 10  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 11  # GNU General Public License for more details. 
 12   
 13  # You should have received a copy of the GNU General Public License 
 14  # along with this program; if not, write to the Free Software 
 15  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 16   
 17  """ 
 18   
 19  This module provides the AIPSTV class.  This class makes it possible 
 20  to control the AIPS TV from Python. 
 21   
 22  """ 
 23   
 24  # Generic Python stuff. 
 25  import os, socket, struct, time 
 26   
 27   
28 -class AIPSTV(object):
29 - def __init__(self, host='localhost'):
30 self.host = host 31 self.port = socket.getservbyname('sssin', 'tcp') 32 self._lock_pid = 0 33 self._server_pid = 0 34 return
35
36 - def _send(self, opcode, dat1=0, dat2=0, dat3=0, dat4=0):
37 """Send command to the AIPS TV server.""" 38 39 s = struct.pack('!6h', opcode, dat1, dat2, dat3, dat4, 0) 40 self._socket.send(s) 41 if opcode == 12: 42 return 43 s = self._socket.recv(4) 44 (status,) = struct.unpack('!h2x', s) 45 if status: 46 msg = "AIPS TV returned %d", status 47 raise IOError, msg 48 return
49
50 - def _open(self):
51 """Open connection to the AIPS TV server.""" 52 53 self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 54 self._socket.connect((self.host, self.port)) 55 self._send(11) 56 return
57
58 - def _close(self):
59 """Close connection to the AIPS TV server.""" 60 61 self._socket.send(struct.pack('!6h', 12, 0, 0, 0, 0, 0)) 62 self._socket.close() 63 return
64
65 - def running(self):
66 """Check if the AIPS TV server is running.""" 67 68 # Check by opening a connection to the TV. 69 try: 70 self._open(); 71 except: 72 return False 73 74 self._close() 75 return True
76
77 - def exists(self):
78 """Alias for AIPSTV.running().""" 79 80 return self.running()
81
82 - def start(self):
83 """Start the AIPS TV server.""" 84 85 # Check if we already started the AIPS TV. 86 if self._lock_pid and self._server_pid: 87 raise RuntimeError, "the AIPS TV has already been started" 88 89 # Create an environment for the AIPS TV processes. 90 env = os.environ.copy() 91 env['TVDEV'] = 'TVDEV01' 92 env['TVDEV01'] = 'sssin:localhost' 93 94 # Start the AIPS TV lock daemon. 95 file = env['LOAD'] + '/TVSERV.EXE' 96 self._lock_pid = os.spawnve(os.P_NOWAIT, file, ['TVSERVER'], env) 97 98 # Start the AIPS TV server. 99 file = env['LOAD'] + '/XAS' 100 self._server_pid = os.spawnve(os.P_NOWAIT, file, ['XAS'], env) 101 102 # Wait until the TV server has been started. 103 time.sleep(2) 104 return
105
106 - def clear(self):
107 """Init the AIPS TV server.""" 108 109 self._open() 110 self._send(15) 111 self._close() 112 return
113
114 - def kill(self):
115 """Close down the AIPS TV server.""" 116 117 self._open() 118 self._socket.send(struct.pack('!6h', 18, 0, 0, 0, 0, 0)) 119 self._socket.close() 120 121 # Kill of the zombies. 122 waited = False 123 if self._lock_pid: 124 os.waitpid(self._lock_pid, 0) 125 self._lock_pid = 0 126 waited = True 127 pass 128 if self._server_pid: 129 os.waitpid(self._server_pid, 0) 130 self._server_pid = 0 131 waited = True 132 pass 133 if not waited: 134 # Take a nap to avoid confusing users with the output of 135 # the dying processes. 136 time.sleep(2) 137 138 return
139 140 pass # Class AIPSTV
141