Commit 608f38a5 authored by Philipp Auersperg's avatar Philipp Auersperg

added procbridge

parent 56d72aeb
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"@file:DependsOnMaven(\"co.gongzh.procbridge:procbridge:2.0.0-gson\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"@file:DependsOnMaven(\"com.google.code.gson:gson:2.8.5\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import co.gongzh.procbridge.ProcBridge\n",
"import co.gongzh.procbridge.ProcBridgeException\n",
"import co.gongzh.procbridge.MessageHandler\n",
"\n",
"import com.google.gson.JsonObject\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"java.lang.RuntimeException: Socket can not be connected.\n",
"\tat co.gongzh.procbridge.ProcBridge.<init>(ProcBridge.java:54)\n",
"Caused by: java.net.ConnectException: Verbindungsaufbau abgelehnt\n",
"\tat java.net.PlainSocketImpl.socketConnect(Native Method)\n",
"\tat java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)\n",
"\tat java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)\n",
"\tat java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)\n",
"\tat java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)\n",
"\tat java.net.Socket.connect(Socket.java:589)\n",
"\tat co.gongzh.procbridge.ProcBridge.<init>(ProcBridge.java:46)\n",
"\tat Line_9.<init>(Unknown Source)\n",
"\tat sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)\n",
"\tat sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)\n",
"\tat sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)\n",
"\tat java.lang.reflect.Constructor.newInstance(Constructor.java:422)\n",
"\tat org.jetbrains.kotlin.cli.common.repl.GenericReplEvaluator.eval(GenericReplEvaluator.kt:95)\n",
"\tat org.jetbrains.kotlin.cli.common.repl.GenericReplCompilingEvaluator.eval(GenericReplCompilingEvaluator.kt:74)\n",
"\tat org.jetbrains.kotlin.cli.common.repl.GenericReplCompilingEvaluator.compileAndEval(GenericReplCompilingEvaluator.kt:49)\n",
"\tat uy.kohesive.keplin.kotlin.script.SimplifiedRepl.compileAndEval(SimplifiedRepl.kt:113)\n",
"\tat uy.kohesive.keplin.kotlin.script.SimplifiedRepl.compileAndEval$default(SimplifiedRepl.kt:112)\n",
"\tat org.jetbrains.kotlin.jupyter.ReplForJupyter.eval(repl.kt:41)\n",
"\tat org.jetbrains.kotlin.jupyter.ProtocolKt$shellMessagesHandler$res$1.invoke(protocol.kt:61)\n",
"\tat org.jetbrains.kotlin.jupyter.ProtocolKt$shellMessagesHandler$res$1.invoke(protocol.kt)\n",
"\tat org.jetbrains.kotlin.jupyter.ProtocolKt.evalWithIO(protocol.kt:165)\n",
"\tat org.jetbrains.kotlin.jupyter.ProtocolKt.shellMessagesHandler(protocol.kt:60)\n",
"\tat org.jetbrains.kotlin.jupyter.IkotlinKt.kernelServer(ikotlin.kt:83)\n",
"\tat org.jetbrains.kotlin.jupyter.IkotlinKt.main(ikotlin.kt:41)"
]
}
],
"source": [
"val host = \"127.0.0.1\"\n",
"val port = 8877\n",
"val timeout = 10000 // 10 seconds\n",
"\n",
"class Handler: MessageHandler {\n",
" override fun onMessage(message: JsonObject) {\n",
" val x=1/0\n",
" TODO(\"not implemented\") //To change body of created functions use File | Settings | File Templates.\n",
" }\n",
" override fun onError(e: ProcBridgeException?) {\n",
" TODO(\"not implemented\") //To change body of created functions use File | Settings | File Templates.\n",
" }\n",
"}\n",
"\n",
"\n",
"val pb = ProcBridge(host, port, timeout, Handler())\n",
"// pb.startHandlingMessages()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"sending text:{}\n",
"wrote data:[B@3b47adeb\n",
"sending text:{elements: [1, 2, 3, 4, 5]}\n",
"wrote data:[B@68ebc1ed\n"
]
}
],
"source": [
"\n",
"// val resp = \n",
"pb.sendMessage(\"echo\", \"{}\")\n",
"// println(resp) // prints \"{}\"\n",
"\n",
"// val resp1 = \n",
"pb.sendMessage(\"add\", \"{elements: [1, 2, 3, 4, 5]}\")\n",
"// println(resp1) // prints \"{result: 15}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pb"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Kotlin",
"language": "kotlin",
"name": "kotlin"
},
"language_info": {
"file_extension": "kt",
"name": "kotlin"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"@file:DependsOnMaven(\"co.gongzh.procbridge:procbridge:2.0.0-gson\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"@file:DependsOnMaven(\"co.gongzh.procbridge:procbridge:2.0.0-gradle\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"@file:DependsOnMaven(\"com.google.code.gson:gson:2.8.5\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import co.gongzh.procbridge.APIHandler\n",
"import co.gongzh.procbridge.ProcBridgeServer\n",
"import com.google.gson.JsonArray\n",
"import com.google.gson.JsonObject\n",
"import com.google.gson.JsonPrimitive\n",
"\n",
"import java.io.BufferedReader\n",
"import java.io.IOException\n",
"import java.io.InputStreamReader\n",
"import java.util.Locale\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"val port = 8877\n",
"val server = ProcBridgeServer(port)\n",
"\n",
"server.setDelegate(object : Any() {\n",
"\n",
" @APIHandler\n",
" internal fun echo(arg: JsonObject): JsonObject {\n",
" println(\"arg received: $arg\")\n",
" return arg\n",
" }\n",
"\n",
" @APIHandler\n",
" internal fun add(arg: JsonObject): JsonObject {\n",
" val elements = arg.get(\"elements\").asJsonArray\n",
" var sum = 0\n",
" for (i in 0 until elements.size()-1) {\n",
" sum += elements.get(i).asInt\n",
" }\n",
" val result = JsonObject()\n",
" result.addProperty(\"result\", sum)\n",
" return result\n",
" }\n",
"\n",
"})\n",
"\n",
"try {\n",
" server.start()\n",
"\n",
" try {\n",
" BufferedReader(InputStreamReader(System.`in`)).use { reader ->\n",
" var line: String?\n",
" do{\n",
" line = reader.readLine()\n",
" } while (line != null && line != \"exit\")\n",
" }\n",
" } catch (ignored: IOException) {\n",
" }\n",
"\n",
" server.stop()\n",
"\n",
"} catch (ex: IOException) {\n",
" ex.printStackTrace()\n",
"}\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"1 .. 10\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"1 until 10"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"val line=\"asd\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"line == null"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"daddd\\$adasd\".split(\"!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"@file:DependsOnMaven(\"org.bd:bluntschli:1.0.0\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"@file:DependsOnMaven(\"org.gradle.sample:maven-publish:1.0.0\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import bluntschli.App"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"App().greeting"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"listOf(1,2,3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Kotlin",
"language": "kotlin",
"name": "kotlin"
},
"language_info": {
"file_extension": "kt",
"name": "kotlin"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from procbridge import ProcBridgeServer, ProcBridge"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"host = '127.0.0.1'\n",
"port = 8077\n",
"\n",
"def request_handler(api, arg):\n",
" if api == 'echo':\n",
" return arg\n",
" elif api == 'add':\n",
" return {'result': sum(x for x in arg['elements'])}\n",
" else:\n",
" raise Exception('unknown api')\n",
"\n",
"server = ProcBridgeServer(host, port, request_handler)\n",
"server.start()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"host = '127.0.0.1'\n",
"port = 8077\n",
"\n",
"client = ProcBridge(host, port)\n",
"\n",
"print(client.request('echo', {})) # prints \"{}\"\n",
"\n",
"print(client.request('add', { # prints \"{result: 15}\"\n",
" 'elements': [1, 2, 3, 4, 5]\n",
"}))"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# class api_handler(object):\n",
"# def __init__(self, m):\n",
"# print (\"method:\",m)\n",
"# import pdb;pdb.set_trace()\n",
"# self.method=m\n",
" \n",
"# def __call__(self, *a, **kw):\n",
"# return self.method(*a, **kw)\n",
"\n",
"def api_handler(f):\n",
" import pdb;pdb.set_trace()\n",
" print handlers\n",
" def wrapper(self, *a, **kw):\n",
" return f(self, *a, **kw)\n",
" \n",
" return wrapper\n",
" \n",
"class Delegate(object):\n",
" \"\"\"\"\"\"\n",
" handlers={}\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"> <ipython-input-12-e89af1259f2e>(12)api_handler()\n",
"-> print handlers\n",
"(Pdb) MyDelegate\n",
"<class '__main__.MyDelegate'>\n",
"(Pdb) __class__\n",
"*** NameError: name '__class__' is not defined\n",
"(Pdb) self\n",
"*** NameError: name 'self' is not defined\n",
"(Pdb) cls\n",
"*** NameError: name 'cls' is not defined\n"
]
}
],
"source": [
"class MyDelegate (Delegate):\n",
" handlers={}\n",
" \n",
" @api_handler\n",
" def echo(self, echo, **kw):\n",
" return echo\n",
"\n",
" def add(self, elements, **kw):\n",
" return {'result': sum(x for x in elements)}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"dd=MyDelegate()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ss'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dd.echo(\"ss\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
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