Step by Step Service Deployment Tutorial - mohamedalsherif/SampleWebService GitHub Wiki
This Tutorial serves as a step by step introduction on how to create and run a service using LAS2peer.
In this tutorial we will use Eclipse to demonstrate how to run a service on our localhost.
Follow these seven steps to create and run a service.
1- Open Eclipse and create an empty Java Project. Lets call it MyFirstServiceProject.
2- Create a folder named log and another folder named lib in your project and add the following jars to the lib folder : las2peer, commons-codec, xpp3, simpleXML, FreePastry, httpServer, httpConnector and then add them also to your project's build path.
Now we have all the needed libraries to run LAS2peer on our machine. We need to add some services to test the functionality of the LAS2peer server.
3- Create a java class in you project and call it MyFirstService.java and put it in a package called i5.las2peer.services.myFirstService.
Your project should look like this :
4- This service should be a subclass of i5.las2peer.Service. You can add in your test service as many service methods as you like. The service implementation we will use can be seen here :
package i5.las2peer.services.myFirstService;
import i5.las2peer.api.Service;
public class MyFirstService extends Service
{
public MyFirstService()
{
//constructor.
//may be used for service values initialization
}
public String isAlive()
{
logMessage("isAlive Method Called");
return "The Service is alive";
}
public void insertData(int key, String data)
{
//do something
}
}
Now that we created our service and have all needed libraries to run the server we may finally export our package to a jar file and run the LAS2peer server.
5- To export the package in Eclipse , right click the package -> export -> java -> jar file, and name the jar myFirstService. After that place the jar in the lib folder that contains all the other jars.
6- Open the windows command line and then move to the project directory and after that type the following commands in order:
java -cp lib\* i5.las2peer.tools.L2pNodeLauncher windows_shell -s 9010 - interactive
This command opens the LAS2peer node on port 9010.
If the LAS2peer node is running correctly on the the port 9010, the command line looks as following:
startService('i5.las2peer.services.myFirstService.MyFirstService')
This command starts the service that we have created in our project.
usr=i5.las2peer.security.UserAgent.createUserAgent("Pass")
usr.unlockPrivateKey("Pass")
registerUserAgent(usr,"Pass")
These three commands add one user agent and registers that user agent in that node, such that service methods can be invoked. To get more information on how to add user agents refer to Adding-User-Agents.
7- Test the Service by invoking the service methods using the invoke command.
In order to call the isAlive method for example we enter the following command :
invoke('i5.las2peer.services.myFirstService.MyFirstService','isAlive')
If the service is running correctly the output should be as the one seen in the following screenshot.
After invoking the isAlive
method, the log message entry that was generated in the service function can be found in a log file in the log folder that was created earlier in the project.
To call the insertData
method on the console, a little extra work has to be done.
You need to instantiate a local Integer Object and call the method with it as a parameter. To achieve that you basically write the following in the console key = new java.lang.Integer ('5')
. Finally you can use the invoke command to call the method as seen in the following screenshot.
For more explanations on how to use the Command Line refer to L2pNodeLauncher-Commands and Command List for L2PNodeLauncher Interactive Mode.
Calling the service methods on the command line is one convenient way to test and understand the running services. However you could run the LAS2peer-Tutorial-Project and find out alternative ways to run you service.