From 55baba7354da6209d05db26697f4d9ef2a9407d0 Mon Sep 17 00:00:00 2001 From: xtof Date: Sun, 29 Mar 2020 21:11:25 +0200 Subject: [PATCH] SoundModem Package refactoring --- .../ModuleSoundModem/SoundModemCli.java | 21 ++- .../ModuleSoundModem/SoundModemClient.java | 146 +++++++++--------- .../SoundModemConfiguration.java | 36 +++-- 3 files changed, 105 insertions(+), 98 deletions(-) diff --git a/ModuleSoundModem/src/main/java/org/josast/ModuleSoundModem/SoundModemCli.java b/ModuleSoundModem/src/main/java/org/josast/ModuleSoundModem/SoundModemCli.java index fc62b57..13521a7 100644 --- a/ModuleSoundModem/src/main/java/org/josast/ModuleSoundModem/SoundModemCli.java +++ b/ModuleSoundModem/src/main/java/org/josast/ModuleSoundModem/SoundModemCli.java @@ -7,7 +7,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Arrays; +import java.util.logging.Logger; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; @@ -22,6 +22,8 @@ import org.apache.commons.cli.ParseException; * */ public class SoundModemCli { + + private Logger log = Logger.getLogger(getClass().getName()); private static String version = "0.1"; private String configurationfile = "defaultConfigFile.ini"; @@ -34,7 +36,7 @@ public class SoundModemCli { * */ public SoundModemCli() { - // TODO Auto-generated constructor stub + } public String getConfigurationfile() { @@ -119,10 +121,9 @@ public class SoundModemCli { public void execute() { - byte[] cbuf = new byte[1000]; // use soudmodem configuration file - SoundModemConfiguration config = new SoundModemConfiguration(configurationfile); - SoundModemClient sm = new SoundModemClient(config); + + SoundModemClient sm = new SoundModemClient(configurationfile); // tant que connecté à SoundModem => reçoit des trames int cpt = 0; @@ -140,16 +141,14 @@ public class SoundModemCli { while (true) { - int nb = sm.receiveData(cbuf); - sm.decode(cbuf, nb); - // extrait la partie utile - byte[] temp = Arrays.copyOf(cbuf, nb); + + byte[] temp = sm.receivedKissData(); + try { Files.write(Paths.get(generateRepository + "\\Frame_" + cpt+++".bin"), temp); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.severe(e.toString()); } System.out.println("compteur" + cpt++); } diff --git a/ModuleSoundModem/src/main/java/org/josast/ModuleSoundModem/SoundModemClient.java b/ModuleSoundModem/src/main/java/org/josast/ModuleSoundModem/SoundModemClient.java index 0ad3be7..66330cd 100644 --- a/ModuleSoundModem/src/main/java/org/josast/ModuleSoundModem/SoundModemClient.java +++ b/ModuleSoundModem/src/main/java/org/josast/ModuleSoundModem/SoundModemClient.java @@ -1,8 +1,7 @@ package org.josast.ModuleSoundModem; import java.io.*; import java.net.*; -import java.text.SimpleDateFormat; -import java.util.Date; +import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; @@ -12,37 +11,82 @@ import org.josast.AX25.KissFrame; public class SoundModemClient { private Logger log = Logger.getLogger(getClass().getName()); - private FileOutputStream dataFile ; - private SoundModemConfiguration smConfiguration = new SoundModemConfiguration("config.ini"); + + + private SoundModemConfiguration smConfiguration = null; private Socket skt=null; private InputStream in =null; private boolean open =false; + private int bufferSize=1024; + /** + * Initialise connection to soundmodem application with default value + * + */ public SoundModemClient() { - try { - skt = new Socket(smConfiguration.getIPadress(), smConfiguration.getPort()); - in = skt.getInputStream(); - openDataFile(); - open=true; - } catch (IOException e) { - log.log(Level.SEVERE, "create stream " + e.toString()); - } + + smConfiguration = new SoundModemConfiguration("config.ini"); + openSocket(); + } + /** + * Initialise connection to soundmodem application with configuration + * + */ + public SoundModemClient( SoundModemConfiguration smConfig) { + + smConfiguration=smConfig; + openSocket(); + + } + /** + * Initialise connection to soundmodem application with configuration file + * + */ + public SoundModemClient(String filename) { + + smConfiguration = new SoundModemConfiguration(filename); + openSocket(); + } + + private void openSocket() { + + try { + skt = new Socket(smConfiguration.getIPadress(), smConfiguration.getPort()); + in = skt.getInputStream(); + open=true; + } catch (IOException e) { + log.log(Level.SEVERE, "create stream " + e.toString()); + } } - public SoundModemClient( SoundModemConfiguration smConfiguration) { - try { - skt = new Socket(smConfiguration.getIPadress(), smConfiguration.getPort()); - in = skt.getInputStream(); - openDataFile(); - open=true; + /** + * wait bytes from sound modem + * + * @return bytes received in KISS format + */ + public byte[] receivedKissData() + { + byte[] cbuf = new byte[bufferSize]; + int nbbyteread=0; + + try { + nbbyteread = in.read(cbuf); + } catch (IOException e) { - log.log(Level.SEVERE, "create stream " + e.toString()); + + log.log(Level.SEVERE, "Error Read data " + e.toString()); + try { + in.close(); + } catch (IOException e1) { + log.log(Level.SEVERE, "Closing " + e1.toString()); + } } - + return Arrays.copyOf(cbuf, nbbyteread); } - + + @Deprecated public int receiveData (byte[] cbuf) { try { @@ -65,10 +109,10 @@ public class SoundModemClient { - + @Deprecated public AX25Frame decode (byte[] frame ,int nb) { - writeFile(frame, nb); + KissFrame kf=new KissFrame(); KissData kissData = kf.extactMessage(frame, nb); @@ -84,61 +128,23 @@ public class SoundModemClient { } + public void close() + { + try { + in.close(); + } catch (IOException e1) { + log.log(Level.SEVERE, "Closing " + e1.toString()); + } + } - public static void main(String[] args) { - // TODO Auto-generated method stub - - byte[] cbuf= new byte[1000]; - SoundModemClient sm = new SoundModemClient(); - int cpt=0; - System.out.println("client Sound Modem"); - while(true) - { - - - - int nb=sm.receiveData(cbuf); - sm.decode(cbuf,nb); - System.out.println("compteur"+cpt++); - } - - } public boolean isOpen() { return open; } + - private void writeFile(byte[] data, int nb) - { - try { - dataFile.write(data, 0, nb); - dataFile.write(1); - dataFile.write(2); - dataFile.write(3); - dataFile.write(4); - dataFile.flush(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - private void openDataFile() - { - Date aujourdhui = new Date(); - SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd-HHmmss"); - - try { - dataFile = new FileOutputStream (formater.format(aujourdhui)+"-"+".bin.log"); - - - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } } diff --git a/ModuleSoundModem/src/main/java/org/josast/ModuleSoundModem/SoundModemConfiguration.java b/ModuleSoundModem/src/main/java/org/josast/ModuleSoundModem/SoundModemConfiguration.java index 5f21419..78595b9 100644 --- a/ModuleSoundModem/src/main/java/org/josast/ModuleSoundModem/SoundModemConfiguration.java +++ b/ModuleSoundModem/src/main/java/org/josast/ModuleSoundModem/SoundModemConfiguration.java @@ -5,34 +5,40 @@ import java.util.logging.Logger; import org.josast.property.CONFIG; import org.josast.property.ConfigObject; +/** + * @author Christophe + * + */ public class SoundModemConfiguration extends ConfigObject { - + private Logger log = Logger.getLogger(getClass().getName()); - - private String IPadress= "localhost"; + + private String IPadress = "localhost"; private int port = 8100; - + protected CONFIG ConfigFile = CONFIG.getInstance(); - + public SoundModemConfiguration(String configFilename) { super(configFilename); + // vérifie si la propriété est dans le fichier String temp = ConfigFile.GetProperty("IPadress"); + if (temp == null) { // config file not initialised ConfigFile.SetProperty("IPadress", IPadress); - ConfigFile.SetProperty("port", ""+port); + ConfigFile.SetProperty("port", "" + port); log.severe("properties file not initialised - a file is created with properties with defaults value"); - System.out.println("config file does not content required properties \n the file "+ configFilename +" has been set up with properties to configure"); + System.out.println("config file does not content required properties \n the file " + configFilename + + " has been set up with properties to configure"); } else { IPadress = temp; String portString = ConfigFile.GetProperty("port"); - port = Integer.parseInt(portString); + port = Integer.parseInt(portString); } - - + } - + public String getIPadress() { return IPadress; } @@ -49,12 +55,8 @@ public class SoundModemConfiguration extends ConfigObject { this.port = port; } - - - public String toString() - { - return IPadress+":"+port; + public String toString() { + return IPadress + ":" + port; } - } -- GitLab