1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
#!/usr/bin/env python
"""
pylib_tcp 0.1
Library supporting TCP servers, clients, and connections.
Copyright 2008 Stephan Schulz, schulz@eprover.org
This code is part of the support structure for the equational
theorem prover E. Visit
http://www.eprover.org
for more information.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program ; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston,
MA 02111-1307 USA
The original copyright holder can be contacted as
Stephan Schulz (I4)
Technische Universitaet Muenchen
Institut fuer Informatik
Boltzmannstrasse 3
Garching bei Muenchen
Germany
or via email (address above).
"""
import sys
import re
import string
import select
import os
import socket
import pylib_generic
import pylib_io
lineend_re = re.compile("\n|\r\n")
jobend_re = re.compile("(^|\n|\r\n)\.(\n|\r\n)")
class connection(object):
def __init__(self, conn, rec_end = lineend_re):
self.sock = conn[0]
self.peeradr = conn[1]
self.inbuffer = ""
self.outbuffer = ""
self.filenum = self.sock.fileno()
self.rec_end = rec_end
def __str__(self):
return "<conn (%d) to %s>" % (self.filenum, str(self.peeradr))
def fileno(self):
return self.filenum
def peer_adr(self):
return self.peeradr
def send(self):
if self.sendable() and not pylib_io.write_will_block(self):
res = 0
try:
res = self.sock.send(self.outbuffer)
except socket.error:
pass
if res > 0:
self.outbuffer = self.outbuffer[res:]
return res
return 0
def write(self, data):
"""
Add data to the write buffer and try to send it. Returns number
of bytes actually written. Any remaining characters are
buffered for future calls to send().
"""
self.outbuffer = self.outbuffer+data
return self.send()
def sendable(self):
return self.outbuffer!=""
def readable(self):
return self.filenum != -1
def recv(self):
try:
res = self.sock.recv(2048)
except socket.error:
res = ""
if res == "":
self.close()
return res
self.inbuffer = self.inbuffer+res
return res
def read(self):
"""
Try to read complete expressions separated by self.rec_end off
the connection. Returns a list [expr*], where each expr is
terminated by a rec_end instance. If any record is "", the
connection has been broken. An empty list just indicates that
no complete expression has been found yet.
"""
tmp = self.recv()
res = list()
mo = self.rec_end.search(self.inbuffer)
while mo:
res.append(self.inbuffer[:mo.start()]+"\n")
self.inbuffer = self.inbuffer[mo.end():]
mo = self.rec_end.search(self.inbuffer)
if tmp == "":
res.append("")
return res
def close(self):
if self.filenum!=-1:
self.sock.close()
self.outbuffer = ""
self.filenum = -1
class tcp_server(object):
"""
Class implementing the listening port of a server and
creating connected sockets.
"""
def __init__(self, port):
self.udpout = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
inst = None
while port < 65536:
try:
self.port = port
self.listen_sock.bind(("", port))
self.listen_sock.listen(5)
pylib_io.verbout("TCP Server listening on port "+str(self.port))
return
except socket.error, inst:
port = port+1
raise inst
def fileno(self):
"""
Return the servers fileno to support select.
"""
return self.listen_sock.fileno()
def accept(self,rec_end = lineend_re):
try:
connect = connection(self.listen_sock.accept(), rec_end)
except:
connect = None
return connect
class etcp_server(tcp_server):
"""
Class implementing a specialized E TCP server.
"""
def __init__(self, port, emark):
tcp_server.__init__(self, port)
tmp = pylib_io.run_shell_command("hostname")
try:
self.hostname = tmp[0].strip("\n")
except IndexError:
self.hostname = "<unknown>"
self.emark = emark
def announce_self(self, addr, ):
msg = "eserver:%d:%s:%f\n"%(self.port,self.hostname, self.emark)
pylib_io.verbout("Announcing: "+msg+" to "+str(addr))
self.udpout.sendto(msg ,0, addr)
class tcp_client(object):
"""
Class implementing a TCP client, creating a connection from a given
address.
"""
def __init__(self, rec_end = lineend_re):
self.rec_end = rec_end
def connect(self, address):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(address)
return connection((sock,address), self.rec_end)
class etcp_client(tcp_client):
"""
Class implementing an E-Server specific TCP client, creating a
connection from a given address and sending the init message.
"""
def __init__(self, port, emark):
tcp_client.__init__(self, jobend_re)
self.port = port
self.emark = emark
tmp = pylib_io.run_shell_command("hostname")
try:
self.hostname = tmp[0].strip("\n")
except IndexError:
self.hostname = "<unknown>"
self.emark = emark
def connect(self, address):
connection = tcp_client.connect(self, address)
if connection:
msg = "eserver:%d:%s:%f\n"%(self.port,self.hostname, self.emark)
pylib_io.verbout("Initial msg: "+msg+" send to "+str(address))
connection.write(msg)
return connection
|