unsigned Aggregate::displayChunks() const { fancyprint(activeChunks() << "/" << totalChunks(),NOTIFY); int j = m_chunk.size(); for(auto i=0;i<m_chunk.size();i++){ uintmax_t lower = std::atoi(m_chunk[i]->file()->filename().c_str()); uintmax_t down= m_chunk[i]->txn()->range().lb()+m_chunk[i]->txn()->bytesDone(); uintmax_t higher = m_chunk[i]->txn()->range().ub(); std::string myColor; if(m_chunk[i]->txn()->isComplete()) myColor = SUCCESS; else if(m_chunk[i]->txn()->hasFailed()) myColor = ERROR; else if(m_chunk[i]->txn()->isDownloading()) myColor = WARNING; else myColor = NOTIFY; fancyprint(lower << ":" << down << ":"<< higher<< " ",myColor); } std::cout << progressbar(progress(),COLOR(0,CC::WHITE,CC::PURPLE),COLOR(0,CC::PURPLE,CC::WHITE)); print( " " << round(progress(),2) << "%\t" << formatTime(timeRemaining()) << "\t" << formatByte(speed()) << "ps\t"); return j+1; }
void Aggregate::merger() { // Just sleep for a while // Only for syncronization in main loop boost::this_thread::sleep(boost::posix_time::millisec(500)); // If total size downloaded isn't equal // to the size of file downloaded then // do not merge the Chunks if( m_filesize != bytesTotal()) Throw(ex::Error,"Downloaded bytes greater than total filesize."); fancyprint("Merging!",NOTIFY); fancyprint("Do not close this window.",WARNING); File tmp(tempName()); tmp.write(Node::FORCE); // TODO try binary appends and storing to "tmp" // Append the content to "tmp" for(unsigned i=0; i < m_chunk.size(); i++){ print(i+1 << " of " << m_chunk.size()); tmp.append(*(m_chunk[i]->file())); std::cout << DELETE; } std::cout << DELETE; tmp.move(prettyName(),Node::NEW); // Remove the old directory Directory(m_hashedUrl).remove(Node::FORCE); fancyprint("Complete!",SUCCESS); }
static GString *receive(struct sr_output *o, const struct sr_dev_inst *sdi, const struct sr_datafeed_packet *packet) { struct context *ctx; const struct sr_datafeed_analog *analog; struct sr_probe *probe; GSList *l; const float *fdata; int i, p; (void)sdi; if (!o || !o->sdi) return NULL; ctx = o->internal; g_string_set_size(ctx->out, 0); switch (packet->type) { case SR_DF_HEADER: break; case SR_DF_FRAME_BEGIN: g_string_append_printf(ctx->out, "FRAME-BEGIN\n"); break; case SR_DF_FRAME_END: g_string_append_printf(ctx->out, "FRAME-END\n"); break; case SR_DF_ANALOG: analog = packet->payload; fdata = (const float *)analog->data; for (i = 0; i < analog->num_samples; i++) { for (l = analog->probes, p = 0; l; l = l->next, p++) { probe = l->data; g_string_append_printf(ctx->out, "%s: ", probe->name); fancyprint(analog->unit, analog->mqflags, fdata[i + p], ctx->out); } } break; } return ctx->out; }
static int receive(struct sr_output *o, const struct sr_dev_inst *sdi, const struct sr_datafeed_packet *packet, GString **out) { const struct sr_datafeed_analog *analog; struct sr_probe *probe; GSList *l; const float *fdata; int i, p; (void)sdi; *out = NULL; if (!o || !o->sdi) return SR_ERR_ARG; switch (packet->type) { case SR_DF_FRAME_BEGIN: *out = g_string_new("FRAME-BEGIN\n"); break; case SR_DF_FRAME_END: *out = g_string_new("FRAME-END\n"); break; case SR_DF_ANALOG: analog = packet->payload; fdata = (const float *)analog->data; *out = g_string_sized_new(512); for (i = 0; i < analog->num_samples; i++) { for (l = analog->probes, p = 0; l; l = l->next, p++) { probe = l->data; g_string_append_printf(*out, "%s: ", probe->name); fancyprint(analog->unit, analog->mqflags, fdata[i + p], *out); } } break; } return SR_OK; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int cid=-1; // Handle of the current connection (invalid until we identify) int jarg=0; // Number of first string arg: becomes 1 if id is specified /*********************************************************************/ // Parse the first argument to see if it is a specific database handle if ( nrhs>0 && mxIsNumeric(prhs[0]) ) { if ( mxGetM(prhs[0])!=1 || mxGetN(prhs[0])!=1 ) { showusage(); mexPrintf("First argument is array %d x %d, but it should be a scalar\n", mxGetM(prhs[0]),mxGetN(prhs[0]) ); mexErrMsgTxt("Invalid connection handle"); } double xid = *mxGetPr(prhs[0]); cid = int(xid); if ( double(cid)!=xid || cid<0 || cid>=MAXCONN ) { showusage(); mexPrintf("dbHandle = %g -- Must be integer between 0 and %d\n", xid,MAXCONN-1); mexErrMsgTxt("Invalid connection handle"); } jarg = 1; if (debug) mexPrintf("| Explicit cid = %d\n",cid); } /*********************************************************************/ // Check that the remaining arguments are all character strings { for ( int j=jarg ; j<nrhs ; j++ ) { if (!mxIsChar(prhs[j])) { showusage(); mexErrMsgTxt("All args must be strings, except dbHandle"); }}} /*********************************************************************/ // Identify what action he wants to do enum querytype { OPEN, CLOSE, CLOSEALL, USENAMED, USE, STATUS, STATSALL, QUOTE, CMD } q; char *query = NULL; if (nrhs<=jarg) q = STATSALL; else { query = mxArrayToString(prhs[jarg]); if (streq(query,"open")) q = OPEN; else if (streq(query,"close")) q = CLOSE; else if (streq(query,"closeall")) q = CLOSEALL; else if (streq(query,"use")) q = USENAMED; else if (streq(query,"use",3)) q = USE; else if (streq(query,"status")) q = STATUS; else if (streq(query,"quote")) q = QUOTE; else q = CMD; } if (debug) { switch(q) { case OPEN: mexPrintf("| q = OPEN\n"); break; case CLOSE: mexPrintf("| q = CLOSE\n"); break; case CLOSEALL: mexPrintf("| q = CLOSEALL\n"); break; case USENAMED: mexPrintf("| q = USENAMED\n"); break; case USE: mexPrintf("| q = USE\n"); break; case STATUS: mexPrintf("| q = STATUS\n"); break; case STATSALL: mexPrintf("| q = STATSALL\n"); break; case QUOTE: mexPrintf("| q = QUOTE\n"); break; case CMD: mexPrintf("| q = CMD\n"); break; mexPrintf("| q = ??\n"); }} /*********************************************************************/ // If he did not specify the handle, choose the appropriate one // If there are no previous connections, then we will still have // cid=-1, and this will be handled in the appropriate place. if (jarg==0) { if (q==OPEN) { for ( cid=0 ; cid<MAXCONN & c[cid].isopen ; cid++ ); if (cid>=MAXCONN) mexErrMsgTxt("Can\'t find free handle"); } else if (ncid>0) cid=prevcid[ncid-1]; } if (debug) mexPrintf("| cid = %d\n",cid); // Shorthand notation so we don't need to write c[cid]... // These values must not be used if cid<0 mp dummyconn; mp &conn = (cid>=0) ? c[cid].conn : dummyconn; bool dummyisopen; bool &isopen = (cid>=0) ? c[cid].isopen : dummyisopen; if (q==OPEN) { if (cid<0) { mexPrintf("cid = %d !\n",cid); mexErrMsgTxt("Internal code error\n"); } // Close connection if it is open if (isopen) { mexWarnMsgIdAndTxt("mysql:ConnectionAlreadyOpen", "Connection %d has been closed and overwritten",cid); mysql_close(conn); conn=NULL; isopen=false; stackdelete(cid); } // Extract information from input arguments char *host=NULL; if (nrhs>=jarg+2) host = mxArrayToString(prhs[jarg+1]); char *user=NULL; if (nrhs>=jarg+3) user = mxArrayToString(prhs[jarg+2]); char *pass=NULL; if (nrhs>=jarg+4) pass = mxArrayToString(prhs[jarg+3]); int port = hostport(host); // returns zero if there is no port if (nlhs<1) { mexPrintf("Connecting to host=%s", (host) ? host : "localhost" ); if (port) mexPrintf(" port=%d",port); if (user) mexPrintf(" user=%s",user); if (pass) mexPrintf(" password=%s",pass); mexPrintf("\n"); } // Establish and test the connection // If this fails, then conn is still set, but isopen stays false if (!(conn=mysql_init(conn))) mexErrMsgTxt("Couldn\'t initialize MySQL connection object"); if (!mysql_real_connect( conn, host, user, pass, NULL,port,NULL,0 )) mexErrMsgTxt(mysql_error(conn)); const char *c=mysql_stat(conn); if (c) { if (nlhs<1) mexPrintf("%s\n",c); } else mexErrMsgTxt(mysql_error(conn)); isopen=true; ncid++; if (ncid>MAXCONN) { mexPrintf("ncid = %d ?\n",ncid); mexErrMsgTxt("Internal logic error\n"); } prevcid[ncid-1] = cid; if (debug) stackprint(); // Now we are OK -- return the connection handle opened. setScalarReturn(nlhs,plhs,cid); } else if (q==CLOSE) { if ( cid>=0 && isopen ) { if (debug) mexPrintf("| Closing %d\n",cid); mysql_close(conn); conn = NULL; isopen=false; stackdelete(cid); } if (debug) stackprint(); } else if (q==CLOSEALL) { while (ncid>0) { if (debug) stackprint(); cid = prevcid[ncid-1]; if (debug) mexPrintf("| Closing %d\n",cid); if (!(c[cid].isopen)) { mexPrintf("Connection %d is not marked open!\n",cid); mexErrMsgTxt("Internal logic error"); } mysql_close(c[cid].conn); c[cid].conn=NULL; c[cid].isopen=false; ncid--; }} else if ( q==USE || q==USENAMED ) { if ( cid<0 || !isopen ) mexErrMsgTxt("Not connected"); if (mysql_ping(conn)) { stackdelete(cid); isopen=false; mexPrintf(mysql_error(conn)); mexPrintf("\nClosing connection %d\n",cid); mexErrMsgTxt("Use command failed"); } char *db=NULL; if (q==USENAMED) { if (nrhs>=2) db=mxArrayToString(prhs[jarg+1]); else mexErrMsgTxt("Must specify a database to use"); } else { db = query + 3; while ( *db==' ' || *db=='\t' ) db++; } if (mysql_select_db(conn,db)) mexErrMsgTxt(mysql_error(conn)); if (nlhs<1) mexPrintf("Current database is \"%s\"\n",db); else setScalarReturn(nlhs,plhs,1.); } else if (q==STATUS) { if (nlhs<1) // He wants a report { // print connection handle only if multiple connections char idstr[10]; idstr[0]=0; if ( cid>=0 && ncid>1 ) sprintf(idstr,"(%d) ",cid); if ( cid<0 || !isopen ) { mexPrintf("%sNot connected\n",idstr,cid); return; } if (mysql_ping(conn)) { mexErrMsgTxt(mysql_error(conn)); } mexPrintf("%s%-30s Server version %s\n", idstr, mysql_get_host_info(conn), mysql_get_server_info(conn) ); } else // He wants a return value for this connection { double *pr=setScalarReturn(nlhs,plhs,0.); if ( cid<0 || !isopen ) { *pr=1.; return; } if (mysql_ping(conn)) { *pr=2.; return; }} } else if (q==STATSALL) { if (debug) stackprint(); if (ncid==0) mexPrintf("No connections open\n"); else if (ncid==1) mexPrintf("1 connection open\n"); else mexPrintf("%d connections open\n",ncid); for ( int j=0 ; j<ncid ; j++ ) { cid = prevcid[j]; if (mysql_ping(c[cid].conn)) mexPrintf("%2d: %s\n",cid,mysql_error(conn)); else mexPrintf("%2d: %-30s Server version %s\n", cid, mysql_get_host_info(c[cid].conn), mysql_get_server_info(c[cid].conn) ); } } // Quote the second string argument and return it (Chris Rodgers) else if (q==QUOTE) { if ((nrhs-jarg)!=2) mexErrMsgTxt("mysql('quote','string_to_quote') takes two string arguments!"); // Check that we have a valid connection if ( cid<0 || !isopen ) mexErrMsgTxt("No connection open"); if (mysql_ping(conn)) { stackdelete(cid); isopen=false; mexErrMsgTxt(mysql_error(conn)); } const mxArray *a = prhs[jarg+1]; int llen = mxGetM(a)*mxGetN(a)*sizeof(mxChar); char *from = (char *) mxCalloc(llen+1,sizeof(char)); if (mxGetString(a,from,llen)) mexErrMsgTxt("Can\'t copy string"); int l = strlen(from); /* Allocate memory for input and output strings. */ char *to = (char*) mxCalloc( llen*2+3, sizeof(char)); /* Call the C subroutine. */ to[0] = '\''; int n = mysql_real_escape_string( conn, to+1, from, l ); to[n+1] = '\''; /* Set C-style string output_buf to MATLAB mexFunction output*/ plhs[0] = mxCreateString(to); mxFree(from); mxFree(to); // just in case Matlab forgets } else if (q==CMD) { // Check that we have a valid connection if ( cid<0 || !isopen ) mexErrMsgTxt("No connection open"); if (mysql_ping(conn)) { stackdelete(cid); isopen=false; mexPrintf(mysql_error(conn)); mexPrintf("Closing connection %d\n",cid); mexErrMsgTxt("Query failed"); } // Execute the query (data stays on server) if (mysql_query(conn,query)) mexErrMsgTxt(mysql_error(conn)); // Download the data from server into our memory // We need to be careful to deallocate res before returning. // Matlab's allocation routines return instantly if there is not // enough free space, without giving us time to dealloc res. // This is a potential memory leak but I don't see how to fix it. MYSQL_RES *res = mysql_store_result(conn); // As recommended in Paul DuBois' MySQL book (New Riders, 1999): // A NULL result set after the query can indicate either // (1) the query was an INSERT, DELETE, REPLACE, or UPDATE, that // affect rows in the table but do not return a result set; or // (2) an error, if the query was a SELECT, SHOW, or EXPLAIN // that should return a result set but didn't. // Distinguish between the two by checking mysql_field_count() // We return in either case, either correctly or with an error if (!res) { if (!mysql_field_count(conn)) { unsigned long nrows=mysql_affected_rows(conn); if (nlhs<1) { mexPrintf("%u rows affected\n",nrows); return; } else { setScalarReturn(nlhs,plhs,nrows); return; }} else mexErrMsgTxt(mysql_error(conn)); } unsigned long nrow=mysql_num_rows(res), nfield=mysql_num_fields(res); // If he didn't ask for any output (nlhs=0), // then display the output and return if ( nlhs<1 ) { fancyprint(res); mysql_free_result(res); return; } // If we are here, he wants output // He must give exactly the right number of output arguments // if ( nlhs != nfield ) // { mysql_free_result(res); // mexPrintf("You specified %d output arguments, " // "and got %d columns of data\n",nlhs,nfield); // mexErrMsgTxt("Must give one output argument for each column"); } if(nlhs > 1) mexErrMsgIdAndTxt( "MYSQL query: ", "Too many output arguments."); // Fix the column types to fix MySQL C API sloppiness MYSQL_FIELD *f = mysql_fetch_fields(res); fix_types( f, res ); // Create the Matlab arrays for output // double **pr = (double **) mxMalloc( nfield * sizeof(double *) ); // { for ( int j=0 ; j<nfield ; j++ ) // { if ( can_convert(f[j].type) ) // { if (!( plhs[j] = mxCreateDoubleMatrix( nrow, 1, mxREAL ) )) // { mysql_free_result(res); // mexErrMsgTxt("Unable to create numeric matrix for output"); } // pr[j] = mxGetPr(plhs[j]); } // else // { if (!( plhs[j] = mxCreateCellMatrix( nrow, 1 ) )) // { mysql_free_result(res); // mexErrMsgTxt("Unable to create cell matrix for output"); } // pr[j] = NULL; }}} if (!( plhs[0] = mxCreateCellMatrix( nrow, nfield ) )){ mysql_free_result(res); mexErrMsgTxt("Unable to create cell matrix for output"); } // Load the data into the cells mysql_data_seek(res,0); for ( int i=0 ; i<nrow ; i++ ){ MYSQL_ROW row = mysql_fetch_row(res); if (!row) { mexPrintf("Scanning row %d for data extraction\n",i+1); mexErrMsgTxt("Internal error: Failed to get a row"); } // for ( int j=0 ; j<nfield ; j++ ){ // if (can_convert(f[j].type)) { // pr[j][i] = field2num(row[j],f[j].type); // } // else { // mxArray *c = mxCreateString(row[j]); // mxSetCell(plhs[j],i,c); // } // } for ( int j=0 ; j <nfield ; j++ ){ mxArray *c = mxCreateString(row[j]); mxSetCell(plhs[0],j*nrow + i,c); } } mysql_free_result(res); } else { mexPrintf("Unknown query type q = %d\n",q); mexErrMsgTxt("Internal code error"); } }
static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, GString **out) { struct context *ctx; const struct sr_datafeed_analog_old *analog_old; const struct sr_datafeed_analog *analog; struct sr_channel *ch; GSList *l; float *fdata; unsigned int i; int num_channels, c, ret, si, digits; char *number, *suffix; *out = NULL; if (!o || !o->sdi) return SR_ERR_ARG; ctx = o->priv; switch (packet->type) { case SR_DF_FRAME_BEGIN: *out = g_string_new("FRAME-BEGIN\n"); break; case SR_DF_FRAME_END: *out = g_string_new("FRAME-END\n"); break; case SR_DF_ANALOG_OLD: analog_old = packet->payload; fdata = (float *)analog_old->data; *out = g_string_sized_new(512); num_channels = g_slist_length(analog_old->channels); for (si = 0; si < analog_old->num_samples; si++) { for (l = analog_old->channels, c = 0; l; l = l->next, c++) { ch = l->data; g_string_append_printf(*out, "%s: ", ch->name); fancyprint(analog_old->unit, analog_old->mqflags, fdata[si * num_channels + c], *out); } } break; case SR_DF_ANALOG: analog = packet->payload; num_channels = g_slist_length(analog->meaning->channels); if (!(fdata = g_try_malloc( analog->num_samples * num_channels * sizeof(float)))) return SR_ERR_MALLOC; if ((ret = sr_analog_to_float(analog, fdata)) != SR_OK) return ret; *out = g_string_sized_new(512); if (analog->encoding->is_digits_decimal) { if (ctx->digits == DIGITS_ALL) digits = analog->encoding->digits; else digits = analog->spec->spec_digits; } else { /* TODO we don't know how to print by number of bits yet. */ digits = 6; } sr_analog_unit_to_string(analog, &suffix); for (i = 0; i < analog->num_samples; i++) { for (l = analog->meaning->channels, c = 0; l; l = l->next, c++) { ch = l->data; g_string_append_printf(*out, "%s: ", ch->name); number = g_strdup_printf("%.*f", digits, fdata[i * num_channels + c]); g_string_append(*out, number); g_free(number); g_string_append(*out, " "); g_string_append(*out, suffix); g_string_append(*out, "\n"); } } g_free(suffix); break; } return SR_OK; }