1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
extern crate hyper;
extern crate futures;
extern crate tokio_core;
extern crate tokio_io;
extern crate uuid;
extern crate chrono;
extern crate tera;
/*
#[macro_use]
extern crate lazy_static;
*/
#[macro_use]
extern crate serde_json;
/*
#[macro_use]
extern crate serde_derive;
*/
extern crate ansi_term;
extern crate etag;
extern crate sequence_trie;
extern crate byteorder;
extern crate net2;
extern crate num_cpus;
extern crate url;

#[cfg(feature = "use_cervus")]
extern crate cervus;

mod ice_server;
mod delegates;
mod router;
pub mod glue;
mod config;
mod static_file;
mod session_storage;
mod time;
mod template;
mod logging;
mod stat;
pub mod streaming;
pub mod ext;
mod prefix_tree;

#[cfg(test)]
mod prefix_tree_test;

use std::any::Any;
use std::sync::{Arc, Mutex, RwLock};
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_void};
use std::borrow::BorrowMut;
use ice_server::IceServer;
use delegates::{ServerHandle, SessionHandle, ContextHandle};

#[no_mangle]
pub fn ice_create_server() -> ServerHandle {
    let server = Arc::new(Mutex::new(IceServer::new()));
    Arc::into_raw(server)
}

#[no_mangle]
pub unsafe fn ice_server_listen(handle: ServerHandle, addr: *const c_char) -> *mut std::thread::JoinHandle<()> {
    let handle = &*handle;

    let server = handle.lock().unwrap();
    server.listen(CStr::from_ptr(addr).to_str().unwrap());

    std::ptr::null_mut()
}

#[no_mangle]
pub unsafe fn ice_server_router_add_endpoint(handle: ServerHandle, p: *const c_char) -> *const RwLock<router::Endpoint> {
    let handle = &*handle;

    let server = handle.lock().unwrap();
    let mut router = server.prep.router.write().unwrap();
    let ep = router.add_endpoint(CStr::from_ptr(p).to_str().unwrap());

    Arc::into_raw(ep)
}

#[no_mangle]
pub unsafe fn ice_server_set_session_cookie_name(handle: ServerHandle, name: *const c_char) {
    let handle = &*handle;

    let mut server = handle.lock().unwrap();
    *server.prep.session_cookie_name.lock().unwrap() = CStr::from_ptr(name).to_str().unwrap().to_string();
}

#[no_mangle]
pub unsafe fn ice_server_set_session_timeout_ms(handle: ServerHandle, t: u64) {
    let handle = &*handle;

    let mut server = handle.lock().unwrap();
    *server.prep.session_timeout_ms.write().unwrap() = t;
}

#[no_mangle]
pub unsafe fn ice_server_add_template(handle: ServerHandle, name: *const c_char, content: *const c_char) -> bool {
    let handle = &*handle;

    let server = handle.lock().unwrap();
    let ret = server.prep.templates.add(
        CStr::from_ptr(name).to_str().unwrap(),
        CStr::from_ptr(content).to_str().unwrap()
    );

    ret
}

#[no_mangle]
pub unsafe fn ice_server_set_max_request_body_size(handle: ServerHandle, size: u32) {
    let handle = &*handle;

    let mut server = handle.lock().unwrap();
    *server.prep.max_request_body_size.lock().unwrap() = size;
}

#[no_mangle]
pub unsafe fn ice_server_disable_request_logging(handle: ServerHandle) {
    let handle = &*handle;

    let mut server = handle.lock().unwrap();
    *server.prep.log_requests.lock().unwrap() = false;
}

#[no_mangle]
pub unsafe fn ice_server_set_async_endpoint_cb(handle: ServerHandle, cb: extern fn (i32, *mut delegates::CallInfo)) {
    let handle = &*handle;

    let mut server = handle.lock().unwrap();
    *server.prep.async_endpoint_cb.lock().unwrap() = Some(cb);
}

#[no_mangle]
pub unsafe fn ice_server_set_endpoint_timeout_ms(handle: ServerHandle, t: u64) {
    let handle = &*handle;

    let mut server = handle.lock().unwrap();
    *server.prep.endpoint_timeout_ms.lock().unwrap() = t;
}

#[no_mangle]
pub unsafe fn ice_server_set_custom_app_data(handle: ServerHandle, ptr: *const c_void) {
    let handle = &*handle;

    let server = handle.lock().unwrap();
    server.prep.custom_app_data.set_raw(ptr);
}

#[no_mangle]
pub unsafe fn ice_server_cervus_load_bitcode(handle: ServerHandle, name: *const c_char, data: *const u8, data_len: u32) -> bool {
    let handle = &*handle;
    let server = handle.lock().unwrap();

    let name = CStr::from_ptr(name).to_str().unwrap();
    let data = std::slice::from_raw_parts(data, data_len as usize).to_vec();

    server.load_module(name, data.as_slice());
    true
}

#[no_mangle]
pub unsafe fn ice_context_render_template(handle: ContextHandle, name: *const c_char, data: *const c_char) -> *mut c_char {
    let handle = &*handle;

    let ret = match handle.templates.render_json(
        CStr::from_ptr(name).to_str().unwrap(),
        CStr::from_ptr(data).to_str().unwrap()
    ) {
        Some(v) => CString::new(v).unwrap().into_raw(),
        None => std::ptr::null_mut()
    };

    ret
}

#[no_mangle]
pub unsafe fn ice_context_create_session(handle: ContextHandle) -> SessionHandle {
    let handle = &*handle;

    let ret = Arc::into_raw(handle.session_storage.create_session());

    ret
}

#[no_mangle]
pub unsafe fn ice_context_get_session_by_id(handle: ContextHandle, id: *const c_char) -> SessionHandle {
    let handle = &*handle;
    let id = CStr::from_ptr(id).to_str().unwrap();

    let ret = match handle.session_storage.get_session(id) {
        Some(v) => Arc::into_raw(v),
        None => std::ptr::null()
    };

    ret
}

#[no_mangle]
pub unsafe fn ice_context_get_stats(handle: ContextHandle) -> *mut c_char {
    let handle = &*handle;

    let ret = CString::new(handle.stats.serialize().to_string()).unwrap().into_raw();

    ret
}

#[no_mangle]
pub unsafe fn ice_context_stats_set_custom(handle: ContextHandle, k: *const c_char, v: *const c_char) {
    let handle = &*handle;

    let k = CStr::from_ptr(k).to_str().unwrap().to_string();
    let v = CStr::from_ptr(v).to_str().unwrap().to_string();

    handle.stats.set_custom(k, v);
}

#[no_mangle]
pub unsafe fn ice_context_set_custom_app_data(handle: ContextHandle, ptr: *const c_void) {
    let handle = &*handle;

    handle.custom_app_data.set_raw(ptr);
}

#[cfg(feature = "cervus")]
#[no_mangle]
pub unsafe fn ice_context_get_service_handle_by_module_name_and_service_name(
    handle: ContextHandle,
    module_name: *const c_char,
    service_name: *const c_char
) -> *mut cervus::manager::ServiceHandle {
    let handle = &*handle;
    let module_name = CStr::from_ptr(module_name).to_str().unwrap();
    let service_name = CStr::from_ptr(service_name).to_str().unwrap();

    match handle.get_service_by_name(module_name, service_name) {
        Some(v) => Box::into_raw(Box::new(v)),
        None => std::ptr::null_mut()
    }
}

#[cfg(feature = "cervus")]
#[no_mangle]
pub unsafe fn ice_core_service_handle_call_with_raw_pointer(
    handle: *mut cervus::manager::ServiceHandle,
    ptr: *mut c_void
) -> *mut cervus::engine::ModuleResource {
    let handle = &*handle;
    let ret = match handle.call(Box::new(ptr) as Box<Any>) {
        Some(v) => v,
        None => return std::ptr::null_mut()
    };
    Box::into_raw(
        Box::new(
            cervus::engine::ModuleResource::from(ret)
        )
    )
}

#[cfg(feature = "cervus")]
#[no_mangle]
pub unsafe fn ice_core_destroy_module_resource(handle: *mut cervus::engine::ModuleResource) {
    Box::from_raw(handle);
}

#[cfg(feature = "cervus")]
#[no_mangle]
pub unsafe fn ice_core_destroy_service_handle(handle: *mut cervus::manager::ServiceHandle) {
    Box::from_raw(handle);
}

#[no_mangle]
pub unsafe fn ice_core_destroy_context_handle(handle: ContextHandle) {
    Arc::from_raw(handle);
}

#[no_mangle]
pub unsafe fn ice_core_fire_callback(call_info: *mut delegates::CallInfo, resp: *mut glue::response::Response) -> bool {
    let call_info = Box::from_raw(call_info);
    let resp = Box::from_raw(resp);

    match call_info.tx.send(resp) {
        Ok(_) => true,
        Err(_) => false
    }
}

#[no_mangle]
pub unsafe fn ice_core_borrow_request_from_call_info(call_info: *mut delegates::CallInfo) -> *mut glue::request::Request {
    let mut call_info = &mut *call_info;

    let req = call_info.req.borrow_mut() as *mut glue::request::Request;

    req
}

#[no_mangle]
pub unsafe fn ice_core_get_custom_app_data_from_call_info(call_info: *mut delegates::CallInfo) -> *const c_void {
    let call_info = &*call_info;

    call_info.custom_app_data.get_raw()
}

#[no_mangle]
pub unsafe fn ice_core_endpoint_get_id(ep: *const RwLock<router::Endpoint>) -> i32 {
    let ep = &*ep;
    ep.read().unwrap().id
}

#[no_mangle]
pub unsafe fn ice_core_endpoint_set_flag(ep: *const RwLock<router::Endpoint>, name: *const c_char, value: bool) {
    let ep = &*ep;
    ep.write().unwrap().flags.insert(CStr::from_ptr(name).to_str().unwrap().to_string(), value);
}

#[no_mangle]
pub unsafe fn ice_core_destroy_cstring(v: *mut c_char) {
    CString::from_raw(v);
}

#[no_mangle]
pub unsafe fn ice_core_cervus_enabled() -> bool {
    if cfg!(feature = "use_cervus") {
        true
    } else {
        false
    }
}