Following on from the style used by http and smtp

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: t by clients. The next task is to do something more meaningful with the messages. Later, we’ll arrange for them to be sent back to clients to make a chat system, but first we need to establish a protocol for how we’re going to interpret messages. It should be clear by now that anything you send (e.g. via telnet) to this server ends up appearing in the ‘onMessage’ method as a string of text. We could at this stage build a simple server that simply ‘reflects’ all the messages that are sent to it from clients back to any clients that have connected (i.e. so a message sent from one person gets sent to all the others). But it would be nice to do something more sophisticated. For example, we should really allow clients to register a name, so you can tell who sent which message. It would also be nice to be able to send messages either to all members of the chat session, or privately to specific users (in which case, of course, we need to know their name!). Achieving this means we’ll need to encode some more meaning in the text that’s being exchanged; for example, what ‘screen name’ is being used by which user, or the name of another user to which you’d like to send a private message. Following on from the style used by http and smtp servers, we’ll simply establish a protocol based on text commands that look like this: <COMMAND> <some parameters> so we might have something like MESSAGE Hello world Where MESSAGE becomes the command, and ‘hello world’ becomes the parameters. In python a line such as... (command, sep, parameter) = message.strip().partition(' ') will take the string, and separate it out on the first space we encounter1. For example if we did... myMessage = 'MESSAGE Hello World' (command, sep, parameter) = message.strip().partition(' ') print 'Command is ', command print 'Message is ',parameter we should get an output of: Command is MESSAGE Message is Hello World You can use this (or any variation you like) in your onMessage method to start interpreting commands as they are sent to the server. Write some code in the onMessage method to do exactly this, and test using telnet that you can indeed parse out a command and its parameters. Task 4 : designing a protocol [4 marks] Step away from...
View Full Document

This document was uploaded on 02/10/2014.

Ask a homework question - tutors are online