Commit 68c80aa3 authored by Philipp Auersperg's avatar Philipp Auersperg

...

parent 4d459d61
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Kotlin2JsCompilerArguments">
<option name="sourceMapEmbedSources" />
</component>
<component name="Kotlin2JvmCompilerArguments">
<option name="jvmTarget" value="1.8" />
</component>
......
......@@ -15,6 +15,7 @@ plugins {
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
mavenLocal()
jcenter()
}
......@@ -33,15 +34,25 @@ dependencies {
// YAJ
compile("info.laht:yaj-rpc:0.10.2")
compile("info.laht:yaj-rpc-tcp:0.10.2")
compile("info.laht:yaj-rpc-ws:0.10.2")
implementation("info.laht:yaj-rpc:0.10.2")
implementation("info.laht:yaj-rpc-tcp:0.10.2")
implementation("info.laht:yaj-rpc-ws:0.10.2")
testCompile("com.natpryce:hamkrest:1.6.0.0")
implementation ("org.slf4j:slf4j-api:$slf4j_version")
runtimeOnly("org.slf4j:slf4j-log4j12:$slf4j_version")
//ProcBridge
compile("co.gongzh.procbridge:procbridge:2.0.0-gson")
}
application {
// Define the main class for the application
mainClassName = "RpcTest.AppKt"
mainClassName = "RpcTest.ProcBridgeTestKt"
}
package RpcTest
import co.gongzh.procbridge.ProcBridge
import co.gongzh.procbridge.ProcBridgeException
import co.gongzh.procbridge.MessageHandler
import com.google.gson.JsonObject
import co.gongzh.procbridge.APIHandler
import co.gongzh.procbridge.ProcBridgeServer
import com.google.gson.JsonArray
import com.google.gson.JsonPrimitive
class Delegate {
@APIHandler
internal fun echo(arg: JsonObject): JsonObject {
println("arg received: $arg")
return arg
}
@APIHandler
internal fun add(arg: JsonObject): JsonObject {
val elements = arg.get("elements").asJsonArray
var sum = 0
for (i in 0 until elements.size()-1) {
sum += elements.get(i).asInt
}
val result = JsonObject()
result.addProperty("result", sum)
return result
}
}
fun makeServer(port: Int = 8877) : ProcBridgeServer {
val server = ProcBridgeServer(port)
server.setDelegate(Delegate())
return server
}
fun main(args: Array<String>) {
val host = "127.0.0.1"
val port = 8877
val timeout = 10000 // 10 seconds
val server= makeServer(port)
server.start()
class Handler: MessageHandler {
override fun onMessage(message: JsonObject) {
println("ok: $message")
}
override fun onError(e: ProcBridgeException?) {
println("shit: $e")
}
}
val pb = ProcBridge(host, port, timeout, Handler())
// val resp =
pb.sendMessage("echo", "{}")
// println(resp) // prints "{}"
// val resp1 =
pb.sendMessage("add", "{elements: [1, 2, 3, 4, 5]}")
// println(resp1) // prints "{result: 15}"
// pb.startHandlingMessages()
print("enter key to proceed:")
val text=readLine() !!
println("text: $text")
server.stop()
}
\ No newline at end of file
......@@ -6,8 +6,13 @@ package RpcTest
import info.laht.yajrpc.RpcService
import info.laht.yajrpc.RpcMethod
import info.laht.yajrpc.RpcHandler
import info.laht.yajrpc.net.RpcServer
import info.laht.yajrpc.net.RpcClient
import info.laht.yajrpc.net.tcp.RpcTcpClient
import info.laht.yajrpc.net.tcp.RpcTcpServer
import info.laht.yajrpc.net.ws.RpcWebSocketServer
import info.laht.yajrpc.net.ws.RpcWebSocketClient
import org.slf4j.Logger
import org.slf4j.LoggerFactory
......@@ -32,18 +37,26 @@ class SimpleService (override val serviceName: String = "SimpleService"): RpcSer
@RpcMethod
fun point(x:Double=0.0, y:Double=0.0, z:Double=0.0) =
Point(x,y,z)
@RpcMethod
fun makeerror(s:String):String {
if (s.startsWith("e"))
throw Exception("intended Error")
return "Hello $s"
}
}
class App {
val port=9000
val server: RpcTcpServer by lazy {RpcTcpServer(RpcHandler(SimpleService()))}
val client: RpcTcpClient by lazy {RpcTcpClient(host="localhost", port=port)}
val server: RpcWebSocketServer by lazy {RpcWebSocketServer(RpcHandler(SimpleService()))}
val client: RpcWebSocketClient by lazy {RpcWebSocketClient(host="localhost", port=port)}
val greeting: String
get() {
return "Hello world."
}
fun startServer() : RpcTcpServer{
fun startServer() : RpcServer{
server.port=this.port
println("app starting server at port ${this.port}")
......@@ -53,16 +66,23 @@ class App {
fun startClient() : RpcTcpClient {
fun startClient() : RpcClient {
return client
}
private companion object {
private val LOG: Logger = LoggerFactory.getLogger("APP")
}
}
fun main(args: Array<String>) {
println(App().greeting)
val app = App()
println(app.greeting)
app.startServer()
println("waiting...")
Thread.sleep(1000000)
println("finished.")
}
......@@ -9,9 +9,11 @@ import kotlin.test.assertNotNull
import com.natpryce.hamkrest.*
import com.natpryce.hamkrest.assertion.assert
import info.laht.yajrpc.RpcListParams
import info.laht.yajrpc.RpcParams
import info.laht.yajrpc.RpcResponse
import info.laht.yajrpc.net.RpcClient
import info.laht.yajrpc.net.tcp.RpcTcpClient
import info.laht.yajrpc.net.ws.RpcWebSocketClient
import org.slf4j.Logger
import org.slf4j.LoggerFactory
......@@ -24,6 +26,9 @@ class AppTest {
val theApp by lazy {println("starting App");App()}
val server by lazy {theApp.startServer()}
val client: RpcClient by lazy {theApp.startClient()}
val pyclient by lazy {
RpcWebSocketClient(host="localhost", port=5000)
}
@Test fun testAppHasAGreeting() {
assertNotNull(theApp.greeting, "app should have a greeting")
......@@ -34,39 +39,49 @@ class AppTest {
println("TEST:server started")
}
@Test fun checkClient() {
println("TEST:starting client")
val ww = client.write("SimpleService.echo", RpcListParams("Clint Eastwood"))
ww.get().also{
println("res: $it")
}
val res=ww.get().getResult(String::class.java)
println("res2:${res}")
client.write("SimpleService.add", RpcListParams(1,2)).get().also {
val res=it.getResult(Int::class.java)
println("add: $res")
}
client.write("SimpleService.point", RpcListParams(1.0,2.0,3.0)).get().also{
val res=it.getResult(Point::class.java)
println("point:$res")
}
@Test
fun checkClient() {
// println("TEST:starting client")
// val ww = client.write("SimpleService.echo", RpcListParams("Clint Eastwood"))
// ww.get().also{
// println("res: $it")
// }
// val res=ww.get().getResult(String::class.java)
//
// println("res2:${res}")
// client.write("SimpleService.add", RpcListParams(1,2)).get().also {
// val res=it.getResult(Int::class.java)
// println("add: $res")
// }
//
// client.write("SimpleService.point", RpcListParams(1.0,2.0,3.0)).get().also{
// val res=it.getResult(Point::class.java)
// println("point:$res")
// }
////
// pyclient.write("ping", RpcParams.listParams()).get().also{
// val res=it.getResult(String::class.java)
// println("answer fro python: $res")
// }
}
@Test fun testpoint_sync() {
@Test
fun testpoint_sync() {
println("========================================================testpoint $server")
server
client.write("SimpleService.point", RpcListParams(11.0,12.0,13.0), 100000)
//// client.write("SimpleService.point", RpcListParams(11.0,12.0,13.0))
.get().also{
val res=it.getResult(Point::class.java)
println("point1:$res")
}
// client.write("SimpleService.makeerror",RpcListParams("error"), 100000).get().also {
// val res=it.getResult(String::class.java)
// println("makeerror: $res")
// }
}
@Test fun whatsoever() {
// @Test
fun whatsoever() {
assert.that("xyzzy", startsWith("xy") and endsWith("y"))
// val vv=org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY
// System.setProperty(, "Info");
......
......@@ -8,12 +8,14 @@
"\n",
"Wir versuchen die ommunikation zwischen Kotlin-App und Python-Service ueber JSON-RPC abzuwickeln\n",
"\n",
"Kotlinseitig gibts mit [YAJ-RPC](https://github.com/markaren/YAJ-RPC) eine vielversprechende Implementation, die wir hier testen wollen"
"Kotlinseitig gibts mit [YAJ-RPC](https://github.com/markaren/YAJ-RPC) eine vielversprechende Implementation, die wir hier testen wollen\n",
"\n",
"socketio on android: https://stackoverflow.com/questions/48658294/kotlin-not-supporting-socket-io-in-android"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
......@@ -106,14 +108,203 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"@file:DependsOnMaven(\"org.jetbrains.kotlinx:kotlin-sockets:0.0.4\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"error: unresolved reference: kotlinx\n",
"import kotlinx.coroutines.experimental.*\n",
" ^\n",
"error: unresolved reference: kotlinx\n",
"import kotlinx.sockets.*\n",
" ^\n",
"error: unresolved reference: kotlinx\n",
"import kotlinx.sockets.Socket\n",
" ^\n"
]
}
],
"source": [
"\n",
"import kotlinx.coroutines.experimental.*\n",
"import kotlinx.sockets.*\n",
"import kotlinx.sockets.Socket\n",
"import java.net.*\n",
"import java.nio.*"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"val s=\"asdasd\\$asdasd\""
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\".split(\"\\$\")[0]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import java.util.stream.*"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"val ss=IntStream.iterate(0,{i -> i+1})"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"val ss1=ss.map({x->x*2})"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"fun "
"val ss2=ss1.limit(3)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ss2.sum()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"error: unresolved reference: getNext\n",
"ss.getNext()\n",
" ^\n"
]
}
],
"source": [
"ss.getNext()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"val it=ss.iterator()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"it.next()"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"java.lang.IllegalStateException: stream has already been operated upon or closed\n",
"\tat java.util.stream.AbstractPipeline.<init>(AbstractPipeline.java:203)\n",
"\tat java.util.stream.IntPipeline.<init>(IntPipeline.java:91)\n",
"\tat java.util.stream.IntPipeline$StatefulOp.<init>(IntPipeline.java:619)\n",
"\tat java.util.stream.SliceOps$2.<init>(SliceOps.java:236)\n",
"\tat java.util.stream.SliceOps.makeInt(SliceOps.java:236)\n",
"\tat java.util.stream.IntPipeline.limit(IntPipeline.java:375)"
]
}
],
"source": [
"ss.limit(3).sum()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
......
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# JSONRPC server in python\n",
"\n",
"nach [diesem Tutoorial](https://bcb.github.io/jsonrpc/socketio)\n",
"\n",
"Achtung: jsonrpcserver geht nur bis ersin 3.3.4 it python 2.7"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from jsonrpcserver import Methods\n",
"methods = Methods()\n",
"\n",
"@methods.add\n",
"def ping(name='sepp'):\n",
" print 'ping called:, name'\n",
" return 'pong'\n",
"\n",
"if __name__ == '__main__':\n",
" methods.serve_forever()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import jsonrpcserver\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dir(jsonrpcserver)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from flask import Flask\n",
"from flask_socketio import SocketIO\n",
"from jsonrpcserver import Methods, dispatch\n",
"\n",
"app = Flask(__name__)\n",
"socketio = SocketIO(app)\n",
"methods = Methods()\n",
"\n",
"@methods.add\n",
"def ping():\n",
" print 'ponged'\n",
" \n",
" \n",
" return 'pong'\n",
"\n",
"@socketio.on('message')\n",
"def handle_message(request):\n",
" print 'handle message:', request\n",
" return dispatch(methods, request)\n",
"\n",
"socketio.run(app, port=5000, host='0.0.0.0')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"help(socketio.run)\n",
"ws = websocket.WebSocket()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from flask import Flask, render_template\n",
"from flask_socketio import SocketIO\n",
"\n",
"app = Flask(__name__)\n",
"app.config['SECRET_KEY'] = 'secret!'\n",
"socketio = SocketIO(app)\n",
"\n",
"if __name__ == '__main__':\n",
" socketio.run(app, port=5000, host='0.0.0.0')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
......@@ -26,7 +129,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"version": "2.7.15"
}
},
"nbformat": 4,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment