improve naming

This commit is contained in:
Simon Laux
2022-06-28 13:25:41 +02:00
parent 271d54e420
commit 659e48bd3f
5 changed files with 46 additions and 46 deletions

View File

@@ -23,7 +23,7 @@ typedef struct _dc_provider dc_provider_t;
typedef struct _dc_event dc_event_t; typedef struct _dc_event dc_event_t;
typedef struct _dc_event_emitter dc_event_emitter_t; typedef struct _dc_event_emitter dc_event_emitter_t;
typedef struct _dc_accounts_event_emitter dc_accounts_event_emitter_t; typedef struct _dc_accounts_event_emitter dc_accounts_event_emitter_t;
typedef struct _dc_json_api_instance dc_json_api_instance_t; typedef struct _dc_jsonrpc_instance dc_jsonrpc_instance_t;
/** /**
* @mainpage Getting started * @mainpage Getting started
@@ -5180,7 +5180,7 @@ int64_t dc_lot_get_timestamp (const dc_lot_t* lot);
/** /**
* @class dc_json_api_instance_t * @class dc_jsonrpc_instance_t
* *
* Opaque object for using the json rpc api from the cffi bindings. * Opaque object for using the json rpc api from the cffi bindings.
*/ */
@@ -5191,41 +5191,41 @@ int64_t dc_lot_get_timestamp (const dc_lot_t* lot);
* @memberof dc_accounts_t * @memberof dc_accounts_t
* @param account_manager The accounts object as created by dc_accounts_new(). * @param account_manager The accounts object as created by dc_accounts_new().
* @return Returns the jsonrpc instance, NULL on errors. * @return Returns the jsonrpc instance, NULL on errors.
* Must be freed using dc_json_api_unref() after usage. * Must be freed using dc_jsonrpc_unref() after usage.
* *
*/ */
dc_json_api_instance_t* dc_get_json_api(dc_accounts_t* account_manager); dc_jsonrpc_instance_t* dc_jsonrpc_init(dc_accounts_t* account_manager);
/** /**
* Free a jsonrpc instance. * Free a jsonrpc instance.
* *
* @memberof dc_json_api_instance_t * @memberof dc_jsonrpc_instance_t
* @param json_api_instance jsonrpc instance as returned from dc_get_json_api(). * @param jsonrpc_instance jsonrpc instance as returned from dc_jsonrpc_init().
* If NULL is given, nothing is done and an error is logged. * If NULL is given, nothing is done and an error is logged.
*/ */
void dc_json_api_unref(dc_json_api_instance_t* json_api_instance); void dc_jsonrpc_unref(dc_jsonrpc_instance_t* jsonrpc_instance);
/** /**
* Makes an asynchronous jsonrpc request, * Makes an asynchronous jsonrpc request,
* returns immediately and once the result is ready it can be retrieved via dc_get_next_json_response() * returns immediately and once the result is ready it can be retrieved via dc_jsonrpc_next_response()
* the jsonrpc specification defines an invocation id that can then be used to match request and response. * the jsonrpc specification defines an invocation id that can then be used to match request and response.
* *
* @memberof dc_json_api_instance_t * @memberof dc_jsonrpc_instance_t
* @param json_api_instance jsonrpc instance as returned from dc_get_json_api(). * @param jsonrpc_instance jsonrpc instance as returned from dc_jsonrpc_init().
* @param request JSON-RPC request as string * @param request JSON-RPC request as string
*/ */
void dc_json_request(dc_json_api_instance_t* json_api_instance, char* request); void dc_jsonrpc_request(dc_jsonrpc_instance_t* jsonrpc_instance, char* request);
/** /**
* Get the next json_rpc response, blocks until there is a new event, so call this in a loop from a thread. * Get the next json_rpc response, blocks until there is a new event, so call this in a loop from a thread.
* *
* @memberof dc_json_api_instance_t * @memberof dc_jsonrpc_instance_t
* @param json_api_instance jsonrpc instance as returned from dc_get_json_api(). * @param jsonrpc_instance jsonrpc instance as returned from dc_jsonrpc_init().
* @return JSON-RPC response as string * @return JSON-RPC response as string
* If NULL is returned, the accounts_t belonging to the jsonrpc instance is unref'd and no more events will come; * If NULL is returned, the accounts_t belonging to the jsonrpc instance is unref'd and no more events will come;
* in this case, free the jsonrpc instance using dc_json_api_unref(). * in this case, free the jsonrpc instance using dc_jsonrpc_unref().
*/ */
char* dc_get_next_json_response(dc_json_api_instance_t* json_api_instance); char* dc_jsonrpc_next_response(dc_jsonrpc_instance_t* jsonrpc_instance);
/** /**
* @class dc_event_emitter_t * @class dc_event_emitter_t

View File

@@ -4445,52 +4445,52 @@ mod jsonrpc {
use deltachat_jsonrpc::api::CommandApi; use deltachat_jsonrpc::api::CommandApi;
use deltachat_jsonrpc::yerpc::{MessageHandle, RpcHandle}; use deltachat_jsonrpc::yerpc::{MessageHandle, RpcHandle};
pub struct dc_json_api_instance_t { pub struct dc_jsonrpc_instance_t {
receiver: async_std::channel::Receiver<deltachat_jsonrpc::yerpc::Message>, receiver: async_std::channel::Receiver<deltachat_jsonrpc::yerpc::Message>,
handle: MessageHandle<CommandApi>, handle: MessageHandle<CommandApi>,
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn dc_get_json_api( pub unsafe extern "C" fn dc_jsonrpc_init(
account_manager: *mut dc_accounts_t, account_manager: *mut dc_accounts_t,
) -> *mut dc_json_api_instance_t { ) -> *mut dc_jsonrpc_instance_t {
if account_manager.is_null() { if account_manager.is_null() {
eprintln!("ignoring careless call to dc_get_json_api()"); eprintln!("ignoring careless call to dc_jsonrpc_init()");
return ptr::null_mut(); return ptr::null_mut();
} }
let cmd_api = let cmd_api =
deltachat_jsonrpc::api::CommandApi::new_from_cffi((*account_manager).inner.clone()); deltachat_jsonrpc::api::CommandApi::new_from_arc((*account_manager).inner.clone());
let (request_handle, receiver) = RpcHandle::new(); let (request_handle, receiver) = RpcHandle::new();
let handle = MessageHandle::new(request_handle, cmd_api); let handle = MessageHandle::new(request_handle, cmd_api);
let instance = dc_json_api_instance_t { receiver, handle }; let instance = dc_jsonrpc_instance_t { receiver, handle };
Box::into_raw(Box::new(instance)) Box::into_raw(Box::new(instance))
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn dc_json_api_unref(json_api_instance: *mut dc_json_api_instance_t) { pub unsafe extern "C" fn dc_jsonrpc_unref(jsonrpc_instance: *mut dc_jsonrpc_instance_t) {
if json_api_instance.is_null() { if jsonrpc_instance.is_null() {
eprintln!("ignoring careless call to dc_json_api_unref()"); eprintln!("ignoring careless call to dc_jsonrpc_unref()");
return; return;
} }
Box::from_raw(json_api_instance); Box::from_raw(jsonrpc_instance);
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn dc_json_request( pub unsafe extern "C" fn dc_jsonrpc_request(
json_api_instance: *mut dc_json_api_instance_t, jsonrpc_instance: *mut dc_jsonrpc_instance_t,
request: *const libc::c_char, request: *const libc::c_char,
) { ) {
if json_api_instance.is_null() || request.is_null() { if jsonrpc_instance.is_null() || request.is_null() {
eprintln!("ignoring careless call to dc_json_request()"); eprintln!("ignoring careless call to dc_jsonrpc_request()");
return; return;
} }
let api = &*json_api_instance; let api = &*jsonrpc_instance;
let handle = &api.handle; let handle = &api.handle;
let request = to_string_lossy(request); let request = to_string_lossy(request);
async_std::task::spawn(async move { async_std::task::spawn(async move {
@@ -4499,14 +4499,14 @@ mod jsonrpc {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn dc_get_next_json_response( pub unsafe extern "C" fn dc_jsonrpc_next_response(
json_api_instance: *mut dc_json_api_instance_t, jsonrpc_instance: *mut dc_jsonrpc_instance_t,
) -> *mut libc::c_char { ) -> *mut libc::c_char {
if json_api_instance.is_null() { if jsonrpc_instance.is_null() {
eprintln!("ignoring careless call to dc_get_next_json_response()"); eprintln!("ignoring careless call to dc_jsonrpc_next_response()");
return ptr::null_mut(); return ptr::null_mut();
} }
let api = &*json_api_instance; let api = &*jsonrpc_instance;
async_std::task::block_on(api.receiver.recv()) async_std::task::block_on(api.receiver.recv())
.map(|result| serde_json::to_string(&result).unwrap_or_default().strdup()) .map(|result| serde_json::to_string(&result).unwrap_or_default().strdup())
.unwrap_or(ptr::null_mut()) .unwrap_or(ptr::null_mut())

View File

@@ -39,7 +39,7 @@ impl CommandApi {
} }
} }
pub fn new_from_cffi(accounts: Arc<RwLock<Accounts>>) -> Self { pub fn new_from_arc(accounts: Arc<RwLock<Accounts>>) -> Self {
CommandApi { accounts } CommandApi { accounts }
} }

View File

@@ -19,7 +19,7 @@ interface NativeAccount {}
export class AccountManager extends EventEmitter { export class AccountManager extends EventEmitter {
dcn_accounts: NativeAccount dcn_accounts: NativeAccount
accountDir: string accountDir: string
json_rpc_started = false jsonRpcStarted = false
constructor(cwd: string, os = 'deltachat-node') { constructor(cwd: string, os = 'deltachat-node') {
super() super()
@@ -122,17 +122,17 @@ export class AccountManager extends EventEmitter {
if (!callback) { if (!callback) {
throw new Error('no callback set') throw new Error('no callback set')
} }
if (this.json_rpc_started) { if (this.jsonRpcStarted) {
throw new Error('jsonrpc was started already') throw new Error('jsonrpc was started already')
} }
binding.dcn_accounts_start_jsonrpc(this.dcn_accounts, callback.bind(this)) binding.dcn_accounts_start_jsonrpc(this.dcn_accounts, callback.bind(this))
debug('Started jsonrpc handler') debug('Started jsonrpc handler')
this.json_rpc_started = true this.jsonRpcStarted = true
} }
jsonRPCRequest(message: string) { jsonRPCRequest(message: string) {
if (!this.json_rpc_started) { if (!this.jsonRpcStarted) {
throw new Error( throw new Error(
'jsonrpc is not active, start it with startJSONRPCHandler first' 'jsonrpc is not active, start it with startJSONRPCHandler first'
) )

View File

@@ -36,7 +36,7 @@ typedef struct dcn_accounts_t {
uv_thread_t event_handler_thread; uv_thread_t event_handler_thread;
napi_threadsafe_function threadsafe_jsonrpc_handler; napi_threadsafe_function threadsafe_jsonrpc_handler;
uv_thread_t jsonrpc_thread; uv_thread_t jsonrpc_thread;
dc_json_api_instance_t* jsonrpc_instance; dc_jsonrpc_instance_t* jsonrpc_instance;
int gc; int gc;
} dcn_accounts_t; } dcn_accounts_t;
@@ -2928,7 +2928,7 @@ NAPI_METHOD(dcn_accounts_unref) {
dcn_accounts->event_handler_thread = 0; dcn_accounts->event_handler_thread = 0;
} }
if (dcn_accounts->jsonrpc_instance) { if (dcn_accounts->jsonrpc_instance) {
dc_json_request(dcn_accounts->jsonrpc_instance, "{}"); dc_jsonrpc_request(dcn_accounts->jsonrpc_instance, "{}");
uv_thread_join(&dcn_accounts->jsonrpc_thread); uv_thread_join(&dcn_accounts->jsonrpc_thread);
dcn_accounts->jsonrpc_instance = NULL; dcn_accounts->jsonrpc_instance = NULL;
} }
@@ -3248,7 +3248,7 @@ static void accounts_jsonrpc_thread_func(void* arg)
TRACE("accounts_jsonrpc_thread_func starting"); TRACE("accounts_jsonrpc_thread_func starting");
char* response; char* response;
while (true) { while (true) {
response = dc_get_next_json_response(dcn_accounts->jsonrpc_instance); response = dc_jsonrpc_next_response(dcn_accounts->jsonrpc_instance);
if (response == NULL) { if (response == NULL) {
// done or broken // done or broken
break; break;
@@ -3271,7 +3271,7 @@ static void accounts_jsonrpc_thread_func(void* arg)
break; break;
} }
} }
dc_json_api_unref(dcn_accounts->jsonrpc_instance); dc_jsonrpc_unref(dcn_accounts->jsonrpc_instance);
dcn_accounts->jsonrpc_instance = NULL; dcn_accounts->jsonrpc_instance = NULL;
TRACE("accounts_jsonrpc_thread_func ended"); TRACE("accounts_jsonrpc_thread_func ended");
napi_release_threadsafe_function(dcn_accounts->threadsafe_jsonrpc_handler, napi_tsfn_release); napi_release_threadsafe_function(dcn_accounts->threadsafe_jsonrpc_handler, napi_tsfn_release);
@@ -3339,7 +3339,7 @@ NAPI_METHOD(dcn_accounts_start_jsonrpc) {
TRACE("done"); TRACE("done");
dcn_accounts->gc = 0; dcn_accounts->gc = 0;
dcn_accounts->jsonrpc_instance = dc_get_json_api(dcn_accounts->dc_accounts); dcn_accounts->jsonrpc_instance = dc_jsonrpc_init(dcn_accounts->dc_accounts);
TRACE("creating uv thread.."); TRACE("creating uv thread..");
uv_thread_create(&dcn_accounts->jsonrpc_thread, accounts_jsonrpc_thread_func, dcn_accounts); uv_thread_create(&dcn_accounts->jsonrpc_thread, accounts_jsonrpc_thread_func, dcn_accounts);
@@ -3355,7 +3355,7 @@ NAPI_METHOD(dcn_json_rpc_request) {
NAPI_STATUS_THROWS(napi_throw_type_error(env, NULL, msg)); NAPI_STATUS_THROWS(napi_throw_type_error(env, NULL, msg));
} }
NAPI_ARGV_UTF8_MALLOC(request, 1); NAPI_ARGV_UTF8_MALLOC(request, 1);
dc_json_request(dcn_accounts->jsonrpc_instance, request); dc_jsonrpc_request(dcn_accounts->jsonrpc_instance, request);
free(request); free(request);
} }