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

View File

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

View File

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