Changed interface to no longer be dependent on all sorts of weird classes

This commit is contained in:
GideonLeGrange 2013-07-14 16:17:43 +02:00
parent 148e7b1912
commit d11e439fde
5 changed files with 30 additions and 23 deletions

View File

@ -98,15 +98,13 @@ public class ApiConnection {
* @return A command object that can be used to cancel the command. * @return A command object that can be used to cancel the command.
* @throws MikrotikApiException * @throws MikrotikApiException
*/ */
public Command execute(String cmd, ResultListener lis) throws MikrotikApiException { public String execute(String cmd, ResultListener lis) throws MikrotikApiException {
return execute(Parser.parse(cmd), lis); return execute(Parser.parse(cmd), lis);
} }
/** cancel a command */ /** cancel a command */
public void cancel(Command can) throws MikrotikApiException { public void cancel(String tag) throws MikrotikApiException {
Command cmd = new Command("/cancel"); execute(String.format("/cancel tag=%s", tag));
cmd.addParameter("tag", can.getTag());
execute(cmd);
} }
private List<Result> execute(Command cmd) throws MikrotikApiException { private List<Result> execute(Command cmd) throws MikrotikApiException {
@ -116,7 +114,7 @@ public class ApiConnection {
} }
private Command execute(Command cmd, ResultListener lis) throws MikrotikApiException { private String execute(Command cmd, ResultListener lis) throws MikrotikApiException {
String tag = nextTag(); String tag = nextTag();
cmd.setTag(tag); cmd.setTag(tag);
listeners.put(tag, lis); listeners.put(tag, lis);
@ -127,7 +125,7 @@ public class ApiConnection {
} catch (IOException ex) { } catch (IOException ex) {
throw new ApiConnectionException(ex.getMessage(), ex); throw new ApiConnectionException(ex.getMessage(), ex);
} }
return cmd; return tag;
} }
private ApiConnection() { private ApiConnection() {
@ -265,9 +263,14 @@ public class ApiConnection {
if (l instanceof ResponseListener) { if (l instanceof ResponseListener) {
ResponseListener rl = (ResponseListener) l; ResponseListener rl = (ResponseListener) l;
if (res instanceof Done) { if (res instanceof Done) {
rl.completed((Done) res); if (rl instanceof SyncListener) {
((SyncListener)rl).completed((Done)res);
}
else {
rl.completed();
}
} else if (res instanceof Error) { } else if (res instanceof Error) {
rl.error((Error) res); rl.error(new ApiCommandException((Error) res));
} }
} }
} }
@ -409,12 +412,16 @@ public class ApiConnection {
private class SyncListener implements ResponseListener { private class SyncListener implements ResponseListener {
public synchronized void error(Error err) { public synchronized void error(MikrotikApiException ex) {
this.err = err; this.err = ex;
notify(); notify();
} }
public synchronized void completed(Done done) { public void completed() {
notify();
}
synchronized void completed(Done done) {
if (done.getHash() != null) { if (done.getHash() != null) {
Result res = new Result(); Result res = new Result();
res.put("ret", done.getHash()); res.put("ret", done.getHash());
@ -427,7 +434,7 @@ public class ApiConnection {
results.add(result); results.add(result);
} }
private List<Result> getResults() throws ApiCommandException, ApiConnectionException { private List<Result> getResults() throws MikrotikApiException {
try { try {
synchronized (this) { // don't wait if we already have a result. synchronized (this) { // don't wait if we already have a result.
if ((err == null) && results.isEmpty()) { if ((err == null) && results.isEmpty()) {
@ -438,12 +445,12 @@ public class ApiConnection {
throw new ApiConnectionException(ex.getMessage(), ex); throw new ApiConnectionException(ex.getMessage(), ex);
} }
if (err != null) { if (err != null) {
throw new ApiCommandException(err); throw err;
} }
return results; return results;
} }
private List<Result> results = new LinkedList<Result>(); private List<Result> results = new LinkedList<Result>();
private Error err; private MikrotikApiException err;
} }
} }

View File

@ -10,7 +10,7 @@ import java.util.List;
* *
* @author GideonLeGrange * @author GideonLeGrange
*/ */
public class Command { class Command {
@Override @Override
public String toString() { public String toString() {

View File

@ -4,7 +4,7 @@ package me.legrange.mikrotik;
* Internal representation of !done * Internal representation of !done
* @author GideonLeGrange * @author GideonLeGrange
*/ */
public class Done extends Response { class Done extends Response {
Done(String tag) { Done(String tag) {
super(tag); super(tag);

View File

@ -4,7 +4,7 @@ package me.legrange.mikrotik;
* Used to encapsulate API error information. We need to pass both the message and the tag (if one was used). * Used to encapsulate API error information. We need to pass both the message and the tag (if one was used).
* @author GideonLeGrange * @author GideonLeGrange
*/ */
public class Error extends Response { class Error extends Response {
Error(String tag, String message) { Error(String tag, String message) {
super(tag); super(tag);

View File

@ -7,11 +7,11 @@ package me.legrange.mikrotik;
*/ */
public interface ResponseListener extends ResultListener { public interface ResponseListener extends ResultListener {
/** called if the command associated with this listener experiences a trap */ /** called if the command associated with this listener experiences an error */
void error(Error err); void error(MikrotikApiException ex);
/** called when the command associated with this listener is done */ /** called when the command associated with this listener is done */
void completed(Done done); void completed();
} }