static int bthrottler_brecv(int s, void *buf, size_t len, int64_t deadline) { struct bthrottlersock *obj = hdata(s, bsock_type); dsock_assert(obj->vfptrs.type == bthrottler_type); /* If recv-throttling is off forward the call. */ if(obj->recv_full == 0) return brecv(obj->s, buf, len, deadline); /* Get rid of the corner case. */ if(dsock_slow(len == 0)) return 0; while(1) { /* If there's capacity receive as much data as possible. */ if(obj->recv_remaining) { size_t torecv = len < obj->recv_remaining ? len : obj->recv_remaining; int rc = brecv(obj->s, buf, torecv, deadline); if(dsock_slow(rc < 0)) return -1; obj->recv_remaining -= torecv; buf = (char*)buf + torecv; len -= torecv; if(len == 0) return 0; } /* Wait till capacity can be renewed. */ int rc = msleep(obj->recv_last + obj->recv_interval); if(dsock_slow(rc < 0)) return -1; /* Renew the capacity. */ obj->recv_remaining = obj->recv_full; obj->recv_last = now(); } }
static void bthrottler_close(int s) { struct bthrottlersock *obj = hdata(s, bsock_type); dsock_assert(obj && obj->vfptrs.type == bthrottler_type); int rc = hclose(obj->s); dsock_assert(rc == 0); free(obj); }
static void mlog_close(int s) { struct mlogsock *obj = hdata(s, msock_type); dsock_assert(obj && obj->vfptrs.type == mlog_type); int rc = hclose(obj->s); dsock_assert(rc == 0); free(obj); }
static void tcplistener_close(int s) { struct tcplistener *lst = hdata(s, tcplistener_type); dill_assert(lst); int rc = dsclose(lst->fd); dill_assert(rc == 0); free(lst); }
int tcpaccept(int s, int64_t deadline) { int err; struct tcplistener *lst = hdata(s, tcplistener_type); if(dill_slow(!lst)) return -1; /* Try to get new connection in a non-blocking way. */ ipaddr addr; socklen_t addrlen; int as = dsaccept(lst->fd, (struct sockaddr*)&addr, &addrlen, deadline); if(dill_slow(as < 0)) return -1; tcptune(as); /* Create the object. */ struct tcpconn *conn = tcpconn_create(); if(dill_slow(!conn)) {err = errno; goto error1;} conn->fd = as; conn->addr = addr; /* Bind the object to a handle. */ int hndl = bsock(tcpconn_type, conn, &tcpconn_vfptrs); if(dill_slow(hndl < 0)) {err = errno; goto error2;} return hndl; error2: tcpconn_destroy(conn); error1:; int rc = dsclose(s); dill_assert(rc == 0); errno = err; return -1; }
int mlog_stop(int s) { struct mlogsock *obj = hdata(s, msock_type); if(dsock_slow(obj && obj->vfptrs.type != mlog_type)) { errno = ENOTSUP; return -1;} int u = obj->s; free(obj); return u; }
int bthrottler_stop(int s) { struct bthrottlersock *obj = hdata(s, bsock_type); if(dsock_slow(obj && obj->vfptrs.type != bthrottler_type)) { errno = ENOTSUP; return -1;} int u = obj->s; free(obj); return u; }
static int mlog_msend(int s, const void *buf, size_t len, int64_t deadline) { struct mlogsock *obj = hdata(s, msock_type); dsock_assert(obj->vfptrs.type == mlog_type); fprintf(stderr, "send %8zuB: 0x", len); size_t i; for(i = 0; i != len; ++i) fprintf(stderr, "%02x", (int)((uint8_t*)buf)[i]); fprintf(stderr, "\n"); return msend(obj->s, buf, len, deadline); }
static ssize_t mlog_mrecv(int s, void *buf, size_t len, int64_t deadline) { struct mlogsock *obj = hdata(s, msock_type); dsock_assert(obj->vfptrs.type == mlog_type); ssize_t sz = mrecv(obj->s, buf, len, deadline); if(dsock_slow(sz < 0)) return -1; fprintf(stderr, "recv %8zuB: 0x", len); size_t i; for(i = 0; i != sz && i != len; ++i) fprintf(stderr, "%02x", (int)((uint8_t*)buf)[i]); fprintf(stderr, "\n"); return sz; }
void RateStateSimWindow::recalc(void) { std::vector<std::vector<realtype> > results; double time_max = 200, time_step = 0.01; RSParams params(NBLOCKS, NEQ, NPARAMS, time_step, time_max); unsigned int i, npoints; double xi, vi, hi; std::cerr << param_a << " " << param_b << " " << param_k << " " << param_r << " " << param_w << std::endl; for (i=0;i<params.num_blocks();++i) { params.param(i, A_PARAM) = RCONST(param_a); params.param(i, B_PARAM) = RCONST(param_b); params.param(i, K_PARAM) = RCONST(param_k); params.param(i, R_PARAM) = RCONST(param_r); params.param(i, W_PARAM) = RCONST(param_w); params.init_val(i, EQ_X) = RCONST(-10.0); params.init_val(i, EQ_V) = RCONST(1.0); params.init_val(i, EQ_H) = RCONST(1.0); } run_rate_state_sim(results, params); npoints = results.size(); QVector<QPointF> xdata(npoints), vdata(npoints), hdata(npoints), fdata(npoints), dp_data(npoints); double force_integral=0, old_t=0; for (i=0;i<results.size();++i) { xi = results[i][1]; vi = results[i][2]; hi = results[i][3]; xdata[i] = QPointF(results[i][0], xi); vdata[i] = QPointF(results[i][0], vi); hdata[i] = QPointF(results[i][0], hi); fdata[i] = QPointF(results[i][0], F(0, vi, hi, params)); dp_data[i] = QPointF(results[i][0], results[i][0]); force_integral += (results[i][0]-old_t)*F(0, vi, hi, params); old_t = results[i][0]; } force_integral /= (results.size()/(time_step*time_max)); //std::cerr << force_integral << std::endl; position_data->setData(new QwtPointSeriesData(xdata)); velocity_data->setData(new QwtPointSeriesData(vdata)); theta_data->setData(new QwtPointSeriesData(hdata)); force_data->setData(new QwtPointSeriesData(fdata)); driver_data->setData(new QwtPointSeriesData(dp_data)); position_plot->replot(); velocity_plot->replot(); theta_plot->replot(); force_plot->replot(); }
static void dill_proc_close(int h) { struct dill_proc *proc = hdata(h, dill_proc_type); dill_assert(proc); /* This may happen if forking failed. */ if(dill_slow(proc->pid < 0)) {free(proc); return;} /* There is a child running. Let's send it a kill signal. */ int rc = close(proc->closepipe); dill_assert(rc == 0); /* Wait till it finishes. */ /* TODO: For how long can this block? */ rc = waitpid(proc->pid, NULL, 0); dill_assert(rc >= 0); free(proc); }
static ssize_t mthrottler_mrecv(int s, void *buf, size_t len, int64_t deadline) { struct mthrottlersock *obj = hdata(s, msock_type); dsock_assert(obj->vfptrs.type == mthrottler_type); /* If recv-throttling is off forward the call. */ if(obj->recv_full == 0) return mrecv(obj->s, buf, len, deadline); /* If there's no quota wait till it is renewed. */ if(!obj->recv_remaining) { int rc = msleep(obj->recv_last + obj->recv_interval); if(dsock_slow(rc < 0)) return -1; obj->recv_remaining = obj->recv_full; obj->recv_last = now(); } /* Receive the message. */ int rc = mrecv(obj->s, buf, len, deadline); if(dsock_slow(rc < 0)) return -1; --obj->recv_remaining; return 0; }
int bthrottler_start(int s, uint64_t send_throughput, int64_t send_interval, uint64_t recv_throughput, int64_t recv_interval) { if(dsock_slow(send_throughput != 0 && send_interval <= 0 )) { errno = EINVAL; return -1;} if(dsock_slow(recv_throughput != 0 && recv_interval <= 0 )) { errno = EINVAL; return -1;} /* Check whether underlying socket is a bytestream. */ if(dsock_slow(!hdata(s, bsock_type))) return -1; /* Create the object. */ struct bthrottlersock *obj = malloc(sizeof(struct bthrottlersock)); if(dsock_slow(!obj)) {errno = ENOMEM; return -1;} obj->vfptrs.hvfptrs.close = bthrottler_close; obj->vfptrs.type = bthrottler_type; obj->vfptrs.bsend = bthrottler_bsend; obj->vfptrs.brecv = bthrottler_brecv; obj->s = s; obj->send_full = 0; if(send_throughput > 0) { obj->send_full = send_throughput * send_interval / 1000; obj->send_remaining = obj->send_full; obj->send_interval = send_interval; obj->send_last = now(); } obj->recv_full = 0; if(recv_throughput > 0) { obj->recv_full = recv_throughput * recv_interval / 1000; obj->recv_remaining = obj->recv_full; obj->recv_interval = recv_interval; obj->recv_last = now(); } /* Create the handle. */ int h = handle(bsock_type, obj, &obj->vfptrs.hvfptrs); if(dsock_slow(h < 0)) { int err = errno; free(obj); errno = err; return -1; } return h; }
int mlog_start(int s) { /* Check whether underlying socket is a bytestream. */ if(dsock_slow(!hdata(s, msock_type))) return -1; /* Create the object. */ struct mlogsock *obj = malloc(sizeof(struct mlogsock)); if(dsock_slow(!obj)) {errno = ENOMEM; return -1;} obj->vfptrs.hvfptrs.close = mlog_close; obj->vfptrs.type = mlog_type; obj->vfptrs.msend = mlog_msend; obj->vfptrs.mrecv = mlog_mrecv; obj->s = s; /* Create the handle. */ int h = handle(msock_type, obj, &obj->vfptrs.hvfptrs); if(dsock_slow(h < 0)) { int err = errno; free(obj); errno = err; return -1; } return h; }
// --- tERROR pr_call _PropertyGetStr( tPO* po, tHANDLE* handle, tDATA* result, tPROPID propid, tSTRING buffer, tDWORD size, tCODEPAGE receive_cp ) { tERROR error = errUNEXPECTED; tDATA len = 0; const tDATA* table; tINTERFACE* iface; tCODEPAGE src_cp; PR_TRACE_A0( MakeObject(handle), "Enter _PropertyGetStr method" ); /* if ( !KNOWN_CP(receive_cp) ) error = errCODEPAGE_NOT_SUPPORTED; */ if ( 0 == (iface=handle->iface) ) error = errINTERFACE_NOT_ASSIGNED_YET; else if ( propid == pgPLUGIN_NAME ) { if ( iface->plugin ) { tHANDLE* plugin; tINT lock = 1; if ( _HCP(plugin,po,lock,iface->plugin,&error) ) { error = _PropertyGetStr( po, plugin, &len, pgMODULE_NAME, buffer, size, receive_cp ); runlockc(po,lock); } } else error = errMODULE_NOT_FOUND; } else if ( (0 == (table=_PropTableSearch(iface,propid,0))) && (0==(table=_PropTableSearch(iface,pgPROP_LAST_CALL,0))) ) error = errPROPERTY_NOT_FOUND; else if ( (cPROP_BUFFER_READ|cPROP_BUFFER_HSTRING) == (PROP_MODE(table) & (cPROP_BUFFER_READ|cPROP_BUFFER_HSTRING)) ) { hSTRING hstr = *(hSTRING*)(((tBYTE*)*hdata(handle))+PROP_OFFSET(table)); if ( hstr ) { tDWORD l; error = CALL_String_ExportToBuff( hstr, &l, cSTRING_WHOLE, buffer, size, receive_cp, cSTRING_Z ); len = l; } else error = errOBJECT_NOT_FOUND; } else { tIntFnPropIO func; tPTR src = 0; tDATA src_len = 0; if ( PR_FAIL(error=_PropertyGetStrCP(po,handle,&src_cp,propid)) ) src_cp = receive_cp; // cannot get source CP, just get string witout conversion !!! if ( PROP_MODE(table) & cPROP_BUFFER_SHARED ) { func = 0; if ( PROP_MODE(table) & (cPROP_BUFFER_SHARED_PTR & ~cPROP_BUFFER_SHARED) ) src = PROP_BUFFER(table); else if ( PROP_MODE(table) & (cPROP_BUFFER_SHARED_VAR & ~cPROP_BUFFER_SHARED) ) src = (tSTRING)(*(tPTR*)PROP_BUFFER(table)); else src = &((tDATA*)table)[1]; // &PROP_BUFFER(table) !!! Palm OS doesn't accept it } else if ( 0 != (func=PROP_GETFN(table)) ) src = 0; else if ( (PROP_MODE(table) & cPROP_BUFFER_READ) ) { func = 0; src = ((tBYTE*)*hdata(handle))+PROP_OFFSET(table); } if ( src ) src_len = PROP_SIZE(table); if ( !func && !src ) error = errPROPERTY_NOT_READABLE; else if ( buffer ) { len = src_len; if ( src_cp != receive_cp ) { if ( func ) { if ( !CopyTo || !CalcExportLen ) error = errCODEPAGE_CONVERSION_UNAVAILABLE; else { tPTR buff = 0; tDWORD dwLen = 0; call_func5( error, DPO, func, MakeObject(handle), &dwLen, propid, 0, 0 ); if ( PR_FAIL(error) ) ; else if ( PR_FAIL(error=PrAlloc(&buff,dwLen)) ) ; else { call_func5( error, DPO, func, MakeObject(handle), &dwLen, propid, buff, dwLen ); if ( PR_SUCC(error) ) { //tDWORD ex_len = 0; //call_func6( error, 0/*DPO*/, CalcExportLen, buff, dwLen, src_cp, receive_cp, cSTRING_Z, &ex_len ); //if ( PR_FAIL(error) ) // ; //else if ( size < ex_len ) // dwLen = 0, error = errBUFFER_TOO_SMALL; //else call_func8( error, 0/*DPO*/, CopyTo, buffer, size, receive_cp, buff, dwLen, src_cp, cSTRING_Z, &dwLen ); } } if ( buff ) PrFree( buff ); len = dwLen; } } else if ( !CalcExportLen || !CalcImportLen ) len = 0, error = errCODEPAGE_CONVERSION_UNAVAILABLE; else { tDWORD dwLen = 0; tDWORD dwSrcLen = (tDWORD)src_len; call_func5( error, 0/*DPO*/, CalcImportLen, src, src_cp, cSTRING_Z, dwSrcLen, &dwSrcLen ); if ( PR_FAIL(error) ) dwLen = 0; else { call_func6( error, 0/*DPO*/, CalcExportLen, src, dwSrcLen, src_cp, receive_cp, cSTRING_Z, &dwLen ); if ( PR_FAIL(error) ) dwLen = 0; else if ( size < dwLen ) dwLen = 0, error = errBUFFER_TOO_SMALL; else call_func8( error, 0/*DPO*/, CopyTo, buffer, size, receive_cp, src, dwSrcLen, src_cp, cSTRING_Z, &dwLen ); } len = dwLen; } } else if ( func ) { tDWORD dwLen = 0; call_func5( error, DPO, func, MakeObject(handle), &dwLen, propid, buffer, size ); len = dwLen; } else if ( !CalcExportLen ) len = 0, error = errCODEPAGE_CONVERSION_UNAVAILABLE; else { tDWORD dwLen = 0; call_func6( error, 0/*DPO*/, CalcExportLen, src, (tDWORD)src_len, src_cp, receive_cp, cSTRING_Z, &dwLen ); if ( PR_FAIL(error) ) dwLen = 0; else if ( size < dwLen ) dwLen = 0, error = errBUFFER_TOO_SMALL; else if ( CopyTo ) { call_func8( error, 0/*DPO*/, CopyTo, buffer, size, receive_cp, src, dwLen, src_cp, cSTRING_Z, &dwLen ); } else { dwLen = (tDWORD)src_len; memcpy( buffer, src, src_len ); } len = dwLen; } } else if ( func ) { tDWORD dwLen = 0; call_func5( error, DPO, func, MakeObject(handle), &dwLen, propid, 0, 0 ); if ( PR_SUCC(error) && (src_cp != receive_cp) && dwLen && ConvertLen ) call_func4( error, 0/*DPO*/, ConvertLen, dwLen, &dwLen, src_cp, receive_cp ); len = dwLen; } else if ( !CalcExportLen ) len = src_len; else { tDWORD dwLen = 0; call_func6( error, 0/*DPO*/, CalcExportLen, src, (tDWORD)src_len, src_cp, receive_cp, cSTRING_Z, &dwLen ); if ( PR_SUCC(error) ) len = dwLen; else len = src_len; } } if ( result ) *result = len; PR_TRACE_A2( MakeObject(handle), "Leave _PropertyGetStr method, ret tDWORD = %u, error = %terr", len, error ); return error; }
int brecv(int s, void *buf, size_t len, int64_t deadline) { struct hvfptr *h = hdata(s, bsock_type); if(dsock_slow(!h)) return -1; struct bsockvfptrs *b = (struct bsockvfptrs*)h; return b->brecv(s, buf, len, deadline); }
int tcpport(int s) { struct tcplistener *lst = hdata(s, tcplistener_type); if(dill_slow(!lst)) return -1; return lst->port; }
// --- tERROR pr_call _PropertySetStr( tPO* po, tHANDLE* handle, tDATA* result, tPROPID propid, tSTRING buffer, tDWORD size, tCODEPAGE src_cp ) { tERROR error = errOK; tDATA len = 0; const tDATA* table; tINTERFACE* iface; tDWORD init_prop_pos = 0xffffffff; PR_TRACE_A0( MakeObject(handle), "Enter _PropertySetStr method" ); /* if ( !KNOWN_CP(receive_cp) ) error = errCODEPAGE_NOT_SUPPORTED; */ if ( 0 == (iface=handle->iface) ) error = errINTERFACE_NOT_ASSIGNED_YET; else if ( (0 == (table = _PropTableSearch(iface,propid,&init_prop_pos))) && (0==(table=_PropTableSearch(iface,pgPROP_LAST_CALL,&init_prop_pos))) ) error = errPROPERTY_NOT_FOUND; /* else if ( !KNOWN_CP(src_cp) ) error = errCODEPAGE_NOT_SUPPORTED; */ else if ( PROP_MODE(table) & cPROP_BUFFER_SHARED ) // shared buffer is RO !!! error = errPROPERTY_NOT_WRITABLE; else if ( ((PROP_MODE(table) & cPROP_WRITABLE_ON_INIT) != 0) && ((handle->flags & hf_OPERATIONAL_MODE) != 0) ) // property cannot be set in this operational mode error = errPROPERTY_NOT_WRITABLE; else if ( (cPROP_BUFFER_WRITE|cPROP_BUFFER_HSTRING) == (PROP_MODE(table) & (cPROP_BUFFER_WRITE|cPROP_BUFFER_HSTRING)) ) { hSTRING* pstr = (hSTRING*)(((tBYTE*)*hdata(handle))+PROP_OFFSET(table)); if ( !*pstr ) { tHANDLE* hstr; rlock(po); if ( PR_SUCC(error=_ObjectIIDCreate(po,g_hRoot,&hstr,IID_STRING,PID_ANY,SUBTYPE_ANY)) ) { if ( PR_SUCC(error=_ObjectCreateDone(po,hstr)) ) *pstr = (hSTRING)MakeObject( hstr ); else _ObjectClose( po, hstr, 0 ); } runlock(po); } if ( PR_SUCC(error) ) { tDWORD dwLen = 0; error = CALL_String_ImportFromBuff( *pstr, &dwLen, buffer, size, src_cp, cSTRING_Z ); len = dwLen; } if ( PR_SUCC(error) && buffer && (init_prop_pos < 32) ) handle->init_props &= ~( 1 << init_prop_pos ); } else { tIntFnPropIO func; tPTR dst; tDATA dst_len = 0; tCODEPAGE dst_cp; if ( PR_FAIL(error=_PropertyGetStrCP(po,handle,&dst_cp,propid)) ) dst_cp = src_cp; // cannot get destination CP, just put string witout conversion !!! if ( 0 != (func=PROP_SETFN(table)) ) dst = 0; else if ( PROP_MODE(table) & cPROP_BUFFER_WRITE ) { dst = ((tBYTE*)*hdata(handle))+PROP_OFFSET(table); dst_len = PROP_SIZE(table); } else dst = 0; if ( !func && !dst ) error = errPROPERTY_NOT_WRITABLE; else if ( buffer ) { if ( src_cp != dst_cp ) { if ( func ) { if ( !CalcExportLen || !CopyTo ) error = errCODEPAGE_CONVERSION_UNAVAILABLE; else { tPTR tmp = 0; tDWORD tmp_len = size; call_func6( error, 0/*DPO*/, CalcExportLen, buffer, size, src_cp, dst_cp, cSTRING_Z, &tmp_len ); if ( PR_FAIL(error) ) ; else if ( PR_FAIL(error=PrAlloc(&tmp,tmp_len)) ) ; else { call_func8( error, 0/*DPO*/, CopyTo, tmp, tmp_len, dst_cp, buffer, size, src_cp, cSTRING_Z, &tmp_len ); if ( PR_SUCC(error) ) { tDWORD dwLen = 0; call_func5( error, DPO, func, MakeObject(handle), &dwLen, propid, tmp, tmp_len ); len = dwLen; } } if ( tmp ) PrFree( tmp ); } } else if ( CopyTo ) { tDWORD dwLen = 0; call_func8( error, 0/*DPO*/, CopyTo, dst, (tDWORD)dst_len, dst_cp, buffer, size, src_cp, cSTRING_Z, &dwLen ); len = dwLen; } else error = errCODEPAGE_CONVERSION_UNAVAILABLE; } else if ( !size && ((error=errSTRING_LEN_NOT_SPECIFIED,!CalcExportLen) || PR_FAIL(error=CalcExportLen(buffer,0,src_cp,src_cp,cSTRING_Z,&size))) ) ; else if ( func ) { tDWORD dwLen = 0; call_func5( error, DPO, func, MakeObject(handle), &dwLen, propid, buffer, size ); len = dwLen; } else if ( dst_len < size ) { len = 0; error = errBUFFER_TOO_SMALL; } else { memcpy( dst, buffer, (tDWORD)(len=size) ); error = errOK; } if ( PR_SUCC(error) && (init_prop_pos < 32) ) handle->init_props &= ~( 1 << init_prop_pos ); } else if ( func ) { tDWORD dwLen = (tDWORD)len; call_func5( error, DPO, func, MakeObject(handle), &dwLen, propid, 0, 0 ); if ( PR_SUCC(error) && (src_cp != dst_cp) && dwLen ) { if ( ConvertLen ) { call_func4( error, 0/*DPO*/, ConvertLen, dwLen, &dwLen, dst_cp, src_cp ); } else error = errCODEPAGE_CONVERSION_UNAVAILABLE; } len = dwLen; } else if ( src_cp == dst_cp ) len = dst_len; else if ( ConvertLen ) { tDWORD dwLen = 0; call_func4( error, 0/*DPO*/, ConvertLen, (tDWORD)dst_len, &dwLen, dst_cp, src_cp ); len = dwLen; } else error = errCODEPAGE_CONVERSION_UNAVAILABLE; } if ( result ) *result = len; PR_TRACE_A2( MakeObject(handle), "Leave _PropertySetStr method, ret tDWORD = %u, error = %terr", (tDWORD)len, error ); return error; }