SHAFT Tutorial

HOME

Overview

The API for SHAFT consists of the following classes:

API of Shaft class

A Shaft object can be in three possible modes. In the client mode it is connected to a broker and capable of sending publications, subscriptions and unsubscription requests. It can also request the broker to open notification connections and receive notifications from a broker. In the server mode it encapsulates a server socket to which brokers can open notification connections. In the passive mode it is not connected to any broker and can either connect to a broker or accept a notification connections from a broker. A freshly created Shaft object is in passive mode. The following methods should be invoked on a passive Shaft object: The following methods should be invoked on a Shaft object in client mode: The following methods should be invoked on a Shaft object in client or server mode: The following methods can be invoked on any Shaft object:

API of ConnectInfo class

The ConnectInfo class encapsulates a socket connection. It contains an IP address, a port number and a backlog (for server sockets). The following methods can be used to view and modify its data members:

API of PubInfo class

The PubInfo class encapsulates a publication to be sent to a broker. It contains three data members - the publication pub, the id to be assigned to the publication id and a flag indicating whether the broker should acknowledge the receipt of the publication ack . These data members can be viewed and modified by the following methods:

API of SubInfo class

The SubInfo class encapsulates a subscription to be sent to a broker. It contains six data members - the subscription sub, the id to be assigned to the subscription id, the IP address and port to which any notification for this subscription should be sent ipAddress and port, the number of seconds after which the subscription should expire validity , and a flag indicating whether the broker should acknowledge the receipt of the subscription ack. These data members can be viewed and modified by the following methods:

API of UnsubInfo class

The UnsubInfo class encapsulates an unsubscription request to be sent to a broker. It contains two data members - the id of the subscription to be removed subId and a flag indicating whether the broker should acknowledge the receipt of the subscription ack. These data members can be viewed and modified by the following methods:

API of NotInfo class

The NotInfo class encapsulates a notification received from a broker. It has two data members - the id of the subscription for which this is a notification subId and the publication which matched the subscription pub. These data members can be viewed and modified by the following methods:

Publication Language

A publication is just an alternating sequence of variables and constants separated by whitespace and beginning with a variable. A publication of the form "V1 C1 V2 C2 ... Vn Cn" where V1, V2, ..., Vn are variables and C1, C2, ..., Cn are constants assigns value Ci to variable Vi for i = 1,2, ..., n. For example the publication "name 'John Smith' age 30" assigns value 'John Smith' to the variable name and the value 30 to the variable age. The definition of a variable and a constant is the same as that for the BLADE query language described in the BLADE tutorial .

Example

The following program demonstrates most of what has been described above. It depicts a client that opens a connection with a broker (that is assumed to be listening at IP address 100.100.100.100 and port 9999). The client itself is running on IP address 101.101.101.101. After connecting to the broker the client establishes a notification connection at port 10000. It then sends a subscription and a publication such that a notification will be generated. It receives the notification and closes the notification connection. Then it unsubscribes the subscription. Finally it disconnects from the broker and quits.

#include "Shaft.h"

int main()
{
  //connect to the broker
  Shaft cliShaft;
  ConnectInfo cInfo;
  cInfo.SetIPAddress("100.100.100.100");
  cInfo.SetPort(9999);
  cliShaft.Connect(cInfo);

  //open the server socket
  Shaft srvShaft;
  cInfo.SetPort(10000);
  srvShaft.Open(cInfo);

  //establish the notification connection
  Shaft notShaft;
  cInfo.SetIPAddress("101.101.101.101");
  cliShaft.StartNotConnect(srvShaft,notShaft,cInfo);

  //send the subscription. the notification of the subscription
  //should be sent via the notification connection just established.
  //the subscription should expire in one hour.
  SubInfo subInfo;
  subInfo.SetId("sub");
  subInfo.SetSub("name $eq$ \'foobar\'");
  subInfo.SetIPAddress("101.101.101.101");
  subInfo.SetPort(10000);
  subInfo.SetValidity(3600);
  cliShaft.Subscribe(subInfo);

  //send the publication
  PubInfo pubInfo;
  pubInfo.SetPub("name \'foobar\'");
  cliShaft.Publish(pubInfo);

  //get the notification
  NotInfo notInfo;
  notShaft.GetNotification(notInfo);

  //close the notification connection
  cliShaft.CloseNotConnect(cInfo);
  notShaft.Disconnect();

  //unsubscribe
  UnsubInfo unsubInfo;
  unsubInfo.SetSubId("sub");
  cliShaft.Unsubscribe(unsubInfo);

  //disconnect from the broker
  cliShaft.Disconnect();

  return 0;
}

Assuming that the program was saved to shaft.cpp it can be compiled on Linux as follows:

$ g++ -DBLADE_OS_LINUX shaft.cpp -lshaft -lspear

On Windows the compilation can be done as follows:

$ cl -DBLADE_OS_WIN32 shaft.cpp libshaft.lib libspear.lib -link -NODEFAULTLIB:LIBC.lib

If you wish to write a broker please read the BLADE and JBLADE tutorials.

HOME