00001 #!/usr/local/bin/python
00002 #
00003 # Copyright 2004
00004 # by
00005 # The Board of Trustees of the
00006 # Leland Stanford Junior University.
00007 # All rights reserved.
00008 #
00009
00010
00011 __facility__ = "Online"
00012 __abstract__ = "Loopback mode handler class"
00013 __author__ = "Amedeo Perazzo <perazzo@slac.stanford.edu> SLAC - GLAST I&T/Online"
00014 __date__ = ("$Date: 2004/08/24 22:57:25 $").split(' ')[1]
00015 __version__ = "$Revision: 2.2 $"
00016 __release__ = "$Name: P04-06-05 $"
00017 __credits__ = "SLAC"
00018
00019 import socket
00020 import threading
00021 import struct
00022
00023 class Loopback(object):
00024 def __init__(self):
00025 self.__lock = threading.Lock()
00026 self.__lock.acquire()
00027 self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
00028 self.__sock.bind(('localhost', 0))
00029 self.__sock.connect(self.__sock.getsockname())
00030
00031 def socket(self):
00032 return self.__sock
00033
00034 def commit(self, value):
00035 packet = struct.pack('I', value)
00036 self.__sock.send(packet)
00037 self.__lock.acquire()
00038
00039 def acknowledge(self):
00040 packet = self.__sock.recv(4)
00041 self.__lock.release()
00042 value = struct.unpack('I', packet)
00043 return value[0]