SockIt
Public Member Functions | Private Attributes

SockItAPI Class Reference

#include <SockItAPI.h>

List of all members.

Public Member Functions

 SockItAPI (const SockItPtr &plugin, const FB::BrowserHostPtr &host)
virtual ~SockItAPI ()
 Destructor. Remember that this object will not be released until the browser is done with it; this will almost definitely be after the plugin is released.
SockItPtr getPlugin ()
 Gets a reference to the plugin that was passed in when the object was created. If the plugin has already been released then this will throw a FB::script_error that will be translated into a javascript exception in the page.
boost::shared_ptr< NetworkThreadcreate_thread ()
boost::shared_ptr< TcpServercreate_tcp_server (int port, boost::optional< map< string, string > > options)
boost::shared_ptr< TcpClientcreate_tcp_client (const string &host, int port, boost::optional< map< string, string > > options)
boost::shared_ptr< UdpServercreate_udp_server (int port, boost::optional< map< string, string > > options)
boost::shared_ptr< UdpClientcreate_udp_client (const string &host, int port, boost::optional< map< string, string > > options)
binary convert_to_binary (const vector< byte > bytes)
FB::VariantList convert_from_binary (binary data)

Private Attributes

NetworkThread default_thread
SockItWeakPtr m_plugin
FB::BrowserHostPtr m_host

Detailed Description

Exposes the plugin's API to the browser. Primarily auto-generated by FireBreath, but edited to add root methods for the plugin and expose them to the javascript.

See also:
http://www.firebreath.org/

Definition at line 35 of file SockItAPI.h.


Constructor & Destructor Documentation

SockItAPI::SockItAPI ( const SockItPtr &  plugin,
const FB::BrowserHostPtr &  host 
)

Constructor for the JSAPI object. Registers root API methods for the plugin to be available to Javascript.

See also:
FB::JSAPIAuto::registerMethod
FB::JSAPIAuto::registerProperty
FB::JSAPIAuto::registerEvent

Definition at line 12 of file SockItAPI.cpp.

References convert_from_binary(), convert_to_binary(), create_tcp_client(), create_tcp_server(), create_thread(), create_udp_client(), and create_udp_server().

                                                                          :
    m_plugin(plugin), m_host(host)
{
    // Register root methods for creating network threads
    registerMethod("createThread", make_method(this, &SockItAPI::create_thread));

    // Register root methods for creating servers & clients
    registerMethod("createUdpClient", make_method(this, &SockItAPI::create_udp_client));
    registerMethod("createUdpServer", make_method(this, &SockItAPI::create_udp_server));
    registerMethod("createTcpClient", make_method(this, &SockItAPI::create_tcp_client));
    registerMethod("createTcpServer", make_method(this, &SockItAPI::create_tcp_server));

    // Register methods for converting to and from binary data
    registerMethod("toBinary", make_method(this, &SockItAPI::convert_to_binary));
    registerMethod("fromBinary", make_method(this, &SockItAPI::convert_from_binary));
}
SockItAPI::~SockItAPI ( ) [virtual]

Destructor. Remember that this object will not be released until the browser is done with it; this will almost definitely be after the plugin is released.

Definition at line 29 of file SockItAPI.cpp.

{
}

Member Function Documentation

FB::VariantList SockItAPI::convert_from_binary ( binary  data)

Utility function for converting a string, assumed to be binary data returned from convert_to_binary.

Parameters:
dataThe (binary) data to be converted to an array of characters

Definition at line 83 of file SockItAPI.cpp.

Referenced by SockItAPI().

{
    vector<byte> bytes;
    for(int i = 0; i < data.size(); i++)
    {
        bytes.push_back((unsigned char) data.data()[i]);
    }

    FB::VariantList fb_bytes;
    std::copy(bytes.begin(), bytes.end(), std::back_inserter(fb_bytes));
    return fb_bytes;
}
binary SockItAPI::convert_to_binary ( const vector< byte bytes)

Utility function for converting an array of integers, assumed to hexadecimal numbers, to a string.

Parameters:
bytesThe bytes of data to be converted into a string

Definition at line 70 of file SockItAPI.cpp.

Referenced by SockItAPI().

{
    // Convert it to a character array and create a string from it
    binary data;

    for (int i = 0; i < bytes.size(); i++)
    {
        data.push_back((unsigned char) bytes[i]);
    }

    return data;
}
boost::shared_ptr< TcpClient > SockItAPI::create_tcp_client ( const string &  host,
int  port,
boost::optional< map< string, string > >  options 
)

Creates a new TCP client on the default NetworkThread.

Parameters:
hostThe hostname to which this TCP client will connect
portThe port on the remote host to which this client should connect
optionsThe set of options passed in from Javascript.
Returns:
A shared pointer to a newly created TCP client

Definition at line 53 of file SockItAPI.cpp.

References NetworkThread::create_tcp_client(), and default_thread.

Referenced by SockItAPI().

{
    return default_thread.create_tcp_client(host, port, options);
}
boost::shared_ptr< TcpServer > SockItAPI::create_tcp_server ( int  port,
boost::optional< map< string, string > >  options 
)

Creates a new TCP server on the default NetworkThread.

Parameters:
portThe port on which this new TCP server should listen
optionsThe set of options passed in from Javascript.
Returns:
A shared pointer to a newly created TCP server

Definition at line 48 of file SockItAPI.cpp.

References NetworkThread::create_tcp_server(), and default_thread.

Referenced by SockItAPI().

{
    return default_thread.create_tcp_server(port, options);
}
boost::shared_ptr< NetworkThread > SockItAPI::create_thread ( )

Creates a new NetworkThread object on which we can create new clients and servers.

Returns:
A shared pointer to a newly created NetworkThread object

Definition at line 43 of file SockItAPI.cpp.

Referenced by SockItAPI().

{
    return boost::shared_ptr<NetworkThread>(new NetworkThread());
}
boost::shared_ptr< UdpClient > SockItAPI::create_udp_client ( const string &  host,
int  port,
boost::optional< map< string, string > >  options 
)

Creates a new UDP client on the default NetworkThread.

Parameters:
hostThe hostname to which this UDP client will connect
portThe port on the remote host to which this client should connect
optionsThe set of options passed in from Javascript.
Returns:
A shared pointer to a newly created UDP client

Definition at line 64 of file SockItAPI.cpp.

References NetworkThread::create_udp_client(), and default_thread.

Referenced by SockItAPI().

{
    return default_thread.create_udp_client(host, port, options);
}
boost::shared_ptr< UdpServer > SockItAPI::create_udp_server ( int  port,
boost::optional< map< string, string > >  options 
)

Creates a new UDP server on the default NetworkThread.

Parameters:
portThe port on which this new UDP server should listen
optionsThe set of options passed in from Javascript.
Returns:
A shared pointer to a newly created UDP server

Definition at line 59 of file SockItAPI.cpp.

References NetworkThread::create_udp_server(), and default_thread.

Referenced by SockItAPI().

{
    return default_thread.create_udp_server(port, options);
}
SockItPtr SockItAPI::getPlugin ( )

Gets a reference to the plugin that was passed in when the object was created. If the plugin has already been released then this will throw a FB::script_error that will be translated into a javascript exception in the page.

Definition at line 33 of file SockItAPI.cpp.

References m_plugin.

{
    SockItPtr plugin(m_plugin.lock());
    if (!plugin)
    {
        throw FB::script_error("The plugin is invalid");
    }
    return plugin;
}

Member Data Documentation

The default network thread used if no thread is explicitly created.

Definition at line 127 of file SockItAPI.h.

Referenced by create_tcp_client(), create_tcp_server(), create_udp_client(), and create_udp_server().

FB::BrowserHostPtr SockItAPI::m_host [private]

A handle on the browser to gather information if necessary.

Definition at line 137 of file SockItAPI.h.

SockItWeakPtr SockItAPI::m_plugin [private]

A handle on the plugin object itself.

Definition at line 132 of file SockItAPI.h.

Referenced by getPlugin().


The documentation for this class was generated from the following files:
 All Classes Files Functions Variables Typedefs Friends Defines