0
votes

I am confronting a problem with my school project, this code works perfectly:

    String adresse = "localhost";
    int port = 6565;
    new Thread(() -> {
        Serveur serveur = new Serveur(port);
        Patient ps;

        String messageHL7 = serveur.getMessageHL7();
        String messageHL7final = serveur.getMessageHL7lisible();
        System.out.println("---------------------------------------");
        System.out.println("messageHL7final : ");
        System.out.println(messageHL7final);
        System.out.println("---------------------------------------");
        ps = serveur.getPs();
        Parser parse = new Parser(messageHL7);
        System.out.println("---------------------------------------");
        System.out.println("parser : ");
        System.out.println(parse.getPatient().getBirth());
        System.out.println("---------------------------------------");
        serveur.fermeture();
        System.out.println("---------------------------------------");
        System.out.println("Patient serveur nom de famille : " + ps.getFamillyName());
        System.out.println("Patient serveur prénom : " + ps.getFirstName());
        System.out.println("Patient serveur IPP : " + ps.getID());
        System.out.println("Patient serveur est mort : " + ps.isDeath());
        System.out.println("Patient serveur sexe : " + ps.getCharSex());
        System.out.println("Patient serveur date de naissance : " + ps.getBirth());
        System.out.println("---------------------------------------");
    }).start();

    DPI dpi = DAOFactory.getDpiDAO().find(new ArrayList<>(Arrays.asList("IPP")), new ArrayList<>(Arrays.asList("170000001")));

    Client client = new Client(dpi, adresse, port);

    System.out.println("---------------------------------------");
    System.out.println("Patient de la base de donnée: " + dpi.toString());
    System.out.println("---------------------------------------");

    System.out.println("---------------------------------------");
    System.out.println("test : ");
    Patient p = client.getP();
    System.out.println("---------------------------------------");

    System.out.println("---------------------------------------");
    System.out.println("Patient client nom de famille : " + p.getFamillyName()
            + "\nPatient client prénom : " + p.getFirstName()
            + "\nPatient client sexe : " + p.getCharSex()
            + "\nPatient client date de naissance : " + p.getBirth()
            + "\nPatient clientIPP : " + p.getID()
            + "\nPatient client est mort : " + p.isDeath()
            + "\nPatient client date de décès : " + p.getDeath()
            + "\nPatient client est parti le : " + p.getDateDicharge());
    System.out.println("---------------------------------------");

sending me this response:

---------------------------------------
Patient de la base de donnée: Guille Angélique, FEMME, né(e) le  1970-10-21 IPP : 170000001
---------------------------------------
---------------------------------------
test : 
---------------------------------------
---------------------------------------
Patient client nom de famille : Angélique
Patient client prénom : Guille
Patient client sexe : F
Patient client date de naissance : Wed Oct 21 19:20:13 CET 1970
Patient clientIPP : 170000001
Patient client est mort : false
Patient client date de décès : null
Patient client est parti le : null
---------------------------------------
year:null
month:null
day:null
hour:null
minutes:null
seconde:null
-------------apresProtocole-----------------
date de naissanceWed Oct 21 19:20:13 CET 1970
---------------------------------------
messageHL7final : 
MSH|^~\&|||||20170321192013+0100||ADT^A01|1525900894|P|2.5||||||||||
EVN|A01|20170321192013+0100|||||
PID|1||170000001||Angélique^Guille||19701021192013+0100|F|||||||||||||||||||||N||||||||||||||||||||||
PV1||Inpatient|^^^^^^^^|||^^^^^^^^||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||


---------------------------------------
year:null
month:null
day:null
hour:null
minutes:null
seconde:null
---------------------------------------
parser : 
Mon Nov 21 19:20:13 CET 3870
---------------------------------------
---------------------------------------
Patient serveur nom de famille : Angélique
Patient serveur prénom : Guille
Patient serveur IPP : 170000001
Patient serveur est mort : false
Patient serveur sexe : F
Patient serveur date de naissance : Wed Oct 21 19:20:13 CET 1970
---------------------------------------

but when I tried to decompose the server and the client (I removed the sout):

serveur runable:

int port = 6565;//port d'écoute de mon serveur
    new Thread(() -> {
        Serveur serveur = new Serveur(port);
        Patient ps;

        String messageHL7 = serveur.getMessageHL7();
        String messageHL7final = serveur.getMessageHL7lisible();
        ps = serveur.getPs();
        Parser parse = new Parser(messageHL7);
    }).start();

and the client runable:

    String adresse = "localhost"; //adresse du serveur à joindre
    int port = 6565; //port d'écoute du serveur

    DPI dpi = DAOFactory.getDpiDAO().find(new ArrayList<>(Arrays.asList("IPP")), new ArrayList<>(Arrays.asList("170000001")));

    Client client = new Client(dpi, adresse, port);
    Patient p = client.getP();

it bring me back an error:

mars 21, 2017 7:25:02 PM protocole.MinLLPReader getMessage GRAVE: null java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:209) at java.net.SocketInputStream.read(SocketInputStream.java:141) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.BufferedReader.fill(BufferedReader.java:161) at java.io.BufferedReader.readLine(BufferedReader.java:324) at java.io.BufferedReader.readLine(BufferedReader.java:389) at protocole.MinLLPReader.getMessage(MinLLPReader.java:50) at library.interfaces.ServeurHL7.protocole(ServeurHL7.java:84) at interoperabilite.Serveur.(Serveur.java:28) at interoperabilite.TestHL7Serveur.lambda$main$0(TestHL7Serveur.java:24) at java.lang.Thread.run(Thread.java:745)

Exception in thread "Thread-0" java.lang.NullPointerException at protocole.MinLLPReader.getMessage(MinLLPReader.java:56) at library.interfaces.ServeurHL7.protocole(ServeurHL7.java:84) at interoperabilite.Serveur.(Serveur.java:28) at interoperabilite.TestHL7Serveur.lambda$main$0(TestHL7Serveur.java:24) at java.lang.Thread.run(Thread.java:745)

So after this I looked on the internet for help but I didn't find a case close to mine. The things that bother me is that it work when they are together but if i take them apart it doesn't.

I looked at this thread: communication between run() method and other class method (java threads) and thus this explanation http://javarevisited.blogspot.fr/2012/07/countdownlatch-example-in-java.html but didn't get the explanation. And I don't even know if this is similar because the final goal is that one computer will run the server code when he require the reception of the HL7 message and the other one run the client code to transmit this message.

1
Does it sout the patient details if you only run the server but not the client? Alternatively, does it work if you run both, but delete all the code in the server thread except Serveur serveur = new Serveur(port);?whistling_marmot
Try to reduce your way-too-large input into a true minimal reproducible example; and btw: translate that code to English then. Will dramatically improve your chances of getting an answer.GhostCat

1 Answers

0
votes

Temporary solution I have found:

for the client i add a thread with an infinite while and it worked, still unknown why. I will look further tomorrow:

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package interoperabilite;

import api.Parser;
import db.GestionnaireDB.DAOFactory;
import java.util.ArrayList;
import java.util.Arrays;
import library.interfaces.Patient;
import nf.DPI.DPI;

/**
 *
 * @author quentin
 */
public class TestHL7Client {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String adresse = "localhost"; //adresse du serveur à joindre
        int port = 6565; //port d'écoute du serveur
        new Thread(() -> {
            while(true){

            }
        }).start();
        DPI dpi = DAOFactory.getDpiDAO().find(new ArrayList<>(Arrays.asList("IPP")), new ArrayList<>(Arrays.asList("170000001")));

        Client client = new Client(dpi, adresse, port);

        System.out.println("---------------------------------------");
        System.out.println("Patient de la base de donnée: " + dpi.toString());
        System.out.println("---------------------------------------");

        System.out.println("---------------------------------------");
        System.out.println("test : ");
        Patient p = client.getP();
        System.out.println("---------------------------------------");

        System.out.println("---------------------------------------");
        System.out.println("Patient client nom de famille : " + p.getFamillyName()
                + "\nPatient client prénom : " + p.getFirstName()
                + "\nPatient client sexe : " + p.getCharSex()
                + "\nPatient client date de naissance : " + p.getBirth()
                + "\nPatient clientIPP : " + p.getID()
                + "\nPatient client est mort : " + p.isDeath()
                + "\nPatient client date de décès : " + p.getDeath()
                + "\nPatient client est parti le : " + p.getDateDicharge());
        System.out.println("---------------------------------------");
    }

}

and the server part is still:

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package interoperabilite;

import api.Parser;
import library.interfaces.Patient;

/**
 *
 * @author quentin
 */
public class TestHL7Serveur {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int port = 6565;//port d'écoute de mon serveur
        new Thread(() -> {
            Serveur serveur = new Serveur(port);
            Patient ps;

            System.out.println("TestHL7Serveur");
            String messageHL7 = serveur.getMessageHL7();
            String messageHL7final = serveur.getMessageHL7lisible();
            System.out.println("---------------------------------------");
            System.out.println("messageHL7final : ");
            System.out.println(messageHL7final);
            System.out.println("---------------------------------------");
            ps = serveur.getPs();
            Parser parse = new Parser(messageHL7);
            System.out.println("---------------------------------------");
            System.out.println("parser : ");
            System.out.println(parse.getPatient().getBirth());
            System.out.println("---------------------------------------");
            serveur.fermeture();
            System.out.println("---------------------------------------");
            System.out.println("Patient serveur nom de famille : " + ps.getFamillyName());
            System.out.println("Patient serveur prénom : " + ps.getFirstName());
            System.out.println("Patient serveur IPP : " + ps.getID());
            System.out.println("Patient serveur est mort : " + ps.isDeath());
            System.out.println("Patient serveur sexe : " + ps.getCharSex());
            System.out.println("Patient serveur date de naissance : " + ps.getBirth());
            System.out.println("---------------------------------------");
        }).start();
    }
}

client send:

-----------testConnexion------------
PV1: PV1||Inpatient|^^^^^^^^|||^^^^^^^^||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---------------------------------------
Patient de la base de donnée: Guille Angélique, FEMME, né(e) le  1970-10-21 IPP : 170000001
---------------------------------------
---------------------------------------
test : 
---------------------------------------
---------------------------------------
Patient client nom de famille : Angélique
Patient client prénom : Guille
Patient client sexe : F
Patient client date de naissance : Wed Oct 21 22:08:52 CET 1970
Patient clientIPP : 170000001
Patient client est mort : false
Patient client date de décès : null
Patient client est parti le : null
---------------------------------------

server received:

year:null
month:null
day:null
hour:null
minutes:null
seconde:null
date de naissanceWed Oct 21 22:08:52 CET 1970
TestHL7Serveur
---------------------------------------
messageHL7final : // HL7 message:
MSH|^~\&|||||20170321220852+0100||ADT^A01|-1254244946|P|2.5||||||||||
EVN|A01|20170321220852+0100|||||
PID|1||170000001||Angélique^Guille||19701021220852+0100|F|||||||||||||||||||||N||||||||||||||||||||||
PV1||Inpatient|^^^^^^^^|||^^^^^^^^||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||


---------------------------------------
year:null
month:null
day:null
hour:null
minutes:null
seconde:null
---------------------------------------
parser : 
Mon Nov 21 22:08:52 CET 3870
---------------------------------------
---------------------------------------
Patient serveur nom de famille : Angélique
Patient serveur prénom : Guille
Patient serveur IPP : 170000001
Patient serveur est mort : false
Patient serveur sexe : F
Patient serveur date de naissance : Wed Oct 21 22:08:52 CET 1970
---------------------------------------

I think that i will send back a message from the server to the client to stop the infinite loop since it work this way.

I will update with the minimal complete and verifiable but it's tricky because we use an API compiled. And if someone know why with the infinite loop + thread this work please explain me :)