wsky / top-link

embedded duplex multi-channel endpoint and connection management for c#/java/...
6 stars 1 forks source link

about RPC/remoting support and should impl by who and where #3

Closed wsky closed 11 years ago

wsky commented 11 years ago

https://github.com/wsky/top-link/commit/8e73ba838a68e709588720f6d0e244ef3e0013a7

// like sync RPC, but do not guarantee timing, just a sample
    public byte[] call(byte[] data, int offset, int length) throws ChannelException {
        final String sync = new String("call");
        // FIXME:if error, remove this once-handler
        this.channel.addOnceChannelHandler(new ChannelHandler() {
            @Override
            public void onReceive(byte[] data, int offset, int length, EndpointContext context) {
                buffer.position(0);
                buffer.put(data, offset, length);
                synchronized (sync) {
                    sync.notify();
                }
            }
        });
        this.channel.send(data, offset, length);
        synchronized (sync) {
            try {
                sync.wait(5000);
            } catch (InterruptedException e) {
                throw new ChannelException("rpc call time out", e);
            }
        }
        byte[] result = new byte[this.buffer.position()];
        this.buffer.position(0);
        this.buffer.get(result);
        return result;
    }

At this layer to achieve this function is not suitable for

upper layer app can use custom channelhandler to imple it for timing

wsky commented 11 years ago

upperlayer rpc control

flag 0-65533
header
payload
pair<flag, handler>

top-remoting will do this

wsky commented 11 years ago

https://github.com/wsky/top-link/compare/0b269d3f13...92f076a34e

remoting implementation v0.1

low-level implementation to support application extension.

Server Bind

URI uri = new URI("ws://localhost:9001/link");
WebSocketServerChannel serverChannel = new WebSocketServerChannel(uri.getHost(), uri.getPort());
Endpoint server = new Endpoint();
server.setChannelHandler(new RemotingServerChannelHandler() {
    @Override
    public byte[] onRequest(ByteBuffer buffer) {
        return "ok".getBytes();
    }
});
server.bind(serverChannel);

Call

ByteBuffer resultBuffer = RemotingService.connect(uri).call("hi".getBytes(), 0, 2);
assertEquals("ok", new String(new byte[] { resultBuffer.get(), resultBuffer.get() }));
wsky commented 11 years ago

swaggersocket protocol impl

https://github.com/wordnik/swaggersocket/tree/master/modules/swaggersocket-protocol/src/main/java/com/wordnik/swaggersocket/protocol

https://github.com/wordnik/swaggersocket/tree/master/protocol

use identity for timing

Use heartbeat

the following protocol, identity is flag, but not extendable

{
    "handshake" : {
        "protocolVersion" : "1.0",
        "protocolName" : "SwaggerSocket",
        "dataFormat" : "JSON",
        "identity" : 0, 
        "path" : "/any_url",
        "method" : "GET",

        "headers" : [
            {
                "name" : "name",
                "value" : "value"
            }
        ],
        "queryStrings" : [
            {
                "name" : "name",
                "value" : "value"
            }
        ]
    },
    "requests" : [
        {
            "uuid" : 0,
            "method" : "POST",
            "path" : "/any_url/",
            "headers" : [
                {
                    "name" : "Content-Type",
                    "value" : "test/plain"
                }
            ],
            "queryStrings" : [
                {
                    "name" : "foo2",
                    "value" : "bar2"
                }
            ],
            "messageBody" : "Swagger Socket Protocol is cool"
        }
    ]
}
frankie8849 commented 11 years ago

Very Usefull Case! Thanks!

wsky commented 11 years ago

@natame spread and use :)

wsky commented 11 years ago

Netty issue#1112 https://github.com/netty/netty/issues/1112

Add support for full WebSocket Request/Response message handling
wsky commented 11 years ago

top-link.remoting done