Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

tcp-server and protocol passed testing

  • Loading branch information...
commit 732a0ae5b7bc1575d702fc6b96b4e4d9d30f258d 1 parent a70c24f
Leon Lee authored
10 bootstrap/src/main/groovy/org/riderzen/ogs/bootStrap.groovy
... ... @@ -1,10 +0,0 @@
1   -package org.riderzen.ogs
2   -
3   -/*
4   - * User: Leon Lee <mail.lgq@gmail.com>
5   - * Created On: 2012-12-05
6   - */
7   -
8   -def config = container.config
9   -
10   -container.deployModule("org.riderzen.ogs.tcp-server-v1.0", config)
31 bootstrap/src/main/resources/bootstrap.groovy
... ... @@ -0,0 +1,31 @@
  1 +/**
  2 + * User: Leon Lee <mail.lgq@gmail.com>
  3 + * Date: 12-12-22
  4 + */
  5 +
  6 +def logger = container.logger
  7 +def config = container.config
  8 +def appConfig = [
  9 + tcpServerConfig: [
  10 + id: 1
  11 + ],
  12 + protocolConfig: [
  13 + id: 2
  14 + ]
  15 +]
  16 +
  17 +logger.info "starting ogs..."
  18 +
  19 +container.deployModule("org.riderzen.ogs.platform.tcp-server-v1.0", appConfig.tcpServerConfig, 1) {
  20 + container.deployModule("org.riderzen.ogs.platform.protocol-v1.0", appConfig.protocolConfig, 1) {
  21 + logger.info "ogs was started"
  22 + if (config['test']) {
  23 + logger.info 'deploying test mod'
  24 + container.deployModule('org.riderzen.ogs.platform.test-v1.0', null, 1) {
  25 + logger.info "starting test... ${it}"
  26 + vertx.sharedData.getMap('ogs')['test-mod-id'] = it
  27 + }
  28 + }
  29 + }
  30 +}
  31 +
3  bootstrap/src/main/resources/config.json
... ... @@ -0,0 +1,3 @@
  1 +{
  2 + "test": true
  3 +}
3  bootstrap/src/main/resources/mod.json
... ... @@ -0,0 +1,3 @@
  1 +{
  2 + "main": "bootstrap.groovy"
  3 +}
5 build.gradle
@@ -78,6 +78,11 @@ configure(subprojects.findAll { project -> !emptyProjects.find {it == project.na
78 78 from project.file("build/mod")
79 79 }
80 80
  81 + task installMod(type: Copy, dependsOn: 'copyMod') {
  82 + into "${System.getenv('VERTX_HOME').replaceAll('\\\\', '/')}/mods"
  83 + from "build/mod"
  84 + }
  85 +
81 86 task prepareVertxTest(type: Sync, dependsOn: 'copyMod') {
82 87 from 'build/mod'
83 88 into 'build/tmp/mod-test'
2  platform/protocol/src/main/resources/log4j.properties
@@ -4,5 +4,5 @@ log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4 4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5 5 log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n
6 6
7   -log4j.category.org.vertx=ERROR
  7 +log4j.category.org.vertx=DEBUG
8 8 log4j.category.org.riderzen=DEBUG
6 platform/protocol/src/main/resources/protocol-parser.groovy
@@ -18,8 +18,8 @@ def logger = container.logger
18 18
19 19 logger.info "starting protocol-parser"
20 20
21   -eb.registerHandler(config?.address ?: Address.appProtocol.val) { Message rawMessage ->
22   - Buffer buffer = rawMessage.body as Buffer
  21 +eb.registerHandler(config?.address ?: Address.appProtocol.val) { Message upstream ->
  22 + def buffer = upstream.body
23 23
24 24 MessagePack msgpack = new MessagePack()
25 25 Unpacker unpacker = msgpack.createUnpacker(new ByteArrayInputStream(buffer.getBytes()))
@@ -40,6 +40,6 @@ eb.registerHandler(config?.address ?: Address.appProtocol.val) { Message rawMess
40 40 Packer packer = msgpack.createPacker(out)
41 41 packer.write((String) reply.body)
42 42
43   - reply.reply new Buffer(out.toByteArray())
  43 + upstream.reply new Buffer(out.toByteArray())
44 44 }
45 45 }
9 platform/tcp-server/src/main/resources/tcp-server.groovy
... ... @@ -1,5 +1,3 @@
1   -import org.msgpack.MessagePack
2   -import org.msgpack.packer.Packer
3 1 import org.riderzen.ogs.common.Address
4 2 import org.vertx.groovy.core.buffer.Buffer
5 3 import org.vertx.groovy.core.eventbus.Message
@@ -42,15 +40,18 @@ if (backlog > 0)
42 40 server.acceptBacklog = backlog
43 41
44 42 server.connectHandler { sock ->
45   - sock.dataHandler { Buffer buffer ->
  43 + sock.dataHandler { buffer ->
46 44 logger.debug("received ${buffer.getLength()} bytes of data")
  45 + println("received ${buffer.getLength()} bytes of data")
47 46 eb.send(Address.appProtocol.val, buffer) { Message message ->
48 47 logger.debug "received message ${message.body}"
49   - sock << message.body as Buffer
  48 + println "received message ${message.body}"
  49 + sock.write(buffer as Buffer)
50 50 }
51 51 }
52 52 sock.exceptionHandler { e ->
53 53 logger.error("caught error", e)
  54 + println("caught error", e)
54 55 }
55 56
56 57 }.listen(port, host)
12 platform/tcp-server/src/test/groovy/org/riderzen/ogs/protocol/TcpServerTest.groovy
@@ -40,12 +40,12 @@ class TcpServerTest extends VertxTestBase {
40 40
41 41 @Test
42 42 def void testTcpServer() {
43   - getVertx().eventBus().registerHandler('ogs.protocol', new Handler<Message>() {
44   - @Override
45   - void handle(Message event) {
46   - logger.debug("get event" + event.body)
47   - }
48   - })
  43 +// getVertx().eventBus().registerHandler('ogs.protocol', new Handler<Message>() {
  44 +// @Override
  45 +// void handle(Message event) {
  46 +// logger.debug("get event" + event.body)
  47 +// }
  48 +// })
49 49
50 50 // sleep(1000000)
51 51
6 platform/tcp-server/src/test/resources/deployer.js
@@ -4,6 +4,6 @@ var config = {
4 4
5 5 };
6 6
7   -vertx.deployModule('org.riderzen.ogs.platform.tcp-server-v1.0', config, 1, function(id) {
8   - console.log('deployed module: org.riderzen.ogs.platform.tcp-server-v1.0 with id:' + id);
9   -});
  7 +//vertx.deployModule('org.riderzen.ogs.platform.tcp-server-v1.0', config, 1, function(id) {
  8 +// console.log('deployed module: org.riderzen.ogs.platform.tcp-server-v1.0 with id:' + id);
  9 +//});
1  platform/test/gradle.properties
... ... @@ -0,0 +1 @@
  1 +modulename=org.riderzen.ogs.platform.test
3  platform/test/src/main/resources/mod.json
... ... @@ -0,0 +1,3 @@
  1 +{
  2 + "main": "test.groovy"
  3 +}
49 platform/test/src/main/resources/test.groovy
... ... @@ -0,0 +1,49 @@
  1 +import com.google.gson.Gson
  2 +import org.msgpack.MessagePack
  3 +import org.msgpack.packer.Packer
  4 +import org.msgpack.unpacker.Unpacker
  5 +import org.vertx.groovy.core.buffer.Buffer
  6 +
  7 +/**
  8 + * User: Leon Lee <mail.lgq@gmail.com>
  9 + * Date: 12-12-22
  10 + */
  11 +def logger = container.logger
  12 +def eb = vertx.eventBus
  13 +def client = vertx.createNetClient()
  14 +
  15 +def testAddress = 'test.tcp.protocol'
  16 +eb.registerHandler(testAddress) { message ->
  17 + println "received message in $testAddress ${message.body}"
  18 +
  19 + message.reply new Gson().toJson([status: 200, data: 'ok'])
  20 +}
  21 +
  22 +client.connect(6543, "127.0.0.1") { socket ->
  23 + println "started test client"
  24 +
  25 + socket.exceptionHandler { ex -> println "Failed to connect $ex" }
  26 +
  27 + MessagePack msgpack = new MessagePack()
  28 + socket.dataHandler { buffer ->
  29 + ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer.getBytes())
  30 + Unpacker upacker = msgpack.createUnpacker(inputStream)
  31 + def message = upacker.read(String.class)
  32 + println "Received message ${message}"
  33 +
  34 + logger.debug "Received message ${message}"
  35 +
  36 + def mod_id = vertx.sharedData.getMap('ogs')['test-mod-id']
  37 + vertx.sharedData.getMap('ogs').remove('test-mod-id')
  38 + container.undeployModule(mod_id) {
  39 + logger.info "testing was done!"
  40 + }
  41 + }
  42 +
  43 + ByteArrayOutputStream out = new ByteArrayOutputStream()
  44 + Packer packer = msgpack.createPacker(out)
  45 + packer.write(new Gson().toJson([endpoint: testAddress, params: [id: 1, name: 'test']]))
  46 +
  47 + Buffer buffer = new Buffer(out.toByteArray())
  48 + socket << buffer
  49 +}
2  settings.gradle
... ... @@ -1,5 +1,5 @@
1 1 include 'bootstrap', 'platform', 'modules'
2   -include 'platform:common', 'platform:protocol', 'platform:tcp-server'
  2 +include 'platform:common', 'platform:protocol', 'platform:tcp-server', 'platform:test'
3 3 include 'modules:sys-auth'
4 4
5 5

0 comments on commit 732a0ae

Please sign in to comment.
Something went wrong with that request. Please try again.