forked from actck/jrodns
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
881 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
# User-specific stuff: | ||
.idea/ | ||
docker/ | ||
run/ | ||
*.iml | ||
find.xml | ||
ignore.xml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cn.foperate.ros.api | ||
|
||
data class ApiConnectionOptions( | ||
val username: String, | ||
val password: String, | ||
val host: String, | ||
val port: Int = RxApiConnection.DEFAULT_PORT | ||
) | ||
|
||
fun apiConnectionOptionsOf(username: String, | ||
password: String, | ||
host: String) | ||
= ApiConnectionOptions(username, password, host) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cn.foperate.ros.api | ||
|
||
import java.lang.Exception | ||
|
||
|
||
/** | ||
* Thrown by the Mikrotik API to indicate errors | ||
* | ||
* @author Aston Mei | ||
*/ | ||
open class MikrotikApiException(msg: String? = null, err: Throwable? = null) : Exception(msg, err) | ||
|
||
|
||
/** | ||
* Exception thrown if the Api experiences a connection problem | ||
*/ | ||
open class ApiConnectionException : MikrotikApiException { | ||
constructor(msg: String?) : super(msg) {} | ||
constructor(msg: String?, err: Throwable?) : super(msg, err) {} | ||
} | ||
|
||
class ApiTimeoutException(message: String):ApiConnectionException(message) | ||
|
||
/** | ||
* Thrown if there is a problem unpacking data from the Api. | ||
* @author GideonLeGrange | ||
*/ | ||
class ApiDataException : MikrotikApiException { | ||
internal constructor(msg: String?) : super(msg) {} | ||
internal constructor(msg: String?, err: Throwable) : super(msg, err) {} | ||
} | ||
|
||
class ApiCommandException(map: Map<String, String>) : MikrotikApiException(map["message"]) { | ||
var category = map["category"]?.toInt() ?: 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cn.foperate.ros.api | ||
|
||
import cn.foperate.ros.munity.AsyncSocketFactory | ||
import io.vertx.kotlin.core.net.netClientOptionsOf | ||
import io.vertx.mutiny.core.Vertx | ||
import org.slf4j.LoggerFactory | ||
|
||
object AsyncClient | ||
{ | ||
private val log = LoggerFactory.getLogger(AsyncClient::class.java) | ||
@JvmStatic | ||
fun main(args:Array<String>) { | ||
log.debug("异步客户端测试开始") | ||
|
||
val vertx = Vertx.vertx() | ||
val sf = AsyncSocketFactory(vertx, netClientOptionsOf( | ||
connectTimeout = RxApiConnection.DEFAULT_CONNECTION_TIMEOUT | ||
)) | ||
RxApiConnection.connection(sf, apiConnectionOptionsOf( | ||
host = "192.168.28.1", | ||
username = "jdns", | ||
password = "Ocwesw2r3" | ||
)).subscribe().with ({ conn -> | ||
log.info("Login successed") | ||
val command = Command("/ip/firewall/address-list/print", queries= mapOf( | ||
"list" to "PROXY" | ||
), props = listOf("address","timeout")) //,q where return address,timeout" | ||
conn.executeAsMulti(command) | ||
.onCompletion().invoke { | ||
conn.close() | ||
} | ||
.subscribe().with { | ||
log.debug(it.toString()) | ||
} | ||
}){ | ||
log.error(it.toString()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cn.foperate.ros.api | ||
|
||
/** | ||
* A command sent to a Mikrotik. This internal class is used to build complex commands | ||
* with parameters, queries and property lists. | ||
* | ||
* @author GideonLeGrange | ||
*/ | ||
class Command(val cmd: String, | ||
val params: Map<String, String?> = mapOf(), | ||
val queries: Map<String, String> = mapOf(), | ||
val props: List<String> = listOf() | ||
) { | ||
var tag = "" | ||
override fun toString(): String { | ||
return "cmd[$tag] = $cmd, params = $params, queries = $queries" | ||
} | ||
|
||
fun toSentence():List<String> { | ||
val list = mutableListOf<String>() | ||
list.add(cmd) | ||
params.forEach { (k, v) -> | ||
list.add("=$k=$v") | ||
} | ||
if (tag.isNotBlank()) { | ||
list.add(".tag=$tag") | ||
} | ||
if (props.isNotEmpty()) { | ||
val propList = props.joinToString(",") | ||
list.add("=.proplist=$propList") | ||
} | ||
queries.forEach { (k, v) -> | ||
list.add("?$k=$v") | ||
} | ||
return list | ||
} | ||
} |
Oops, something went wrong.