[Tutor] bind problem

Kevin python_newbie at vedorian.com
Wed Sep 15 20:23:14 CEST 2004


Skipped content of type multipart/alternative-------------- next part --------------
#!/usr/local/bin/python
#
# mmaker.py - main program stuff
#
#



SERVER_PORT = 4000



import sys
import time


import sServer

import mWorld


#
# Main Program
#

print "Starting MUD Maker Server"

if len(sys.argv) > 1:
    port = int(eval(sys.argv[1]))
else:
    port = SERVER_PORT



world = mWorld.World()

world.boot()

server = sServer.Server(port, world)


#
# Main Loop
#



print "Waiting for connections"

lastSec = time.time()
while world._running:
#   try:
	time0 = time.time()

	server.checkConnections(0.1)
	server.processConnections()

	
	if time0 - lastSec >= 1: # 1 second
	    lastSec = time0
	    world.processTimers()

	time1 = time.time()

	# sleep the time that was left to fill 1 cycle
	wait = 0.1 - (time1 - time0)
	if wait > 0:
#	    print 'waiting extra free time ', wait
	    time.sleep(wait)


#    except KeyboardInterrupt:
#	print "Got KeyboardInterrupt: shutting down."
#	server.shutdown()
#	break


print "Shutting down."
-------------- next part --------------
# server.py - handles client connections and related stuff
#
#
#


from socket import *
import select

import sConnection



class Server:

    def __init__(self, port, world):
	self._sock = socket(AF_INET, SOCK_STREAM)
	#
	# Relationships
	#
	self._connections = {} # Connection
	#
	# Other Data
	#
	self._world = world # World

	self._descriptors = []

	self._sock.bind('', port)
	self._sock.setblocking(0)
	self._sock.listen(5)


    def shutdown(self):
	self._sock.close()
	for c in self._connections.items():
	    _, con = c
	    con.destroy()


    def removeConnection(self, conn):
	self._descriptors.remove(conn._fd)
	del self._connections[conn._fd]


    def closeConnection(self, conn):
	print 'closing connection'

	self.removeConnection(self, conn)
	conn.destroy()


    def checkConnections(self, timeout):

	fd = self._sock.fileno()

	ind, outd, exd = select.select(self._descriptors + [fd], \
			    self._descriptors, self._descriptors, timeout)

	# accept new connection
	if fd in ind:
	    print 'new connection'
	    new = self._sock.accept()
	    conn = sConnection.Connection(self, new, self._world)
	    self._descriptors.append(conn._fd)
	    self._connections[conn._fd] = conn
	    ind.remove(fd)

	# read inputs
	for f in ind:
	    if self._connections[f].receiveInput() < 0:
		# connection was closed
		self.closeConnection(self, f)

	# send outputs
	for f in self._descriptors:
	    self._connections[f].setCanSendOutput(f in outd)

	# close other stuff
	for f in exd:
	    self.closeConnection(self, f)


    def processConnections(self):
	for item in self._connections.items():
	    _, client = item
	    if client.checkStatus():
		cmdline = client.getNextInput()
		if cmdline != None:
		    if cmdline == '':
			client.putPrompt()
		    else:
			if client.handleInput:
			    client.handleInput(client, cmdline)
			else:
			    self._world.execute(client._player, cmdline, 1)
		    client.putPrompt()

	    client.flush()



More information about the Tutor mailing list