Skip to content
yangodev edited this page Oct 1, 2014 · 12 revisions

So, here's an example declaration of a dictionary type:

import org.meshpoint.anode.idl.Dictionary;

class Person implements Dictionary {
  public String name;
  public int age;
  public String gender;
}

This is going to be a dictionary type, which means that when it is passed between JS and java, it is passed by value.

So you can create a Person instance in JS just as:

{ name: 'Joe', age: 32, gender: 'male'}

Or you can equally create an instance in Java using

Person p = new Person();
p.age = "Joe";
p.age = 32;
p.gender = "male";

Now imagine we have some JS running that receives Person data, as JSON, say, and we want to pass that data to Java.

So we need to create a interface that represents a'Platform object" whose implementation will be in Java.

So maybe out WebIDL for that interface is:

interface PersonReceiver {
  onPerson(Person p);
  onMultiplePeople(Person[] p);
};

So we need a Java class that makes the same declaration, and it will be like this:



import org.meshpoint.anode.bridge.Env;
import org.meshpoint.anode.java.Base;

public abstract class PersonReceiver extends Base {

	private static short classId = Env.getInterfaceId(PersonReceiver.class);
	protected PersonReceiver() { super(classId); }
	
	public abstract void onPerson(Person p);
	public abstract void onMultiplePeople(Person[] p);
}

Finally, we'll need a class that implements this, which will look like this:


public class PersonReceiverImpl extends PersonReceiver {

	@Override
	public void onPerson(Person p) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onMultiplePeople(Person[] p) {
		// TODO Auto-generated method stub

	}

}

Now, in order that our code is loadable by the bridge, we need an implementation of IModule; it might as well be the same PersonReceiverImpl class, so now it looks like this:

import org.meshpoint.anode.module.IModule;
import org.meshpoint.anode.module.IModuleContext;

public class PersonReceiverImpl extends PersonReceiver implements IModule {

	@Override
	public void onPerson(Person p) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onMultiplePeople(Person[] p) {
		// TODO Auto-generated method stub

	}

	@Override
	public Object startModule(IModuleContext ctx) {
		// TODO Auto-generated method stub

		/* here we return ourselves, so we're passing an instance of PersonReceiver back to JS */
		return this;
	}

	@Override
	public void stopModule() {
		// TODO Auto-generated method stub
		
	}

}

Finally, some js to load our module will be:


var personrecvr = require('bridge').load('path.to.PersonReceiverImpl', this);

and in js you can call the PersonReceiver methods, passing in Person instances that you built from JSON.

Clone this wiki locally