Package com.motorola.oem.ipd

IPD - IP Director.

See:
          Description

Interface Summary
IpDirectorAvailabilityListener A listener for IP Director(IPD) state changed event.
 

Class Summary
LocalSocketManager Local sockets managment.
 

Package com.motorola.oem.ipd Description

IPD - IP Director. Enables local socket service over a Serial connectivity. It directs IP packets from/to an External device to/from the MIDlet.
Opening a Local socket depends on the IPD state. The IPD is controled and configured by the External device using certain AT commands.
Read the below IPD states section in order to understand this dependancy.
To learn more about how the External device can activate/deactivate the IPD, please refer G24 User Guide.

IP Director states


IPD STATE ACTIVE

Once IPD state turns to LocalSocketManager.IPD_STATE_ACTIVE, the IPD will take care of the local socket's (IP) packets routing (directing) between the MIDlet and the External Device's application.
MIDlet and External device can talk to each other using GCF IP connections.
Depending on the GPRS service availability, MIDlet and External Device may communicate to the Internet.

IPD STATE INACTIVE

Once IPD state turns to LocalSocketManager.IPD_STATE_INACTIVE,
Local socket service becomes unavailable.
Depending on the GPRS service availability, MIDlet may communicate only to the Internet

Code example:

Download a full TCP/UDP local sockets example sources here as a NetBeans project.

Example description:
The follwing MIDlet (IpdSocketsMIDlet.java) will manage 3 local socket threads according to the IPD state.
An IPD state listener thread class (IpdListener.java) is created by the MIDlet in order to manage the local sockets according to the IPD state:
1) TCP listen socket - simply echos back client's data - File ServerSocketTcp.java
2) UDP server socket - simply echos back client's data - Files: ServerSocketUdp.java and UDPsender.java
3) TCP client socket - File ClientSocketTcp.java


Example code glance:
The following 2 files demostrate a basic implementation of the IpDirectorAvailabilityListener.
 
  //----------------------------------------------------------------------------
  // MIDlet
  // FILE:    IpdSocketsMIDlet.java 
  // Description: Simply creates an IPD listener thread. 
  //----------------------------------------------------------------------------
  
  package lsockets;

  import javax.microedition.midlet.MIDlet;
  
  // Listens on IPD state changed event.
  //Once IPD state turns to ACTIVE, MIDlet opens 3 local sockets
  public class IpdSocketsMIDlet extends MIDlet {
      
      public IpdSocketsMIDlet() {
            System.out.println("MIDlet: IpdSocketsMIDlet is initializing...");
      }
      
      public void startApp() {
          // create listener thread
            new IpdListener();
      }
      
      public void pauseApp() {
      }
      
      public void destroyApp(boolean unconditional) {
      }
      
  }
    

 
  //----------------------------------------------------------------------------
  // Listener implements IpDirectorAvailabilityListener interface
  // FILE:    IpdListener.java 
  // Description: Listens on IPD state changed event. 
  //              Once IPD state turns to ACTIVE, 3 local sockets are opened.
  //----------------------------------------------------------------------------
  
  package lsockets;

  import com.motorola.oem.ipd.IpDirectorAvailabilityListener;
  import com.motorola.oem.ipd.LocalSocketManager;
  
  public class IpdListener implements IpDirectorAvailabilityListener {
      // 3 sockets:
      private ServerSocketTcp sst = null;
      private ServerSocketUdp ssd = null;
      private ClientSocketTcp cst = null;
      
      static  boolean socketsInitialized = false;
      
      public IpdListener() {
          System.out.println("MIDlet: IpdListener is initializing...");
        
          // set this class as IPD listener
          LocalSocketManager.setIpDirectorListener(this);
          if(LocalSocketManager.getIpdState() == LocalSocketManager.IPD_STATE_ACTIVE) {
                openSockets();
          }
      }
      // implement handler
      public void onIpdStateChanged(int newIpdState) {
          System.out.println("IpdListener: IPD State Changed to "+newIpdState);
        
          switch(newIpdState){
                case LocalSocketManager.IPD_STATE_ACTIVE:
                        if(!socketsInitialized){
                          openSockets();
                        }
                        break;
                case LocalSocketManager.IPD_STATE_INACTIVE:
                        if(socketsInitialized){
                          closeSockets();
                        }
                        break;
                default :
                        break;
          }
        
      }
      
      // open local sockets
      public  void openSockets() {
      
          // Open a TCP server thread listening on the local port
          sst = new ServerSocketTcp(LocalSocketManager.getListenPort());
      
          // Open a UDP server thread listening on the local port
          ssd = new ServerSocketUdp(LocalSocketManager.getListenPort());
      
          // Open a TCP client thread to the external device's IP on port
          // 5678(remote server's port).
          cst = new ClientSocketTcp(LocalSocketManager.getRemoteAddress(), 5678);
      
          socketsInitialized = true;      
      }
      
      // close all local sockets
      public  void closeSockets() {
          System.out.println("IpdListener: closeSockets() entered");
          if(sst != null ){sst.close(); sst = null;}
          if(ssd != null ){ssd.close(); ssd = null;}
          if(cst != null ){cst.close(); cst = null;}
        
          socketsInitialized = false;
      }
      
  }