Skip to main content

Remote service interaction with Versile and Groovy - Part 1

Versile is a compact but powerful technology for remote service interaction. I will write a few blog posts about using this technology with Groovy. In fact the examples presented should be applicable for other JVM scripting languages as well as pure Java - since Versile Java is the only library needed here.

Creating a class which methods can be exposed remotely is easy with Versile. Simply inherit the VExternal class and annotate the methods with the @Publish annotation:


class Svc extends VExternal {
    @Publish(show=true, ctx=false) public String hello(String name) {
return "hello "+name
    }    
}

A few more lines of code is needed to put this service on the air, but before we get to that, I'll show you how to connect to it and call the hello method remotely:


// Get remote object
proxy = VUrl.resolve("vop://localhost/")

// Call method on remote object
print proxy.hello("Peter")


That was easy? To be able to call the service we need to create a listener for VOP (Versile Object Protocol) connections. The listener must be provided with credentials to identify it, and a gateway that will create instances of the class to be exposed remotely. The gateway class implements the VGatewayFactory interface like this:


// A gateway that can be connected to a VOPService listener
class GW implements VGatewayFactory {    
    public VObject build() {
return new Svc()
    }
}

And credentials are created using VDI (Versile Decentral Identity), which in many ways can be compared to SSH-keygen. Since Versile communication is secure by default, VDI is used to identify the other party - and can also be used for authentication (more on this in a later post). In Groovy, creation of credentials looks like this:


// Create server identification credentials
key = VDecentralIdentity.dia(1024, "", "", "someServerPwd");
credentials = new VPrivateCredentials(key);


And then we're ready to create and start the listener using the gateway and credentials:


// Create the service using the gateway and credentials
service = new VOPService(new GW(),credentials,null);
// Start the service
service.start()

To test this for yourself, start two groovy shells (one for the listener, and one for connecting to it):

groovysh -cp versile-java-0.8.1-beta.jar

Paste into the listener shell:


import org.versile.Versile
import org.versile.reactor.service.VOPService
import org.versile.orb.service.VGatewayFactory
import org.versile.orb.entity.VObject
import org.versile.orb.external.VExternal
import org.versile.orb.external.Publish
import org.versile.common.auth.VPrivateCredentials;
import org.versile.crypto.VDecentralIdentity;

// Select license
Versile.setInternalUseAGPL()

class Svc extends VExternal {
    @Publish(show=true, ctx=false) public String hello(String name) {
return "hello "+name
    }    
}
// A gateway that can be connected to a VOPService listener
class GW implements VGatewayFactory {    
    public VObject build() {
return new Svc()
    }
}
// Create server identification credentials
key = VDecentralIdentity.dia(1024, "", "", "someServerPwd");
credentials = new VPrivateCredentials(key);

// Create the service using the gateway and credentials
service = new VOPService(new GW(),credentials,null);
// Start the service
service.start()

And in the other shell (the client):


import org.versile.Versile
import org.versile.reactor.url.VUrl
import org.versile.orb.entity.VProxy

// Select license
Versile.setInternalUseAGPL()

// Let's be able to call methods directly on the VProxy object, rather than using call method
VProxy.metaClass.methodMissing = {String name,args -> 
    return delegate.call(name,args)    
}

// Get remote object
proxy = VUrl.resolve("vop://localhost/")

// Call method on remote object
print proxy.hello("Peter")

The client should output "hello Peter".




Comments

Popular posts from this blog

Angular components not reloading on route change

Spent a long time wondering why route changes caused strange effects on my component, and found out that it was because my component wasn't reloading at all. I had this assumption that when a route parameter changed (e.g. /projects/1 changed to /projects/2 ) the component for the route would be reloaded. But this is not the default behaviour of the Angular router. The default behaviour of the Angular router is to reuse the route if the configuration is the same (and not reload the component). But we can override this by providing a RouteReuseStrategy to our @NgModule: providers: [ { provide: RouteReuseStrategy, useClass: AARouteReuseStrategy } ] The full custom implementation of the RouteReuseStrategy will then be like this (and it's the shouldReuseRoute method that changes the behaviour so that the component is reloaded on route parameter change): export class AARouteReuseStrategy extends RouteReuseStrategy { shouldDetach(route: ActivatedRou...

Using Angular i18n translation strings outside templates

As of today in Angular 6 i18n is only available in the templates. So what if we want to use translated messages programatically and outside templates? I still use the component template html file to declare the message to be translated, but by obtaining the TemplateRef ViewChild in the component typescript file I can get the translated messages from typescript. <ng-template #boardInvitationEmailBody let-organization="organization" let-url="url"> <ng-container i18n>You have been invited to join the board portal for</ng-container> {{organization}}. <ng-container i18n> Please follow the link to: </ng-container> {{url}} </ng-template> So in the typescript code i can now call the createEmailBodyTranlated text method to get the translated text for use outside the template. @ViewChild('boardInvitationEmailBody') boardInvitationEmailBody: TemplateRef ; createEmailBodyTranslatedText(org...

Using GIT for offline web app syncing and storage

Offline web applications, progressive web apps (PWA) using service workers for being available offline also needs a way to sync changes with the server. Mostly we store data in JSON documents, and we could identify changes per line - which is something GIT is excellent at. So how could we make the browser a GIT client? I've been working on this for some months now, and compiled libgit2 to webassembly using emscripten - and it turns out to work very well. Some video demos: Cloning a repository: https://youtu.be/rcBluzpUWE4 Merging of file changes: https://youtu.be/xfGrMwLy_tw The project is here: https://github.com/fintechneo/angular-git-filebrowser