Skip to content

Commit b7a1620

Browse files
Merge branch 'master' of github.com:GideonLeGrange/mikrotik-java
2 parents 8094704 + 3b45c54 commit b7a1620

File tree

8 files changed

+39
-71
lines changed

8 files changed

+39
-71
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
language: java

src/main/java/me/legrange/mikrotik/ApiConnectionException.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@
66
*/
77
public class ApiConnectionException extends MikrotikApiException {
88

9+
/**
10+
* Create a new exception.
11+
*
12+
* @param msg The message
13+
*/
914
public ApiConnectionException(String msg) {
1015
super(msg);
1116
}
1217

18+
/**
19+
* Create a new exception
20+
* @param msg The message
21+
* @param err The underlying cause
22+
*/
1323
public ApiConnectionException(String msg, Throwable err) {
1424
super(msg, err);
1525
}

src/main/java/me/legrange/mikrotik/MikrotikApiException.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@
77
*/
88
public class MikrotikApiException extends Exception {
99

10+
11+
/**
12+
* Create a new exception
13+
* @param msg The message
14+
*/
1015
public MikrotikApiException(String msg) {
1116
super(msg);
1217
}
1318

19+
20+
/**
21+
* Create a new exception
22+
* @param msg The message
23+
* @param err The underlying cause
24+
*/
1425
public MikrotikApiException(String msg, Throwable err) {
1526
super(msg, err);
1627
}

src/main/java/me/legrange/mikrotik/impl/ApiConnectionImpl.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public final class ApiConnectionImpl extends ApiConnection {
3131
/**
3232
* Create a new API connection to the give device on the supplied port
3333
*
34+
* @param fact The socket factory used to construct the connection socket.
3435
* @param host The host to which to connect.
3536
* @param port The TCP port to use.
3637
* @param timeOut The connection timeout
@@ -176,7 +177,7 @@ private class Reader extends Thread {
176177
private Reader() {
177178
super("Mikrotik API Reader");
178179
}
179-
180+
180181
private String take() throws ApiConnectionException, ApiDataException {
181182
Object val = null;
182183
try {
@@ -195,15 +196,13 @@ private String take() throws ApiConnectionException, ApiDataException {
195196
private boolean isEmpty() {
196197
return queue.isEmpty();
197198
}
198-
199+
199200
@Override
200201
public void run() {
201202
while (connected) {
202203
try {
203204
String s = Util.decode(in);
204-
if (s != null) {
205-
put(s);
206-
}
205+
put(s);
207206
} catch (ApiDataException ex) {
208207
put(ex);
209208
} catch (ApiConnectionException ex) {
@@ -232,7 +231,7 @@ private class Processor extends Thread {
232231
private Processor() {
233232
super("Mikrotik API Result Processor");
234233
}
235-
234+
236235
@Override
237236
public void run() {
238237
while (connected) {
@@ -420,35 +419,22 @@ private Error unpackError() throws MikrotikApiException {
420419
return err;
421420
}
422421

423-
private void queue(Response res) {
424-
String tag = res.getTag();
425-
if (tag != null) {
426-
ResultListener rl = listeners.get(tag);
427-
if (rl != null) {
428-
if (res instanceof Result) {
429-
rl.receive((Result) res);
430-
} else {
431-
// rl.error((Error)res);
432-
}
433-
}
434-
}
435-
}
436422
private final List<String> lines = new LinkedList<>();
437423
private String line;
438424
}
439425

440-
private class SyncListener implements ResultListener {
426+
private static class SyncListener implements ResultListener {
441427

442428
@Override
443429
public synchronized void error(MikrotikApiException ex) {
444430
this.err = ex;
445-
notify();
431+
notifyAll();
446432
}
447433

448434
@Override
449435
public synchronized void completed() {
450436
complete = true;
451-
notify();
437+
notifyAll();
452438
}
453439

454440
synchronized void completed(Done done) {
@@ -458,7 +444,7 @@ synchronized void completed(Done done) {
458444
results.add(res);
459445
}
460446
complete = true;
461-
notify();
447+
notifyAll();
462448
}
463449

464450
@Override

src/main/java/me/legrange/mikrotik/impl/Parser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private void returns() throws ParseException {
175175
}
176176
next();
177177
}
178-
cmd.addProperty(props.toArray(new String[]{}));
178+
cmd.addProperty(props.toArray(new String[props.size()]));
179179
}
180180

181181
private void expect(Token...tokens) throws ParseException {

src/main/java/me/legrange/mikrotik/impl/Scanner.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package me.legrange.mikrotik.impl;
1717

18+
import java.util.Locale;
1819
import static me.legrange.mikrotik.impl.Scanner.Token.*;
1920
/**
2021
* A simple scanner.
@@ -117,7 +118,7 @@ private Token name() throws ScanException {
117118
text.append(c);
118119
nextChar();
119120
}
120-
String val = text.toString().toLowerCase();
121+
String val = text.toString().toLowerCase(Locale.getDefault());
121122
switch (val) {
122123
case "where":
123124
return WHERE;
@@ -184,17 +185,6 @@ private void nextChar() {
184185
}
185186
}
186187

187-
/**
188-
* look ahead one character
189-
*/
190-
private char peek() {
191-
if (pos < line.length()) {
192-
return line.charAt(pos);
193-
} else {
194-
return '\n';
195-
}
196-
}
197-
198188
/**
199189
* check if the character matches the give expression
200190
*/

src/main/java/me/legrange/mikrotik/impl/Util.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.InputStream;
55
import java.io.OutputStream;
66
import java.io.UnsupportedEncodingException;
7+
import java.nio.charset.Charset;
78
import java.security.MessageDigest;
89
import java.security.NoSuchAlgorithmException;
910
import java.util.List;
@@ -71,7 +72,7 @@ private static void decode(InputStream in, StringBuilder result) throws ApiDataE
7172
}
7273
buf[i] = (byte) (c & 0xFF);
7374
}
74-
String res = new String(buf);
75+
String res = new String(buf, Charset.forName("UTF-8"));
7576
if (result.length() > 0) {
7677
result.append("\n");
7778
}
@@ -121,11 +122,11 @@ static String hashMD5(String s) throws ApiDataException {
121122
* @return - converted string.
122123
*/
123124
static String hexStrToStr(String s) {
124-
String ret = "";
125+
StringBuilder ret = new StringBuilder();
125126
for (int i = 0; i < s.length(); i += 2) {
126-
ret += (char) Integer.parseInt(s.substring(i, i + 2), 16);
127+
ret.append((char) Integer.parseInt(s.substring(i, i + 2), 16));
127128
}
128-
return ret;
129+
return ret.toString();
129130
}
130131

131132
/**

src/test/java/me/legrange/mikrotik/ApiConnectionTest.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)