CS11 Lab 7 - Chatter

This week you will take your Spread knowledge and begin constructing a simple console-based chat program. You will use the Spread Toolkit to send and receive messages between different chat clients; this will be very similar to what you have already done for the sprecho program from last week. In fact, you should be able to use significant portions of the code from last week's lab, especially if you were careful to write last week's lab in a reasonably modular way.

The user interface portion of the code will be provided by the ncurses API. Fortunately you don't have to create this user interface yourself; a simple set of C++ classes is provided for you to use in this lab. The main challenge of using ncurses for the user interface code is that it is not thread-safe. Thus, we must be careful to ensure that the rest of the program doesn't break ncurses in some way.

The main issue is that our program has two sources of input, the console and the network. Fortunately, we can configure ncurses to time out if no input is available, and we can use the Spread SP_poll() function to tell if any messages are waiting on the network. This allows us to create a simple main loop in our program that handles input from both sources:

    Do {
        Check for a complete message from console input.
        If a message is available, perform appropriate processing.
            (May be a "quit" command, for example.)

        While there is Spread data to receive:
            Receive a Spread message and handle it.
    }
    Until user decides to exit chat program.

This week, your primary task is to integrate Spread into the ncurses chat user interface. This should not be too difficult, especially if you were careful to write last week's sprecho lab in a reasonably clean, modular way.

Your Tasks

  1. Download the nchat source files:

    Write a make file for these classes to build a program nchat (or name it something else if you like). To build programs using the ncurses API you must add -lcurses to the linker flags (LDFLAGS).

    Get the program building, then try it out. Verify that you can type messages and have them show up in the chat window. When you type "quit" the program will exit.

  2. Add Spread connect/disconnect code to the chat program.

    This week's program is really just a glorified version of the sprecho program, where each client in the Spread network can send and receive text messages. However, the basic operations required are identical.

    You will need to connect to the Spread network before entering the main loop, and disconnect from the Spread network after the main loop exits. (Don't worry about updating the user information; that is beyond the scope of this week's assignment.) You can continue to use a hard-coded group name that all chat clients connect to; this is the simplest way to implement the chat program.

  3. Update the main loop code to include network sends and receives.

    This should be simple, especially if you have a couple of "send string" and "receive string" functions in your sprecho implementation that you can move over to this week's lab. All you really have to do is this:

    This is a pretty simple of asynchronous IO, but it is necessary to work with ncurses safely. Fortunately, this loop won't peg your CPU because the console-input check will take 200ms to time out. This means that the user interface will be nice and responsive to users typing, and any incoming Spread message will only be delayed by at most 0.2 seconds. That won't be noticeable at all!

  4. Try out your Spread-enabled chat program.

    You will need to update your makefile to include the Spread headers and libraries in the build process. Make sure it is easy to build the nchat program from your makefile, and provide a clean target as well.

    Make sure that your updated chat program works properly with 2 or 3 peers all running the chat program at the same time. Try to make sure that messages don't show up multiple times in the chat window; this could happen if your user input code prints the message as well as sending it to the network, without using SELF_DISCARD. (You actually don't have to use SELF_DISCARD in this program, but you do need to make sure that whatever you do, messages only show up once.)

Once your new ncurses chat program is working, leave it in your ~/cs11/cppdgc/lab7 directory.


Copyright (C) 2007, California Institute of Technology.
Last updated May 24, 2007.