"""Main loop for the MUFF Engine"""
-# Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-#
-# - Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
-# - Redistributions in binary form must reproduce the above
-# copyright notice, this list of conditions and the following
-# disclaimer in the documentation and/or other materials provided
-# with the distribution.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-# POSSIBILITY OF SUCH DAMAGE.
+# Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>, all rights reserved.
+# Licensed per terms in the LICENSE file distributed with this software.
+# string.strip is used to clean up leading/trailing whitespace in user input
import string
+
+# time.sleep is used in the loop to save cpu and provide crude pulse timing
import time
+# hack to load all modules in the muff package
import muff
for module in muff.__all__:
exec("import " + module)
def main():
+ """The main loop."""
+
+ # loop indefinitely while the world is not flagged for termination
while not muffvars.terminate_world:
- if not muffvars.newsocket:
- muffsock.initialize_server_socket()
+
+ # open the listening socket if it hasn't been already
+ if not muffvars.newsocket: muffsock.initialize_server_socket()
+
+ # pause for a configurable amount of time (decimal seconds)
time.sleep(muffconf.config_data.getfloat("general", "increment"))
+
+ # the world was flagged for a reload of all code/data
if muffvars.reload_modules:
+
+ # reload the muff package
reload(muff)
+
+ # reload all modules listed in the muff package
for module in muff.__all__:
exec("reload(muff." + module + ")")
- muffvars.reload_modules = 0
+
+ # reset the reload flag
+ muffvars.reload_modules = False
+
+ # assign a user if a new connection is waiting
user = muffsock.check_for_connection(muffvars.newsocket)
+
+ # there was a new connection
if user:
+
+ # welcome to the user list
muffvars.userlist.append(user)
+
+ # make a note of it
+ # TODO: need to log this crap
print len(muffvars.userlist),"connection(s)"
+
+ # iterate over the connected users
for each_user in muffvars.userlist:
+
+ # show the user a menu as needed
each_user.show_menu()
- input_data = ""
+
+ # check for some input
+ # TODO: make a separate function for this
try:
input_data = each_user.connection.recv(1024)
except:
- pass
+ input_data = ""
+ # we got something
if input_data:
+
+ # tack this on to any previous partial input
each_user.partial_input += input_data
- if each_user.partial_input and each_user.partial_input[-1] == "\n":
+
+ # the held input ends in a newline
+ if each_user.partial_input[-1] == "\n":
+
+ # filter out non-printable characters
each_user.partial_input = filter(lambda x: x>=' ' and x<='~', each_user.partial_input)
+
+ # strip off leading/trailing whitespace
each_user.partial_input = string.strip(each_user.partial_input)
+
+ # move it to the end of the input queue
each_user.input_queue.append(each_user.partial_input)
+
+ # reset the held partial input
each_user.partial_input = ""
+
+ # pass the first item in the input
+ # queue to the main handler
muffcmds.handle_user_input(each_user, each_user.input_queue[0])
+
+ # remove the first item from the queue
each_user.input_queue.remove(each_user.input_queue[0])
+
+ # the loop has terminated, so tear down all sockets
+ # TODO: move the save from command_halt() to here
muffsock.destroy_all_sockets()
+
+ # log a final message
+ # TODO: need a logging function for this kind of stuff
print "Shutting down now."