JNI and event functions

Viewed 142

In my JNI project I need to add events/callbacks to the java client that will receive this event from the native library. In c++ I do it this way:

Dialog based:

SetProcessEventCallback((ProcessEvent) KSampleDlg::OnProcessEvent, this);

void KSampleDlg::OnProcessEvent(float fPercent, float fDeviceBuffer, float fCache, double dBytesWritten, double dImageSize, KSampleBurnDlg *pDlg) 
{
    pDlg->SetProgress((int)fPercent, (int)fCache);
}

In console app It works like

SetProcessEventCallback(OnProcess, 0);

void OnProcess(float fPercent, float fDeviceBuffer, float fCache, 
               double dBytesWritten, double dImageSize, void *pUserData)
{
    static int nChars = 0;
    g_cOutputLock.Lock();

    while( (fPercent/100.0)*80 > nChars )
    {
        g_strBuffer += _T("*");
        nChars++;
    }
    g_cOutputLock.UnLock();
}

After this event callback is set the library will fire this event to the given function. I can set the function and callback in the native lib but I have no idea how to fire this to the java app. Anyone can give me a hint how to handle this with JNI?

1 Answers

On the C++ side, use pipe(2) to create a pair of file descriptors. Then just write updates to the write end and close when you are done.

On the Java side, take the file descriptor of the read end (eg pass it by JNI). Let Java open an InputStream on /dev/fd/X and read until EOF in a separate thread.

Related