Skip to content

Commit

Permalink
Refactoring and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DrTon committed Jul 7, 2018
1 parent d35960c commit b2ad7ee
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
MicroAvia Java libraries.

#### Modules List
- jmavlib-log: MicroAvia logs parsing (MicroAvia ULog v2)
- jmalib-log: MicroAvia logs parsing (MicroAvia ULog v2)

Based on deprecated [jMAVlib](https://github.com/DrTon/jMAVlib) project with some major improvements. Added Maven support.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
*/
public abstract class BinaryLogReader implements LogReader {
protected ByteBuffer buffer;
protected FileChannel channel = null;
protected long channelPosition = 0;
private FileChannel channel;
private long channelPosition = 0;

public BinaryLogReader(String fileName) throws IOException {
buffer = ByteBuffer.allocate(8192);
Expand All @@ -39,7 +39,7 @@ public int fillBuffer() throws IOException {
return n;
}

public void fillBuffer(int required) throws IOException {
protected void fillBuffer(int required) throws IOException {
if (buffer.remaining() < required) {
buffer.compact();
int n = channel.read(buffer);
Expand All @@ -51,11 +51,11 @@ public void fillBuffer(int required) throws IOException {
}
}

protected long position() throws IOException {
protected long position() {
return channelPosition - buffer.remaining();
}

protected int position(long pos) throws IOException {
protected void position(long pos) throws IOException {
buffer.clear();
channel.position(pos);
channelPosition = pos;
Expand All @@ -65,6 +65,5 @@ protected int position(long pos) throws IOException {
throw new EOFException();
}
channelPosition += n;
return n;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.microavia.jmalib.log;

import com.microavia.jmalib.log.ulog.Message;

import java.nio.ByteBuffer;

public class Subscription {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import com.microavia.jmalib.log.ValueParser;

public abstract class AbstractParser implements ValueParser {
protected final LogParserContext context;
protected int size = 0;
abstract class AbstractParser implements ValueParser {
final LogParserContext context;
int size = 0;

public AbstractParser(LogParserContext context) {
AbstractParser(LogParserContext context) {
this.context = context;
}

public static AbstractParser createFromTypeString(LogParserContext context, String typeString) {
private static AbstractParser createFromTypeString(LogParserContext context, String typeString) {
FieldParser field = FieldParser.create(context, typeString);
if (field != null) {
return field;
Expand All @@ -22,12 +22,12 @@ public static AbstractParser createFromTypeString(LogParserContext context, Stri
throw new RuntimeException("Unsupported type: " + typeString);
}

public static AbstractParser createFromFormatString(LogParserContext context, String formatStr) {
static AbstractParser createFromFormatString(LogParserContext context, String formatStr) {
if (formatStr.contains("[")) {
// Array
String[] q = formatStr.split("\\[");
String typeString = q[0];
int arraySize = Integer.parseInt(q[1].split("\\]")[0]);
int arraySize = Integer.parseInt(q[1].split("]")[0]);
if (typeString.equals("char") || typeString.equals("byte")) {
// Array that parsed as field
return FieldParser.create(context, typeString, arraySize);
Expand All @@ -42,7 +42,7 @@ public static AbstractParser createFromFormatString(LogParserContext context, St

abstract public AbstractParser clone();

public int size() {
int size() {
return size;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.nio.ByteBuffer;

public class ArrayParser extends AbstractParser {
class ArrayParser extends AbstractParser {
private final LogParserContext context;
private final AbstractParser[] items;

public ArrayParser(LogParserContext context, AbstractParser itemType, int arraySize) {
ArrayParser(LogParserContext context, AbstractParser itemType, int arraySize) {
super(context);
this.context = context;
this.size = itemType.size() * arraySize;
Expand All @@ -17,11 +17,11 @@ public ArrayParser(LogParserContext context, AbstractParser itemType, int arrayS
setOffset(0);
}

public AbstractParser[] getItems() {
AbstractParser[] getItems() {
return items;
}

public AbstractParser get(int idx) {
AbstractParser get(int idx) {
return items[idx];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
* Created by ton on 26.10.15.
*/
class FieldParser extends AbstractParser {
public final String type;
protected int offset = 0;
private final String type;
private int offset = 0;
private ValueParser valueParser;

public FieldParser(LogParserContext context, String type, ValueParser parser, int size) {
private FieldParser(LogParserContext context, String type, ValueParser parser, int size) {
super(context);
this.type = type;
this.valueParser = parser;
this.size = size;
}

public static FieldParser create(LogParserContext context, String typeString) {
static FieldParser create(LogParserContext context, String typeString) {
ValueParser valueParser;
int size;
switch (typeString) {
Expand Down Expand Up @@ -74,7 +74,7 @@ public static FieldParser create(LogParserContext context, String typeString) {
return new FieldParser(context, typeString, valueParser, size);
}

public static FieldParser create(LogParserContext context, String typeString, int size) {
static FieldParser create(LogParserContext context, String typeString, int size) {
ValueParser valueParser;
switch (typeString) {
case "char": {
Expand All @@ -100,6 +100,10 @@ public static FieldParser create(LogParserContext context, String typeString, in
return new FieldParser(context, typeString + "[]", valueParser, size);
}

String getType() {
return type;
}

@Override
public AbstractParser clone() {
return new FieldParser(context, type, valueParser, size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
import java.util.LinkedHashMap;
import java.util.Map;

public class StructParser extends AbstractParser {
public final LinkedHashMap<String, AbstractParser> fields;
class StructParser extends AbstractParser {
private final LinkedHashMap<String, AbstractParser> fields;

public StructParser(LogParserContext context, LinkedHashMap<String, AbstractParser> fields) {
private StructParser(LogParserContext context, LinkedHashMap<String, AbstractParser> fields) {
super(context);
this.fields = fields;
setOffset(0);
}

public StructParser(LogParserContext context, String formatStr) {
StructParser(LogParserContext context, String formatStr) {
super(context);
if (formatStr.length() > 1) {
String[] fieldDescrs = formatStr.split(";");
fields = new LinkedHashMap<>(fieldDescrs.length);
for (int i = 0; i < fieldDescrs.length; i++) {
String fieldDescr = fieldDescrs[i];
for (String fieldDescr : fieldDescrs) {
String[] p = fieldDescr.split(" ");
String name = p[1];
AbstractParser field = AbstractParser.createFromFormatString(context, p[0]);
Expand All @@ -32,11 +31,11 @@ public StructParser(LogParserContext context, String formatStr) {
setOffset(0);
}

public LinkedHashMap<String, AbstractParser> getFields() {
LinkedHashMap<String, AbstractParser> getFields() {
return fields;
}

public AbstractParser get(String key) {
AbstractParser get(String key) {
return fields.get(key);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.microavia.jmalib.log.ulog;

public class Message {
class Topic {
private final String name;
private final StructParser struct;
private final int id;

public Message(String name, StructParser struct, int id) {
Topic(String name, StructParser struct, int id) {
this.name = name;
this.struct = struct;
this.id = id;
Expand All @@ -15,11 +15,11 @@ public String getName() {
return name;
}

public StructParser getStruct() {
StructParser getStruct() {
return struct;
}

public int getId() {
int getId() {
return id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public class ULogReader extends BinaryLogReader {
private String systemName = "";
private String systemConfig = "";
private long dataStart = 0;
private Map<Integer, Message> messagesById = new HashMap<>();
private Map<String, Message> messagesByName = new HashMap<>();
private Map<String, Topic> topicByName = new HashMap<>();
private Map<String, String> fieldsList = null;
private Map<Integer, List<Subscription>> subscriptions = new HashMap<>();
private Set<Subscription> updatedSubscriptions = new HashSet<>();
Expand All @@ -38,7 +37,7 @@ public class ULogReader extends BinaryLogReader {
private Map<String, Object> parameters = new HashMap<>();
private List<Exception> errors = new ArrayList<>();
private int logVersion = 0;
private int headerSize = 2;
private int headerSize = 4;
private LogParserContext context = new LogParserContext();

public ULogReader(String fileName) throws IOException, FormatErrorException {
Expand All @@ -53,7 +52,7 @@ public static void main(String[] args) throws Exception {
try {
while (true) {
long t = reader.readUpdate();
for (Subscription s : reader.updatedSubscriptions) {
for (Subscription s : reader.getUpdatedSubscriptions()) {
System.out.println(t + " " + s.getPath() + " " + s.getValue());
}
}
Expand Down Expand Up @@ -122,7 +121,7 @@ private void updateStatistics() throws IOException, FormatErrorException {
logVersion = Integer.parseInt(logVersionStr.substring(3));
headerSize = 4;
} else {
throw new FormatErrorException("Unknown header");
throw new FormatErrorException("Unsupported file format");
}
startMicroseconds = -1;
timeLast = -1;
Expand All @@ -140,7 +139,7 @@ private void updateStatistics() throws IOException, FormatErrorException {

public Subscription addSubscription(String path) {
String[] parts = path.split("\\.");
Message msg = null;
Topic msg = null;
int multiIdFilter = -1;
AbstractParser parser = null;
for (String p : parts) {
Expand All @@ -149,7 +148,7 @@ public Subscription addSubscription(String path) {
if (pp.length > 1) {
multiIdFilter = Integer.parseInt(pp[1].split("]")[0]);
}
msg = messagesByName.get(pp[0]);
msg = topicByName.get(pp[0]);
parser = msg.getStruct();
} else {
if (parser instanceof StructParser) {
Expand Down Expand Up @@ -202,7 +201,7 @@ public Set<Subscription> getUpdatedSubscriptions() {
}

@Override
public boolean seek(long seekTime) throws IOException, FormatErrorException {
public boolean seek(long seekTime) throws IOException {
position(dataStart);
if (seekTime == 0) { // Seek to start of log
return true;
Expand Down Expand Up @@ -232,13 +231,12 @@ public Map<String, String> getFields() {
}

/**
* Read next message from log
* Read and handle next message from log
*
* @return log message
* @throws IOException on IO error
* @throws EOFException on end of stream
*/
public void readMessage(MessageHandler handler) throws IOException {
private void readMessage(MessageHandler handler) throws IOException {
while (true) {
fillBuffer(headerSize);
long pos = position();
Expand Down Expand Up @@ -296,9 +294,8 @@ private void handleHeaderMessage(long pos, int msgType, int msgSize) throws IOEx
String structName = descr[1];
System.out.printf("ADD: %s:%s\n", name, structName);
StructParser struct = context.getStructs().get(structName);
Message message = new Message(name, struct, msgId);
messagesById.put(msgId, message);
messagesByName.put(name, message);
Topic topic = new Topic(name, struct, msgId);
topicByName.put(name, topic);
addFieldsToList(name, struct);
break;
}
Expand Down Expand Up @@ -352,7 +349,7 @@ private void handleHeaderMessage(long pos, int msgType, int msgSize) throws IOEx

private void addFieldsToList(String path, AbstractParser value) {
if (value instanceof FieldParser) {
fieldsList.put(path, ((FieldParser) value).type);
fieldsList.put(path, ((FieldParser) value).getType());
} else if (value instanceof ArrayParser) {
AbstractParser[] items = ((ArrayParser) value).getItems();
for (int i = 0; i < items.length; i++) {
Expand Down Expand Up @@ -390,7 +387,7 @@ private void handleDataMessage(long pos, int msgType, int msgSize) {
buffer.position(bp + msgSize);
}

public String getString(ByteBuffer buffer, int len) {
private String getString(ByteBuffer buffer, int len) {
byte[] strBuf = new byte[len];
buffer.get(strBuf);
String[] p = new String(strBuf, context.getCharset()).split("\0");
Expand Down

0 comments on commit b2ad7ee

Please sign in to comment.