All pastes #275518 Raw Edit

Mine

public text v1 · immutable
·published 2006-12-11 16:22 UTC
rendered paste body
import logging, os, sys, socket
def create_socket(host,port):
	
	try: 
		# create a socket
		# associate the socket with a port.
		sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		sock.bind((host, port))
		return sock

	except: 
		#sys.exit()
		return -1

		
''' Initialization of variables used'''


HOST = 'localhost' 			# can leave this blank on the server side
port =  40000				# check if this port is avaiable on the system.
MAX_PORT = port + 1000
MAX_MOUNT_POINTS = 5			# Currently 5 partitions can be mounted
MAX_XML_FILELEN = 1000000
#FILENAME = 'daemon_input'
#logging.basicConfig(level=logging.DEBUG,
#                   format='%(asctime)s %(levelname)s %(message)s',
#                    filename='/tmp/semfs_daemon.log',
#                    filemode='w')


''' creating the socket connection between daemon and driver'''

while port<=MAX_PORT:

	fs_socket = create_socket(HOST,port)
	if port == MAX_PORT and fs_socket == -1:
		logging.error('Cannot find a suitable port')
		sys.exit(1)
	elif fs_socket == -1:
		logging.error('Cannot Create the Socket by the Daemon at port '+ str(port))
		port += 1
		continue
	else:
		logging.info('Socket created by the daemon in port '+ str(port))	
		break
	
try:
	
	# accept "call" from client
	fs_socket.listen(MAX_MOUNT_POINTS)
	fs_driver, driver_address = fs_socket.accept()
	logging.info( 'client at ' + str(driver_address) + ' accepted ')

except:
	
	logging.error('Unable to accept the clients request')

''' Read a xml file from the socket '''

try:

	# read string from client (assumed here to be so short that one call to
	# recv() is enough), and make multiple copies (to show the need for the
	# "while" loop on the client side)
	
	'''f = open(FILENAME, 'wb')
	size = s.recv(HDR_SZ)
	size = struct.unpack(HDR, size)
 	while size > 0:
	 	data = s.recv(4096)
	 	if not data: 
	  		break
	 	size -= len(data)
	 	f.write(data)
	'''    
	
	data = fs_driver.recv(MAX_XML_FILELEN)
	logging.info(data)  					# debugging info

except:
	
	logging.error('Unable to read from the socket')
	#sys.exit()

''' Return the result to the driver'''

try:
	# the client will block until server sends)
	# Thexml should be parsed and the corresponding action should be taken
	
	data_return = '<data>ok</data>'
	''' 
	TODO
	Write the function data_return = daemon_process(xmldata)
	'''
	fs_driver.send(data_return)

except:
	
	logging.error('Unable to write the result to the driver')


#close the Connection
logging.info('Daemon Closing')
fs_driver.close()