Mischiefblog
I make apps for other people

Posts from August, 2005

Portable users

Posted by Chris Jones
On August 26th, 2005 at 15:35

Permalink | Trackback | Links In |

Comments Off on Portable users
Posted in Design Journal

I’ve been reconsidering portable users and the problem of identity. Previously, I’ve given thought to a dynamic peer-to-peer network of connected spaces, with certificates providing identity.

Without certificates (and as a relatively light-weight solution):

  1. Generate a hash or key in the client, based on real name, email address, and other characteristics.
  2. When connecting to a server, combine the key with a secret (password) and a server key to generate a new hash. This secondary hash is what uniquely identifies the user on that server.
  3. To prevent server operators from using the hash to spoof the user, the user should use a different secret on each server, and the hashed key and secret should be generated on the client-side. If the server only stores the product of the hashed user key and the server key, if the server’s database is compromised, the user keys are kept safe.

(more…)

Two more Java NIO things to keep in mind

Posted by Chris Jones
On August 24th, 2005 at 15:07

Permalink | Trackback | Links In |

Comments Off on Two more Java NIO things to keep in mind
Posted in Java, Tech

Selector.select() timeouts don’t sleep
When calling the Selector.select() with a timeout value, it won’t necessarily sleep–the code will consume all the CPU if it’s in a non-blocking while() loop. Use Thread.sleep() instead to minimize CPU usage.

When writing variable length messages, allocate a new ByteBuffer
Create a new ByteBuffer the size of the message unless all your messages are the same length. Oversized buffers will write a lot of nulls. Forgetting to flip() the buffer after write()-ing to it will write nulls from the current buffer mark to the end, missing your message completely.

byte [] baMsg = message.toBytes();
 
outBuffer = ByteBuffer.allocateDirect(baMsg.length);
 
outBuffer.put(baMsg);
outBuffer.flip();
sockChannel.write(outBuffer);

Closing client sockets properly in Java

Posted by Chris Jones
On August 23rd, 2005 at 14:09

Permalink | Trackback | Links In |

Comments Off on Closing client sockets properly in Java
Posted in Java, Python, Tech

To properly close a client socket, so that the other side knows without attempting to read or send something, you must notify the peer that you are closing the socket for read and/or write. Simply closing the file descriptor won’t notify the peer.

In Python (assuming the socket is connected, pick one of):

sock.shutdown(socket.SHUT_RD)
sock.shutdown(socket.SHUT_RW)
sock.shutdown(socket.SHUT_RDRW)
 
and
 
sock.close()

It’s not that different in Java. Using NIO, assuming you have a SocketChannel available:

// shut down the connection
if (sockChannel != null) {
    try {
        // close the output channel to signal this connection is closing
        // to the server
        sockChannel.socket().shutdownOutput();
        // optionally, call sockChannel.socket().shutdownInput() as well
 
        // close the channel and socket file descriptor
        sockChannel.close();
    } catch (IOException e) {
        client.raiseError("Unable to close connection to server.", e);
    }
}

Detecting disconnected sockets when reading

Posted by Chris Jones
On August 10th, 2005 at 15:49

Permalink | Trackback | Links In |

Comments Off on Detecting disconnected sockets when reading
Posted in Java, Python, Tech

Detecting disconnected sockets in Python (and C)
# read from the client socket
try:
    msg = reader.recv(NetServer.max_message_length)
 
    if len(msg) == 0:
        # socket closed
        self.shutdown_client_socket(reader)
        NetServer.logger.info('Connection ' +
             str(reader.getpeername()) + ' closed.')
    else:
        # pass the message to the client to handle it
        self.clients[cli_sock].read_message(msg)
except socket.error, err:
    NetServer.logger.error('recv() error')
    # kill the client
    self.shutdown_client_socket(reader)

When I detect that 0 bytes were read from a readable socket, assume that the socket was closed.
(more…)

wxPython

Posted by Chris Jones
On August 8th, 2005 at 11:01

Permalink | Trackback | Links In |

Comments Off on wxPython
Posted in Java, Python, Tech

I’ve been mucking around with Python and wxPython for the past week. My early impressions are that it’s a very useful framework, with more controls than Swing, but not necessarily implemented as cleanly. My current pet peeve is in how tabbed panes are constructed (visually).

It’s well documented, though again, it’s hard to be better documented than Swing. I found the C++ documentation is sufficient for Python coding, and the large example application helps in learning how to use the framework, although I can’t get dynamic layouts to work in wx yet. I’ve written some rather embarassing client code that connects to a Python high-performance TCP echo server, mostly cobbled together from examples and some modifications.
(more…)

Or maybe the Undead Priest

Posted by Chris Jones
On August 3rd, 2005 at 10:45

Permalink | Trackback | Links In |

Comments Off on Or maybe the Undead Priest
Posted in Games

Becky had a good point: why start over when most content after level 30 is shared between Alliance and Horde?

While I’m back to playing my (now, level 35) Undead Priest, she is concentrating on her Troll Hunter (level 33). When I catch up to her Tauren Druid (level 37), we’ll start duoing again.

I spent 30 minutes playing a Troll Mage (level 9) on another server and found it to be less painful than I remember.

Sucked back in

Posted by Chris Jones
On August 2nd, 2005 at 13:52

Permalink | Trackback | Links In |

Comments (1) |
Posted in Games

Saturday, we reactivated World of Warcraft.

The next thing I knew, it was Sunday night and I had leave for work in seven hours. (Okay, I’ll admit that’s an exaggeration.)

During evenings when I have time to play and on the weekends, we’re duoing a Priest and Druid. Things go down quickly, and we have virtually no downtime, don’t worry much about adds, etc. My Priest is the anchor dragging down the kill rate, since I occasionally need to tank up on fresh mana, especially if Spirit Tap doesn’t fire or I don’t have time to sit between fights. We rolled up a fresh pair of Night Elves instead of using our mid-30s pair, in part because the Alliance quests will be fresher, but also because Becky’s tired of riding Kodos.

When I’m not playing, she plays her new Tauren Hunter and is using a Stirgid Owl as a pet.