How to get the data from a data trigger using APAX

A data trigger is a mechanism that provides automatic notification when a specific sequence of characters has been detected by APAX in the incoming data stream. This relieves you of the need to continually scan the incoming data, instead APAX will notify your code that the data has been seen.

A data trigger is defined by calling the AddDataTrigger method. This method returns an index that is used to uniquely identify the data trigger (remember that any number of data triggers can be active in a given period of time).

When the conditions for a particular data trigger are met, notification is made via the OnDataTrigger event. One of the arguments passed to the event handler is the trigger index. By comparing this index against a list of saved data trigger indexes in your code, it is easy to use the trigger notification to automate some process. For example the following Visual Basic code snippet shows a simple FTP login sequence:

int trigUser = Apax.AddDataTrigger("220 ", 0, False, False)

int trigPass = Apax.AddDataTrigger("331 ", 0, False, False)

int trigLogin = Apax.AddDataTrigger("230 ", 0, False, False)

...

Private Sub Apax1_OnDataTrigger(ByVal Index As Long,

  ByVal Timeout As Boolean, ByVal Data As Variant, ByVal Size As Long,

  ReEnable As Boolean)

 

  Select Case Index

    Case trigUser:

      Apax1.PutStringCRLF ("USER anonymous")

    Case trigPass:

      Apax1.PutStringCRLF ("PASS emailaddress@domain.org")

    Case trigLogin:

      MsgBox ("Logged in");

  End Select

End Sub

In this example, simply knowing which data trigger fired is sufficient to automate the login procedure. However, let us suppose that you wish to be notified when any sequence of characters starting with the letter ‘a’ and ending with the letter ‘z’ have been received. The data trigger can easily be defined using the wild card character ‘*’ in the trigger string, but when the trigger fires, it is likely that you will want to know more than which data trigger fired. You want the data that was bracketed by the letters ‘a’ and ‘z’, but how do you get at it?

The data collected by the data trigger is passed to the OnDataTrigger event handler as a variant array of bytes. The number of bytes in the array is identified by the Size parameter, and also within the variant's safe array structure itself.

When accessing the Data argument within a Visual Basic project, you can simply treat it as an array of bytes. Assuming the data consists of printable ASCII characters, the following code sets the form's caption to the data received:

Private Sub Apax1_OnDataTrigger(ByVal Index As Long,

  ByVal Timeout As Boolean, ByVal Data As Variant, ByVal Size As Long,

  ReEnable As Boolean)

 

  Caption = ""

  For i = 1 To Size

    Caption = Caption & Chr(Data(i))

  Next

End Sub

With C++ projects, the data can be accessed via the pvData pointer of the variant's tagSAFEARRAY.  The following example assigns the data to CString variable:

void CExampleDlg::OnOnDataTriggerApax1(long Index, BOOL Timeout,

  const VARIANT FAR& Data, long Size, BOOL FAR* ReEnable)

{

    CString strItem = (char*) Data.parray->pvData;

}

It’s a bit more involved with Delphi projects. You can use the VarArrayLock function to obtain the pvData pointer, but you also need to remember to call VarArrayUnlock when you are finished.

procedure TForm1.Apax1DataTrigger(Sender: TObject; Index: Integer;

  Timeout: WordBool; Data: OleVariant; Size: Integer;

  var ReEnable: WordBool);

var

  pvData : PByteArray;

  i : Integer;

begin

  Caption := '';

  pvData := VarArrayLock(Data);

  for i := 1 to Size do

    Caption := Caption + Char(pvData^[i-1]);

  VarArrayUnLock(Data);

end;

 

(Note: the data that is passed via the OnRXD event is handled in the same manner.)

This site is not affiliated, endorsed, or otherwise associated with the entity formerly known as TurboPower Software. The owners and maintainers of Aprozilla.com were merely meager employees of the aforementioned organization, providing this site out of the pure goodness of their collective hearts. SourceForge.net Logo

Last updated: July 22, 2003.