com.alarexgroup.m2mplf.net.http
Class HttpAsyncCommunicator

java.lang.Object
  extended by java.lang.Thread
      extended by com.alarexgroup.m2mplf.net.http.HttpAsyncCommunicator
All Implemented Interfaces:
java.lang.Runnable
public class HttpAsyncCommunicator
extends java.lang.Thread

This class is designated for HTTP registry communication. When sends HTTP request, application can carry on the next work without waiting on server's replay. Answer is proccessed as far as is returned. Regarding to common GPRS communication delay and speed in dates transfer is this way very convenient.

Example:

import com.alarexgroup.m2mplf.M2Mlet;
import com.alarexgroup.m2mplf.logging.Logger;
import com.alarexgroup.m2mplf.net.http.HTTPEventListener;
import com.alarexgroup.m2mplf.net.http.HttpAsyncCommunicator;
import com.alarexgroup.m2mplf.net.http.HttpCommunicator;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
public class HTTPAsyncExample extends M2Mlet implements HTTPEventListener {
    
    
    public void appStart() {
        //Url in example is working, simulates slow server, which returns answer to request after 20s.
        HttpAsyncCommunicator http = HttpAsyncCommunicator.getHttpAsyncCommunicator(this,"bearer_type=gprs;access_point=internet;timeout=40");
        http.openConnection("http://www.m2marchitect.cz/demo/m2mtest.php",HttpCommunicator.METHOD_GET);
        http.addHeaderProperty("Klic","Hodnota");
        http.setRequestId(1);
        http.sendRequest(null);//We do not need send any dates, we only create request to server.
        Logger.logInfo(Request sent!);
    }
    
    public void appPause() {
    }
    
    public void appStop(boolean unconditional) {
    }
    
    public void onHTTPEvent(int requestId, int errorCode, DataInputStream dis) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        if (requestId==1) {
            try {
                //It is our answer to request with id = 1
                while (dis.available()>0){
                    bos.write(dis.read());
                }
            catch (IOException ex) {
                //problem with reading
            }
            //Dates are now in ByteArrayOutputStream
            // Dates is i.e. possible to convert to String or record into log
            Logger.logInfo(new String(bos.toByteArray()));           
        }
    }   
}

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Method Summary
 int addHeaderProperty(java.lang.String key, java.lang.String value)
          Append a parameter to HTTP header
 void close()
          communicator logged off
static HttpAsyncCommunicator getHttpAsyncCommunicator(HTTPEventListener listener, java.lang.String connectionProfile)
          Returns initialized HTTPAsyncCommunicator.
 int openConnection(java.lang.String url, java.lang.String method)
          Opens connection to concrete URL.
 void run()
          internal method
 void sendRequest(byte[] data)
          data sending - in HTTP request
 void setRequestId(int id)
          ID setting - to match REQUEST-ANSWER in onHTTPEvent method
 
Methods inherited from class java.lang.Thread
activeCount, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, checkAccess, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

getHttpAsyncCommunicator

public static HttpAsyncCommunicator getHttpAsyncCommunicator(HTTPEventListener listener,
                                                             java.lang.String connectionProfile)
Returns initialized HTTPAsyncCommunicator.

Parameters:
listener - HTTPEventListener - this class implements HTTPEventListener interface. Into this class is directed call of onHTTPEvent menthod - for input data processing.
connectionProfile - connection profile
Returns:
HTTPAsyncCommunicator

openConnection

public int openConnection(java.lang.String url,
                          java.lang.String method)
Opens connection to concrete URL.

Parameters:
url - url - server address in form "http://www.youraddress.cz", eventually includes parameters.
method - POST, GET, HEAD
Returns:
errorCode

addHeaderProperty

public int addHeaderProperty(java.lang.String key,
                             java.lang.String value)
Append a parameter to HTTP header

Parameters:
key - - name of parameter
value - - value of parameter
Returns:
error code

sendRequest

public void sendRequest(byte[] data)
data sending - in HTTP request

Parameters:
data - to send

setRequestId

public void setRequestId(int id)
ID setting - to match REQUEST-ANSWER in onHTTPEvent method

Parameters:
id - identifier

run

public void run()
internal method

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

close

public void close()
communicator logged off