0

I am trying to communicate with JNI with java using pipe I tried this.

my JNI

void createPipe(std::string path){

    //pipe test

    const char* PATH = path.c_str();
    char* line = "Hello Pipe!";
    int pipe;
    int fd;
    errno = 0;
    __android_log_write(ANDROID_LOG_ERROR, "NDK_FOO_TAG 0", PATH);
    // open a named pipe
    mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
    pipe = mkfifo(PATH, mode);
    if(errno != 0) {
        __android_log_write(ANDROID_LOG_ERROR, "NDK_FOO_TAG 1", strerror(errno));
       //fd = open(PATH, O_WRONLY);
    }
    __android_log_write(ANDROID_LOG_DEBUG, "NDK_FOO_TAG", "file opened successfully");

    // actually write out the data and close the pipe
    int err = write(pipe, line, strlen(line));
    __android_log_write(ANDROID_LOG_DEBUG, "NDK_FOO_TAG Success",strerror(errno));

    // close the pipe
    close(pipe);

    //-----------
}

Native call

extern "C"
JNIEXPORT void JNICALL
Java_com_mynative_library_minterface_InitPipe(JNIEnv *env, jobject instance,
                                                    jstring pipePath_) {
    const char *pipePath = env->GetStringUTFChars(pipePath_, 0);
    createPipe(pipePath);

    // TODO

    env->ReleaseStringUTFChars(pipePath_, pipePath);
}

JAVA

private void pipeRead(){
        try{
            // Reader reader = new FileReader(PATH);
            FileReader mReader = new FileReader(FIFO_PATH);
            System.out.println("NativeCommunicator Started pipe reader waiting .2..");
            BufferedReader mPipeReader= new BufferedReader(mReader);
            System.out.println("NativeCommunicator Started pipe reader waiting ...");
            while (mPipeReader.ready()) {
                System.out.println("NativeCommunicator Data from Pipe : " + mPipeReader.readLine());
            }
            mPipeReader.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

Java code always waiting in FileReader mReader = new FileReader(FIFO_PATH);

Stack trace

E/NDK_FOO_TAG 0: /data/user/0/com.myproject.pipe/files/pipe_0
11-29 19:37:15.821 32476-32476/com.myproject.pipe D/NDK_FOO_TAG: file opened successfully
11-29 19:37:15.821 32476-32476/com.myproject.pipe D/NDK_FOO_TAG Success: Success
11-29 19:39:32.471 665-701/? E/QCALOG: [MessageQ] ProcessNewMessage: [XT-CS] unknown deliver target [OS-Agent]
11-29 19:39:32.689 665-701/? E/QCALOG: [MessageQ] ProcessNewMessage: [XTWiFi-PE] unknown deliver target [OS-Agent]
11-29 19:39:32.880 665-701/? E/QCALOG: [MessageQ] ProcessNewMessage: [XT-CS] unknown deliver target [OS-Agent]
11-29 19:39:37.471 665-701/? E/QCALOG: [MessageQ] ProcessNewMessage: [XT-CS] unknown deliver target [OS-Agent]
CLIFFORD P Y
  • 16,974
  • 6
  • 30
  • 45
  • Can you post a full stack trace of the thread that's stuck in `new FileReader(FIFO_PATH);`, including any native methods? I suspect you're seeing the Java equivalent of https://stackoverflow.com/questions/8507810/why-does-my-program-hang-when-opening-a-mkfifo-ed-pipe, where the `open()` of a FIFO hangs until a writer process opens it. – Andrew Henle Nov 29 '17 at 13:52
  • added stack trace – CLIFFORD P Y Nov 29 '17 at 14:09
  • The data you wrote to the pipe won't magically stay there after you close it. You seem to be looking for an ordinary file. NB It isn't valid to use or log `errno` unless there was an error. – user207421 Nov 29 '17 at 16:03

0 Answers0