entropia / libsocket-can-java

SocketCAN JNI wrapper
MIT License
44 stars 49 forks source link

response time problem #16

Open Gligor721 opened 9 years ago

Gligor721 commented 9 years ago

Hi

i have one problem with libsocket-can-java.I send a can frame and the microcontroler in another site receive mess and response with ack but i don receive this ack but with candump i see that i receive the can frame but in java no ack.In microcontroler i enter time delay so the microcontroler now receive the message from me and after 10ms response with ack and know in java i receive the ack.What is the time that java is needed?Or this is another problem?Pls help me.

p.s.In java i have main that only receive canframe and is puting in fifo and another thread poll frames from this fifo.

Gligor Shijakovski

procrastimax commented 5 years ago

Heyy, I know it's too late for this but I had the same problem and could solve it. Dumb how I am, I thought the CanSocket would be threadsafe and so I implemented an AsyncWorkerQueue which I used to send and receive can frames. For this I used one instance of the CanSocket. I could easily send all frames, but when receiving the receive order was mixed up. Even after implement a wait-timeout after sending a frame I couldn't make it work.

I now implemented a CanController class which uses the CanSocket but is thread-save and a singleton. I used this kind of singleton class:

public class SingletonClass {

    private static volatile SingletonClass sSoleInstance;

    //private constructor.
    private SingletonClass(){

        //Prevent form the reflection api.
        if (sSoleInstance != null){
            throw new RuntimeException("Use getInstance() method to get the single instance of this class.");
        }
    }

    public static SingletonClass getInstance() {
        //Double check locking pattern
        if (sSoleInstance == null) { //Check for the first time

            synchronized (SingletonClass.class) {   //Check for the second time.
              //if there is no instance available... create new one
              if (sSoleInstance == null) sSoleInstance = new SingletonClass();
            }
        }

        return sSoleInstance;
    }
}

This code snippet is from The perfect singleton.

Hopefully this helps someone.