//=============================================== class GMain { public static void main(String[] _args) { GProcess lProcess = new GProcess(); lProcess.init(); lProcess.run(_args); lProcess.getLogs().showLogsX(); lProcess.clean(); } } //===============================================
//=============================================== public void run(String[] _args) { String lModule = ""; if(_args.length > 0) lModule = _args[0]; if(lModule.equals("")) { m_logs.addError("Le module est obligatoire."); } else if(lModule.equals("test")) { runTest(_args); } else if(lModule.equals("server")) { runServer(_args); } else { m_logs.addError("Le module est inconnu."); } } //===============================================
//=============================================== public void run(String[] _args) { String lModule = ""; if(_args.length > 0) lModule = _args[0]; if(lModule.equals("")) { m_logs.addError("Le module est obligatoire."); } else if(lModule.equals("test")) { runTest(_args); } else if(lModule.equals("server")) { runServer(_args); } else { m_logs.addError("Le module est inconnu."); } } //===============================================
//=============================================== public void addLogs(GLog _obj) { for(int i = 0; i < _obj.m_map.size(); i++) { GLog lObj = _obj.m_map.get(i); GLog lNew = new GLog(); lNew.setObj(lObj); m_map.add(lNew); } } //===============================================
//=============================================== public void runServer() { while(true) { if(!m_continue) break; GSocket lClient = new GSocket(); try { lClient.m_socket = lServer.accept(); } catch(Exception e) { m_srvLogs.addError("La connexion au serveur a échoué."); break; } GThread lThread = new GThread(); lThread.setObj(lClient); lThread.start(); } } //===============================================
//=============================================== public void addLogs(GLog _obj) { for(int i = 0; i < _obj.m_map.size(); i++) { GLog lObj = _obj.m_map.get(i); GLog lNew = new GLog(); lNew.setObj(lObj); m_map.add(lNew); } } //===============================================
//=============================================== public boolean hasErrors() { for(int i = 0; i < m_map.size(); i++) { GLog lObj = m_map.get(i); if(lObj.m_type.equals("error")) return true; } return false; } //===============================================
//=============================================== public class GManager extends GObject { //=============================================== protected String m_module = ""; protected String m_method = ""; //=============================================== public GManager() { } //=============================================== public GManager(String _module, String _method) { m_module = _module; m_method = _method; } //=============================================== } //===============================================
//=============================================== public class GXml { //=============================================== public Element addObj(String _name) { Element lNode = new Element(_name); m_node.addContent(lNode); return lNode; } //=============================================== public Element addObj(String _name, String _value) { Element lNode = new Element(_name); lNode.setText(_value); m_node.addContent(lNode); return lNode; } //=============================================== } //===============================================
//=============================================== public class GSocket extends GObject { //=============================================== private static final int BUFFER_SIZE = 1024; // 1 ko private static final int BUFFER_MAX = 1*1024*1024; // 1 Mo //=============================================== } //===============================================
//=============================================== public class GSocket extends GObject { //=============================================== public String readData() { while(true) { byte[] lBuffer = new byte[BUFFER_SIZE]; int lBytes = lStreamIn.read(lBuffer); if(lBytes == -1) break; lData += new String(lBuffer, 0, lBytes, StandardCharsets.UTF_8); if(lStreamIn.available() <= 0) break; if(lData.length() >= BUFFER_MAX) { m_srvLogs.addError("La limite des données est atteinte."); break; } } } //=============================================== } //===============================================
//=============================================== public class GLog { //=============================================== private ArrayList<GLog> m_map = new ArrayList<GLog>(); //=============================================== } //===============================================
//=============================================== public class GLog { //=============================================== public void addLogs(GLog _obj) { for(int i = 0; i < _obj.m_map.size(); i++) { GLog lObj = _obj.m_map.get(i); GLog lNew = new GLog(); lNew.setObj(lObj); m_map.add(lNew); } } //=============================================== } //===============================================
//=============================================== public class GThread extends Thread { //=============================================== GObject m_obj = null; //=============================================== public GThread() { } //=============================================== public void setObj(GObject _obj) { m_obj = _obj; } //=============================================== public void run() { m_obj.runThread(); } //=============================================== } //===============================================
//=============================================== public void runServer() { while(true) { GThread lThread = new GThread(); lThread.setObj(lClient); lThread.start(); } } //===============================================
//=============================================== public void runThread() { GSocket lClient = this; String lData = lClient.readData(); GServer lServer = new GServer(); lServer.run(lData); lServer.sendResponse(lClient); lClient.closeSocket(); } //===============================================
//=============================================== public class GLog { } //===============================================
//=============================================== public class GLog { //=============================================== private String m_type = ""; private String m_side = ""; private String m_msg = ""; private ArrayList<GLog> m_map = new ArrayList<GLog>(); //=============================================== } //===============================================
//=============================================== public class GLog { //=============================================== public GLog() { } //=============================================== } //===============================================
//=============================================== public class GLog { //=============================================== public void addLogs(GLog _obj) { for(int i = 0; i < _obj.m_map.size(); i++) { GLog lObj = _obj.m_map.get(i); GLog lNew = new GLog(); lNew.setObj(lObj); m_map.add(lNew); } } //=============================================== } //===============================================
//=============================================== public class GObject { //=============================================== public GObject() { } //=============================================== public String toBase64(String _data) { return Base64.getEncoder().encodeToString(_data.getBytes()); } //=============================================== public String fromBase64(String _data) { return new String(Base64.getDecoder().decode(_data)); } //=============================================== } //===============================================
//=============================================== public class GCalculator extends GObject { //=============================================== public GCalculator() { } //=============================================== public String serialize() { lDom.addData(lCode, "expression", toBase64(m_expression)); } //=============================================== public void deserialize(String _data) { m_expression = fromBase64(lDom.getData(lCode, "expression")); } //=============================================== } //===============================================
//=============================================== public class GCalculator extends GManager { //=============================================== public GCalculator() { } //=============================================== public void deserialize(String _data) { super.deserialize(_data); } //=============================================== } //===============================================
//=============================================== public class GObject { //=============================================== public GObject() { } //=============================================== public void runThread() {} //=============================================== } //===============================================
//=============================================== public class GSocket extends GObject { //=============================================== public GSocket() { } //=============================================== public void runThread() { GSocket lClient = this; String lData = lClient.readData(); GServer lServer = new GServer(); lServer.run(lData); lServer.sendResponse(lClient); lClient.closeSocket(); } //=============================================== } //===============================================
//=============================================== public class GThread extends Thread { //=============================================== GObject m_obj = null; //=============================================== public void run() { m_obj.runThread(); } //=============================================== } //===============================================
//=============================================== public class GSocket extends GObject { //=============================================== public void runServer() { int lPort = 9040; ServerSocket lServer = null; try { lServer = new ServerSocket(lPort); System.out.print(String.format("Démarrage du serveur...\n")); while(true) { if(!m_continue) break; GSocket lClient = new GSocket(); try { lClient.m_socket = lServer.accept(); } catch(Exception e) { m_srvLogs.addError("La connexion au serveur a échoué."); break; } GThread lThread = new GThread(); lThread.setObj(lClient); lThread.start(); } System.out.print(String.format("Arrêt du serveur...\n")); lServer.close(); } catch(Exception e) { m_srvLogs.addError("Le démarrage du serveur a échoué."); } if(m_srvLogs.hasErrors()) { m_logs.addLogs(m_srvLogs); } } //=============================================== } //===============================================
//=============================================== public class GSocket extends GObject { //=============================================== public String callServer(String _data) { String lAddress = "127.0.0.1"; int lPort = 9040; String lDataOut = ""; try { InetAddress lInetAddress = InetAddress.getByName(lAddress); m_socket = new Socket(lInetAddress, lPort); sendData(_data); lDataOut = readData(); closeSocket(); } catch(Exception e) { m_srvLogs.addError("La connexion au serveur a échoué."); } if(m_srvLogs.hasErrors()) { m_logs.addError("La connexion au serveur a échoué."); } return lDataOut; } //=============================================== } //===============================================
//=============================================== public class GSocket extends GObject { //=============================================== public void sendData(String _data) { try { DataOutputStream lStreamOut = new DataOutputStream(m_socket.getOutputStream()); lStreamOut.write(_data.getBytes()); } catch(Exception e) { m_srvLogs.addError("L'envoi des données a échoué."); } } //=============================================== } //===============================================
//=============================================== public class GSocket extends GObject { //=============================================== public String readData() { String lData = ""; try { InputStream lStreamIn = m_socket.getInputStream(); while(true) { byte[] lBuffer = new byte[BUFFER_SIZE]; int lBytes = lStreamIn.read(lBuffer); if(lBytes == -1) break; lData += new String(lBuffer, 0, lBytes, StandardCharsets.UTF_8); if(lStreamIn.available() <= 0) break; if(lData.length() >= BUFFER_MAX) { m_srvLogs.addError("La limite des données est atteinte."); break; } } } catch(Exception e) { m_srvLogs.addError("La lecture des données a échoué."); } return lData; } //=============================================== } //===============================================
//=============================================== public class GSocket extends GObject { //=============================================== public void closeSocket() { try { m_socket.close(); } catch(Exception e) { m_srvLogs.addError("La fermeture du socket a échoué."); } } //=============================================== } //===============================================
//=============================================== public void createDoc() { m_doc = new Document(); m_node = new Element("root"); m_doc.addContent(m_node); } //===============================================
//=============================================== public boolean loadXml(String _data) { try { SAXBuilder lSax = new SAXBuilder(); m_doc = lSax.build(new StringReader(_data)); m_node = m_doc.getRootElement(); } catch (Exception e) { m_logs.addError("La création du document a échoué."); } return !m_logs.hasErrors(); } //===============================================
//=============================================== public boolean loadFile(String _filename) { try { SAXBuilder lSax = new SAXBuilder(); lSax.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); lSax.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); m_doc = lSax.build(new File(_filename)); m_node = m_doc.getRootElement(); } catch(Exception ex) { m_logs.addError("Le chargement du fichier a échoué."); } return !m_logs.hasErrors(); } //===============================================
//=============================================== public Element addObj(String _name) { Element lNode = new Element(_name); m_node.addContent(lNode); return lNode; } //===============================================
//=============================================== public Element addValue(String _name, String _value) { Element lNode = new Element(_name); lNode.setText(_value); m_node.addContent(lNode); return m_node; } //===============================================
//=============================================== public int countNode(String _path) { if(m_doc == null) return 0; XPathFactory lXPathF = XPathFactory.instance(); XPathExpression<Element> lXPathE = lXPathF.compile(_path, Filters.element()); List<Element> lNodes = lXPathE.evaluate(m_node); return lNodes.size(); } //===============================================
//=============================================== public Element getNode(String _path) { if(m_doc == null) return null; XPathFactory lXPathF = XPathFactory.instance(); XPathExpression<Element> lXPathE = lXPathF.compile(_path, Filters.element()); List<Element> lNodes = lXPathE.evaluate(m_node); if(lNodes.size() == 0) return null; return lNodes.get(0); } //===============================================
//=============================================== public void loadNode(String _data) { if(_data.equals("")) return; GCode lDom = new GCode(); lDom.loadXml(_data); m_node.addContent(lDom.m_node.detach()); } //===============================================
//=============================================== public String getValue() { if(m_node == null) return ""; return m_node.getValue(); } //===============================================
//=============================================== public String toString() { if(m_doc == null) return ""; XMLOutputter lXmlOut = new XMLOutputter(Format.getPrettyFormat()); String lData = lXmlOut.outputString(m_doc); return lData; } //===============================================
//=============================================== public String toNode() { if(m_node == null) return ""; XMLOutputter lXmlOut = new XMLOutputter(Format.getPrettyFormat()); String lData = lXmlOut.outputString(m_node); return lData; } //===============================================
//=============================================== public class GCalculator extends GManager { //=============================================== public void onRunCalculator(String _data) { Expression lExpression = new Expression(m_expression); Double lResult = lExpression.calculate(); } //=============================================== } //===============================================