def main():
"""The main loop."""
- # loop indefinitely while the world is not flagged for termination
- while not muffvars.terminate_world:
-
- # 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"))
+ # loop indefinitely while the world is not flagged for termination or
+ # there are connected users
+ while not muffvars.terminate_world or muffvars.userlist:
# the world was flagged for a reload of all code/data
if muffvars.reload_modules:
for module in muff.__all__:
exec("reload(muff." + module + ")")
+ # move data into new persistent objects
+ muffmisc.reload_data()
+
# 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
- muffmisc.log(str(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()
-
- # disconnect users with the appropriate state
- if each_user.state == "disconnecting":
-
- # save to cold storage
- each_user.save()
-
- # close the connection
- each_user.connection.close()
-
- # remove from the list
- each_user.remove()
-
- else:
-
- # check for some input
- # TODO: make a separate function for this
- try:
- input_data = each_user.connection.recv(1024)
- except:
- input_data = ""
- # we got something
- if input_data:
-
- # tack this on to any previous partial
- each_user.partial_input += input_data
-
- # the held input ends in a newline
- if each_user.partial_input[-1] == "\n":
-
- # filter out non-printables
- each_user.partial_input = filter(lambda x: x>=' ' and x<='~', each_user.partial_input)
-
- # strip off extra whitespace
- each_user.partial_input = string.strip(each_user.partial_input)
-
- # put on the end of the queue
- each_user.input_queue.append(each_user.partial_input)
-
- # reset the held partial input
- each_user.partial_input = ""
-
- # pass first item in the input
- # queue to the main handler
- muffcmds.handle_user_input(each_user, each_user.input_queue[0])
-
- # then remove it from the queue
- each_user.input_queue.remove(each_user.input_queue[0])
+ # do what needs to be done on each pulse
+ muffmisc.on_pulse()
- # the loop has terminated, so tear down all sockets
- # TODO: move the save from command_halt() to here
- muffsock.destroy_all_sockets()
+ # the loop has terminated, so save persistent variables
+ muffvars.save()
# log a final message
muffmisc.log("Shutting down now.")