Beispiel #1
0
ShmACli::ShmACli(char const *mountpt, int mode) throw (Error) {
  sdbx("ShmACli: %s %x",mountpt,mode);
  key_t key = ftok(mountpt,'M');
  if (key<0)
    throw SysErr("ShmCli","ftok");
  shmid = shmget(key,0,mode);
  if (shmid<0) {
    if (errno == ENOENT || errno==EIDRM)
      throw Error("ShmCli","No shared memory found, start server first");
    throw Error("ShmCli","shmget");
  }
  struct shmid_ds buf;
  if (shmctl(shmid, IPC_STAT, &buf))
    throw Error("ShmCli","shmctl");
  size_ = buf.shm_segsz;
  data_ = shmat(shmid, 0, 0);
  if (data_<0)
    throw Error("ShmCli","shmat");
}
Beispiel #2
0
  bool exec(char *cmdwargs, Cmap *cmds, bool coloncomma) throw(Error) {
    letsquit = false;
    while (*cmdwargs==' ')
      cmdwargs++; // skip initial spaces
    if (*cmdwargs == '!' && shell_enabled) {
      // shell command
      if (system(cmdwargs+1))
	throw SysErr("Cmdr","Shell failed");
      return false;
    }
    char **args = tokenize(cmdwargs, coloncomma);
    if (!args[0])
      return 0; // null command, no action
    if (!strcmp(args[0],"?")) {
      help(argc,args+1,cmds);
      return false;
    }
    Cmap *c = parse(args[0], cmds);
    if (argc<c->argc_min || argc>c->argc_max)
      throw Usage(c->cmd,c->usage);
    (c->foo)(argc,args+1);
    return letsquit;
  }
int main(int argc, char **argv) {
  int thresh_digi=0;
  float thresh_std=3;
  char *noisefn=0;
  int length_sams=FREQKHZ*3;
  int asym_sams = 10;
  int blank_sams = 20;
  int ahead_sams = 5;
  raw_t rail1_digi=0;
  raw_t rail2_digi=4095;
  int period_sams=0; // meaning: don't use
  int delay_sams=0;
  int forcepeg_sams=0;
  char c;
  while ((c=getopt(argc,argv,"t:x:n:l:a:b:A:r:p:d:f:"))!=-1) {
    switch (c) {
    case 't': thresh_digi=atoi(optarg); thresh_std=0; break;
    case 'x': thresh_std=atof(optarg); thresh_digi=0; break;
    case 'n': noisefn=optarg; break;
    case 'l': length_sams=int(FREQKHZ*atof(optarg)); break;
    case 'a': asym_sams=int(FREQKHZ*atof(optarg)); break;
    case 'b': blank_sams=int(FREQKHZ*atof(optarg)); break;
    case 'A': ahead_sams=int(FREQKHZ*atof(optarg)); break;
    case 'r': { rail1_digi=atoi(optarg); char *x=strchr(optarg,','); rail2_digi=x?atoi(x+1):rail1_digi; } break;
    case 'p': period_sams=int(FREQKHZ*atof(optarg)); break;
    case 'd': delay_sams=int(FREQKHZ*atof(optarg)); break;
    case 'f': forcepeg_sams=int(FREQKHZ*atof(optarg)); break;
    default: usage();
    }
  }

  if (thresh_digi!=0 && (thresh_std!=0 || noisefn!=0))
    usage();
  if (optind<argc)
    usage();


  FILE *in = stdin; //fopen("/data/dw/orlando-010812/post13-21-1.raw.60hz.raw","rb");
  FILE *out= stdout; //fopen("/home/wagenaar/tmp/paf.raw","wb");



  int fragsams=4*BLOCKSIZE;
  if (thresh_std!=0 && noisefn==0) 
    fragsams=FREQKHZ*2000;
  int log2fragsams=int(ceil(log(fragsams+0.)/log(2.)));
  fragsams=1<<log2fragsams;
  try {
    CyclBuf<Sample> source(log2fragsams);
    CyclBuf<Sample> dest(LOG2BLOCKSIZE4);
    timeref_t filledto = 0;
    timeref_t basesubto = 0;
    timeref_t processedto = 0;
    timeref_t savedto = 0;
    timeref_t nextpeg = delay_sams ? delay_sams : INFTY;
    float thresh[NCHANS];
    
    Sample basesub; basesub.settoint(0); basesub.setelctoint(-2048);
    
    if (thresh_std!=0) {
      NoiseLevels noise;
      if (noisefn) {
	noise.load(noisefn);
      } else {
	// go get noise estimate
	int n=fread(&source[0],sizeof(Sample),fragsams,in);
	if (n!=fragsams)
	  throw SysErr("posthocartifilt","Cannot read enough data for noise estimte");
	filledto = n;
	noise.train(&source[0],n);
	noise.makeready();
      }
      for (int hw=0; hw<NCHANS; hw++) {
	thresh[hw] = noise[hw]*thresh_std;
	basesub[hw]= (raw_t)(-noise.mean(hw));
      }
      
    } else {
      for (int hw=0; hw<NCHANS; hw++)
	thresh[hw] = thresh_digi;
    }

//    for (int hw=0; hw<NCHANS; hw++)
//	fprintf(stderr, "hw=%2i thresh=%5.1f\n",hw,thresh[hw]);

    LocalFit *fitters[NCHANS];
    LF_Source *sources[NCHANS];
    LF_Dest *dests[NCHANS];
    for (int hw=0; hw<NCHANS; hw++) {
      sources[hw] = new LF_Source(hw,source);
      dests[hw] = new LF_Dest(hw,dest);
      fitters[hw] = new LocalFit(*sources[hw],*dests[hw], 0,
				 int(thresh[hw]), length_sams,
				 blank_sams, ahead_sams, asym_sams);
      fitters[hw]->setrail(rail1_digi+basesub[hw],rail2_digi+basesub[hw]);
    }

    bool at_eof=false;
    bool go_on=true;
    while (go_on) {
      go_on=false;

      // -- save some stuff
      timeref_t mightsaveto = processedto & ~BLOCKMASK;
      while (savedto<mightsaveto) {
	go_on=true;
	fwrite(&dest[savedto],sizeof(Sample),BLOCKSIZE,out);
	savedto+=BLOCKSIZE;
      }

      // -- subtract baseline
      while (basesubto < filledto) {
	source[basesubto++]+=basesub;
//	  Sample &s(source[basesubto++]);
//	  for (int j=0; j<NCHANS; j++)
//	    s[j]-=basesub;
      }

      // -- subtract artifacts
      timeref_t mightprocessto = filledto - forcepeg_sams - 3*length_sams -2;
      if (mightprocessto > savedto + 4*BLOCKSIZE)
	mightprocessto = savedto + 4*BLOCKSIZE;
      while (processedto < mightprocessto) {
	go_on=true;
	if (nextpeg < mightprocessto) {
	  for (timeref_t tt=processedto; tt<nextpeg+forcepeg_sams; tt++)
	    for (int hw=NCHANS; hw<TOTALCHANS; hw++)
	      dest[tt][hw]=source[tt][hw];
	  //	  fprintf(stderr,"Forcepeg: %.5f %.5f\n",nextpeg/25000.,(nextpeg+forcepeg_sams)/25000.);
	  for (int hw=0; hw<NCHANS; hw++) 
	    if (fitters[hw]->forcepeg(nextpeg,nextpeg+forcepeg_sams) != nextpeg+forcepeg_sams)
	      throw Error("posthocartifilt",
			  "LocalFit doesn't like my data (while pegging)!");
	  processedto=nextpeg+forcepeg_sams;
	  nextpeg += period_sams;
	} else {
	  for (timeref_t tt=processedto; tt<mightprocessto; tt++)
	    for (int hw=NCHANS; hw<TOTALCHANS; hw++)
	      dest[tt][hw]=source[tt][hw];
	  for (int hw=0; hw<NCHANS; hw++)
	    if (fitters[hw]->process(mightprocessto) != mightprocessto)
	      throw Error("posthocartifilt", "LocalFit doesn't like my data!");
	  processedto=mightprocessto;
	}
      }

      // -- read input
      if (!at_eof) {
	int n=fread(&source[filledto],sizeof(Sample),BLOCKSIZE,in);
	if (n<0) 
	  throw SysErr("posthocartifilt","Cannot read from input");
	filledto+=n;
	if (n>0)
	  go_on=true;
	if (n!=BLOCKSIZE)
	  at_eof=true;
      }
    }


    // -- EOF!
    
    // let's process the last bit...
    timeref_t mightprocessto = filledto - 2*length_sams-1;
    for (timeref_t tt=processedto; tt<mightprocessto; tt++)
      for (int hw=NCHANS; hw<TOTALCHANS; hw++)
	dest[tt][hw]=source[tt][hw];
    for (int hw=0; hw<NCHANS; hw++) 
      if (fitters[hw]->process(mightprocessto) != mightprocessto)
	throw Error("posthocartifilt", "LocalFit doesn't like my data!");

    // let's save last bit
    for (timeref_t t=savedto; t<processedto; t++)
      fwrite(&dest[t],sizeof(Sample),1,out);

    
    return 0;
  } catch (Error const &e) {
    e.report();
    exit(3);
  }
}
Beispiel #4
0
int deliver_message (struct output_handler *handler, char *msg)
{
	struct timeval timeout;
	int todo, retry, msglen, s;
	fd_set fds;

	/* Initialize variables */
	FD_ZERO(&fds);
	todo = msglen = strlen(msg);
	retry = handler->retry;

	/* Try to send the message with reasonable effort */
	while (retry > 0)
	{
		/* If not connected, connect */
		if (handler->state == os_disconnected)
		{
			handler->connect(handler);
		}

		/* If valid filedescriptor, listen for signals */
		if (handler->fd != -1)
		{
			/* Add the handler's fd to the watchlist */
			FD_SET(handler->fd, &fds);
		}
		
		/* Set maximum timeout */
		timeout.tv_sec  = 30;
		timeout.tv_usec = 0;

		/* Perform select */
		if (handler->state == os_ready || handler->state == os_sending)
			s = select(handler->fd + 1, NULL, &fds, NULL, &timeout);
		else
		{
			timeout.tv_sec = 5;
			s = select(0, NULL, NULL, NULL, &timeout);
		}
		
		/* Determine select status */
		switch(s)
		{
			/* Error occured */
			case -1:
				SysErr(errno, "While doing select for message sending");
				return 0;

			/* Timeout occured */
			case 0:
				Log2(warning, "Output handling timeout", "[output.c]{deliver_message}");
				retry--;
				break;

			/* Activity on filedescriptor */
			default:
				Log2(warning, "Output is active", "[output.c]{deliver_message}");
				switch (handler->state)
				{
					/* Try to connect */
					case os_disconnected:
						CustomLog(__FILE__, __LINE__, warning, "Trying to connect output channel!");
						handler->connect(handler);
						break;
						
					/* Check if connection is established */
					case os_connecting:
						CustomLog(__FILE__, __LINE__, warning, "Still connecting output channel!");
						handler->connect(handler);
						break;
					
					/* Try to send the message */
					case os_ready:
					case os_sending:
						retry = 3;
						CustomLog(__FILE__, __LINE__, warning, "Trying to send message(msg=%p, msglen=%d, cont=%p)!", msg, msglen, todo);
						if (FD_ISSET(handler->fd, &fds) && handler->write(handler, msg, msglen, &todo))
						{
							/* Write succesfully completed */
							return 1;
						}
						break;
						
					/* Do a reset */
					case os_error:
						CustomLog(__FILE__, __LINE__, warning, "Resetting output channel!");
						break;
						
					/* Impossible */
					default:
						Log2(impossible, "IMPOSSIBLE state", "[output.c]{deliver_message}");
						return 0;
				}

				/* Continue */
				break;
		}
		
		/* If we're in error state, disconnect */
		if (handler->state == os_error)
		{
			SysErr(handler->err, "[output.c]{deliver_message} Handler is in an error state");
			handler->disconnect(handler);
			retry --;
		}
	}

	/* Out of retry's */
	Log(error, "Maximum amount of retry's reached");
	return 0;
}