⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gcprovider.c

📁 jtapi for telephone
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
    Copyright (c) 2002 Westhawk Ltd. (www.westhawk.co.uk) 

    All rights reserved. 

    Permission is hereby granted, free of charge, to any person obtaining a 
    copy of this software and associated documentation files (the 
    "Software"), to deal in the Software without restriction, including 
    without limitation the rights to use, copy, modify, merge, publish, 
    distribute, and/or sell copies of the Software, and to permit persons 
    to whom the Software is furnished to do so, provided that the above 
    copyright notice(s) and this permission notice appear in all copies of 
    the Software and that both the above copyright notice(s) and this 
    permission notice appear in supporting documentation. 

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 
    OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
    HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 
    INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 
    FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 
    NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 
    WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 

    Except as contained in this notice, the name of a copyright holder 
    shall not be used in advertising or otherwise to promote the sale, use 
    or other dealings in this Software without prior written authorization 
    of the copyright holder.
*/
#include <jni.h>
#include <jvmpi.h>
#include <stdio.h>
#include <srllib.h>

/*
 * GlobalCall header(s)
 */
#include <gclib.h>
#include <gcerr.h>
#define MAX_STRING_SIZE 256
/* These 2 are hacks - C treats Structs and Arrays interchangablely
 * which allows us to use structs as arguments to InvokexxxA()
 * avoiding need to keep track of the sequence of args in the array
 * or indeed to allocate the array.
 * 
 */
struct eventData {
    jvalue line;
    jvalue event;
    };
struct callInfo {
    jvalue line;
    jvalue crn;
    jvalue dest;
    jvalue orig;
    };
    
static JavaVM *vm;
static jobject gcprovinstance = 0;

static jmethodID miscEventID = 0;
static jmethodID alertingID = 0;
static jmethodID unblockedID = 0;
static jmethodID blockedID = 0;
static jmethodID offeredID = 0;
static jmethodID openedID = 0;
static jmethodID openFailedID = 0;
static jmethodID acceptID = 0;
static jmethodID taskFailID = 0;
static jmethodID answeredID = 0;
static jmethodID dropCallID = 0;
static jmethodID releaseCallID = 0;
static jmethodID releaseCallFailID = 0;
static jmethodID callStatusID = 0;
static jmethodID connectedID = 0;
static jmethodID disconnectedID = 0;
static jmethodID createCallID = 0;
static jmethodID proceedingID = 0;
  
static JavaVMAttachArgs attachArgs = { JNI_VERSION_1_2, NULL, NULL} ;

int print_error(char *function)  
{  
    int            cclibid;       /* cclib id for gc_ErrorValue( ) */  
    int            gc_error;      /* GlobalCall error code */  
    long           cclib_error;   /* Call Control Library error code */   
    char           *gcmsg;        /* points to the gc error message string */  
    char           *ccmsg;        /* points to the cclib error message string */  
    gc_ErrorValue( &gc_error, &cclibid, &cclib_error);  
    gc_ResultMsg( LIBID_GC, (long) gc_error, &gcmsg);  
    gc_ResultMsg( cclibid, cclib_error , &ccmsg);  
    printf ("%s failed, gc(0x%lx) - %s, cc(0x%lx) - %s\n", function ,
             gc_error, gcmsg, cclib_error, ccmsg);  
    return (gc_error);  
}  
/*  
-- This function is called to print GC_INFO to the system console  
-- Typically it would be called after a call to gc_ErrorInfo( )  
-- or gc_ResultInfo( ) to print the resulting GC_INFO data structure  
*/  
void printGC_INFO(GC_INFO *a_Info)  
{  
    printf("a_Info->gcValue = 0x%x\n", a_Info->gcValue);  
    printf("a_Info->gcMsg = %s\n", a_Info->gcMsg);  
    printf("a_Info->ccLibId = %d\n", a_Info->ccLibId);  
    printf("a_Info->ccLibName = %s\n", a_Info->ccLibName);  
    printf("a_Info->ccValue = 0x%x\n", a_Info->ccValue);  
    printf("a_Info->ccMsg = %s\n", a_Info->ccMsg);  
    printf("a_Info->additionalInfo = %s\n", a_Info->additionalInfo);   
}

/*  
-- This function can be called anytime after a GlobalCall event has occurred  
-- This procedure prints the result information to the console with no other side effects  
*/  
void printResultInfo(METAEVENT *a_metaeventp)  
{  
    int         retCode;  
    GC_INFO     t_Info;  
    retCode = gc_ResultInfo(a_metaeventp, &t_Info);  
    if (retCode == GC_SUCCESS) {  
        printGC_INFO(&t_Info);  
    } else {  
        printf("gc_ResultInfo( ) call failed\n");  
    }  
}  


void print_all_cclibs_status(void)  
{  
    int                  i;  
    char                 str[MAX_STRING_SIZE], str1[MAX_STRING_SIZE];  
    GC_CCLIB_STATUSALL   cclib_status_all;  
    if (gc_CCLibStatusEx("GC_ALL_LIB", &cclib_status_all) != GC_SUCCESS) {  
        /* error handling */  
    }    
    strcpy(str, " Call Control Library Status:\n");  
    for (i = 0; i < GC_TOTAL_CCLIBS; i++) {  
        switch (cclib_status_all.cclib_state[i].state) {  
            case GC_CCLIB_CONFIGURED:  
                sprintf(str1, "%s - configured\n",   
                        cclib_status_all.cclib_state[i].name);  
                break;  
            case GC_CCLIB_AVAILABLE:  
                sprintf(str1, "%s - available\n",   
                        cclib_status_all.cclib_state[i].name);  
                break;  
            case GC_CCLIB_FAILED:  
                sprintf(str1, "%s - is not available for use\n",   
                        cclib_status_all.cclib_state[i].name);  
                break;  
            default:  
                sprintf(str1, "%s - unknown CCLIB status\n",   
                        cclib_status_all.cclib_state[i].name);  
                break;  
        }  
        strcat(str, str1);  
    }  
    printf(str);  
}

void createCall(JNIEnv *env,CRN crn,LINEDEV ldev,char *destaddr,char *origaddr){
    struct callInfo ci;
    jstring jdestaddr;
    jstring jorigaddr;

    jdestaddr =  (*env)->NewStringUTF(env,destaddr);
    jorigaddr =  (*env)->NewStringUTF(env,origaddr);

    ci.line.i = (jint) ldev;
    ci.crn.i = (jint) crn;
    ci.dest.l = jdestaddr;
    ci.orig.l = jorigaddr;
    
    (*env)->CallVoidMethodA(env, gcprovinstance, createCallID,(jvalue *)&ci);
}

void pushCallInfo(JNIEnv *env,int crn){
    char destaddr[50];
    char origaddr[50];

    LINEDEV ldev;
    
    ldev = 0;
    strcpy(destaddr,"unknown");
    strcpy(origaddr,"unknown");
    if(gc_CRN2LineDev(crn, &ldev) != GC_SUCCESS) {
        print_error("crnToLine");
    }   
    if(gc_GetCallInfo(crn, DESTINATION_ADDRESS, destaddr) != GC_SUCCESS) {
        print_error("destination address");
    }
    if(gc_GetCallInfo(crn, ORIGINATION_ADDRESS, origaddr) != GC_SUCCESS) {
        print_error("ORIGINATION address");
    }
    createCall(env,crn,ldev,destaddr,origaddr);
}

jmethodID eventToMethod(METAEVENT *mev_p){
    jmethodID ret = NULL;
    switch (mev_p->evttype) {
        case GCEV_OPENEX: ret = openedID;
            break;
        case GCEV_OPENEX_FAIL: ret = openFailedID;
            printResultInfo(mev_p);
            break;
        case GCEV_UNBLOCKED: ret = unblockedID;
            break;
        case GCEV_BLOCKED: ret = blockedID;
            break;
        case GCEV_ACCEPT: ret = acceptID;
            break;
        case GCEV_TASKFAIL: ret = taskFailID;
            printResultInfo(mev_p);
            break;
        case GCEV_ANSWERED: ret = answeredID;
            break;
        case GCEV_DROPCALL: ret = dropCallID;
            break;
        case GCEV_RELEASECALL: ret = releaseCallID;
            break;
        case GCEV_RELEASECALL_FAIL: ret = releaseCallFailID;
            printResultInfo(mev_p);
            break;
        case GCEV_CALLSTATUS: ret = callStatusID;
            printResultInfo(mev_p);
            break;
        case GCEV_CONNECTED: ret = connectedID;
            break;
        case GCEV_DISCONNECTED: ret = disconnectedID;
            break;
        case GCEV_OFFERED: ret = offeredID;
            break;
        case GCEV_ALERTING: ret = alertingID;
            break;
        case GCEV_PROCEEDING: ret = proceedingID;
            break;
        default: ret = NULL;
    }
    return ret; 
}


long handleEvent() {
    JNIEnv *env;
    int error;
    struct eventData ev;
    int a =0;
    jint arg;
    jmethodID handleEventID = NULL;
    
    
    METAEVENT         metaevent;  
    if (gc_GetMetaEvent(&metaevent) != GC_SUCCESS) {  
        /* process error return as shown */  
        error = print_error("gc_GetMetaEvent");  
        return(error);  
    }   
    
    handleEventID = eventToMethod(&metaevent);
    
    if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_2) != JNI_OK) {
        (*vm)->AttachCurrentThread(vm, (void **)&env, &attachArgs);
        a=1;
    }
    /* special case for OFFERED - need to create a 'call' */
    if (metaevent.evttype == GCEV_OFFERED){
        pushCallInfo(env,metaevent.crn);
    }
    /* decide what to pass upwards */
    if (metaevent.crn == 0) {
        arg = (jint) metaevent.linedev;
    } else {
        arg = (jint) metaevent.crn;
    }
    if (handleEventID != NULL) {
        (*env)->CallVoidMethod(env, gcprovinstance, handleEventID,arg);
    } else {
        ev.line.i = (jint) metaevent.linedev;
        ev.event.i = (jint) metaevent.evttype;
        (*env)->CallVoidMethodA(env, gcprovinstance, miscEventID,(jvalue *)&ev);
    }
    
    if (a) 
        (*vm)->DetachCurrentThread(vm);
    return 0;
}

throw(JNIEnv *env, char * which, char *what)
{
    jclass newExcCls;
    char errbuf[128];

   newExcCls = (*env)->FindClass(env, which);
   if (newExcCls != 0)  {
		sprintf(errbuf, what);
		(*env)->ThrowNew(env, newExcCls, errbuf);
   }
   return 0;
}



int gcinit( JNIEnv *env , jobject obj)  
{   
    int            gc_error;         /* GlobalCall error code */  
    long           cc_error;         /* Call Control Library error code */   
    char           *msg;             /* points to the error message string */
    int res;  
    jclass clst;
       
    /* Issue a gc_Start( ) Call */  
    if ( (res = gc_Start( NULL )) != GC_SUCCESS )    {  
        /* process error return as shown */  
        gc_ErrorValue( &gc_error, NULL , &cc_error);  
        gc_ResultMsg( LIBID_GC, (long) gc_error, &msg);  
        printf ("Error in gc_Start ErrorValue: %d - %s\n",   
                 gc_error, msg);  
    } else {
        clst = (*env)->GetObjectClass(env, obj);
        gcprovinstance = (*env)->NewGlobalRef(env, obj);
        if (gcprovinstance == 0) {
            throw(env, "java/lang/RuntimeException",
                    "Instance GCProvider reference error!");
            res = -1;
        }
    }
    if (res != -1){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -