📄 sensor.cpp
字号:
/***************************************************************** * * * This file is a part of the eXtremeDB-HA Application Framework * * Copyright (c) 2001-2006 McObject LLC * * All Rights Reserved * * * ***************************************************************** * ++ * * PROJECT: eXtremeDB(tm) (c) 2003 McObject LLC * * SUBSYSTEM: HA framework * * MODULE: sensor.cpp * * ABSTRACT: Sensor_1 class implementation * * * VERSION: 1.0 * * HISTORY: * 1.0- 1 SS 18-Feb-2004 Created it was * * -- *****************************************************************/#include "sensor.hpp"#include "mcoha.hpp"#define STRINGS_DIM 10#define STRINGS_STEP 100#define MAX_VECT_LEN 10static char *strings[STRINGS_DIM];/* * static members */short rand2( short lowlimit, short uplimit ){ int n = rand(); return (short)(( n % (uplimit - lowlimit + 1) ) + lowlimit);}uint4 rand4(){ int n = rand(); return (uint4)n;}void make_strings( void ){ int i, j, strsz; for ( j = 0; j < STRINGS_DIM; j++ ) { strsz = STRINGS_STEP * ( j + 1 ); strings[j] = (char*)malloc( strsz + 1 ); for ( i=0; i < strsz; i++ ) strings[j][i] = (char)rand2('a', 'z'); strings[j][strsz] = 0; }}char* get_random_string(){ return strings[rand2(0, STRINGS_DIM - 1)];}/* class Sensor_1 */void Sensor_1::init(int num_rec){ monitorDB::Svalue sv; uint2 vl, i; MCO_RET rc = MCO_S_OK; char * str; uint2 len; for ( ; num_rec > 0; num_rec-- ) { rc = ha->StartTransaction(MCO_READ_WRITE, MCO_TRANS_FOREGROUND); if ( rc != MCO_S_OK ) break; rc = create(ha->t); if ( rc != MCO_S_OK ) break; rc = tm_put(num_rec); if ( rc != MCO_S_OK ) break; rc = value4_put(rand4()); if ( rc != MCO_S_OK ) break; vl = rand2(1, MAX_VECT_LEN); rc = valuev_alloc(vl); if ( rc != MCO_S_OK ) break; for ( i=0; i < vl; i++ ) { rc = valuev_put(i, sv); if ( rc != MCO_S_OK ) break; rc = sv.tm_put(num_rec); if ( rc != MCO_S_OK ) break; rc = sv.u4_put(rand4()); if ( rc != MCO_S_OK ) break; str = get_random_string(); len = (uint2) strlen(str); rc = sv.bytes_put(str, len); if ( rc != MCO_S_OK ) break; } if ( rc != MCO_S_OK ) break; rc = ha->TransCommit(); if ( rc != MCO_S_OK ) break; } if ( rc != MCO_S_OK ) { printf( "\nerror initializing database %d (object S4) \n", rc ); exit( 1 ); }}int Sensor_1::new_record(int4 time){ mco_cursor_t c; monitorDB::Svalue sv; uint2 vl = 0, i; MCO_RET rc = MCO_S_OK; char * str; uint2 len; int count=4; if ( (rc = create(ha->t)) == MCO_S_OK ) { rc = tm_put(time-1); if ( (rc = tm_put(time)) == MCO_S_OK ) rc = value4_put(rand4()); if ( rc == MCO_S_OK ) vl = rand2(1, MAX_VECT_LEN); if ( rc == MCO_S_OK ) rc = valuev_alloc(vl); } if ( rc != MCO_S_OK ) { printf( "\nerror creating s4 %d\n", rc ); exit( 1 ); } for ( i=0; i < vl; i++ ) { if ( (rc = valuev_put(i, sv)) != MCO_S_OK ) break; if ( (rc = sv.tm_put(time)) != MCO_S_OK ) break; if ( (rc = sv.u4_put(rand4())) != MCO_S_OK ) break; str = get_random_string(); len = (uint2) strlen(str); rc = sv.bytes_put(str, len); if ( rc != MCO_S_OK ) break; } if ( rc != MCO_S_OK ) { printf( "\nerror updating vector s4 %d\n", rc ); exit( 1 ); } rc = IS1tm::cursor(ha->t, &c); if ( rc == MCO_S_OK ) rc = mco_cursor_first(ha->t, &c); if ( rc == MCO_S_OK ) rc = from_cursor(ha->t, &c); if ( rc != MCO_S_OK ) { printf( "\nerror searching s4 %d\n", rc ); exit( 1 ); } rc = tm_put(time); vl = rand2(1, MAX_VECT_LEN); if ( rc == MCO_S_OK ) rc = valuev_size(vl); if ( rc != MCO_S_OK ) { printf( "\nerror creating s4 %d\n", rc ); exit( 1 ); } count += vl*3; for ( i=0; i < vl; i++ ) { rc = valuev_put(i, sv); if ( rc != MCO_S_OK ) break; rc = sv.tm_put(time); if ( rc != MCO_S_OK ) break; rc = sv.u4_put(rand4()); if ( rc != MCO_S_OK ) break; str = get_random_string(); len = (uint2) strlen(str); rc = sv.bytes_put(str, len); if ( rc != MCO_S_OK ) break; } if ( rc != MCO_S_OK ) { printf( "\nerror updating vector s4 %d\n", rc ); exit( 1 ); } rc = IS1tm::cursor(ha->t, &c); if ( rc == MCO_S_OK ) rc = mco_cursor_first(ha->t, &c); if ( rc == MCO_S_OK ) rc = from_cursor(ha->t, &c); if ( rc != MCO_S_OK ) { printf( "\nerror searching s4 %d\n", rc ); exit( 1 ); } rc = remove(); if ( rc != MCO_S_OK ) { printf( "\nerror deleting s4 %d\n", rc ); exit( 1 ); } return count;}void Sensor_1::output(FILE *f){ mco_cursor_t c; MCO_RET rc = MCO_S_OK; mco_time tm; monitorDB::Svalue sv; uint2 vl; int i, j; mco_time tim; uint4 u4; uint4 value4; char str[STRINGS_STEP * STRINGS_DIM + 10 ] = { 0 };// uint2 len; rc = IS1tm::cursor(ha->t, &c); if ( rc == MCO_S_OK ) rc = mco_cursor_first(ha->t, &c); if ( rc == MCO_S_OK ) if ( rc != MCO_S_OK ) { printf( "\nerror searching s4 %d\n", rc ); exit( 1 ); } for ( i=1 ;; i++ ) { rc = from_cursor(ha->t, &c); if ( rc != MCO_S_OK ) break; rc = tm_get(tm); if ( rc != MCO_S_OK ) break; rc = value4_get(value4); if ( rc != MCO_S_OK ) break; fprintf(f, "%6d:%7d %10d\n", i, tm, value4);// fprintf(f, "%6d:%7d\n", i, tm); rc = valuev_size(vl); if ( rc != MCO_S_OK ) break; for ( j=0; j < vl; j++ ) { rc = valuev_at( (uint2)j, sv); if ( rc != MCO_S_OK ) break; rc = sv.tm_get(tim); if ( rc != MCO_S_OK ) break; rc = sv.u4_get(u4); if ( rc != MCO_S_OK ) break; rc = sv.bytes_get(str, sizeof(str)/*, &len*/);if ( rc != MCO_S_OK ) break; fprintf(f, " %7d;%10d|%s|\n", tim, u4, str); } if ( rc != MCO_S_OK ) break; if ( mco_cursor_next(ha->t, &c) != MCO_S_OK ) break; } if ( rc != MCO_S_OK ) { printf( "\nerror getting s4 %d\n", rc ); exit( 1 ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -