Imported from archive.
[mudpy.git] / lib / muff / muffsock.py
index 1ca604c..67010b1 100644 (file)
@@ -1,61 +1,79 @@
 """Socket objects 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.
 
+# need socket.socket for new connection objects and the server's listener
 import socket
 
+# hack to load all modules in the muff package
 import muff
 for module in muff.__all__:
        exec("import " + module)
 
 def check_for_connection(newsocket):
+       """Check for a waiting connection and return a new user object."""
+
+       # try to accept a new connection
        try:
                connection, address = newsocket.accept()
        except:
                return None
+
+       # note that we got one
+       # TODO: we should log this crap somewhere
        print "Connection from", address
+
+       # disable blocking so we can proceed whether or not we can send/receive
        connection.setblocking(0)
+
+       # create a new user object
        user = muffuser.User();
+
+       # associate this connection with it
        user.connection = connection
+
+       # set the user's ipa from the connection's ipa
        user.address = address[0]
+
+       # return the new user object
        return user
 
 def initialize_server_socket():
+       """Create and open the listening socket."""
+
+       # create a new ipv4 stream-type socket object
        newsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+
+       # set the socket options to allow existing open ones to be reused
+       # (fixes a bug where the server can't bind for a minute when restarting
+       # on linux systems)
        newsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+
+       # bind the socket to to our desired server ipa and port
        newsocket.bind((muffconf.config_data.get("network", "host"), muffconf.config_data.getint("network", "port")))
+
+       # disable blocking so we can proceed whether or not we can send/receive
        newsocket.setblocking(0)
+
+       # start listening on the socket
        newsocket.listen(1)
+
+       # note that we're now ready for user connections
+       # TODO: we should log this crap somewhere
        print "Waiting for connection(s)..."
+
+       # store this in a globally-accessible place
        muffvars.newsocket = newsocket
 
 def destroy_all_sockets():
+       """Go through all connected users and close their sockets."""
+
+       # note that we're closing all sockets
+       # TODO: we should log this crap somewhere
        print "Closing remaining connections..."
+
+       # iterate over each connected user and close their associated sockets
        for user in muffvars.userlist:
                user.connection.close()