Thursday, July 28, 2011

Design Primitives in Javascript: Example with Node.js

So here, as an example to bring this together, I'm going to implement a simple service over TCP/IP that returns the current date and time or a (pseudo) random number between 1 and 10.  To demonstrate the service, we'll telnet to the port on which the server runs and submit commands to the service via telnet.

To begin with, I'll cover the architecture and design of the service.  Then, I'll cover the components and how they're implemented, finally followed by how the service itself uses those components to implement the server.

Let's discuss the architecture of the service first.  From a requirements perspective, we're going to return the current date/time when requested, return a random number if requested, and handle unknown values without problems.  That gives us this command protocol:

date:             Retrieves a date
random:       Returns a randomly generated number

These commands are also case sensitive.  We'll also need to handle the case where we're delivered garbage.  The major components include:

Client:                             The client we'll use
Server:                            The server that'll generate the return values
Path:                               The communication path between client/server
Client Host:                    The host upon which the client will run
Server Host:                   The server host node

From a technology perspective, we've chosen:

Comm Protocol:  TCP/IP
Host:                     Localhost, running MacOS X

Other technology specific requirements include Node.js, for communication support, and telnet, to support the local client implementation.

This gives us an integrated technical system stack of a telnet client, running on localhost, attaching to a server instance built atop Node.js, again on the localhost, over TCP/IP.  Commands will be issued in ASCII from the client, where date returns the current date and time, random returns a random number, and all other commands return an unknown command indicator.

Design-wise, we'll put the server using these components:

server.js (script): This is the script that runs the server.  This will import the correct Node.js classes and bootstrap the server using functions from other modules and classes.  This script will depend on the main module and the net module from Node.js.
main.js (module): This is the module that contains all the entry point functions used by the server.js script.  It will also handle command parsing for the service.  This module expects server generator classes to implement the Generator interface.  It will depend on the date, random, and version modules.
date.js (module): This module will implement a class adhering to the Generator interface that returns the current date and time.
random.js(module): This module implements a generator class returning a random number between 1 and 10.
version.js(module): This module contains version information based on the Version interface.

The Generator and Version interfaces are:

interface Generator :
  generate(void) : string


interface Version :
  <<property>> major : int
  <<property>> minor : int
  <<property>> patch : int
  <<property>> status : string

Operationally, requests will be routed through the server script, into functions defined in main, which uses generator classes in date and random.  The server will be started from the command line (i.e. with the command node server.js) as will the client (i.g. from the commandline, telnet localhost 1337).

3 comments:

  1. Nice job Chris! Do you think node.js would be a good choice for teaching concepts related to clients and servers? I have a lot of experience doing that using Java but I wonder if node.js might have some advantages, or at least constitute a simple alternative... of course these days there are probably plenty of choices!

    ReplyDelete
  2. The nice thing about Node.js in that area is the ease with which you can put together server elements, IMO.

    ReplyDelete