This model is basically the same as the Asynchronous model, except that the application can control groupings of devices with separate threads. To do this, use sr_waitevtEx( ). See section
Choose the Extended Asynchronous model for any application that:
Extended Asynchronous Model Advantages
Extended Asynchronous Model Disadvantages
Extended Asynchronous Model Programming Notes
|
Get Dialogic device handle for the current event. | |
|
Get event type for the current event. | |
|
Get a pointer to additional data for the current event. | |
|
Get the number of bytes of additional data that are pointed to by sr_getevtdatap( ). |
Extended Asynchronous Model ExampleAn example of the Extended Asynchronous model is shown below.
****************************************************************
* NAME : int main()
* DESCRIPTION : create thread and poll for keyboard input
* INPUT : none
* OUTPUT : none
* RETURNS : 0 on success; 1 if a failure was encountered
* CAUTIONS : none
****************************************************************/
int main()
{
HANDLE thread_handle[2];
DWORD threadID;
/* show application's title */
printf("Extended Asynchronous Mode Sample Application - hit any key to exit...\n");
/* create one thread to run one state machine */
if ((thread_handle[0] = (HANDLE)_beginthreadex(NULL,
0,
StateMachine1,
(LPVOID)0,
0,
&threadID)) == (HANDLE)-1) {
/* Perform system error processing */
exit(1);
}
/* create a second thread to run the other state machine */
if ((thread_handle[1] = (HANDLE)_beginthreadex(NULL,
0,
StateMachine2,
(LPVOID)2,
0,
&threadID)) == (HANDLE)-1) {
/* Perform system error processing */
exit(1);
}
/* wait for Keyboard input to shutdown program */
getch();
Kbhit_flag++; /* let thread know it's time to abort */
/* sleep here until thread has terminated */
if (WaitForMultipleObjects(2, thread_handle, TRUE, INFINITE)
== WAIT_FAILED) {
printf("ERROR: Failed WaitForMultipleObjects(): error = %ld\n",
GetLastError());
}
return(0);
}
/****************************************************************
* NAME : DWORD WINAPI StateMachine1(LPVOID argp)
* DESCRIPTION : This tread runs the offhook-dial-onhook state machine
* INPUT : LPVOID argp - NULL pointer (not used)
* OUTPUT : none
* RETURNS : 0 on success; 1 if a failure was encountered
* CAUTIONS : none
****************************************************************/
DWORD WINAPI StateMachine1(LPVOID argp)
{
char channame[20];
int chdesc;
int cnt;
int hDevice[MAX_CHAN];
int hEvent;
long EventCode;
int basechn = (int)argp;
for (cnt = basechn; cnt < basechn + (MAX_CHAN/2); cnt++) {
/* build name of voice channel */
sprintf(channame, "dxxxB%dC%d", (cnt / 4) + 1,
(cnt % 4) + 1);
/* open voice channel */
if ((chdesc = dx_open(channame, 0)) == -1) {
printf("%s - FAILED: dx_open(): system error = %d\n",
channame, dx_fileerrno());
return(1);
}
hDevice[cnt] = chdesc;
printf("%s - Voice channel opened\n", ATDV_NAMEP(chdesc));
/* kick off the state machine by going offhook asynchronously */
if (dx_sethook(chdesc, DX_OFFHOOK, EV_ASYNC) == -1) {
printf("%s - FAILED: dx_sethook(DX_OFFHOOK): %s (error #%d)\n",
ATDV_NAMEP(chdesc), ATDV_ERRMSGP(chdesc), ATDV_LASTERR(chdesc));
return(1);
}
printf("%s - Voice channel off-hook initialized\n",ATDV_NAMEP(chdesc));
}
/* loop until Keyboard input is received */
while (!Kbhit_flag) {
/*
* wait for event on the specific list of handles
*/
sr_waitevtEx(&hDevice[basechn], MAX_CHAN/2, -1, &hEvent);
/*
* gather data about the event
*/
chdesc = sr_getevtdev(hEvent);
EventCode = sr_getevttype(hEvent);
switch(EventCode) {
case TDX_SETHOOK:
if (ATDX_HOOKST(chdesc) == DX_OFFHOOK) {
printf("%s - Voice channel off-hook\n",ATDV_NAMEP(chdesc));
/* we went off hook so start dialing */
if (dx_dial(chdesc, "12025551212", NULL, EV_ASYNC) == -1) {
printf("%s - FAILED: dx_dial(): %s (error #%d)\n",
ATDV_NAMEP(chdesc), ATDV_ERRMSGP(chdesc), ATDV_LASTERR(chdesc));
return(1);
}
printf("%s - Voice channel dialing initialized\n",ATDV_NAMEP(chdesc));
} else {
/* we went on hook so go off hook again */
printf("%s - Voice channel on-hook\n",ATDV_NAMEP(chdesc));
/* set the voice channel off-hook */
if (dx_sethook(chdesc, DX_OFFHOOK, EV_ASYNC) == -1) {
printf("%s - FAILED: dx_sethook(DX_OFFHOOK): %s (error #%d)\n",
ATDV_NAMEP(chdesc), ATDV_ERRMSGP(chdesc), ATDV_LASTERR(chdesc));
return(1);
}
printf("%s - Voice channel off-hook initialized\n",ATDV_NAMEP(chdesc));
}
break;
case TDX_DIAL:
printf("%s - Voice channel Done dialing\n",ATDV_NAMEP(chdesc));
/* done dialing so set the voice channel on-hook */
if (dx_sethook(chdesc, DX_ONHOOK, EV_ASYNC) == -1) {
printf("%s - FAILED: dx_sethook(DX_ONHOOK): %s (error #%d)\n",
ATDV_NAMEP(chdesc), ATDV_ERRMSGP(chdesc), ATDV_LASTERR(chdesc));
return(1);
}
printf("%s - Voice channel on-hook initialized\n",ATDV_NAMEP(chdesc));
break;
default:
printf("Received unexpected event 0x%X on device %d\n", EventCode, chdesc);
break;
}
}
for (cnt = basechn; cnt < basechn + (MAX_CHAN/2); cnt++) {
/* close the voice channel */
chdesc = hDevice[cnt];
printf("%s - Voice channel closing\n",ATDV_NAMEP(chdesc));
if (dx_close(chdesc) == -1) {
printf("%s - FAILED: dx_close(): %s (error #%d)\n",
ATDV_NAMEP(chdesc), ATDV_ERRMSGP(chdesc), ATDV_LASTERR(chdesc));
return(1);
}
}
return(0);
}
/****************************************************************
* NAME : DWORD WINAPI StateMachine2(LPVOID argp)
* DESCRIPTION : This tread runs the offhook-playtone-onhook state machine
* INPUT : LPVOID argp - NULL pointer (not used)
* OUTPUT : none
* RETURNS : 0 on success; 1 if a failure was encountered
* CAUTIONS : none
****************************************************************/
DWORD WINAPI StateMachine2(LPVOID argp)
{
char channame[20];
int chdesc;
int cnt;
int hDevice[MAX_CHAN];
int hEvent;
long EventCode;
int basechn = (int)argp;
TN_GEN ToneGeneration;
for (cnt = basechn; cnt < basechn + (MAX_CHAN/2); cnt++) {
/* build name of voice channel */
sprintf(channame, "dxxxB%dC%d", (cnt / 4) + 1,
(cnt % 4) + 1);
/* open voice channel */
if ((chdesc = dx_open(channame, 0)) == -1) {
printf("%s - FAILED: dx_open(): system error = %d\n",
channame, dx_fileerrno());
return(1);
}
hDevice[cnt] = chdesc;
printf("%s - Voice channel opened\n", ATDV_NAMEP(chdesc));
/* kick off the state machine by going offhook asynchronously */
if (dx_sethook(chdesc, DX_OFFHOOK, EV_ASYNC) == -1) {
printf("%s - FAILED: dx_sethook(DX_OFFHOOK): %s (error #%d)\n",
ATDV_NAMEP(chdesc), ATDV_ERRMSGP(chdesc), ATDV_LASTERR(chdesc));
return(1);
}
printf("%s - Voice channel off-hook initialized\n",ATDV_NAMEP(chdesc));
}
/* loop until Keyboard input is received */
while (!Kbhit_flag) {
/*
* wait for event on the specific list of handles
*/
sr_waitevtEx(&hDevice[basechn], MAX_CHAN/2, -1, &hEvent);
/*
* gather data about the event
*/
chdesc = sr_getevtdev(hEvent);
EventCode = sr_getevttype(hEvent);
switch(EventCode) {
case TDX_SETHOOK:
if (ATDX_HOOKST(chdesc) == DX_OFFHOOK) {
printf("%s - Voice channel off-hook\n",ATDV_NAMEP(chdesc));
/* we went off hook so build and play the tone */
dx_bldtngen(&ToneGeneration, 340, 450, -10, -10, 300);
if (dx_playtone(chdesc, &ToneGeneration, (DV_TPT *)NULL, EV_ASYNC) == -1) {
printf("%s - FAILED: dx_playtone(): %s (error #%d)\n",
ATDV_NAMEP(chdesc), ATDV_ERRMSGP(chdesc), ATDV_LASTERR(chdesc));
return(1);
}
printf("%s - Voice channel play tone initialized\n",ATDV_NAMEP(chdesc));
} else {
/* we went on hook so go off hook again */
printf("%s - Voice channel on-hook\n",ATDV_NAMEP(chdesc));
/* set the voice channel off-hook */
if (dx_sethook(chdesc, DX_OFFHOOK, EV_ASYNC) == -1) {
printf("%s - FAILED: dx_sethook(DX_OFFHOOK): %s (error #%d)\n",
ATDV_NAMEP(chdesc), ATDV_ERRMSGP(chdesc), ATDV_LASTERR(chdesc));
return(1);
}
printf("%s - Voice channel off-hook initialized\n",ATDV_NAMEP(chdesc));
}
break;
case TDX_PLAYTONE:
printf("%s - Voice channel Done playine tone\n",ATDV_NAMEP(chdesc));
/* done playing a tone so set the voice channel on-hook */
if (dx_sethook(chdesc, DX_ONHOOK, EV_ASYNC) == -1) {
printf("%s - FAILED: dx_sethook(DX_ONHOOK): %s (error #%d)\n",
ATDV_NAMEP(chdesc), ATDV_ERRMSGP(chdesc), ATDV_LASTERR(chdesc));
return(1);
}
printf("%s - Voice channel on-hook initialized\n",ATDV_NAMEP(chdesc));
break;
default:
printf("Received unexpected event 0x%X on device %d\n", EventCode, chdesc);
break;
}
}
for (cnt = basechn; cnt < basechn + (MAX_CHAN/2); cnt++) {
/* close the voice channel */
chdesc = hDevice[cnt];
printf("%s - Voice channel closing\n",ATDV_NAMEP(chdesc));
if (dx_close(chdesc) == -1) {
printf("%s - FAILED: dx_close(): %s (error #%d)\n",
ATDV_NAMEP(chdesc), ATDV_ERRMSGP(chdesc), ATDV_LASTERR(chdesc));
return(1);
}
}
return(0);
}
Click here to contact Dialogic Customer Engineering
Copyright 2001, Dialogic Corporation