/**
 * Copyright (c) 2006 - 2008 Smaxe Ltd (www.smaxe.com).
 * All rights reserved.
 */

import com.smaxe.me.uv.ObjectEncoding;
import com.smaxe.me.uv.Responder;
import com.smaxe.me.uv.client.License;
import com.smaxe.me.uv.client.NetConnection;

import java.util.Hashtable;

/**
 * <code>ExMeCreateNetConnection</code> - {@link NetConnection} usage example (J2ME).
 * <p> Note: The example shows how to create, configure, set callback object and connect
 * to the server.
 * 
 * @author Andrei Sochirca
 */
public final class ExMeCreateNetConnection extends Object
{
    /**
     * Entry point.
     * 
     * @param args
     * @throws Exception 
     */
    public static void main(final String[] args) throws Exception
    {
        // NOTE:
        // you can get Evaluation Key at:
        // http://www.smaxe.com/order.jsf#request_evaluation_key
        // or buy at:
        // http://www.smaxe.com/order.jsf
        License.setKey("SET-YOUR-KEY");
        
        final NetConnection connection = new NetConnection();
        
        connection.objectEncoding(ObjectEncoding.AMF3);
        
        // configure NetConnection
        connection.configuration().put(NetConnection.Configuration.INACTIVITY_TIMEOUT, new Integer(30) /*seconds*/);
//        // BlackBerry specific socket connection parameters
//        connection.configuration().put(NetConnection.Configuration.CONNECTION_PARAMETERS, ";interface=wifi;deviceside=true");
        
        connection.client(new ClientHandler());
        
        connection.addEventListener(new NetConnectionListener());
        
        connection.connect("rtmp://localhost:1935/app", null /*arguments*/);
    }
    
    
    /**
     * <code>ClientHandler</code> - server invokes methods of this class.
     */
    public static class ClientHandler extends Object
    {
        /**
         * Constructor.
         */
        public ClientHandler()
        {
        }
        
        /**
         * Test invocation.
         * 
         * @return result string
         */
        public String testInvocation()
        {
            System.out.println("ClientHandler#testInvocation()");
            
            // emulate time spent for method execution
            try
            {
                Thread.sleep(1000);
            }
            catch (Exception e) {}
            
            return "ClientHandler#testInvocation() result";
        }
    }
    
    /**
     * <code>NetConnectionListener</code> - {@link NetConnection} listener implementation.
     */
    public static class NetConnectionListener extends NetConnection.ListenerAdapter
    {
        /**
         * Constructor.
         */
        public NetConnectionListener()
        {
        }
        
        public void onAsyncError(final NetConnection source, final String message, final Exception e)
        {
            System.out.println("NetConnection#onAsyncError: " + message + " " + e);
        }
        
        public void onIOError(final NetConnection source, final String message)
        {
            System.out.println("NetConnection#onIOError: " + message);
        }
        
        public void onNetStatus(final NetConnection source, final Hashtable info)
        {
            System.out.println("NetConnection#onNetStatus: " + info);
            
            final Object code = info.get("code");
            
            if (NetConnection.CONNECT_SUCCESS.equals(code))
            {
                source.call("testConnection", new Responder()
                {
                    public void onResult(final Object result)
                    {
                        System.out.println("Method testConnection result: " + result);
                    }
                    
                    public void onStatus(final Hashtable status)
                    {
                        System.out.println("Method testConnection status: " + status);
                    }
                }, null /*arguments*/);
            }
        }
    }
    
    /**
     * Constructor.
     */
    private ExMeCreateNetConnection()
    {
    }
}