void process_event(HNDLE hBuf, HNDLE request_id, EVENT_HEADER * pheader, void *pevent) { static INT ser[10], count, start_time, jam = 0; INT stop_time; INT size, *pdata, id; BUFFER_HEADER buffer_header; double rate; /* accumulate received event size */ size = pheader->data_size; id = pheader->event_id; if (id > 9) id = 9; count += size; /* check if first and last word inside event is equal to size to check that nothing was overwritten... */ if (!jam) { /* only test once */ pdata = (INT *) (pheader + 1); if (pdata[0] != size || pdata[size / 4 - 1] != size) cm_msg(MERROR, "process_event", "--> data jam <--"); jam = 1; } /* if only some events are requested, sleep a little bit to simulate a random event consumer */ if (!all_flag) ss_sleep(10); /* if all events are requested, now check the serial number if no events are missing */ if (all_flag && (INT) pheader->serial_number != ser[id] + 1) cm_msg(MERROR, "process_event", "Serial number mismatch: Ser: %ld, OldSer: %ld, ID: %d, size: %ld\n", pheader->serial_number, ser[id], pheader->event_id, pheader->data_size); ser[id] = pheader->serial_number; /* calculate rates each second */ if (ss_millitime() - start_time > 1000) { stop_time = ss_millitime(); rate = count / 1024.0 / 1024.0 / ((stop_time - start_time) / 1000.0); /* get information about filling level of the buffer */ bm_get_buffer_info(hBufEvent, &buffer_header); size = buffer_header.read_pointer - buffer_header.write_pointer; if (size <= 0) size += buffer_header.size; printf("Level: %4.1lf %%, ", 100 - 100.0 * size / buffer_header.size); printf("Rate: %1.2lf MB/sec\n", rate); start_time = stop_time; count = 0; } }
void multi_output(INT hDB, INT hKey, void *info) { INT i; DWORD act_time; MULTI_INFO *m_info; EQUIPMENT *pequipment; pequipment = (EQUIPMENT *) info; m_info = (MULTI_INFO *) pequipment->cd_info; act_time = ss_millitime(); for (i = 0; i < m_info->num_channels_output; i++) { /* only set channel if demand value differs */ if (m_info->var_output[i] != m_info->output_mirror[i]) { m_info->output_mirror[i] = m_info->var_output[i] * m_info->factor_output[i] - m_info->offset_output[i]; device_driver(m_info->driver_output[i], CMD_SET, i - m_info->channel_offset_output[i], m_info->output_mirror[i]); } } pequipment->odb_in++; }
int scope_wait(int fh, unsigned int timeout) { char str[256]; unsigned int start; start = ss_millitime(); do { scope_send(scope_chn[fh].sock, str, "INR?"); if (ss_millitime() - start > timeout) return FALSE; } while ((atoi(str) & 1) == 0); return TRUE; }
int ftp_get(FTP_CON * con, const char *local_name, const char *remote_name) /* get file */ { int fh; int status; char buff[8192]; char str[256]; int count, i; long total = 0; DWORD start, stop; if (ftp_open_read(con, remote_name) >= 0) return con->err_no; if ((fh = open(local_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0644)) == -1) return FTP_FILE_ERROR; start = ss_millitime(); while ((count = ftp_receive(con->data, buff, sizeof(buff))) > 0) { total += write(fh, buff, count); i = 0; if (ftp_debug_func != NULL) { printf("%c\r", bars[(i++) % 4]); fflush(stdout); } } close(fh); stop = ss_millitime(); status = ftp_close(con); if (ftp_debug_func != NULL) { sprintf(str, "%ld bytes received in %1.2f seconds (%1.2lf kB/sec).", total, (stop - start) / 1000.0, total / 1024.0 / ((stop - start) / 1000.0)); ftp_debug_func(str); } return status; }
int ftp_put(FTP_CON * con, const char *local_name, const char *remote_name) /* put file */ { int fh; int status; char buff[8193]; char str[256]; int count, i = 0; long total = 0; DWORD start, stop; if (ftp_open_write(con, remote_name) >= 0) return con->err_no; if ((fh = open(local_name, O_BINARY)) == -1) return FTP_FILE_ERROR; start = ss_millitime(); while ((count = read(fh, buff, 8192)) > 0) { total += ftp_send(con->data, buff, count); if (ftp_debug_func != NULL) { printf("%c\r", bars[(i++) % 4]); fflush(stdout); } } close(fh); stop = ss_millitime(); status = ftp_close(con); if (ftp_debug_func != NULL) { sprintf(str, "%ld bytes sent in %1.2f seconds (%1.2lf kB/sec).", total, (stop - start) / 1000.0, total / 1024.0 / ((stop - start) / 1000.0)); ftp_debug_func(str); } return status; }
main() { INT status, size, trans, run_number; char host_name[256], str[32]; INT event_id, request_id; DWORD last_time; BOOL via_callback; /* get parameters */ printf("ID of event to request: "); ss_gets(str, 32); event_id = atoi(str); printf("Host to connect: "); ss_gets(host_name, 256); printf("Get all events (0/1): "); ss_gets(str, 32); all_flag = atoi(str); printf("Receive via callback ([y]/n): "); ss_gets(str, 32); via_callback = str[0] != 'n'; /* connect to experiment */ status = cm_connect_experiment(host_name, "", all_flag ? "Power Consumer" : "Consumer", NULL); if (status != CM_SUCCESS) return 1; /* open the "system" buffer, 1M size */ bm_open_buffer("SYSTEM", EVENT_BUFFER_SIZE, &hBufEvent); /* set the buffer cache size */ bm_set_cache_size(hBufEvent, 100000, 0); /* place a request for a specific event id */ bm_request_event(hBufEvent, (WORD) event_id, TRIGGER_ALL, all_flag ? GET_ALL : GET_SOME, &request_id, via_callback ? process_event : NULL); /* place a request for system messages */ cm_msg_register(process_message); /* place a request for transition notification */ cm_register_transition(TR_START, via_callback? transition : NULL); last_time = 0; do { if (via_callback) status = cm_yield(1000); else { /* receive event "manually" and call receive_event */ size = sizeof(event_buffer); status = bm_receive_event(hBufEvent, event_buffer, &size, ASYNC); if (status == BM_SUCCESS) process_event(hBufEvent, request_id, (EVENT_HEADER *) event_buffer, (void *) (((EVENT_HEADER *) event_buffer) + 1)); /* receive transitions "manually" */ if (cm_query_transition(&trans, &run_number, NULL)) transition(run_number, NULL); /* call yield once every 100 ms */ if (ss_millitime() - last_time > 100) { last_time = ss_millitime(); status = cm_yield(0); } } } while (status != RPC_SHUTDOWN && status != SS_ABORT); cm_disconnect_experiment(); return 1; }
void multi_read(EQUIPMENT * pequipment, int channel) { int i, status; DWORD actual_time; MULTI_INFO *m_info; HNDLE hDB; m_info = (MULTI_INFO *) pequipment->cd_info; cm_get_experiment_database(&hDB, NULL); if (channel == -1 || m_info->driver_input[channel]->flags & DF_MULTITHREAD) for (i = 0; i < m_info->num_channels_input; i++) { status = device_driver(m_info->driver_input[i], CMD_GET, i - m_info->channel_offset_input[i], &m_info->var_input[i]); if (status != FE_SUCCESS) m_info->var_input[i] = (float)ss_nan(); else m_info->var_input[i] = m_info->var_input[i] * m_info->factor_input[i] - m_info->offset_input[i]; } else { status = device_driver(m_info->driver_input[channel], CMD_GET, channel - m_info->channel_offset_input[channel], &m_info->var_input[channel]); if (status != FE_SUCCESS) m_info->var_input[channel] = (float)ss_nan(); else m_info->var_input[channel] = m_info->var_input[channel] * m_info->factor_input[channel] - m_info->offset_input[channel]; } /* check if significant change since last ODB update */ for (i = 0; i < m_info->num_channels_input; i++) if ((!ss_isnan(m_info->var_input[i]) && !ss_isnan(m_info->input_mirror[i]) && abs(m_info->var_input[i] - m_info->input_mirror[i]) > m_info->update_threshold[i]) || (ss_isnan(m_info->var_input[i]) && !ss_isnan(m_info->input_mirror[i])) || (!ss_isnan(m_info->var_input[i]) && ss_isnan(m_info->input_mirror[i]))) break; #ifdef DEBUG_THRESHOLDS if (i < m_info->num_channels_input) { printf("%d: %lf -> %lf, threshold %lf\n", i, m_info->var_input[i], m_info->input_mirror[i], m_info->update_threshold[i]); } #endif /* update if change is more than update_sensitivity or last update more than a minute ago */ actual_time = ss_millitime(); if (i < m_info->num_channels_input || actual_time - m_info->last_update > 60000) { m_info->last_update = actual_time; for (i = 0; i < m_info->num_channels_input; i++) m_info->input_mirror[i] = m_info->var_input[i]; db_set_data(hDB, m_info->hKeyInput, m_info->var_input, m_info->num_channels_input * sizeof(float), m_info->num_channels_input, TID_FLOAT); pequipment->odb_out++; } }