SockIt
|
00001 /********************************************************** 00002 Auto-generated SockItAPI.cpp 00003 00004 \**********************************************************/ 00005 00006 #include "JSObject.h" 00007 #include "variant_list.h" 00008 #include "DOM/Document.h" 00009 00010 #include "SockItAPI.h" 00011 00012 SockItAPI::SockItAPI(const SockItPtr& plugin, const FB::BrowserHostPtr& host) : 00013 m_plugin(plugin), m_host(host) 00014 { 00015 // Register root methods for creating network threads 00016 registerMethod("createThread", make_method(this, &SockItAPI::create_thread)); 00017 00018 // Register root methods for creating servers & clients 00019 registerMethod("createUdpClient", make_method(this, &SockItAPI::create_udp_client)); 00020 registerMethod("createUdpServer", make_method(this, &SockItAPI::create_udp_server)); 00021 registerMethod("createTcpClient", make_method(this, &SockItAPI::create_tcp_client)); 00022 registerMethod("createTcpServer", make_method(this, &SockItAPI::create_tcp_server)); 00023 00024 // Register methods for converting to and from binary data 00025 registerMethod("toBinary", make_method(this, &SockItAPI::convert_to_binary)); 00026 registerMethod("fromBinary", make_method(this, &SockItAPI::convert_from_binary)); 00027 } 00028 00029 SockItAPI::~SockItAPI() 00030 { 00031 } 00032 00033 SockItPtr SockItAPI::getPlugin() 00034 { 00035 SockItPtr plugin(m_plugin.lock()); 00036 if (!plugin) 00037 { 00038 throw FB::script_error("The plugin is invalid"); 00039 } 00040 return plugin; 00041 } 00042 00043 boost::shared_ptr<NetworkThread> SockItAPI::create_thread() 00044 { 00045 return boost::shared_ptr<NetworkThread>(new NetworkThread()); 00046 } 00047 00048 boost::shared_ptr<TcpServer> SockItAPI::create_tcp_server(int port, boost::optional<map<string, string> > options) 00049 { 00050 return default_thread.create_tcp_server(port, options); 00051 } 00052 00053 boost::shared_ptr<TcpClient> SockItAPI::create_tcp_client(const string & host, int port, 00054 boost::optional<map<string, string> > options) 00055 { 00056 return default_thread.create_tcp_client(host, port, options); 00057 } 00058 00059 boost::shared_ptr<UdpServer> SockItAPI::create_udp_server(int port, boost::optional<map<string, string> > options) 00060 { 00061 return default_thread.create_udp_server(port, options); 00062 } 00063 00064 boost::shared_ptr<UdpClient> SockItAPI::create_udp_client(const string &host, int port, 00065 boost::optional<map<string, string> > options) 00066 { 00067 return default_thread.create_udp_client(host, port, options); 00068 } 00069 00070 binary SockItAPI::convert_to_binary(const vector<byte> bytes) 00071 { 00072 // Convert it to a character array and create a string from it 00073 binary data; 00074 00075 for (int i = 0; i < bytes.size(); i++) 00076 { 00077 data.push_back((unsigned char) bytes[i]); 00078 } 00079 00080 return data; 00081 } 00082 00083 FB::VariantList SockItAPI::convert_from_binary(binary data) 00084 { 00085 vector<byte> bytes; 00086 for(int i = 0; i < data.size(); i++) 00087 { 00088 bytes.push_back((unsigned char) data.data()[i]); 00089 } 00090 00091 FB::VariantList fb_bytes; 00092 std::copy(bytes.begin(), bytes.end(), std::back_inserter(fb_bytes)); 00093 return fb_bytes; 00094 }