how to add java.io.PrintWriter to embed Jetty Server

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: roger, 99jonathan, imaqine

how to add java.io.PrintWriter to embed Jetty Server

Postby Docta_J » Sun Dec 16, 2012 4:02 am

I am new to Lejos and in my first year of experience with Java. I was able to modify the Intelligence Unleashed segoway to use Dexter Industries DIMU. (I learned a lot!). I am now beginning to use the Dexter Industries WIFI sensor. There is a beta library for this device for Lejos from the manufacturer. I am able to communicate via wifi with the NXT. I am looking to implement an HTML5 websocket server on the NXT. In order to avoid writing code for the long handshake, I am trying to embed a Jetty server. I have worked with Jetty on my home Linux box, and can deploy it without difficulty. As a first step with the NXT, I just want to deploy a simple test server that responds when called. I have tried my code on the JRE 7 linux box and it works. When I compile this using eclipse juno and NXT runtime, I get the "indirectly referenced from required .class files." I have researched this forum, stack overflow and google search. I know I need to add the missing .jar file to the classpath, but here I am stumped. I can obtain the source code for the missing class. It is java.io.PrintWriter.java . However, the classes.jar in NXT runtime are already compiled, then jar'd (is that a verb?). I have tried to add the .java file as a referenced library, and have also placed it in the src (default package). These attempts didn't work. I have also added the source code to the classes source files in the NXJ Development Kit Sources and tried to build the classes.jar again using Ant in Eclipse, but it did not compile due to the addition of the new file to the .io folder. Is there any way to integrate this class, which is absent in the classes.jar in its correct location so it can be referenced and interact appropriately with the rest of the java.io package? I am posting the code for the test server below. I have all of the jetty-specific jars in their appropriate locations. Can I replace the entire java.io class in the NXT runtime library? I suspect that would break more than it fixes.
Code: Select all
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.Writer;
import java.io.IOException;
 
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
 
public class NXTHello extends AbstractHandler
{
    public void handle(String target,
                       Request baseRequest,
                       HttpServletRequest request,
                       HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>You have reached Fredly.  I'm not in right now...</h1>");
    }
 
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);
        server.setHandler(new NXTHello());
 
        server.start();
        server.join();
    }
}
Docta_J
New User
 
Posts: 10
Joined: Sat Jul 07, 2012 3:15 am
Location: North Dakota, USA

Re: how to add java.io.PrintWriter to embed Jetty Server

Postby gloomyandy » Sun Dec 16, 2012 3:42 pm

Hi,
it looks like you are trying to use code that requires many of the more complex Java classes on the NXT. This is a pretty advanced project and will require you to port/write many of these classes to the NXT environment (which has some Java limitations, like no class loader or reflection and is very restricted in terms of available memory). If you want to run a web server you may be better off looking for a simpler one and porting this...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3012
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: how to add java.io.PrintWriter to embed Jetty Server

Postby Docta_J » Sun Dec 16, 2012 4:47 pm

Hi Andy,
Thanks for the quick response. I was afraid that was the case. I will drop the Jetty server idea and I will build on the library supplied by the manufacturer to create a simple websocket server. I am hoping to create a bidirectional TCP link to relay telemetry and commands to and from clients using Chrome with HTML 5 websockets. Since these websockets don't require polling, I am looking to see if I can make observations on dynamic processes in real time with minimal impact on the robot's performance, especially loop times. One application will be dynamic adjustment of PID constants. This should keep me busy and off the streets for a while.

John
Docta_J
New User
 
Posts: 10
Joined: Sat Jul 07, 2012 3:15 am
Location: North Dakota, USA

Re: how to add java.io.PrintWriter to embed Jetty Server

Postby gloomyandy » Sun Dec 16, 2012 6:38 pm

Hi,
Before you go too far you may want to start another thread to ask about how well the WiFi sensor works with leJOS. There may be a later library than the one you have. I don't have any direct experience of this sensor but I know that some of the other devs have had a look at it...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3012
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: how to add java.io.PrintWriter to embed Jetty Server

Postby Docta_J » Sun Dec 16, 2012 8:29 pm

Will do. I just found the appropriate thread 'Dexter Industries WiFi and RS485' and I have asked the question. Thanks for your guidance.

John
Docta_J
New User
 
Posts: 10
Joined: Sat Jul 07, 2012 3:15 am
Location: North Dakota, USA

Re: how to add java.io.PrintWriter to embed Jetty Server

Postby skoehler » Tue Dec 18, 2012 2:30 pm

You have already found the thread, that describes the issues with Dexter Wifi sensor. The bottom line is:
It doesn't work reliably. See my post there for an explanation.

Jetty is a servlet engine. As far as I know, Servlet engines require Reflection. Reflection is not available in leJOS. Hence, you can't run it on leJOS. Aport from that, Jetty is an HTTP server. And a HTTP server will have to deal a lot with character sets. leJOS does not properly support charsets in several places (e.g. the constructor of String). Full support for latin1 and utf8 is on my TODO list, but no other charsets will be supported due to the vast amout of space required for charset<->unicode mappings. So if we implement a HTTP server for leJOS, it will certainly reject anything but latin1/utf8.
skoehler
leJOS Team Member
 
Posts: 1128
Joined: Thu Oct 30, 2008 4:54 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 6 guests

more stuff