Chapter 5. Integrating with Cogent Software

5.1. Callbacks and Cascade DataHub Points

Gamma is an integral part of the rest of Cogent's software. Using Gamma with Photon gives you a way to create GUIs, and then join them quickly and seamlessly with other Cogent tools, such as the Cascade DataHub. Using all these tools together, you can create powerful and reliable applications with substantially less work than it would take in C.

Here is an example, written with 25 lines of code, that creates a "Talk Box", a simple communications device that uses the Cascade DataHub. It works by writing to and reading a single data point on the Cascade DataHub. As the picture illustrates, the text entered on any Talk Box is sent to every other Talk Box that has access to the same datahub point.

 
		

Since this program uses the Cascade DataHub, you must ensure that it is running. If not, start it with these commands entered successively at a shell prompt:

  1. $ qserve

  2. $ nserve

  3. $ datahub

When the Cascade DataHub is running, enter the command for Talk Box followed by a point name argument, like this:

$ phgamma talkbox.g mypoint &

Gamma will then run the following code, starting up a Talk Box. To start more Talk Boxes, enter the same command again. Each Talk Box shows its process ID number in the top right-hand corner. To view the Cascade DataHub, press the View Database button on any Talk Box.

#!/usr/cogent/bin/phgamma

/*
 * This example loads a Talk Box with a text-entry line for writing,
 * and a multi-text field for reading.  When you enter text, it is
 * sent to the datahub, and then appended to the bottom of the text
 * in the multi-text field for reading.  
 *
 * First, load the necessary Photon and PhAB files, and initialize Photon.
 */
 
require_lisp("PhotonWidgets.lsp");
require_lisp("PhabTemplate.lsp");
PtInit(nil);

/*
 * Then create a unique name for this Talk Box, based on its node
 * and process ID numbers. This allows multiple Talk Boxes to run
 * simultaneously.
 */
uniq_name = string(getnid(), ".", getpid());

/*
 * Initialize IPC, read the point name from the command line, and
 * register to receive all changes made to the point.
 */
init_ipc(uniq_name, uniq_name);
dhpoint = symbol(cadr(argv));
register_point(dhpoint);

/*
 * Read the widget file, create it, and assign symbols to the widgets
 * to be used.
 */
wfile = PhabReadWidgetFile ("WidgetFiles/wgt/talkbox.wgtw");
window = PhabCreateWidgets(wfile, nil, nil);

tlkbx = PhabLookupWidget(window,#talkbox,nil);
enttxt = PhabLookupWidget(window,#EntryText,nil);
distxt= PhabLookupWidget(window,#DisplayText,nil);
dhview = PhabLookupWidget(window,#DHviewButton,nil);
idlab = PhabLookupWidget(window,#Label1,nil);

/*
 * Make the reader window display_only.
 */
distxt.text_flags = cons(Pt_EDITABLE, nil);

/*
 * Attach callbacks to read the entered text, delete it after entering,
 * and write it to the datahub. Also attach a callback to scroll the
 * display to show the text properly once it is displayed, and a callback
 * to bring up the datahub viewer when the button is pushed.
 */
PtAttachCallback(enttxt,Pt_CB_TEXT_CHANGED,#entry = enttxt.text_string);		 
PtAttachCallback(enttxt,Pt_CB_ACTIVATE,#enttxt.text_string = "");		 
PtAttachCallback(enttxt,Pt_CB_ACTIVATE,#write_point(dhpoint, entry));
PtAttachCallback(distxt,Pt_CB_TEXT_CHANGED,#distxt.multitext_y_scroll_pos =
		 distxt.multitext_num_lines);	 
PtAttachCallback(dhview,Pt_CB_ACTIVATE,#system("phdhview &"));

/*
 * Add echo and exception functions to the datahub point, so that when
 * it changes, the display text will show the change.  The add_echo_function
 * is activated by changes made from this Talk Box, and the 
 * add_exception_function is activated by changes made from other Talk Boxes.
 */
add_echo_function(dhpoint,#distxt.text_string =
		  string(distxt.text_string, eval(dhpoint),"\n"));
add_exception_function(dhpoint,#distxt.text_string =
		       string(distxt.text_string, eval(dhpoint),"\n"));

/*
 * Have the label text show the PID number.
 */
idlab.text_string = string("Process ID: ", getpid());

PtRealizeWidget(tlkbx);
PtMainLoop();
		

Copyright 1995-2002 by Cogent Real-Time Systems, Inc.