void JsonRPCServerModule::invoke(const string& method, const AmArg& args, AmArg& ret) { if (method == "execRpc"){ // todo: add connection id args.assertArrayFmt("sssisis"); // evq_link, notificationReceiver, requestReceiver, // flags(i), host, port (i), method, [params] if (args.size() > 7) { if (!isArgArray(args.get(7)) && !isArgStruct(args.get(7))) { ERROR("internal error: params to JSON-RPC must be struct or array\n"); throw AmArg::TypeMismatchException(); } } execRpc(args, ret); // sendRequestList(args, ret); } else if (method == "sendMessage"){ args.assertArrayFmt("sisss"); // conn_id, type, method, id, reply_sink, [params] if (args.size() > 5) { if (!isArgArray(args.get(5)) && !isArgStruct(args.get(5))) { ERROR("internal error: params to JSON-RPC must be struct or array\n"); throw AmArg::TypeMismatchException(); } } sendMessage(args, ret); } else if (method == "execServerFunction"){ args.assertArrayFmt("ss"); // method, id, params JsonRpcServer::execRpc(args.get(0).asCStr(), args.get(1).asCStr(), args.get(2), ret); // JsonRpcServer::execRpc(args, ret); } else if (method == "getServerPort"){ ret.push(port); } else if(method == "_list"){ ret.push(AmArg("execRpc")); ret.push(AmArg("sendMessage")); ret.push(AmArg("getServerPort")); ret.push(AmArg("execServerFunction")); // ret.push(AmArg("newConnection")); // ret.push(AmArg("sendRequest")); // ret.push(AmArg("sendRequestList")); } else throw AmDynInvoke::NotImplemented(method); }
void VoiceboxDialog::openMailbox() { cur_msg = new_msgs.begin(); AmArg di_args,ret; di_args.push(domain.c_str()); // domain di_args.push(user.c_str()); // user msg_storage->invoke("userdir_open",di_args,ret); if (!ret.size() || !isArgInt(ret.get(0))) { ERROR("userdir_open for user '%s' domain '%s'" " returned no (valid) result.\n", user.c_str(), domain.c_str() ); return; } userdir_open = true; int ecode = ret.get(0).asInt(); if (MSG_EUSRNOTFOUND == ecode) { DBG("empty mailbox for user '%s' domain '%s'.\n", user.c_str(), domain.c_str() ); closeMailbox(); return; } if (MSG_OK != ecode) { ERROR("userdir_open for user '%s' domain '%s': %s\n", user.c_str(), domain.c_str(), MsgStrError(ret.get(0).asInt())); closeMailbox(); return; } if ((ret.size() < 2) || (!isArgArray(ret.get(1)))) { ERROR("userdir_open for user '%s' domain '%s'" " returned too few parameters.\n", user.c_str(), domain.c_str() ); closeMailbox(); return; } for (size_t i=0;i<ret.get(1).size();i++) { AmArg& elem = ret.get(1).get(i); if (!isArgArray(elem) || elem.size() != 3) { ERROR("wrong element in userdir list.\n"); continue; } string msg_name = elem.get(0).asCStr(); int msg_unread = elem.get(1).asInt(); int size = elem.get(2).asInt(); if (size) { // TODO: treat empty messages as well! if (msg_unread) { new_msgs.push_back(Message(msg_name, size)); } else { saved_msgs.push_back(Message(msg_name, size)); } } } new_msgs.sort(); new_msgs.reverse(); saved_msgs.sort(); saved_msgs.reverse(); DBG("Got %zd new and %zd saved messages for user '%s' domain '%s'\n", new_msgs.size(), saved_msgs.size(), user.c_str(), domain.c_str()); if (new_msgs.size()) { cur_msg = new_msgs.begin(); in_saved_msgs = false; } else { if (saved_msgs.size()) cur_msg = saved_msgs.begin(); in_saved_msgs = true; } }
bool SCDIAction::execute(AmSession* sess, DSMCondition::EventType event, map<string,string>* event_params) { GET_SCSESSION(); if (params.size() < 2) { ERROR("DI needs at least: mod_name, " "function_name (in '%s'\n", name.c_str()); return false; } vector<string>::iterator p_it=params.begin(); string fact_name = trim(*p_it, " \""); AmDynInvokeFactory* fact = AmPlugIn::instance()->getFactory4Di(fact_name); if(!fact) { ERROR("load module for factory '%s'.\n", fact_name.c_str()); return false; } AmDynInvoke* di_inst = fact->getInstance(); if(!di_inst) { ERROR("load module for factory '%s'\n", fact_name.c_str()); return false; } p_it++; string func_name = trim(*p_it, " \""); p_it++; AmArg di_args; while (p_it != params.end()) { string p = trim(*p_it, " \t"); if (p.length() && p[0] == '"') { di_args.push(trim(p,"\"").c_str()); } else if (p.length() > 5 && p.substr(0, 5) =="(int)") { p = resolveVars(p.substr(5), sess, sc_sess, event_params); char* endptr = NULL; long p_i = strtol(p.c_str(), &endptr, 10); if (endptr && *endptr == '\0') { di_args.push((int)p_i); } else { ERROR("converting value '%s' to int\n", p.c_str()); return false; } } else { di_args.push(resolveVars(p, sess, sc_sess, event_params).c_str()); } p_it++; } sc_sess->di_res.clear(); DBG("executing DI function '%s'\n", func_name.c_str()); try { di_inst->invoke(func_name, di_args, sc_sess->di_res); } catch (const AmDynInvoke::NotImplemented& ni) { ERROR("not implemented DI function '%s'\n", ni.what.c_str()); return false; } catch (const AmArg::OutOfBoundsException& oob) { ERROR("out of bounds in DI call '%s'\n", name.c_str()); return false; } catch (const AmArg::TypeMismatchException& oob) { ERROR("type mismatch in DI call '%s'\n", name.c_str()); return false; } catch (...) { ERROR("unexpected Exception in DI call '%s'\n", name.c_str()); return false; } if (get_res) { // rudimentary variables conversion... if (isArgCStr(sc_sess->di_res)) sc_sess->var["DI_res"] = sc_sess->di_res.asCStr(); else if (isArgInt(sc_sess->di_res)) sc_sess->var["DI_res"] = int2str(sc_sess->di_res.asInt()); else if (isArgArray(sc_sess->di_res)) { // copy results to $DI_res0..$DI_resn for (size_t i=0;i<sc_sess->di_res.size();i++) { switch (sc_sess->di_res.get(i).getType()) { case AmArg::CStr: { sc_sess->var["DI_res"+int2str(i)] = sc_sess->di_res.get(i).asCStr(); } break; case AmArg::Int: { sc_sess->var["DI_res"+int2str(i)] = int2str(sc_sess->di_res.get(i).asInt()); } break; default: { ERROR("unsupported AmArg return type!"); } } } } else { ERROR("unsupported AmArg return type!"); } } return false; }