com.alarexgroup.m2mplf.net.tcpip
Class ClientSocketCommunicator

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

Class for TCP-IP comunication. This class behaves as a client.

Example:

import com.alarexgroup.m2mplf.M2Mlet;
import com.alarexgroup.m2mplf.adapters.serial.SerialAdapter;
import com.alarexgroup.m2mplf.logging.Logger;
import com.alarexgroup.m2mplf.net.tcpip.ClientSocketCommunicator;
import com.alarexgroup.m2mplf.net.tcpip.TCPIPEventListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class TCPIPexample extends M2Mlet implements TCPIPEventListener {
    ClientSocketCommunicator tcpip;
    SerialAdapter serial;
   
    public TCPIPexample() {
    }

    
    public void appStart() {
        tcpip = ClientSocketCommunicator.getClientSocketCommunicator(this,"bearer_type=gprs;access_point=internet;timeout=40",0);
        tcpip.openConnection("misha.alarex.net:9999");
       
        OutputStream dos = tcpip.getOutputStream();
        try {
            dos.write(Hello, how are you?.getBytes());
            dos.flush();
            
        catch (IOException ex) {
            Logger.logError("Problem");
            Logger.logError(ex);
        }
       
    }

   
    public void onTCPIPEvent(int errorCode, InputStream dis, int id) {
        
        if (errorCode==ClientSocketCommunicator.ERROR_CODE_ALL_OK) {
           
           //Here are processed input dates from InputStream
            
        }
    }
 
    public void appPause() {
    }
    
    public void appStop(boolean unconditional) {
    }


    
}

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
static int ERROR_CODE_ALL_OK
          All right
static int ERROR_CODE_CONNECTION_NOT_FOUND
          Establish contact isn't possible.
static int ERROR_CODE_INVALID_PARAMETERS
          Bad format of address or connectionProfile
static int ERROR_CODE_IO_EXCEPTION
          IO exception
static int ERROR_CODE_SECURITY_EXCEPTION
          J2ME security error
static int ERROR_CODE_UNKNOWN_ERROR
          Unknown error
static int longSleep
          Optimalization of performance and module's processor load Time (ms) when thread sleep - thread is waiting for input data.
static int shortSleep
          Optimalization of performance and module's processor load Time (ms) of thread sleep - thread is waiting for input data.
static byte SOCKET_OPTION_DELAY
          Value 0 turns off Nagle algorithm for operations with buffer.
static byte SOCKET_OPTION_KEEPALIVE
          Value 0 turns off KEEPALIVE
Value 0 provides active KEEPALIVE
static byte SOCKET_OPTION_LINGER
          Value is the time, during which dates are processing untill the connection is closed (0 = off, value isn't 0 = time in sec. )
static byte SOCKET_OPTION_RCVBUF
          Buffer size - dates receipt
static byte SOCKET_OPTION_SNDBUF
          Buffer size - dates sending
static int timeForSleep
          Thread enters into slower mode After this time (since last comunication).
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Method Summary
 int closeConnection()
          Closes conection
static ClientSocketCommunicator getClientSocketCommunicator(TCPIPEventListener listener, java.lang.String connectionProfile, int communicatorId)
          This method initialize comunicator and returns his instance.
 java.io.InputStream getInputStream()
          Returns InputStream - for dates receipt.
 java.io.OutputStream getOutputStream()
          Returns OutputStream - for dates sending.
 int openConnection(java.lang.String url)
          opens connection to defined address
 void run()
          Internal method - do not use it
 int setSocketOption(byte option, int value)
          Set TCP-IP comunication's parameters.
 
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
 

Field Detail

shortSleep

public static int shortSleep
Optimalization of performance and module's processor load Time (ms) of thread sleep - thread is waiting for input data.

longSleep

public static int longSleep
Optimalization of performance and module's processor load Time (ms) when thread sleep - thread is waiting for input data. This value will be used, if the data didn't came after timeForSleep

timeForSleep

public static int timeForSleep
Thread enters into slower mode After this time (since last comunication). In this mode it less burdes the mudule's sources.

SOCKET_OPTION_DELAY

public static final byte SOCKET_OPTION_DELAY
Value 0 turns off Nagle algorithm for operations with buffer.

SOCKET_OPTION_KEEPALIVE

public static final byte SOCKET_OPTION_KEEPALIVE
Value 0 turns off KEEPALIVE
Value 0 provides active KEEPALIVE

SOCKET_OPTION_LINGER

public static final byte SOCKET_OPTION_LINGER
Value is the time, during which dates are processing untill the connection is closed (0 = off, value isn't 0 = time in sec. )

SOCKET_OPTION_RCVBUF

public static final byte SOCKET_OPTION_RCVBUF
Buffer size - dates receipt

SOCKET_OPTION_SNDBUF

public static final byte SOCKET_OPTION_SNDBUF
Buffer size - dates sending

ERROR_CODE_ALL_OK

public static final int ERROR_CODE_ALL_OK
All right

See Also:
Constant Field Values

ERROR_CODE_CONNECTION_NOT_FOUND

public static final int ERROR_CODE_CONNECTION_NOT_FOUND
Establish contact isn't possible.

See Also:
Constant Field Values

ERROR_CODE_INVALID_PARAMETERS

public static final int ERROR_CODE_INVALID_PARAMETERS
Bad format of address or connectionProfile

See Also:
Constant Field Values

ERROR_CODE_SECURITY_EXCEPTION

public static final int ERROR_CODE_SECURITY_EXCEPTION
J2ME security error

See Also:
Constant Field Values

ERROR_CODE_IO_EXCEPTION

public static final int ERROR_CODE_IO_EXCEPTION
IO exception

See Also:
Constant Field Values

ERROR_CODE_UNKNOWN_ERROR

public static final int ERROR_CODE_UNKNOWN_ERROR
Unknown error

See Also:
Constant Field Values
Method Detail

getClientSocketCommunicator

public static ClientSocketCommunicator getClientSocketCommunicator(TCPIPEventListener listener,
                                                                   java.lang.String connectionProfile,
                                                                   int communicatorId)
This method initialize comunicator and returns his instance.

Parameters:
comunicatorId - - identification - if is used more active TCP-IP conections
listener - TCPIPEventListener
connectionProfile - conection parameters according to SIEMENS TC 65 documentation
Returns:
active comunicator

setSocketOption

public int setSocketOption(byte option,
                           int value)
Set TCP-IP comunication's parameters. It is necessary before connections is open openConnection(java.lang.String)

Parameters:
option - identifier of parameter
SOCKET_OPTION_KEEPALIVE
SOCKET_OPTION_DELAY
SOCKET_OPTION_LINGER
SOCKET_OPTION_RCVBUF
SOCKET_OPTION_SNDBUF
value - value of parameter
Returns:
errorCode

openConnection

public int openConnection(java.lang.String url)
opens connection to defined address

Parameters:
url - address of server in form : 187.10.121.15:6050 or my.server.cz:5050
Returns:
ErrorCode

getOutputStream

public java.io.OutputStream getOutputStream()
Returns OutputStream - for dates sending.

Returns:
DataOutputStream

getInputStream

public java.io.InputStream getInputStream()
Returns InputStream - for dates receipt.

Returns:
InputStream

closeConnection

public int closeConnection()
Closes conection

Returns:
ErrorCode

run

public void run()
Internal method - do not use it

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