| 
 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectohmm.ImageServer
public class ImageServer
HTTP multipart/x-mixed-replace image server.
The source frames are provided via the hook updateImage(java.awt.image.BufferedImage), which
 also sets the width and height.  The compression type, compression quality
 (if appliccable), and maximum served frames per second are controlled by URL
 query parameters "fmt" (string, e.g. "jpeg" or "png"), "fps" (float), and
 "qual" (int, 0=min, 100=max), respectively.  They default to defFmt, defFPS, and defQual if the query params are not
 given.
Key and mouse events are collected from the client and made available in
 image coordinates via the hooks handleKey(char) and handleMouse(int, int, int, int).
Tested with Chrome and Safari. Firefox might or might not work (Mozilla bug 625012). On any browser, non-alphanumeric keycodes may differ from Java std.
The server knows about the following request paths:
Once the server is running, you can connect to it from a remote host by bringing up e.g. Google Chrome and browsing to the url
http://ADDR:PORT/server.htmlwhere ADDR is the IP address and PORT the listening port of the server. You can further control the image format, framerate, and quality like this:
http://ADDR:PORT/server.html?fmt=jpg&fps=10&qual=95
| Nested Class Summary | |
|---|---|
| protected  class | ImageServer.ExchangeStateWraps an HttpExchange with some derived values. | 
| Field Summary | |
|---|---|
|  boolean | dbgDebug next server frame. | 
| static java.lang.String | DEF_FMTDefault defFmt. | 
| static float | DEF_FPSDefault defFPS. | 
| static int | DEF_PORTDefault TCP listen port. | 
| static int | DEF_QUALDefault defQual. | 
| static int | DEF_STOP_SECDefault stop(int)delay seconds. | 
|  java.lang.String | defFmtDefault ImageIO format name. | 
|  float | defFPSDefault max framerate. | 
|  int | defQualDefault image quality in the range 0 (min) to 100 (max). | 
| static int | EVENT_FLAG_ALTKEYEvent flags for handleMouse(int, int, int, int). | 
| static int | EVENT_FLAG_CTRLKEYEvent flags for handleMouse(int, int, int, int). | 
| static int | EVENT_FLAG_LBUTTONEvent flags for handleMouse(int, int, int, int). | 
| static int | EVENT_FLAG_MBUTTONEvent flags for handleMouse(int, int, int, int). | 
| static int | EVENT_FLAG_RBUTTONEvent flags for handleMouse(int, int, int, int). | 
| static int | EVENT_FLAG_SHIFTKEYEvent flags for handleMouse(int, int, int, int). | 
| static int | EVENT_LBUTTONDBLCLKEvent types for handleMouse(int, int, int, int). | 
| static int | EVENT_LBUTTONDOWNEvent types for handleMouse(int, int, int, int). | 
| static int | EVENT_LBUTTONUPEvent types for handleMouse(int, int, int, int). | 
| static int | EVENT_MBUTTONDBLCLKEvent types for handleMouse(int, int, int, int). | 
| static int | EVENT_MBUTTONDOWNEvent types for handleMouse(int, int, int, int). | 
| static int | EVENT_MBUTTONUPEvent types for handleMouse(int, int, int, int). | 
| static int | EVENT_MOUSEMOVEEvent types for handleMouse(int, int, int, int). | 
| static int | EVENT_RBUTTONDBLCLKEvent types for handleMouse(int, int, int, int). | 
| static int | EVENT_RBUTTONDOWNEvent types for handleMouse(int, int, int, int). | 
| static int | EVENT_RBUTTONUPEvent types for handleMouse(int, int, int, int). | 
| protected  java.util.concurrent.ExecutorService | executorThe internal transaction executor. | 
| static int | HTTP_FORBIDDENHTTP response codes | 
| static int | HTTP_NOT_FOUNDHTTP response codes | 
| static int | HTTP_OKHTTP response codes | 
| protected  int | imageClientsNumber of clients to which we're currently serving images. | 
| static java.lang.String | MIME_BOUNDARYMIME multipart boundary | 
|  java.io.PrintStream | msgStreamStrems for messages and warnings, or null to disable. | 
| static java.text.DecimalFormat | NFNumber format for printouts. | 
| protected  boolean | oneClientWhether to limit serving images to one client. | 
|  int | portServer TCP listen port. | 
| protected  com.sun.net.httpserver.HttpServer | serverThe internal server object. | 
| static java.lang.String | SERVER_HTMLServer html file. | 
|  boolean | spewMouseMovesWhether to output lots of debug messages for mousemove events. | 
|  boolean | spewStackTracesWhether to dump stack traces on server errors. | 
|  java.io.PrintStream | warnStreamStrems for messages and warnings, or null to disable. | 
| Constructor Summary | |
|---|---|
| ImageServer()Creates server using DEF_PORT. | |
| ImageServer(int port)Creates server. | |
| Method Summary | |
|---|---|
| protected  java.io.InputStream | getServerHTML()Hook to get the html the server sends to the client. | 
| static java.net.InetAddress[] | getServerIPs()Get the server's non-local IP address(es). | 
| protected  void | handleEcho(ImageServer.ExchangeState xs)Serves debug echo info. | 
| protected  void | handleExchange(com.sun.net.httpserver.HttpExchange xch)Dispatches HTTP exchange to one of the handle*(ExchangeState) methods. | 
| protected  void | handleImage(ImageServer.ExchangeState xs)Serves images. | 
| protected  void | handleKey(char c)Hook to handle key events. | 
| protected  void | handleKey(ImageServer.ExchangeState xs)Handles key event requests. | 
| protected  void | handleMouse(ImageServer.ExchangeState xs)Handles mouse event requests. | 
| protected  void | handleMouse(int t,
            int x,
            int y,
            int m)Hook to handle mouse events. | 
| protected  void | handleServer(ImageServer.ExchangeState xs)Serves SERVER_HTMLusinggetServerHTML(). | 
| protected  void | handleUnknown(ImageServer.ExchangeState xs)Handles unknown request paths. | 
| static void | main(java.lang.String[] argv)Starts a test server. | 
| protected  void | msg(java.lang.String m)Display a message to msgStream, if any. | 
| protected static long | nowMS()Returns current time in milliseconds. | 
|  void | start()Starts server. | 
|  void | stop()stop(int)delayingDEF_STOP_SEC. | 
|  void | stop(int delay)Stops serverandexecutor. | 
| protected  java.awt.image.BufferedImage | updateImage(java.awt.image.BufferedImage bi)Hook called to update the server image. | 
| protected  void | warn(java.lang.String m)Display a warning to warnStream, if any. | 
| Methods inherited from class java.lang.Object | 
|---|
| clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait | 
| Field Detail | 
|---|
public static final int DEF_PORT
public static final java.lang.String DEF_FMT
defFmt.
public static final float DEF_FPS
defFPS.
public static final int DEF_QUAL
defQual.
public static final int DEF_STOP_SEC
stop(int) delay seconds.
public static final java.lang.String SERVER_HTML
public static final int HTTP_OK
public static final int HTTP_NOT_FOUND
public static final int HTTP_FORBIDDEN
public static final java.lang.String MIME_BOUNDARY
public static final int EVENT_MOUSEMOVE
Event types for handleMouse(int, int, int, int).
Compatible with OpenCV event types.
public static final int EVENT_LBUTTONDOWN
Event types for handleMouse(int, int, int, int).
Compatible with OpenCV event types.
public static final int EVENT_RBUTTONDOWN
Event types for handleMouse(int, int, int, int).
Compatible with OpenCV event types.
public static final int EVENT_MBUTTONDOWN
Event types for handleMouse(int, int, int, int).
Compatible with OpenCV event types.
public static final int EVENT_LBUTTONUP
Event types for handleMouse(int, int, int, int).
Compatible with OpenCV event types.
public static final int EVENT_RBUTTONUP
Event types for handleMouse(int, int, int, int).
Compatible with OpenCV event types.
public static final int EVENT_MBUTTONUP
Event types for handleMouse(int, int, int, int).
Compatible with OpenCV event types.
public static final int EVENT_LBUTTONDBLCLK
Event types for handleMouse(int, int, int, int).
Compatible with OpenCV event types.
public static final int EVENT_RBUTTONDBLCLK
Event types for handleMouse(int, int, int, int).
Compatible with OpenCV event types.
public static final int EVENT_MBUTTONDBLCLK
Event types for handleMouse(int, int, int, int).
Compatible with OpenCV event types.
public static final int EVENT_FLAG_LBUTTON
Event flags for handleMouse(int, int, int, int).
Compatible with OpenCV event flags.
public static final int EVENT_FLAG_RBUTTON
Event flags for handleMouse(int, int, int, int).
Compatible with OpenCV event flags.
public static final int EVENT_FLAG_MBUTTON
Event flags for handleMouse(int, int, int, int).
Compatible with OpenCV event flags.
public static final int EVENT_FLAG_CTRLKEY
Event flags for handleMouse(int, int, int, int).
Compatible with OpenCV event flags.
public static final int EVENT_FLAG_SHIFTKEY
Event flags for handleMouse(int, int, int, int).
Compatible with OpenCV event flags.
public static final int EVENT_FLAG_ALTKEY
Event flags for handleMouse(int, int, int, int).
Compatible with OpenCV event flags.
public static final java.text.DecimalFormat NF
public final int port
public volatile java.lang.String defFmt
public volatile float defFPS
public volatile int defQual
public volatile boolean dbg
public volatile java.io.PrintStream msgStream
public volatile java.io.PrintStream warnStream
public volatile boolean spewMouseMoves
public volatile boolean spewStackTraces
protected volatile int imageClients
protected volatile boolean oneClient
protected final com.sun.net.httpserver.HttpServer server
protected final java.util.concurrent.ExecutorService executor
| Constructor Detail | 
|---|
public ImageServer(int port)
            throws java.io.IOException
Creates server.
You may configure public fields before calling start().
port - the TCP listen port
java.io.IOException
public ImageServer()
            throws java.io.IOException
DEF_PORT.
java.io.IOException| Method Detail | 
|---|
protected void handleExchange(com.sun.net.httpserver.HttpExchange xch)
                       throws java.io.IOException
java.io.IOException
protected void handleEcho(ImageServer.ExchangeState xs)
                   throws java.io.IOException
java.io.IOException
protected void handleServer(ImageServer.ExchangeState xs)
                     throws java.io.IOException
SERVER_HTML using getServerHTML().
java.io.IOException
protected java.io.InputStream getServerHTML()
                                     throws java.io.IOException
Hook to get the html the server sends to the client.
Default impl uses getResourceAsStream() to open and return
 SERVER_HTML.
java.io.IOException
protected void handleImage(ImageServer.ExchangeState xs)
                    throws java.lang.Exception
java.lang.Exceptionprotected java.awt.image.BufferedImage updateImage(java.awt.image.BufferedImage bi)
Hook called to update the server image.
bi - the prior image, if any, else null
Will be called by a server exchange handling thread while synchronized
 on this ImageServer instance.
Default impl does nothing. Subclasses can override this to provide images. To avoid creating a lot of memory garbage it is recommended to create the image once and then to update it in place on subsequent frames.
protected void handleMouse(ImageServer.ExchangeState xs)
Handles mouse event requests.
Parses the event parameters and then calls handleMouse(int,
 int, int, int) which can be overridden as desired.
protected void handleMouse(int t,
                           int x,
                           int y,
                           int m)
Hook to handle mouse events.
t - one of the EVENT_* constantsx - the x pixel coordinate of the mouse eventy - the y pixel coordinate of the mouse eventm - bitmask of the EVENT_FLAG_* constants
 This is called when the client notifies the server that a mouse event has occured over the image.
Default impl does nothing. Subclasses can override this to take action on mouse events over the client image.
protected void handleKey(ImageServer.ExchangeState xs)
Handles key event requests.
Parses the event parameters and then calls handleKey(char)
 which can be overridden as desired.
protected void handleKey(char c)
Hook to handle key events.
c - the key character
 This is called when the client notifies the server that a key event has occured over the image.
Default impl does nothing. Subclasses can override this to take action on key events over the client image.
protected void handleUnknown(ImageServer.ExchangeState xs)
                      throws java.io.IOException
java.io.IOExceptionprotected static long nowMS()
protected void msg(java.lang.String m)
msgStream, if any.
protected void warn(java.lang.String m)
warnStream, if any.
public void start()
           throws java.io.IOException
java.io.IOExceptionpublic void stop(int delay)
delay - seconds to wait for any ongoing exchanges to finish
 Call this to bring the server down.
public void stop()
stop(int) delaying DEF_STOP_SEC.
public static java.net.InetAddress[] getServerIPs()
                                           throws java.io.IOException
java.io.IOException
public static void main(java.lang.String[] argv)
                 throws java.io.IOException
Starts a test server.
argv - up to one arg is accepted giving the TCP listen port
 Serves synthetic images marked with their frame number.
java.io.IOException| 
 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||