1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
25 import os, socket, struct, time
26
27
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
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
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
66 """Check if the AIPS TV server is running."""
67
68
69 try:
70 self._open();
71 except:
72 return False
73
74 self._close()
75 return True
76
78 """Alias for AIPSTV.running()."""
79
80 return self.running()
81
83 """Start the AIPS TV server."""
84
85
86 if self._lock_pid and self._server_pid:
87 raise RuntimeError, "the AIPS TV has already been started"
88
89
90 env = os.environ.copy()
91 env['TVDEV'] = 'TVDEV01'
92 env['TVDEV01'] = 'sssin:localhost'
93
94
95 file = env['LOAD'] + '/TVSERV.EXE'
96 self._lock_pid = os.spawnve(os.P_NOWAIT, file, ['TVSERVER'], env)
97
98
99 file = env['LOAD'] + '/XAS'
100 self._server_pid = os.spawnve(os.P_NOWAIT, file, ['XAS'], env)
101
102
103 time.sleep(2)
104 return
105
107 """Init the AIPS TV server."""
108
109 self._open()
110 self._send(15)
111 self._close()
112 return
113
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
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
135
136 time.sleep(2)
137
138 return
139
140 pass
141