grammar calculation is done at start (no more slow decoding/encoding of multiple messages)

This commit is contained in:
blackwiz4rd 2021-03-04 12:45:55 +01:00
parent 35a45da8f6
commit 3d79c7d822
4 changed files with 101 additions and 52 deletions

View File

@ -5,6 +5,9 @@ import java.io.IOException;
import org.apache.commons.cli.*;
import org.xml.sax.SAXException;
import com.siemens.ct.exi.core.exceptions.EXIException;
import com.siemens.ct.exi.core.grammars.Grammars;
import com.siemens.ct.exi.grammars.GrammarFactory;
import com.v2gclarity.risev2g.shared.enumerations.GlobalValues;
import com.fluxlus.V2Gdecoder.dataprocess.*;
/*
@ -53,7 +56,27 @@ public class V2Gdecoder {
//String outputFilePath = cmd.getOptionValue("output"); /* TODO: custom output file */
decodeMode dmode = decodeMode.STRTOSTR;
String result = null;
/* Initialize grammars */
Grammars[] grammars = {null, null, null};
/* BOTTLENECK: slow operation */
try {
grammars[0] = GrammarFactory.newInstance().createGrammars("." + GlobalValues.SCHEMA_PATH_MSG_DEF.toString());
} catch (EXIException e) {
e.printStackTrace();
}
try {
grammars[1] = GrammarFactory.newInstance().createGrammars("." + GlobalValues.SCHEMA_PATH_APP_PROTOCOL.toString());
} catch (EXIException e) {
e.printStackTrace();
}
try {
grammars[2] = GrammarFactory.newInstance().createGrammars("." + GlobalValues.SCHEMA_PATH_XMLDSIG.toString());
} catch (EXIException e) {
e.printStackTrace();
}
if (cmd.hasOption("xml"))
{ // We wan to encode a XML input
if (cmd.hasOption("file"))
@ -66,7 +89,7 @@ public class V2Gdecoder {
if (cmd.hasOption("output"))
dmode = decodeMode.STRTOSTR;
}
result = dataprocess.Xml2Exi(inputFilePath, dmode);
result = dataprocess.fuzzyExiEncoder(inputFilePath, dmode, grammars);
if (!cmd.hasOption("output"))
System.out.println(result);
} else if (cmd.hasOption("exi")) { // We wan to decode an EXI input
@ -80,13 +103,13 @@ public class V2Gdecoder {
if (cmd.hasOption("output"))
dmode = decodeMode.STRTOFILE;
}
result = dataprocess.fuzzyExiDecoded(inputFilePath, dmode);
result = dataprocess.fuzzyExiDecoded(inputFilePath, dmode, grammars);
if (!cmd.hasOption("output"))
{ // output in stdout
System.out.println(result);
}
} else if (cmd.hasOption("web")) { // run a encoder/decoder service on port TCP 9000
MultiThreadedServer server = new MultiThreadedServer(9000);
MultiThreadedServer server = new MultiThreadedServer(9000, grammars);
new Thread(server).start();
}
}

View File

@ -18,8 +18,8 @@ import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import com.v2gclarity.risev2g.shared.enumerations.GlobalValues;
import com.v2gclarity.risev2g.shared.messageHandling.MessageHandler;
import com.v2gclarity.risev2g.shared.utils.MiscUtils;
// import com.v2gclarity.risev2g.shared.messageHandling.MessageHandler;
// import com.v2gclarity.risev2g.shared.utils.MiscUtils;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
@ -27,6 +27,7 @@ import org.xml.sax.helpers.XMLReaderFactory;
import com.siemens.ct.exi.core.EXIFactory;
import com.siemens.ct.exi.grammars.GrammarFactory;
import com.siemens.ct.exi.core.grammars.Grammars;
import com.siemens.ct.exi.main.api.sax.EXIResult;
import com.siemens.ct.exi.main.api.sax.EXISource;
import com.siemens.ct.exi.core.exceptions.EXIException;
@ -39,17 +40,17 @@ import com.fluxlus.V2Gdecoder.binascii.BinAscii;
*/
public class dataprocess {
public MessageHandler messageHandler;
// public MessageHandler messageHandler;
public MessageHandler getMessageHandler() {
return messageHandler;
}
// public MessageHandler getMessageHandler() {
// return messageHandler;
// }
public static void initConfig() {
MiscUtils.loadProperties("./test.properties");
}
// public static void initConfig() {
// MiscUtils.loadProperties("./test.properties");
// }
public static String Xml2Exi(String xmlstr, decodeMode mode) throws IOException, SAXException, EXIException {
public static String Xml2Exi(String inputsc, String xmlstr, decodeMode mode, Grammars grammar) throws IOException, SAXException, EXIException {
/*
* Encode XML to EXI
* In(1): XML string or input file path string
@ -57,29 +58,13 @@ public class dataprocess {
* Out: encoded result string
* */
EXIFactory exiFactory = DefaultEXIFactory.newInstance();
String grammar = null;
ByteArrayOutputStream bosEXI = null;
OutputStream osEXI = null;
String result = null;
String inputsc = null;
String outfile = null;
if (mode == decodeMode.FILETOSTR || mode == decodeMode.FILETOFILE)
{ // In case the input is a file
byte[] rbytes = Files.readAllBytes(Paths.get(xmlstr));
inputsc = new String(rbytes);
} else {
inputsc = xmlstr;
}
if (inputsc.contains("supportedAppProtocol"))
{ // select AppProtocol schema to set V2G grammar
grammar = GlobalValues.SCHEMA_PATH_APP_PROTOCOL.toString();
} else if (inputsc.contains("V2G_Message")) { // select XMLDSIG
grammar = GlobalValues.SCHEMA_PATH_MSG_DEF.toString();
} else { // MSG DEF by default
grammar = GlobalValues.SCHEMA_PATH_XMLDSIG.toString();
}
exiFactory.setGrammars(GrammarFactory.newInstance().createGrammars("." + grammar));
exiFactory.setGrammars(grammar);
EXIResult exiResult = new EXIResult(exiFactory);
if (mode == decodeMode.FILETOSTR || mode == decodeMode.STRTOSTR)
{ // stream output
@ -112,7 +97,7 @@ public class dataprocess {
return result;
}
public static String Exi2Xml(String existr, decodeMode mode, String grammar) throws IOException, SAXException, EXIException, TransformerException {
public static String Exi2Xml(String existr, decodeMode mode, Grammars grammar) throws IOException, SAXException, EXIException, TransformerException {
/*
* Decode EXI data to XML
* In(1): String to decode
@ -126,7 +111,7 @@ public class dataprocess {
Result res = null;
ByteArrayOutputStream outputStream = null;
InputSource is = null;
exiFactory.setGrammars(GrammarFactory.newInstance().createGrammars("." + grammar));
exiFactory.setGrammars(grammar);
if (mode == decodeMode.FILETOSTR || mode == decodeMode.FILETOFILE)
is = new InputSource(inputsc);
@ -162,8 +147,8 @@ public class dataprocess {
return result;
}
public static String fuzzyExiDecoded(String strinput, decodeMode dmode)
public static String fuzzyExiEncoder(String xmlstr, decodeMode dmode, Grammars[] grammars) throws IOException, SAXException
{
/*
* Enumerate V2G grammar to decode EXI data
@ -171,20 +156,58 @@ public class dataprocess {
* In(2): (decodeMode) Input/Output modes
* Out: Decoded result string
*/
String grammar = null;
String result = null;
String inputsc = null;
Grammars grammar = null;
if (dmode == decodeMode.FILETOSTR || dmode == decodeMode.FILETOFILE)
{ // In case the input is a file
byte[] rbytes = Files.readAllBytes(Paths.get(xmlstr));
inputsc = new String(rbytes);
} else {
inputsc = xmlstr;
}
/* Selects grammar intelligenly */
if (inputsc.contains("supportedAppProtocol"))
{ // select AppProtocol schema to set V2G grammar
grammar = grammars[1];
} else if (inputsc.contains("V2G_Message")) { // select XMLDSIG
grammar = grammars[2];
} else { // MSG DEF by default
grammar = grammars[0];
}
try {
result = Xml2Exi(inputsc, xmlstr, dmode, grammar);
} catch(EXIException e)
{
e.printStackTrace();
}
return result;
}
public static String fuzzyExiDecoded(String strinput, decodeMode dmode, Grammars[] grammars)
{
/*
* Enumerate V2G grammar to decode EXI data
* In(1): Input string
* In(2): (decodeMode) Input/Output modes
* Out: Decoded result string
*/
String result = null;
grammar = GlobalValues.SCHEMA_PATH_MSG_DEF.toString();
try {
result = Exi2Xml(strinput, dmode, grammar);
result = Exi2Xml(strinput, dmode, grammars[0]);
} catch (Exception e1) {
try {
grammar = GlobalValues.SCHEMA_PATH_APP_PROTOCOL.toString();
result = Exi2Xml(strinput, dmode, grammar);
result = Exi2Xml(strinput, dmode, grammars[1]);
} catch (Exception e2) {
grammar = GlobalValues.SCHEMA_PATH_XMLDSIG.toString();
try {
result = Exi2Xml(strinput, dmode, grammar);
result = Exi2Xml(strinput, dmode, grammars[2]);
} catch (EXIException e3) {
// do nothing
//e3.printStackTrace();

View File

@ -4,6 +4,7 @@ import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import com.siemens.ct.exi.core.grammars.Grammars;
/*
* Copyright (C) V2Gdecoder by FlUxIuS (Sebastien Dudek)
@ -15,9 +16,11 @@ public class MultiThreadedServer implements Runnable{
protected ServerSocket serverSocket = null;
protected boolean isStopped = false;
protected Thread runningThread= null;
protected Grammars[] grammars = null;
public MultiThreadedServer(int port){
public MultiThreadedServer(int port, Grammars[] grammars){
this.serverPort = port;
this.grammars = grammars;
}
public void run(){
@ -39,7 +42,7 @@ public class MultiThreadedServer implements Runnable{
}
new Thread(
new WorkerRunnable(
clientSocket, "Multithreaded Server")
clientSocket, this.grammars, "Multithreaded Server")
).start();
}
System.out.println("Server Stopped.") ;

View File

@ -12,6 +12,7 @@ import java.util.Map;
import org.xml.sax.SAXException;
import com.siemens.ct.exi.core.exceptions.EXIException;
import com.siemens.ct.exi.core.grammars.Grammars;
import com.fluxlus.V2Gdecoder.dataprocess.dataprocess;
import com.fluxlus.V2Gdecoder.dataprocess.decodeMode;
@ -20,10 +21,12 @@ public class WorkerRunnable implements Runnable{
protected Socket clientSocket = null;
protected String serverText = null;
protected Grammars[] grammars = null;
public WorkerRunnable(Socket clientSocket, String serverText) {
public WorkerRunnable(Socket clientSocket, Grammars[] grammars, String serverText) {
this.clientSocket = clientSocket;
this.serverText = serverText;
this.grammars = grammars;
}
public static Map<String, String> parseHTTPHeaders(InputStream inputStream)
@ -87,14 +90,11 @@ public class WorkerRunnable implements Runnable{
if (headers.get("Format").contains("EXI"))
{
result = dataprocess.fuzzyExiDecoded(body, decodeMode.STRTOSTR);
result = dataprocess.fuzzyExiDecoded(body, decodeMode.STRTOSTR, this.grammars);
} else {
try {
result = dataprocess.Xml2Exi(body, decodeMode.STRTOSTR);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (EXIException e) {
result = dataprocess.fuzzyExiEncoder(body, decodeMode.STRTOSTR, this.grammars);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}