Ejemplo n.º 1
0
int
codaPrestart()
{
  MYSQL *dbsock;
  char tmpp[1000];

  objClass object = localobject;

  MONp monp = (MONp) object->privated;

  printf("INFO: Prestarting\n");

  monp->nend = 1;

  /* connect to database */
  dbsock = dbConnect(getenv("MYSQL_HOST"), expid);

  /* Get the run number */
  sprintf(tmpp,"SELECT runNumber FROM sessions WHERE name='%s'",session);
  if(dbGetInt(dbsock,tmpp,&(object->runNumber))==MON_ERROR) return(MON_ERROR);

  /* DO WE NEED TO GET runType AS WELL ??? */

  /* disconnect from database */
  dbDisconnect(dbsock);

  printf("INFO: prestarting,run %d, type %d\n",
    object->runNumber, object->runType);

  PrestartCount = 0;
  object->nevents = 0;
  object->nlongs = 0;

  codaUpdateStatus("paused");

  return(MON_OK);
}
Ejemplo n.º 2
0
int
main(int argc, char *argv[])
{
  struct hostent *hptr;
 
  TREQUEST myRequest; /* request to send to server */ 
  struct sockaddr_in serverAddr; /* server's socket address */ 
  char replyBuf[REPLY_MSG_SIZE*100]; /* buffer for reply */ 
  char reply; /* if TRUE, expect reply back */ 
  int sockAddrSize; /* size of socket address structure */ 
  int sFd; /* socket file descriptor */ 
  int mlen; /* length of message */
  int rBytes;
  int i, portnum = 0;
  char hostname[128];
  MYSQL *dbsock;
  char tmp[1000], temp[100];
  char *ch;

  /* make sure all arguments are there */
  if (argc<3) {
    printf("Usage: tcpClient <target name> [command]\n");
    exit(1);
  }

  /* create client's socket */ 
  if((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR)
  {
    perror ("socket"); 
    return (ERROR); 
  } 

  /* bind not required - port number is dynamic */ 
  /* build server socket address */ 
  sockAddrSize = sizeof (struct sockaddr_in); 
  bzero ((char *) &serverAddr, sockAddrSize); 
  serverAddr.sin_family = AF_INET; 

  /* get port and host from DB; if no record in DB for <target name> - exit */
  dbsock = dbConnect(getenv("MYSQL_HOST"), "daq");

  /* use 'port' field */
  sprintf(tmp,"SELECT tcpClient_tcp FROM Ports WHERE name='%s'",argv[1]);
  /*
  printf("DB select: >%s<\n",tmp);
  */
  if(dbGetInt(dbsock, tmp, &portnum)==ERROR) return(ERROR);

  sprintf(tmp,"SELECT Host FROM Ports WHERE name='%s'",argv[1]);
  /*
  printf("DB select: >%s<\n",tmp);
  */
  if(dbGetStr(dbsock, tmp, hostname)==ERROR) return(ERROR);

  dbDisconnect(dbsock);
  /*  
  printf("hostname=>%s< portnum=%d\n",hostname,portnum);
  */

  serverAddr.sin_port = htons(portnum/*SERVER_PORT_NUM*/); 

  hptr = gethostbyname(hostname);
  if(hptr == NULL) {
      printf("unknown hostname %s \n",hostname); 
      close(sFd);
      exit(1);
    } else {
      memcpy(&serverAddr.sin_addr,*(hptr->h_addr_list),sizeof(sizeof(struct in_addr)));
    }


  /*
    If the connection cannot be established immediately and O_NONBLOCK is set for
    the file descriptor for the socket, connect() shall fail and set errno to [EINPROGRESS],
    but the connection request shall not be aborted, and the connection shall be established
    asynchronously. Subsequent calls to connect() for the same socket, before the connection
    is established, shall fail and set errno to [EALREADY].

    When the connection has been established asynchronously, select() and poll() shall
    indicate that the file descriptor for the socket is ready for writing.
  */


goto a123;
  {
    int itmp, orig_flags;
    int on = 1; /* blocking */
    int off = 0; /* nonblocking */

    orig_flags = fcntl(sFd, F_GETFL, 0);
    printf("orig_flags=0x%08x\n",orig_flags);

	
    if(fcntl(sFd, F_SETFL, orig_flags | O_NONBLOCK) < 0)
    {
      perror("fcntl(O_NONBLOCK)");
    }
	
	/*
    if(fcntl(sFd, F_SETFL, orig_flags | O_NDELAY) < 0)
    {
      perror("fcntl(O_NDELAY)");
	}
	*/

	/*ioctl: No such file or directory (O_NDELAY or O_NONBLOCK ?)
    itmp = ioctl(sFd,O_NDELAY,(int) &on);
    if(itmp < 0) perror("ioctl");
	*/

  }
a123:

    /* see dpS/dpS.s/dplite.c how DP_cmd works !!! 
    {
      int cval = 1;
      int linger[2];

      linger[0] = 0;
      linger[1] = 0;

      setsockopt(sFd, SOL_SOCKET, SO_LINGER,
		 (char *)linger, sizeof(linger));
      
      setsockopt(sFd, IPPROTO_TCP, TCP_NODELAY, (char *)&cval, sizeof(int));
    }
	*/

  /*
  3.5.  How can I set the timeout for the connect() system call?

  From Richard Stevens ([email protected]):

  Normally you cannot change this.  Solaris does let you do this, on a
  per-kernel basis with the ndd tcp_ip_abort_cinterval parameter.

  The easiest way to shorten the connect time is with an alarm() around
  the call to connect().  A harder way is to use select(), after setting
  the socket nonblocking.  Also notice that you can only shorten the
  connect time, there's normally no way to lengthen it.

  From Andrew Gierth ([email protected]):

  First, create the socket and put it into non-blocking mode, then call
  connect(). There are three possibilities:

  o  connect succeeds: the connection has been successfully made (this
     usually only happens when connecting to the same machine)

  o  connect fails: obvious

  o  connect returns -1/EINPROGRESS. The connection attempt has begun,
     but not yet completed.

  If the connection succeeds:

  o  the socket will select() as writable (and will also select as
     readable if data arrives)

  If the connection fails:

  o  the socket will select as readable *and* writable, but either a
     read or write will return the error code from the connection
     attempt. Also, you can use getsockopt(SO_ERROR) to get the error
     status - but be careful; some systems return the error code in the
     result parameter of getsockopt, but others (incorrectly) cause the
     getsockopt call *itself* to fail with the stored value as the
     error.
*/


  signal(SIGALRM,alarmHandler);
  alarm(10); /* in 3 seconds connect call will be interrupted if did not finished */

  /* connect to server */ 
  /*printf("connecting to server ...\n");*/ 
  if (connect (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR)
  {
    /*perror ("connect");*/ 
    close (sFd); 
	return (ERROR);
  }
  /*printf("connected !!!\n");*/ 

  alarm(0); /* clear alarm so it will not interrupt us */

  /* build request, prompting user for message */ 
  myRequest.msgLen = strlen(argv[2]);
  sprintf(myRequest.message,"%s\0",argv[2]);
  myRequest.reply = TRUE;
  /*  
printf("1: 0x%08x 0x%08x\n", myRequest.msgLen, myRequest.reply);
  */
  /* convert integers to network byte order */
  myRequest.msgLen = htonl(myRequest.msgLen);
  myRequest.reply = htonl(myRequest.reply);
  /*
printf("2: 0x%08x 0x%08x\n", myRequest.msgLen, myRequest.reply);
  */
  /*
  printf(" Sending %d bytes: %s (len=%d)\n",
    myRequest.msgLen, myRequest.message, sizeof (myRequest));
  fflush(stdout);
  */

  /* send request to server */ 
  if(write(sFd, (char *) &myRequest, sizeof(TREQUEST)) == ERROR)
  {
    perror("write"); 
    close(sFd); 
    return(ERROR);
  } 

  if(myRequest.reply) /* if expecting reply, read and display it */
  { 
    int remB = 0;
	/*
    printf("MESSAGE FROM SERVER:\n");
	*/
    while( (rBytes = read(sFd, (char *)&replyBuf[remB], REPLY_MSG_SIZE)) > 0)
    {
#ifdef INSTANT_PRINT
      for(i=0;i<rBytes;i++) printf("%c",replyBuf[remB+i]);fflush(stdout);
#endif
      remB += rBytes;
    }
#ifdef INSTANT_PRINT
    printf("\n");
	fflush(stdout);
#else
    ch = strstr(replyBuf,"value = ");
    if(ch == NULL) mlen = remB;
    else           mlen = (int)ch - (int)replyBuf;
	/*
    printf("buf starts at 0x%08x, strstr returns 0x%08x, mlen=%d\n",
      replyBuf,ch,mlen);
	fflush(stdout);
	*/
    for(i=0; i<mlen; i++)
    {
      printf("%c",replyBuf[i]);
    }
    printf("\n");
#endif
  } 
  close(sFd);
 
  return(OK);
 }
Ejemplo n.º 3
0
int
codaDownload(char *conf)
{
  objClass object = localobject;

  MONp monp = (MONp) object->privated;
  int deflt = 0;
  static char tmp[1000];
  static char tmp2[1000];
  int  ix;  
  int  listArgc;
  char listArgv[LISTARGV1][LISTARGV2];

  MYSQL *dbsock;
  char tmpp[1000];

  
  monp->object = object;


  /***************************************************/
  /* extract all necessary information from database */


  /*****************************/
  /*****************************/
  /* FROM CLASS (former conf1) */

  strcpy(configname,conf); /* Sergey: save CODA configuration name */

  UDP_start();

  /* connect to database */
  dbsock = dbConnect(getenv("MYSQL_HOST"), expid);

  sprintf(tmp,"SELECT value FROM %s_option WHERE name='SPLITMB'",
    configname);
  if(dbGetInt(dbsock, tmp, &monp->split)==MON_ERROR)
  {
    monp->split = 2047 << 20;
    printf("cannot get SPLITMB, set monp->split=%d\n",monp->split);
  }
  else
  {
    printf("get monp->split = %d\n",monp->split);
    monp->split = monp->split<<20;
    printf("set monp->split = %d\n",monp->split);
  }

  sprintf(tmp,"SELECT value FROM %s_option WHERE name='RECL'",
    configname);
  if(dbGetInt(dbsock, tmp, &monp->record_length)==MON_ERROR)
  {
    monp->record_length = 32768;
    printf("cannot get RECL, set monp->record_length=%d\n",monp->record_length);
  }
  else
  {
    printf("get monp->record_length = %d\n",monp->record_length);
  }


  sprintf(tmp,"SELECT value FROM %s_option WHERE name='EvDumpLevel'",
    configname);
  if(dbGetInt(dbsock, tmp, &monp->log_EvDumpLevel)==MON_ERROR)
  {
    monp->log_EvDumpLevel = 0;
    printf("cannot get EvDumpLevel, set monp->log_EvDumpLevel=%d\n",monp->log_EvDumpLevel);
  }
  else
  {
    printf("get monp->log_EvDumpLevel = %d\n",monp->log_EvDumpLevel);
  }


  /* do not nned that !!!???
  sprintf(tmp,"SELECT inputs FROM %s WHERE name='%s'",configname,object->name);
  if(dbGetStr(dbsock, tmp, tmpp)==MON_ERROR)
  {
    printf("cannot get 'inputs' from table>%s< for the name>%s<\n",configname,object->name);
  }
  else
  {
    printf("inputs >%s<\n",tmpp);
  }
  */

  sprintf(tmp,"SELECT outputs FROM %s WHERE name='%s'",configname,object->name);
  if(dbGetStr(dbsock, tmp, tmpp)==MON_ERROR)
  {
    printf("cannot get 'outputs' from table>%s< for the name>%s<\n",configname,object->name);
    return(MON_ERROR);
  }
  else
  {
    strcpy(monp->output_type,tmpp);
    printf("get monp->output_type >%s<\n",monp->output_type);
  }

  /* default output to none */
  monp->output_switch = 3;

  if( !strncmp(monp->output_type,"file",4) ) /* output to binary file */
  {
    sprintf(tmp,"SELECT value FROM %s_option WHERE name='dataFile'",configname);
    if(dbGetStr(dbsock, tmp, tmpp)==MON_ERROR)
    {
      printf("cannot get 'dataFile' from table >%s_option<\n",configname);
      return(MON_ERROR);
    }
    else
    {
      monp->filename = strdup(tmpp); /* Sergey: change it to strcpy(monp->filename,tmpp);*/
      printf("get monp->filename >%s<\n",monp->filename);
    }
    monp->output_switch = 1;
  }
  else if( !strncmp(monp->output_type,"debug",5) ) /* debug dump */
  {
    monp->output_switch = 2;
  }
  else if( !strncmp(monp->output_type,"none",4) ) /* output to /dev/null */
  {
    monp->output_switch = 3;
  }
  else if( !strncmp(monp->output_type,"coda",4) ) /* output in CODA format */
  {
    sprintf(tmp,"SELECT value FROM %s_option WHERE name='dataFile'",configname);
    if(dbGetStr(dbsock, tmp, tmpp)==MON_ERROR)
    {
      printf("cannot get 'dataFile' from table >%s_option<\n",configname);
      return(MON_ERROR);
    }
    else
    {
      monp->filename = strdup(tmpp); /* Sergey: change it to strcpy(monp->filename,tmpp);*/
      printf("get monp->filename >%s<\n",monp->filename);
    }
  	printf("coda format will be used\n");
    monp->output_switch = 4;
  }
  else
  {
    printf("invalid monp->output_type >%s<\n",monp->output_type);
    return(MON_ERROR);
  }

  /*****************************/
  /*****************************/
  /*****************************/

  monp->fd = -1;

  if (monp->mod_id) {
    printf("INFO: Unloading module %x\n", monp->mod_id);

#if defined __sun||LINUX
    if (dlclose ((void *) monp->mod_id) != 0)
    {
      printf("ERROR: failed to unload module to decode >%s<\n",monp->mod_name);
      return MON_ERROR;
    }
#else
    printf("WARN: dynamic loading not yet supported on this platform\n");
#endif
  }
  
  printf("INFO: Downloading configuration '%s'\n", configname);

  strcpy(monp->mod_name,"CODA");


  /* Get the list of readout-lists from the database */
  sprintf(tmpp,"SELECT code FROM %s WHERE name='%s'",configname,object->name);
  if(dbGetStr(dbsock, tmpp, tmp)==MON_ERROR) return(MON_ERROR);
printf("++++++======>%s<\n",tmp);


  /* disconnect from database */
  dbDisconnect(dbsock);


  /* Decode configuration string */
  listArgc = 0;
  if(!((strcmp (tmp, "{}") == 0)||(strcmp (tmp, "") == 0)))
  {
    if(listSplit1(tmp, 1, &listArgc, listArgv)) return(MON_ERROR);
    for(ix=0; ix<listArgc; ix++) printf("nrols [%1d] >%s<\n",ix,listArgv[ix]);
  }
  else
  {
    printf("download: do not split list >%s<\n",tmp);
  }


  /* Get object filename in order to find the ROLs __init routine */
  if(listArgc)
  {
    strcpy(monp->mod_name,listArgv[0]);

    /* Load the decode module */
    sprintf(tmp,"%s/%s_file.so",getenv("CODA_LIB"),listArgv[0]);
#if defined __sun||LINUX
    if (monp->mod_id) dlclose(monp->mod_id);
    monp->mod_id = dlopen ((const char *) tmp, RTLD_NOW | RTLD_GLOBAL);
    if (monp->mod_id == 0) {
      printf("WARN: dlopen failed to open >%s<\n",tmp);
      printf("WARN: >%s<\n",dlerror());
      deflt = 1;
    }
#else
    printf("WARN: dynamic loading not supported\n");
    deflt = 1;
#endif
    
  }
  else
  {
    deflt = 1;
    printf("WARN: row %s table %s no code entry, use CODA file fmt.\n",
      object->name,configname);
  }


  /******************************************************************/
  /* Now look up the routines in the library and fill in the tables */
  if(deflt)
  {
    /* default to CODA format */
    printf("INFO: Using inbuilt (CODA) format\n");
	/*
    monp->open_proc = CODA_open_file;
    monp->close_proc = CODA_close_file;
    monp->write_proc = CODA_write_event;
    */
  }
  else
  {
    IFUNCPTR proc;
    /* find input formatting procs */
    sprintf(tmp,"%s_open_file",monp->mod_name);
    proc = (IFUNCPTR) dlsym (monp->mod_id, tmp);
    monp->open_proc = proc;
    sprintf(tmp,"%s_close_file",monp->mod_name);
    proc = (IFUNCPTR) dlsym (monp->mod_id, tmp);
    monp->close_proc = proc;
    sprintf(tmp,"%s_write_event",monp->mod_name);
    proc = (IFUNCPTR) dlsym (monp->mod_id, tmp);
    monp->write_proc = proc;

    printf("INFO: Loaded module for format %s\n",monp->mod_name);
  }

  /* If we need to initialize, reinitialize, or
   * if et_alive fails on Linux, then initialize.
   */
  if( (et_init == 0)   ||
      (et_reinit == 1) ||
      ((!et_alive(et_sys)) && (et_locality == ET_LOCAL_NOSHARE))
     )
  {
    if(mon_et_initialize() != MON_OK)
    {
      printf("ERROR: mon download: cannot initalize ET system\n");
      return(MON_ERROR);
    }
  }

  codaUpdateStatus("downloaded");

  return(MON_OK);
}