int et_event_CODAswap(et_event *pe) { int length, same_endian=1; length = pe->length/sizeof(int); /* DEPRECATED: Swapping is now done in evio library. */ return ET_ERROR; /* event's data written on diff endian machine as this host? */ if (pe->byteorder != 0x04030201) { same_endian = 0; } /* swap the data */ if (et_CODAswap((int *)pe->pdata, NULL, length, same_endian) != ET_OK) { return ET_ERROR; } /* must also swap the "endian" element of the header * since it's byte order mirrors that of the data */ pe->byteorder = ET_SWAP32(pe->byteorder); return ET_OK; }
int et_event_setendian(et_event *pe, int endian) { int err, myendian; if ( (err = etNetLocalByteOrder(&myendian)) != ET_OK) { return err; } if ((endian != ET_ENDIAN_BIG) && (endian != ET_ENDIAN_LITTLE) && (endian != ET_ENDIAN_LOCAL) && (endian != ET_ENDIAN_NOTLOCAL) && (endian != ET_ENDIAN_SWITCH)) { return ET_ERROR; } if ((endian == ET_ENDIAN_BIG) || (endian == ET_ENDIAN_LITTLE)) { pe->byteorder = (myendian == endian) ? 0x04030201 : 0x01020304; } else if (endian == ET_ENDIAN_LOCAL) { pe->byteorder = 0x04030201; } else if (endian == ET_ENDIAN_NOTLOCAL) { pe->byteorder = 0x01020304; } else { pe->byteorder = ET_SWAP32(pe->byteorder); } return ET_OK; }
/* * Class: org_jlab_coda_et_EtJniAccess * Method: putEvents * Signature: (JI[Lorg/jlab/coda/et/EtEvent;II)V */ JNIEXPORT void JNICALL Java_org_jlab_coda_et_EtJniAccess_putEvents (JNIEnv *env, jobject thisObj, jlong etId, jint attId, jobjectArray events, jint length) { int i, j, status, place; et_event *pe[length]; jclass clazz = NULL; jobject event; jboolean isCopy; jint *controlElements; jintArray controlInts; et_id *etid = (et_id *) etId; if (debug) printf("putEvents (native) : put 'em back\n"); /* create array of (pointers to) events */ for (i=0; i<length; i++) { /* get event object from Java array of events */ event = (*env)->GetObjectArrayElement(env, events, i); /* find ptr to event in C world */ place = (*env)->GetIntField(env, event, fid[0]); pe[i] = ET_P2EVENT(etid, place); /* set fields in event struct that may have been modified in Java */ pe[i]->priority = (*env)->CallIntMethod(env, event, getPriorityVal); pe[i]->length = (uint64_t) ((*env)->GetIntField(env, event, fid[1])); pe[i]->datastatus = (*env)->CallIntMethod(env, event, getDataStatusVal); /* If we're on a little endian machine, ints will be swapped as they go through the jni interface. We don't want this for the int designating the byte order, so swap it back again to compensate. */ pe[i]->byteorder = (*env)->GetIntField(env, event, fid[2]); if (localByteOrder == ET_ENDIAN_LITTLE) { pe[i]->byteorder = ET_SWAP32(pe[i]->byteorder); } /* set control ints */ controlInts = (*env)->GetObjectField(env, event, fid[3]); controlElements = (*env)->GetIntArrayElements(env, controlInts, &isCopy); for (j=0; j<ET_STATION_SELECT_INTS; j++) { pe[i]->control[j] = controlElements[j]; } /* clean up */ if (isCopy == JNI_TRUE) { (*env)->ReleaseIntArrayElements(env, controlInts, controlElements, 0); } (*env)->DeleteLocalRef(env, event); } /* put back array of events (pointers) */ status = et_events_put((et_sys_id)etId, (et_att_id)attId, pe, length); if (status != ET_OK) { if (status == ET_ERROR_DEAD) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtDeadException"); } else if (status == ET_ERROR_CLOSED) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtClosedException"); } else { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtException"); } (*env)->ThrowNew(env, clazz, "putEvents (native): cannot put events"); return; } return; }
/* * Class: org_jlab_coda_et_EtJniAccess * Method: newEvents * Signature: (JIIIIIII)[Lorg/jlab/coda/et/EtEventImpl; */ JNIEXPORT jobjectArray JNICALL Java_org_jlab_coda_et_EtJniAccess_newEvents (JNIEnv *env, jobject thisObj, jlong etId, jint attId, jint mode, jint sec, jint nsec, jint count, jint size, jint group) { int i, numread, status, biteOrder; void *data; et_event *pe[count]; jclass clazz; jobjectArray eventArray; jobject event, byteBuf; /* translate timeout */ struct timespec deltaTime; deltaTime.tv_sec = sec; deltaTime.tv_nsec = nsec; if (debug) printf("newEvents (native) : will attempt to get new events\n"); /* reading array of up to "count" events */ status = et_events_new_group((et_sys_id)etId, (et_att_id)attId, pe, mode, &deltaTime, (size_t)size, count, group, &numread); if (status != ET_OK) { if (status == ET_ERROR_DEAD) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtDeadException"); } else if (status == ET_ERROR_WAKEUP) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtWakeUpException"); } else if (status == ET_ERROR_TIMEOUT) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtTimeoutException"); } else if (status == ET_ERROR_CLOSED) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtClosedException"); } else if (status == ET_ERROR_BUSY) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtBusyException"); } else if (status == ET_ERROR_EMPTY) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtEmptyException"); } else { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtException"); } (*env)->ThrowNew(env, clazz, "newEvents (native): cannot get new events"); return NULL; } /* create array of EventImpl objects */ eventArray = (*env)->NewObjectArray(env, numread, eventImplClass, NULL); /* fill array */ for (i=0; i < numread; i++) { /* If we're on a little endian machine, int args will be swapped as they go through the jni interface. We don't want this for the int designating the byte order, so swap it here to compensate. */ biteOrder = pe[i]->byteorder; if (localByteOrder == ET_ENDIAN_LITTLE) { biteOrder = ET_SWAP32(biteOrder); } /* wrap data pointer in ByteBuffer object */ et_event_getdata(pe[i], &data); byteBuf = (*env)->NewDirectByteBuffer(env, data, (jlong) pe[i]->memsize); /* create event object */ event = (*env)->NewObject(env, eventImplClass, constrMethodId2, /* constructor args ... */ (jint)pe[i]->memsize, (jint)pe[i]->memsize, (jint)pe[i]->place, (jint)pe[i]->owner, (jint)pe[i]->modify, (jint)pe[i]->length, (jint)pe[i]->priority, (jint)biteOrder, (jboolean)pe[i]->temp, byteBuf); /* put event in array */ (*env)->SetObjectArrayElement(env, eventArray, i, event); /* get rid of uneeded references - good if numread is big */ (*env)->DeleteLocalRef(env, event); (*env)->DeleteLocalRef(env, byteBuf); } if (debug) printf("newEvents (native) : filled array!\n"); /* return the array */ return eventArray; }
/* * Class: org_jlab_coda_et_EtJniAccess * Method: getEvents * Signature: (JIIIIII)[Lorg/jlab/coda/et/EtEvent; */ JNIEXPORT jobjectArray JNICALL Java_org_jlab_coda_et_EtJniAccess_getEvents (JNIEnv *env , jobject thisObj, jlong etId, jint attId, jint mode, jint sec, jint nsec, jint count) { int i, j, numread, status, biteOrder; void *data; et_event *pe[count]; jclass clazz; jboolean isCopy; jint* intArrayElems; jintArray controlInts; jobjectArray eventArray; jobject event, byteBuf; /* translate timeout */ struct timespec deltaTime; deltaTime.tv_sec = sec; deltaTime.tv_nsec = nsec; if (debug) printf("getEvents (native) : will attempt to get events\n"); /* reading array of up to "count" events */ status = et_events_get((et_sys_id)etId, (et_att_id)attId, pe, mode, &deltaTime, count, &numread); if (status != ET_OK) { if (status == ET_ERROR_DEAD) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtDeadException"); (*env)->ThrowNew(env, clazz, "getEvents (native): ET_ERROR_DEAD"); } else if (status == ET_ERROR_WAKEUP) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtWakeUpException"); (*env)->ThrowNew(env, clazz, "getEvents (native): ET_ERROR_WAKEUP"); } else if (status == ET_ERROR_TIMEOUT) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtTimeoutException"); (*env)->ThrowNew(env, clazz, "getEvents (native): ET_ERROR_TIMEOUT"); } else if (status == ET_ERROR_CLOSED) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtClosedException"); (*env)->ThrowNew(env, clazz, "getEvents (native): ET_ERROR_CLOSED"); } else if (status == ET_ERROR_BUSY) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtBusyException"); (*env)->ThrowNew(env, clazz, "getEvents (native): ET_ERROR_BUSY"); } else if (status == ET_ERROR_EMPTY) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtEmptyException"); (*env)->ThrowNew(env, clazz, "getEvents (native): ET_ERROR_EMPTY"); } else if (status == ET_ERROR_READ) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtReadException"); (*env)->ThrowNew(env, clazz, "getEvents (native): ET_ERROR_READ"); } else if (status == ET_ERROR_WRITE) { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtWriteException"); (*env)->ThrowNew(env, clazz, "getEvents (native): ET_ERROR_WRITE"); } else { clazz = (*env)->FindClass(env, "org/jlab/coda/et/exception/EtException"); (*env)->ThrowNew(env, clazz, "getEvents (native): ET_ERROR_REMOTE"); } return NULL; } /* create array of EventImpl objects */ eventArray = (*env)->NewObjectArray(env, numread, eventImplClass, NULL); /* fill array */ for (i=0; i < numread; i++) { /*printf("getEvents (native) : data for event %d = %d\n", i, *((int *)pe[i]->pdata));*/ /* create control int array */ controlInts = (*env)->NewIntArray(env, ET_STATION_SELECT_INTS); intArrayElems = (*env)->GetIntArrayElements(env, controlInts, &isCopy); for (j=0; j < ET_STATION_SELECT_INTS; j++) { intArrayElems[j] = pe[i]->control[j]; } if (isCopy == JNI_TRUE) { (*env)->ReleaseIntArrayElements(env, controlInts, intArrayElems, 0); } /* If we're on a little endian machine, int args will be swapped as they go through the jni interface. We don't want this for the int designating the byte order, so swap it here to compensate. */ biteOrder = pe[i]->byteorder; if (localByteOrder == ET_ENDIAN_LITTLE) { biteOrder = ET_SWAP32(biteOrder); } /* wrap data pointer in ByteBuffer object */ et_event_getdata(pe[i], &data); byteBuf = (*env)->NewDirectByteBuffer(env, data, (jlong) pe[i]->memsize); /* create event object */ event = (*env)->NewObject(env, eventImplClass, constrMethodId3, /* constructor args ... */ (jint)pe[i]->memsize, (jint)pe[i]->memsize, (jint)pe[i]->datastatus, (jint)pe[i]->place, (jint)pe[i]->age, (jint)pe[i]->owner, (jint)pe[i]->modify, (jint)pe[i]->length, (jint)pe[i]->priority, (jint)biteOrder, controlInts, (jboolean)pe[i]->temp, byteBuf); /* put event in array */ (*env)->SetObjectArrayElement(env, eventArray, i, event); /* get rid of uneeded references - good if numread is big */ (*env)->DeleteLocalRef(env, event); (*env)->DeleteLocalRef(env, controlInts); (*env)->DeleteLocalRef(env, byteBuf); } if (debug) printf("getEvents (native) : filled array!\n"); /* return the array */ return eventArray; }
int main(int argc,char **argv) { int c, counter, etdead, mode, errflg=0, locality, tmparg; unsigned int newheartbt, oldheartbt=0; int port = ET_BROADCAST_PORT; extern char *optarg; extern int optind, opterr, optopt; uint64_t prev_out; struct timespec timeout, period; double tperiod, hbperiod; et_sys_id sys_id; et_id *id; et_openconfig openconfig; char hostname[ET_MAXHOSTNAMELEN]; char etname[ET_FILENAME_LENGTH]; char *tmp_etname=NULL, *tmp_hostname=NULL; char *pSharedMem, *ptr; et_mem etInfo; int i, swap = 0; /* defaults */ mode = ET_HOST_AS_LOCAL; period.tv_sec = 5; period.tv_nsec = 0; tperiod = period.tv_sec + (1.e-9)*period.tv_nsec; hbperiod = ET_BEAT_SEC + (1.e-9)*ET_BEAT_NSEC; /* decode command line options */ while ((c = getopt(argc, argv, "Hf:t:p:h:")) != EOF) { switch (c) { case 'f': if (strlen(optarg) >= ET_FILENAME_LENGTH) { fprintf(stderr, "%s: ET file name is too long\n", argv[0]); exit(-1); } strcpy(etname, optarg); tmp_etname = etname; break; case 't': tmparg = atoi(optarg); if (tmparg <= 0) { fprintf(stderr, "%s: argument for -t <time period (sec)> must be integer >0\n\n", argv[0]); errflg++; break; } period.tv_sec = tmparg; tperiod = period.tv_sec + (1.e-9)*period.tv_nsec; break; case 'p': tmparg = atoi(optarg); if ((tmparg <= 1024) || (tmparg > 65535)) { fprintf(stderr, "%s: argument for -p <port #> must be integer > 1024 and < 65536\n\n", argv[0]); errflg++; break; } port = tmparg; break; case 'h': if (strlen(optarg) >= ET_MAXHOSTNAMELEN) { fprintf(stderr, "host name is too long\n"); exit(-1); } strcpy(hostname, optarg); tmp_hostname = hostname; break; case 'H': errflg++; break; case '?': errflg++; } } for ( ; optind < argc; optind++) { errflg++; } /* Check the ET system name */ if (tmp_etname == NULL) { fprintf(stderr, "%s: No ET file name given and SESSION env variable not defined\n", argv[0]); exit(-1); } if (errflg) { printf("\nUsage: %s [-f <et_filename>] [-p <port#>] [-h <host>] [-t <time period (sec)>]\n\n", argv[0]); printf(" Monitors an ET system given by -f <et_filename> (default = /tmp/et_sys_<SESSION>)\n"); printf(" Uses port specified by -p <port> for broadcasting on local subnets\n"); printf(" Updates information every -t <time period> seconds (default = 5)\n"); printf(" Assumes host is local unless specified by -h <host>\n"); printf(" which can be: localhost, .local, .remote,\n"); printf(" anywhere, <host name>, or <host IP address>\n\n"); exit(2); } /* before we open things, find out if we're local or not */ if (et_look(&sys_id, etname) != ET_OK) { printf("%s: et_attach problems\n", argv[0]); exit(1); } id = (et_id *) sys_id; ptr = (char *)id->pmap; etInfo.byteOrder = *((uint32_t *)ptr); ptr += sizeof(uint32_t); if (etInfo.byteOrder != 0x01020304) { swap = 1; } etInfo.systemType = *((uint32_t *)ptr); ptr += sizeof(uint32_t); etInfo.majorVersion = *((uint32_t *)ptr); ptr += sizeof(uint32_t); etInfo.minorVersion = *((uint32_t *)ptr); ptr += sizeof(uint32_t); etInfo.headerByteSize = *((uint32_t *)ptr); ptr += sizeof(uint32_t); etInfo.eventByteSize = *((uint64_t *)ptr); ptr += sizeof(uint64_t); etInfo.headerPosition = *((uint64_t *)ptr); ptr += sizeof(uint64_t); etInfo.dataPosition = *((uint64_t *)ptr); ptr += sizeof(uint64_t); etInfo.totalSize = *((uint64_t *)ptr); ptr += sizeof(uint64_t); etInfo.usedSize = *((uint64_t *)ptr); if (swap) { etInfo.systemType = ET_SWAP32(etInfo.systemType); etInfo.majorVersion = ET_SWAP32(etInfo.majorVersion); etInfo.minorVersion = ET_SWAP32(etInfo.minorVersion); etInfo.headerByteSize = ET_SWAP32(etInfo.headerByteSize); etInfo.eventByteSize = ET_SWAP64(etInfo.eventByteSize); etInfo.headerPosition = ET_SWAP64(etInfo.headerPosition); etInfo.dataPosition = ET_SWAP64(etInfo.dataPosition); etInfo.totalSize = ET_SWAP64(etInfo.totalSize); etInfo.usedSize = ET_SWAP64(etInfo.usedSize); } /* print out data */ ptr = (char *)id->pmap; //ptr = (char *)id->pmap + etInfo.dataPosition; for (i=0; i < (etInfo.usedSize + ET_INITIAL_SHARED_MEM_DATA_BYTES)/4 ; i++) { printf("data[%d] = %d\n",i, ET_SWAP32(*((uint32_t *)ptr) )); ptr += sizeof(uint32_t); } et_unlook(sys_id); return 0; }
/****************************************************** * This is another version of CODAswap and allows * specifying separate source and destination buffers * as well as a flag telling whether the data was * written from a same or different endian machine * (i.e. do we swap bank info or not). In the first * case, set same_endian = 1, else 0. * * This function is deprecated. Use the swapping * function contained in the evio library. ******************************************************/ int et_CODAswap(int *src, int *dest, int nints, int same_endian) { int i, j, blen, dtype; int *lp, *lpd; short *sp, *spd; /* DEPRECATED: This function does NOT do a complete job anymore. */ return ET_ERROR; /* int pointers */ if (dest == NULL) { dest = src; } i = 0; while (i < nints) { lp = &src[i]; lpd = &dest[i]; if (same_endian) { blen = src[i] - 1; dtype = ((src[i+1])&0xff00)>>8; } else { blen = ET_SWAP32(src[i]) - 1; dtype = ((ET_SWAP32(src[i+1]))&0xff00)>>8; } *lpd++ = ET_SWAP32(*lp); /* Swap length */ lp++; *lpd = ET_SWAP32(*lp); /* Swap bank header */ i += 2; if(dtype != DT_BANK) { switch(dtswap[dtype]) { case 0: /* No swap */ i += blen; break; case 1: /* short swap */ sp = (short *)&src[i]; spd = (short *)&dest[i]; for(j=0; j<(blen<<1); j++, sp++) { *spd++ = ET_SWAP16(*sp); } i += blen; break; case 2: /* int swap */ lp = &src[i]; lpd = &dest[i]; for(j=0; j<blen; j++, lp++) { *lpd++ = ET_SWAP32(*lp); } i += blen; break; case 3: /* double swap */ lp = &src[i]; lpd = &dest[i]; for(j=0; j<blen; j++, lp++) { *lpd++ = ET_SWAP32(*lp); } i += blen; break; default: /* No swap */ i += blen; } } }