npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lsdsoftware/gson-rmi

v1.0.1

Published

Browser JavaScript library for communicating with GSON-RMI peers

Downloads

7

Readme

Purpose

This library lets you:

  • Serialize and deserialize JSON-RPC 2.0 (http://json-rpc.org) requests and responses and
  • Invoke them on arbitrary methods of a Java object

How to use

Gson gson = new GsonBuilder().registerTypeAdapter(...).create();
RpcRequest request = gson.fromJson(jsonText, RpcRequest.class);
Invoker invoker = new Invoker(new DefaultParamProcessor());
RpcResponse response = invoker.doInvoke(request, someObject);
jsonText = gson.toJson(response);

To expose a method for remote invocation, use the @RMI annotation; methods are matched by name and number of arguments. Parameters and return values can be arbitrary Java objects. If a parameter is a superclass type, a specific subclass can be specified with the @ParamType annotation. The DefaultParamProcessor can be overridden to inject custom parameters or do additional annotation processing.

Basic RPC Server

A basic RPC server implementation is provided. This server listens for TCP connections, receives RPC requests (sent as a stream of JSON objects one after another with no delimiter), and invokes them on a pre-specified object.

public class SampleServer {
        @RMI
        public String someMethod(String name) {
                return "Hello, " + name;
        }

        public static void main(String[] args) throws IOException {
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Exception.class, new ExceptionSerializer())
                        .registerTypeAdapter(Parameter.class, new ParameterSerializer())
                        .create();
                
                new RpcServer(30100, new RpcTarget(new SampleServer(), gson), gson).start();
        }
}

Transport Stack

In the basic RPC server, we defined a basic transport protocol for JSON-RPC messages. JSON-RPC itself does not specify how messages are exchanged, that is because it's only a presentation protocol (OSI layer 5).

This library also includes a generic object-to-object message transport and session management framework (OSI layer 3 and 4), with proxy and multi-casting capabilities. Currently it has a TCP implementation and a WebSocket adapter. But it can be easily extended to support any networking protocol.

The goal of the framework is to ease development of distributed applications. It allows RMI between objects residing in many processes in a network of arbitrary topology, without having to worry about setting up and tearing down connections, serialization, de-serialization, routing and addressing of RPC messages.

With this framework, once the transport layer is set up, you invoke a remote object simply by specifying the route to the object. A route is a list of URIs identifying the hops that the message needs to traverse to reach the destination. Following is a simple program that shows an object in one process invoking an object in another process over TCP:

Callee

public class SampleServer {
        @RMI
        public String sayHello(String name) {
                return "Hello, " + name;
        }

        public static void main(String[] args) throws IOException, URISyntaxException {
                //setup the transport layer
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Exception.class, new ExceptionSerializer())
                        .registerTypeAdapter(Parameter.class, new ParameterSerializer())
                        .create();
                Transport t = new Transport();
                new TcpProxy(Arrays.asList(new InetSocketAddress(30100)), t, gson).start();
                new RmiService(t, gson).start();

                //register an object for remote invocation
                new Call(new Route(new URI("rmi:service")), "register", "herObject", new SampleServer()).send(t);
        }
}

Caller

public class SampleClient {     
        @RMI
        public void returnValueMethod(String greeting, RpcError error) {
                System.out.println(greeting);
        }

        public static void main(String[] args) throws IOException, URISyntaxException {
                //setup the transport layer
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Exception.class, new ExceptionSerializer())
                        .registerTypeAdapter(Parameter.class, new ParameterSerializer())
                        .create();
                Transport t = new Transport();
                new TcpProxy(Arrays.asList(new InetSocketAddress(30101)), t, gson).start();
                new RmiService(t, gson).start();

                //register an object for receiving return values
                new Call(new Route(new URI("rmi:service")), "register", "myObject", new SampleClient()).send(t);

                //invoke the remote object
                new Call(new Route(new URI("tcp://localhost:30100"), new URI("rmi:herObject")), "sayHello", "Jack")
                    .callback(new URI("rmi:myObject"), "returnValueMethod")
                    .send(t);
        }
}

The source code for these examples can be found under the test/ folder.