Ejemplo n.º 1
0
// Accept MIDI bytes then setup/remove synths as requested.
void midirdy () {
	byte buf[40];
	int bytes = read(0, buf, 40);
	if (-1 == bytes) err(1, "read");
	if (!bytes) exit(0);
	for (int i=0; i<bytes; i++) {
		struct mm_msg m;
		switch (mm_inject(buf[i], &m)) {
			case 2: exit(1);
			case 1: continue;
			case 0: {
				double freq = mm_notefreq(m.arg1);
				int id = makeid(m.arg1, m.chan);
				// x/3 isn't special, it just gives decent results.
				if (m.type == MM_NOTEON) noteon(freq, m.arg2/3, id);
				if (m.type == MM_NOTEOFF) noteoff(id); }}}}
Ejemplo n.º 2
0
/** Inspect a host's metering data to determine its current status
  * and problems, then write it to disk.
  * \param host The host to inspect.
  */
void watchthread_handle_host (host *host) {
    int problemcount = 0;
    double totalbadness = 0.0;
    time_t tnow = time (NULL);
    meter *m = host->first;
    meterwatch *w;
    watchadjust *adj = NULL;
    watchtrigger maxtrigger = WATCH_NONE;
    char label[16];
    char uuidstr[40];
    
    pthread_rwlock_wrlock (&host->lock);

    /* We'll store the status information as a meter itself */
    meterid_t mid_status = makeid ("status",MTYPE_STR,0);
    meter *m_status = host_get_meter (host, mid_status);
    fstring ostatus = meter_get_str (m_status, 0);
    meter_setcount (m_status, 0);
    if (ostatus.str[0] == 0) strcpy (ostatus.str, "UNSET");
    
    /* If the data is stale, don't add to badness, just set the status. */
    if ((tnow - host->lastmodified) > 80) {
        if (strcmp (ostatus.str, "STALE") != 0) {
            uuid2str (host->uuid, uuidstr);
            log_info ("Status change host <%s> %s -> STALE", uuidstr, ostatus.str);
            tenant_set_notification (host->tenant, true, "STALE", host->uuid);
        }
        meter_set_str (m_status, 0, "STALE");
    }
    else {

        /* Figure out badness for each meter, and add to host */
        while (m) {
            m->badness = 0.0;
            int handled = 0;
            
            /* Get host-level adjustments in place */
            adj = adjustlist_find (&host->adjust, m->id);
            
            /* First go over the tenant-defined watchers */
            pthread_mutex_lock (&host->tenant->watch.mutex);
            w = host->tenant->watch.first;
            while (w) {
                if ((w->id & MMASK_NAME) == (m->id & MMASK_NAME)) {
                    m->badness += calculate_badness (m, w, adj, &maxtrigger);
                    handled = 1;
                }
                w = w->next;
            }
            pthread_mutex_unlock (&host->tenant->watch.mutex);
       
            /* If the tenant didn't have anything suitable, go over the
               global watchlist */
            if (! handled) {
                pthread_mutex_lock (&APP.watch.mutex);
                w = APP.watch.first;

                while (w) {
                    if ((w->id & MMASK_NAME) == (m->id & MMASK_NAME)) {
                        m->badness += calculate_badness (m, w, adj, 
                                                         &maxtrigger);
                        handled = 1;
                    }
                    w = w->next;
                }
                pthread_mutex_unlock (&APP.watch.mutex);
            }
      
            if (m->badness) problemcount++;
            totalbadness += m->badness;
            m = m->next;
        }
    
        /* Don't raise a CRIT alert on a WARN condition */
        switch (maxtrigger) {
            case WATCH_NONE:
                totalbadness = 0.0;
                break;
        
            case WATCH_WARN:
                if (host->badness > 50.0) totalbadness = 0.0;
                break;
        
            case WATCH_ALERT:
                if (host->badness > 90.0) totalbadness = 0.0;
                break;
        
            case WATCH_CRIT:
                if (host->badness > 150.0) totalbadness = 0.0;
                break;
        }
    
        host->badness += totalbadness;
    
        /* Put up the problems as a meter as well */
        meterid_t mid_problems = makeid ("problems",MTYPE_STR,0);
        meter *m_problems = host_get_meter (host, mid_problems);
    
        /* While we're looking at it, consider the current badness, if there
           are no problems, it should be going down. */
        if (! problemcount) {
            if (host->badness > 100.0) host->badness = host->badness/2.0;
            else if (host->badness > 1.0) host->badness = host->badness *0.75;
            else host->badness = 0.0;
            meter_set_empty_array (m_problems);
        }
        else {
            /* If we reached the top, current level may still be out of
               its league, so allow it to decay slowly */
            if (totalbadness == 0.0) host->badness *= 0.9;
        
            /* Fill in the problem array */
            int i=0;
            meter_setcount (m_problems, problemcount);
            m = host->first;
        
            while (m && (i<16)) {
                if (m->badness > 0.00) {
                    id2str (m->id, label);
                    meter_set_str (m_problems, i++, label);
                }
                m = m->next;
            }
        }
    
        meterid_t mid_badness = makeid ("badness",MTYPE_FRAC,0);
        meter *m_badness = host_get_meter (host, mid_badness);
        meter_setcount (m_badness, 0);
        meter_set_frac (m_badness, 0, host->badness);
    
        const char *nstatus = "UNSET";
    
        /* Convert badness to a status text */
        if (host->badness < 30.0) nstatus = "OK";
        else if (host->badness < 80.0) nstatus = "WARN";
        else if (host->badness < 120.0) nstatus = "ALERT";
        else nstatus = "CRIT";
    
        if (strcmp (nstatus, ostatus.str) != 0) {
            uuid2str (host->uuid, uuidstr);
            log_info ("Status change host <%s> %s -> %s", uuidstr, ostatus.str, nstatus);
            bool isproblem = (host->badness>= 80.0);
            tenant_set_notification (host->tenant, isproblem, nstatus, host->uuid);
        }
    
        meter_set_str (m_status, 0, nstatus);
    }
    
    /* Write to db */
    if (db_open (APP.writedb, host->tenant->uuid, NULL)) {
        db_save_record (APP.writedb, tnow, host);
        db_close (APP.writedb);
    }
    
    pthread_rwlock_unlock (&host->lock);
    
    /* for tallying the summaries we only need read access */

    if (! host->tenant) return;
    
    pthread_rwlock_rdlock (&host->lock);
    m = host->first;
    while (m) {
        summaryinfo_add_meterdata (&host->tenant->summ, m->id, &m->d);
        m = m->next;
    }
    pthread_rwlock_unlock (&host->lock);
}
Ejemplo n.º 3
0
int main()
{
  struct LMF_ccs_eventRecord *pER = NULL;
  struct LMF_ccs_encodingHeader *pEncoH = NULL;
  int index = 0, inputData = 0;
  i8 description[7][charNum] = { {"rsector ID"},
  {"module ID"},
  {"submodule ID"},
  {"crystal ID"},
  {"layer ID"},
  {"gantry's angular position"},
  {"gantry's axial position"}
  };
  u16 limits[7] = { 0 };
  int numericalValue[7] = { 0 };
  i8 input[charNum];
  calculOfEventPosition resultOfCalculOfEventPosition;
  u16 errFlag;

  /* Initialization of the LMF_ccs_eventRecord structure and the LMF_ccs_encodingHeader structure
     We have chose an "one ring scanner" with 8 rsectors.
     Each rsector has 3 rows of modules tangentially and only one column of modules axially
     Each module is divided in 4 columns of submodules and 1 row tangentially
     Finally each submodule is divided in a matrix of 8 rows of 8 columns of crystals
     We have defined 2 layers radially of each crystal */

  /* fill in the LMF_ccs_encodingHeader structure */
  if ((pEncoH =
       (struct LMF_ccs_encodingHeader *)
       malloc(sizeof(struct LMF_ccs_encodingHeader))) == NULL)
    printf
	("\n*** ERROR : in generateEncoH.c : impossible to do : malloc()\n");
  pEncoH = generateEncoH(1);

  if ((pER =
       (struct LMF_ccs_eventRecord *)
       malloc(sizeof(struct LMF_ccs_eventRecord))) == NULL)
    printf("\n***ERROR : in generateER.c : impossible to do : malloc()\n");
  if ((pER->crystalIDs = malloc(sizeof(u16))) == NULL)
    printf("\n***ERROR : in generateER.c : impossible to do : malloc()\n");

  limits[0] = (u16) pEncoH->scannerTopology.totalNumberOfRsectors;
  limits[1] = (u16) pEncoH->scannerTopology.totalNumberOfModules;
  limits[2] = (u16) pEncoH->scannerTopology.totalNumberOfSubmodules;
  limits[3] = (u16) pEncoH->scannerTopology.totalNumberOfCrystals;
  limits[4] = (u16) pEncoH->scannerTopology.totalNumberOfLayers;
  limits[5] = 256;
  limits[6] = 256;

  /* read file.cch and fill in structures LMF_cch */
  if (LMFcchReader(""))
    exit(EXIT_FAILURE);

  /* the user must choose:
     -> the rsector ID,
     -> the module ID,
     -> the submodule ID,
     -> the crystal ID,
     -> the layer ID,
     -> the gantry's angular position,
     -> the gantry's axial position.
   */

  for (index = 0; index < 7; index++) {
    initialize(input);
    printf("Choose a %s (number between 0 and %d):", description[index],
	   (limits[index] - 1));
    if (*gets(input) == '\0')
      numericalValue[index] = 0;
    else {
      inputData = 0;
      inputData = atoi(input);
      if ((inputData >= 0 && inputData < limits[index]) != 1)
	numericalValue[index] = 0;
      else
	numericalValue[index] = inputData;
    }
  }
  pER->gantryAngularPos = (u16) numericalValue[5];
  pER->gantryAxialPos = (u16) numericalValue[6];

  /* with the rsector ID, the module ID, the submodule ID, the crystal ID and the layer ID,
     the makeid function find the event ID */
  pER->crystalIDs[0] =
      makeid(numericalValue[0], numericalValue[1], numericalValue[2],
	     numericalValue[3], numericalValue[4], pEncoH, &errFlag);

  /* calculation of the event position in the 3D laboratory (x,y,z) system */
  resultOfCalculOfEventPosition = locateEventInLaboratory(pEncoH, pER, 0);

  printf("%s: %d\t%s: %d\t%s: %d\t%s: %d\t%s: %d\t%s: %d\t%s: %d\n",
	 description[0], numericalValue[0],
	 description[1], numericalValue[1],
	 description[2], numericalValue[2],
	 description[3], numericalValue[3],
	 description[4], numericalValue[4],
	 description[5], numericalValue[5],
	 description[6], numericalValue[6]);
  printf("x: %f\ty: %f\tz: %f\n",
	 resultOfCalculOfEventPosition.eventInLaboratory3DPosition.radial,
	 resultOfCalculOfEventPosition.eventInLaboratory3DPosition.
	 tangential,
	 resultOfCalculOfEventPosition.eventInLaboratory3DPosition.axial);

  /* frees the allocated memory by a previous call to malloc(), calloc() or realloc() */
  LMFcchReaderDestructor();
  free(pER->crystalIDs);
  free(pER);
  free(pEncoH);

  return (EXIT_SUCCESS);
}