Rev 1954 | Rev 1962 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1954 | Rev 1959 | ||
|---|---|---|---|
| Line 25... | Line 25... | ||
| 25 | 25 | ||
| 26 | #include <boost/lexical_cast.hpp> |
26 | #include <boost/lexical_cast.hpp> |
| 27 | 27 | ||
| 28 | #include "vm/Manager.h" |
28 | #include "vm/Manager.h" |
| 29 | #include "net/Manager.h" |
29 | #include "net/Manager.h" |
| - | 30 | #include "common/common.h" |
|
| - | 31 | #include "common/log.h" |
|
| - | 32 | #include "common/exception.h" |
|
| 30 | 33 | ||
| 31 | namespace atom { |
34 | namespace atom { |
| 32 | namespace vm { |
35 | namespace vm { |
| 33 | namespace plugin { |
36 | namespace plugin { |
| 34 | 37 | ||
| 35 |
|
38 | static const std::string log_module_ = "vm::plugin::socket"; |
| 36 | 39 | ||
| 37 | Socket::Socket(boost::asio::io_service& io_service) : Plugin(io_service) |
40 | Socket::Socket(boost::asio::io_service& io_service) : Plugin(io_service) |
| 38 | { |
41 | { |
| 39 |
|
42 | LOG_DEBUG_ENTER; |
| 40 | 43 | ||
| - | 44 | this->name_ = "socket"; |
|
| - | 45 | ||
| - | 46 | this->ExportFunction("SocketExport_StartServer", Socket::Export_StartServer); |
|
| 41 | this->ExportFunction("SocketExport_Connect", Socket::Export_Connect); |
47 | this->ExportFunction("SocketExport_Connect", Socket::Export_Connect); |
| - | 48 | this->ExportFunction("SocketExport_StopServer", Socket::Export_StopServer); |
|
| 42 | this->ExportFunction("SocketExport_Disconnect", Socket::Export_Disconnect); |
49 | this->ExportFunction("SocketExport_Disconnect", Socket::Export_Disconnect); |
| 43 | this->ExportFunction("SocketExport_Send", Socket::Export_Send); |
50 | this->ExportFunction("SocketExport_Send", Socket::Export_Send); |
| - | 51 | ||
| - | 52 | LOG_DEBUG_EXIT; |
|
| 44 | } |
53 | } |
| 45 | 54 | ||
| 46 | Socket::~Socket() |
55 | Socket::~Socket() |
| 47 | { |
56 | { |
| - | 57 | LOG_DEBUG_ENTER; |
|
| 48 | 58 | LOG_DEBUG_EXIT; |
|
| 49 | } |
59 | } |
| 50 | 60 | ||
| 51 | void Socket::InitializeDone() |
61 | void Socket::InitializeDone() |
| 52 | { |
62 | { |
| - | 63 | LOG_DEBUG_ENTER; |
|
| - | 64 | ||
| 53 | Plugin::InitializeDone(); |
65 | Plugin::InitializeDone(); |
| 54 | 66 | ||
| 55 | this->ImportFunction("Socket_OnNewData"); |
67 | this->ImportFunction("Socket_OnNewData"); |
| 56 | this->ImportFunction("Socket_OnNewState"); |
68 | this->ImportFunction("Socket_OnNewState"); |
| 57 | 69 | ||
| 58 |
|
70 | net::Manager::Instance()->ConnectSlots(net::Manager::SignalOnNewState::slot_type(&Socket::SlotOnNewState, this, _1, _2, _3).track(this->tracker_), net::Manager::SignalOnNewData::slot_type(&Socket::SlotOnNewData, this, _1, _2, _3).track(this->tracker_)); |
| 59 | net::Manager::SignalOnNewData::slot_type(&Socket::SlotOnNewData, this, _1, _2, _3).track(this->tracker_)); |
- | |
| 60 | } |
- | |
| 61 | 71 | ||
| 62 | void Socket::CallOutput(unsigned int request_id, std::string output) |
- | |
| 63 | { |
- | |
| 64 |
|
72 | LOG_DEBUG_EXIT; |
| 65 | } |
73 | } |
| 66 | 74 | ||
| - | 75 | void Socket::CallOutput(unsigned int request_id, std::string output) |
|
| - | 76 | { |
|
| - | 77 | LOG_DEBUG_ENTER; |
|
| - | 78 | ||
| - | 79 | log::Info(log_module_, output); |
|
| - | 80 | ||
| - | 81 | LOG_DEBUG_EXIT; |
|
| - | 82 | } |
|
| - | 83 | ||
| 67 | void Socket::SlotOnNewData(net:: |
84 | void Socket::SlotOnNewData(net::SocketId socket_id, net::SocketId server_id, common::Byteset data) |
| 68 | { |
85 | { |
| - | 86 | LOG_DEBUG_ENTER; |
|
| - | 87 | ||
| 69 |
|
88 | log::Debug(log_module_, data.ToDebugString()); |
| - | 89 | ||
| 70 |
|
90 | this->io_service_.post(boost::bind(&Socket::SlotOnNewDataHandler, this, socket_id, server_id, data)); |
| - | 91 | ||
| - | 92 | LOG_DEBUG_EXIT; |
|
| 71 | } |
93 | } |
| 72 | 94 | ||
| 73 | void Socket::SlotOnNewState(net:: |
95 | void Socket::SlotOnNewState(net::SocketId socket_id, net::SocketId server_id, net::ClientState client_state) |
| 74 | { |
96 | { |
| - | 97 | LOG_DEBUG_ENTER; |
|
| - | 98 | ||
| 75 |
|
99 | this->io_service_.post(boost::bind(&Socket::SlotOnNewStateHandler, this, socket_id, server_id, client_state)); |
| - | 100 | ||
| - | 101 | LOG_DEBUG_EXIT; |
|
| 76 | } |
102 | } |
| 77 | 103 | ||
| 78 | void Socket::SlotOnNewDataHandler(net:: |
104 | void Socket::SlotOnNewDataHandler(net::SocketId socket_id, net::SocketId server_id, common::Byteset data) |
| 79 | { |
105 | { |
| - | 106 | LOG_DEBUG_ENTER; |
|
| 80 |
|
107 | ATOM_VM_PLUGIN_SCOPE; |
| - | 108 | ||
| - | 109 | try |
|
| - | 110 | { |
|
| 81 |
|
111 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
| - | 112 | ||
| 82 |
|
113 | if (data.GetSize() == 0) |
| - | 114 | { |
|
| - | 115 | atom::log::Error(log_module_, "Got empty data!"); |
|
| - | 116 | return; |
|
| - | 117 | } |
|
| 83 | 118 | ||
| - | 119 | arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(socket_id))); |
|
| - | 120 | arguments->push_back(v8::String::New(data.ToCharString().c_str())); |
|
| - | 121 | ||
| - | 122 | if (!this->Call(socket_id, "Socket_OnNewData", arguments)) |
|
| - | 123 | { |
|
| 84 |
|
124 | atom::log::Error(log_module_, "%s failed!", __FUNCTION__); |
| 85 | 125 | } |
|
| - | 126 | } |
|
| - | 127 | catch (std::exception& exception) |
|
| - | 128 | { |
|
| - | 129 | atom::log::Exception(log_module_, exception); |
|
| - | 130 | } |
|
| - | 131 | ||
| - | 132 | LOG_DEBUG_EXIT; |
|
| - | 133 | } |
|
| - | 134 | ||
| - | 135 | void Socket::SlotOnNewStateHandler(net::SocketId socket_id, net::SocketId server_id, net::ClientState client_state) |
|
| - | 136 | { |
|
| - | 137 | LOG_DEBUG_ENTER; |
|
| - | 138 | ATOM_VM_PLUGIN_SCOPE; |
|
| - | 139 | ||
| - | 140 | try |
|
| - | 141 | { |
|
| 86 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
142 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
| - | 143 | ||
| 87 | arguments->push_back(v8::Integer::New( |
144 | arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(socket_id))); |
| 88 | arguments->push_back(v8:: |
145 | arguments->push_back(v8::Uint32::New((unsigned int)client_state)); |
| 89 | 146 | ||
| 90 | this->Call |
147 | if (!this->Call(socket_id, "Socket_OnNewState", arguments)) |
| - | 148 | { |
|
| - | 149 | atom::log::Error(log_module_, "%s failed!", __FUNCTION__); |
|
| - | 150 | } |
|
| - | 151 | } |
|
| - | 152 | catch (std::exception& exception) |
|
| - | 153 | { |
|
| - | 154 | atom::log::Exception(log_module_, exception); |
|
| - | 155 | } |
|
| - | 156 | ||
| - | 157 | LOG_DEBUG_EXIT; |
|
| 91 | } |
158 | } |
| - | 159 | ||
| - | 160 | Value Socket::Export_StartServer(const v8::Arguments& args) |
|
| - | 161 | { |
|
| - | 162 | LOG_DEBUG_ENTER |
|
| - | 163 | ATOM_VM_PLUGIN_SCOPE; |
|
| - | 164 | ||
| - | 165 | net::SocketId socket_id = 0; |
|
| - | 166 | ||
| - | 167 | try |
|
| - | 168 | { |
|
| - | 169 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
|
| - | 170 | ||
| - | 171 | if (args.Length() < 1) |
|
| - | 172 | { |
|
| - | 173 | throw atom::exception::missing_in_param(); |
|
| - | 174 | } |
|
| 92 | 175 | ||
| 93 |
|
176 | socket_id = net::Manager::Instance()->StartServer(net::PROTOCOL_TCP, args[0]->Uint32Value()); |
| - | 177 | } |
|
| - | 178 | catch (std::exception& exception) |
|
| 94 | { |
179 | { |
| 95 | v8::Locker lock; |
- | |
| 96 |
|
180 | atom::log::Exception(log_module_, exception); |
| 97 | v8: |
181 | return handle_scope.Close(v8::Boolean::New(false)); |
| 98 | 182 | } |
|
| 99 | LOG.Debug(std::string(__FUNCTION__) + " called!"); |
- | |
| 100 | 183 | ||
| 101 |
|
184 | LOG_DEBUG_EXIT; |
| 102 |
|
185 | return handle_scope.Close(v8::Integer::New(socket_id)); |
| 103 | arguments->push_back(v8::Uint32::New((unsigned int)client_state)); |
- | |
| 104 | - | ||
| 105 | this->Call(client_id, "Socket_OnNewState", arguments); |
- | |
| 106 | } |
186 | } |
| 107 | 187 | ||
| 108 | Value Socket::Export_Connect(const v8::Arguments& args) |
188 | Value Socket::Export_Connect(const v8::Arguments& args) |
| 109 | { |
189 | { |
| - | 190 | LOG_DEBUG_ENTER; |
|
| - | 191 | ATOM_VM_PLUGIN_SCOPE; |
|
| - | 192 | ||
| 110 |
|
193 | net::SocketId socket_id = 0; |
| - | 194 | ||
| 111 |
|
195 | try |
| - | 196 | { |
|
| 112 |
|
197 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
| - | 198 | ||
| 113 |
|
199 | if (args.Length() < 2) |
| - | 200 | { |
|
| - | 201 | throw atom::exception::missing_in_param(); |
|
| - | 202 | } |
|
| 114 | 203 | ||
| 115 |
|
204 | v8::String::AsciiValue address(args[0]); |
| 116 | 205 | ||
| - | 206 | socket_id = net::Manager::Instance()->Connect(net::PROTOCOL_TCP, std::string(*address), args[1]->Uint32Value()); |
|
| - | 207 | } |
|
| 117 |
|
208 | catch (std::exception& exception) |
| 118 | { |
209 | { |
| 119 |
|
210 | atom::log::Exception(log_module_, exception); |
| 120 | return handle_scope.Close(v8::Boolean::New(false)); |
211 | return handle_scope.Close(v8::Boolean::New(false)); |
| 121 | } |
212 | } |
| - | 213 | ||
| - | 214 | LOG_DEBUG_EXIT; |
|
| - | 215 | return handle_scope.Close(v8::Integer::New(socket_id)); |
|
| - | 216 | } |
|
| - | 217 | ||
| - | 218 | Value Socket::Export_StopServer(const v8::Arguments& args) |
|
| - | 219 | { |
|
| - | 220 | LOG_DEBUG_EXIT; |
|
| - | 221 | ATOM_VM_PLUGIN_SCOPE; |
|
| - | 222 | ||
| - | 223 | try |
|
| - | 224 | { |
|
| - | 225 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
|
| 122 | 226 | ||
| 123 |
|
227 | if (args.Length() < 1) |
| 124 | 228 | { |
|
| - | 229 | throw atom::exception::missing_in_param(); |
|
| 125 |
|
230 | } |
| 126 |
|
231 | |
| 127 |
|
232 | net::Manager::Instance()->StopServer(args[0]->Uint32Value()); |
| 128 | } |
233 | } |
| 129 |
|
234 | catch (std::exception& exception) |
| 130 | { |
235 | { |
| 131 |
|
236 | atom::log::Exception(log_module_, exception); |
| - | 237 | return handle_scope.Close(v8::Boolean::New(false)); |
|
| 132 | } |
238 | } |
| 133 | 239 | ||
| - | 240 | LOG_DEBUG_EXIT; |
|
| 134 |
|
241 | return handle_scope.Close(v8::Boolean::New(true)); |
| 135 | } |
242 | } |
| 136 | 243 | ||
| 137 | Value Socket::Export_Disconnect(const v8::Arguments& args) |
244 | Value Socket::Export_Disconnect(const v8::Arguments& args) |
| 138 | { |
245 | { |
| 139 |
|
246 | LOG_DEBUG_EXIT; |
| 140 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
- | |
| 141 |
|
247 | ATOM_VM_PLUGIN_SCOPE; |
| - | 248 | ||
| - | 249 | try |
|
| 142 | 250 | { |
|
| 143 |
|
251 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
| 144 | 252 | ||
| 145 | if (args.Length() < 1) |
253 | if (args.Length() < 1) |
| 146 | { |
254 | { |
| 147 | LOG.Error(std::string(__FUNCTION__) + ": To few arguments."); |
- | |
| 148 |
|
255 | throw atom::exception::missing_in_param(); |
| 149 | } |
256 | } |
| 150 | 257 | ||
| 151 | net::Manager::Instance()->Disconnect(args[0]->Uint32Value()); |
258 | net::Manager::Instance()->Disconnect(args[0]->Uint32Value()); |
| - | 259 | } |
|
| - | 260 | catch (std::exception& exception) |
|
| 152 | 261 | { |
|
| - | 262 | atom::log::Exception(log_module_, exception); |
|
| 153 | return handle_scope.Close(v8:: |
263 | return handle_scope.Close(v8::Boolean::New(false)); |
| - | 264 | } |
|
| - | 265 | ||
| - | 266 | LOG_DEBUG_EXIT; |
|
| - | 267 | return handle_scope.Close(v8::Boolean::New(true)); |
|
| 154 | } |
268 | } |
| 155 | 269 | ||
| 156 | Value Socket::Export_Send(const v8::Arguments& args) |
270 | Value Socket::Export_Send(const v8::Arguments& args) |
| 157 | { |
271 | { |
| - | 272 | LOG_DEBUG_EXIT; |
|
| 158 |
|
273 | ATOM_VM_PLUGIN_SCOPE; |
| - | 274 | ||
| - | 275 | try |
|
| - | 276 | { |
|
| 159 |
|
277 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
| - | 278 | ||
| 160 |
|
279 | if (args.Length() < 2) |
| 161 | 280 | { |
|
| 162 |
|
281 | throw atom::exception::missing_in_param(); |
| - | 282 | } |
|
| 163 | 283 | ||
| 164 |
|
284 | v8::String::AsciiValue data(args[1]); |
| - | 285 | ||
| - | 286 | net::Manager::Instance()->SendTo(args[0]->Uint32Value(), std::string(*data)); |
|
| - | 287 | } |
|
| - | 288 | catch (std::exception& exception) |
|
| 165 | { |
289 | { |
| 166 |
|
290 | atom::log::Exception(log_module_, exception); |
| 167 | return handle_scope.Close(v8::Boolean::New(false)); |
291 | return handle_scope.Close(v8::Boolean::New(false)); |
| 168 | } |
292 | } |
| 169 | 293 | ||
| 170 | v8::String::AsciiValue data(args[1]); |
- | |
| 171 | - | ||
| 172 | net::Manager::Instance()->SendTo(args[0]->Uint32Value(), std::string(*data)); |
- | |
| 173 | 294 | LOG_DEBUG_EXIT; |
|
| 174 |
|
295 | return handle_scope.Close(v8::Boolean::New(true)); |
| 175 | } |
296 | } |
| 176 | 297 | ||
| 177 | }; // namespace plugin |
298 | }; // namespace plugin |
| 178 | }; // namespace vm |
299 | }; // namespace vm |
| 179 | }; // namespace atom |
300 | }; // namespace atom |
| 180 | - | ||