Commit 43986bc6 authored by Quentin Canel's avatar Quentin Canel

Merge branch '1-remove-inifile-lib' into 'master'

Resolve "Remove iniFile lib"

Closes #1

See merge request iot/BaseStation!3
parents 5d885de6 cd5cdc68
......@@ -21,6 +21,12 @@
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.ini4j/ini4j -->
<dependency>
<groupId>org.ini4j</groupId>
<artifactId>ini4j</artifactId>
<version>0.5.4</version>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
......
......@@ -7,24 +7,24 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.ini4j.Wini;
import fr.o2r.iot.basestation.exceptions.ConfigErrorException;
import fr.o2r.iot.basestation.exceptions.UnknownDataTypeException;
import fr.o2r.iot.basestation.exceptions.UnknownServiceException;
import fr.o2r.iot.basestation.modules.IOModule;
import fr.o2r.iot.basestation.modules.NullIOModule;
import fr.o2r.tools.IniFile;
public class Main {
private static Router router;
private static Logger log = Logger.getLogger(Main.class.getName());
private static IniFile ini;
private static Wini ini;
public static void main(String args[]) {
System.setProperty("line.separator","\r\n");
System.setProperty("line.separator", "\r\n");
router = new Router();
if (args.length == 0 || args[0].isEmpty()) {
......@@ -33,16 +33,16 @@ public class Main {
try {
InputStream in = new FileInputStream(args[0]);
ini = new IniFile(in);
ini = new Wini(in);
} catch (Exception e) {
ABORT("Error parsing config file");
}
// Parse services
Map<String, IOModule> services = new HashMap<>();
for (String service : ini.getSectionStrings("services").keySet()) {
String type = ini.getString("services", service, "").split(":")[0];
String config = ini.getString("services", service, "").split(":", 2)[1];
for (String service : ini.get("services").keySet()) {
String type = ini.get("services", service).split(":")[0];
String config = ini.get("services", service).split(":", 2)[1];
try {
IOModule ioModule = IOModule.createIOModule(service, type, router);
......@@ -60,32 +60,32 @@ public class Main {
}
// Parse packets
for (String packet : ini.getSectionStrings("packets").keySet()) {
int nodeId = Integer.parseInt(ini.getString("packets", packet, "").split(":")[0]);
String packetType = ini.getString("packets", packet, "").split(":")[1];
for (String packet : ini.get("packets").keySet()) {
int nodeId = Integer.parseInt(ini.get("packets", packet).split(":")[0]);
String packetType = ini.get("packets", packet).split(":")[1];
router.acceptPacket(packet, nodeId, packetType);
}
for (String packet : ini.getSectionStrings("incompletePackets").keySet()) {
String service = ini.getString("incompletePackets", packet, "").split(":")[0];
int nodeId = Integer.parseInt(ini.getString("incompletePackets", packet, "").split(":")[1]);
String packetType = ini.getString("incompletePackets", packet, "").split(":")[2];
String config = ini.getString("incompletePackets", packet, "").split(":",4)[3];
if(services.containsKey(service)) {
for (String packet : ini.get("incompletePackets").keySet()) {
String service = ini.get("incompletePackets", packet).split(":")[0];
int nodeId = Integer.parseInt(ini.get("incompletePackets", packet).split(":")[1]);
String packetType = ini.get("incompletePackets", packet).split(":")[2];
String config = ini.get("incompletePackets", packet).split(":", 4)[3];
if (services.containsKey(service)) {
try {
services.get(service).createDummyPacket(packet, nodeId, packetType, config);
router.acceptPacket(packet, nodeId, packetType);
} catch (UnknownDataTypeException e) {
log.error("Ignoring packet " + packet + " because the data type is not known");
}
}
}
// Parse routes
for (String route : ini.getSectionStrings("routes").keySet()) {
String[] routes = ini.getString("routes", route, "").split(",");
for (String route : ini.get("routes").keySet()) {
String[] routes = ini.get("routes", route).split(",");
for (String service : routes) {
IOModule module = services.get(service);
router.addRoute(route, module);
......
package fr.o2r.tools;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IniFile {
private Pattern _section = Pattern.compile("\\s*\\[([^]]*)\\]\\s*");
private Pattern _keyValue = Pattern.compile("\\s*([^=]*)=(.*)");
private Map<String, Map<String, String>> _entries = new HashMap<>();
public IniFile(InputStream inputStream) throws IOException {
load(inputStream);
}
public void load(InputStream inputStream) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
String section = null;
while ((line = br.readLine()) != null) {
Matcher m = _section.matcher(line);
if (m.matches()) {
section = m.group(1).trim();
} else if (section != null) {
m = _keyValue.matcher(line);
if (m.matches()) {
String key = m.group(1).trim();
String value = m.group(2).trim();
Map<String, String> kv = _entries.get(section);
if (kv == null) {
_entries.put(section, kv = new HashMap<>());
}
kv.put(key, value);
}
}
}
}
}
public String getString(String section, String key, String defaultvalue) {
Map<String, String> kv = _entries.get(section);
if (kv == null) {
return defaultvalue;
}
return kv.get(key);
}
public Map<String, String> getSectionStrings(String section) {
return _entries.get(section);
}
public int getInt(String section, String key, int defaultvalue) {
Map<String, String> kv = _entries.get(section);
if (kv == null) {
return defaultvalue;
}
return Integer.parseInt(kv.get(key));
}
public float getFloat(String section, String key, float defaultvalue) {
Map<String, String> kv = _entries.get(section);
if (kv == null) {
return defaultvalue;
}
return Float.parseFloat(kv.get(key));
}
public double getDouble(String section, String key, double defaultvalue) {
Map<String, String> kv = _entries.get(section);
if (kv == null) {
return defaultvalue;
}
return Double.parseDouble(kv.get(key));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment