Пример #1
0
int create_fd(const char *arg, int is_output, enum proto *proto, char *name, size_t max_name_size)
{
  if (strncmp(arg, "udp:", 4) == 0 || strncmp(arg, "UDP:", 4) == 0) {
    *proto = UDP;
    arg += 4;
  } else if (strncmp(arg, "tcp:", 4) == 0 || strncmp(arg, "TCP:", 4) == 0) {
    *proto = TCP;
    arg += 4;
  } else if (strncmp(arg, "file:", 5) == 0) {
    *proto = File;
    arg += 5;
  } else if (strncmp(arg, "eth:", 4) == 0) {
    *proto = Eth;
    arg += 4;
  } else if (strcmp(arg, "null:") == 0) {
    *proto = File;
    arg = "/dev/null";
  } else if (strcmp(arg, "zero:") == 0) {
    *proto = File;
    arg = "/dev/zero";
  } else if (strcmp(arg, "stdin:") == 0) {
    *proto = StdIn;
    arg = "stdin";
  } else if (strcmp(arg, "stdout:") == 0) {
    *proto = StdOut;
    arg = "stdout";
  } else if (strcmp(arg, "stderr:") == 0) {
    *proto = StdErr;
    arg = "stderr";
  } else if (strchr(arg, ':') != 0) {
    *proto = UDP;
  } else if (strcmp(arg, "-") == 0) {
    *proto = is_output ? StdOut : StdIn;
    arg = is_output ? "stdout" : "stdin";
  } else {
    *proto = File;
  }

  if (name != 0)
    strncpy(name, arg, max_name_size);

  switch (*proto) {
    case UDP	:
    case TCP	: return create_IP_socket(arg, is_output, *proto);

    case File	: return create_file(arg, is_output);

    case Eth	: return create_raw_eth_socket(arg, is_output);

    case StdIn	:
    case StdOut:
    case StdErr	: return create_stdio(is_output, *proto);
    default     : fprintf(stderr, "Cannot create fd for unknown proto %d\n", *proto), exit(1);
  }
}
Пример #2
0
int create_fd(char *arg, int is_output, enum proto *proto)
{
  if (strncmp(arg, "udp:", 4) == 0 || strncmp(arg, "UDP:", 4) == 0) {
    *proto = UDP;
    arg += 4;
  } else if (strncmp(arg, "tcp:", 4) == 0 || strncmp(arg, "TCP:", 4) == 0) {
    *proto = TCP;
    arg += 4;
  } else if (strncmp(arg, "file:", 5) == 0) {
    *proto = File;
    arg += 5;
  } else if (strncmp(arg, "eth:", 4) == 0) {
    *proto = Eth;
    arg += 4;
  } else if (strncmp(arg, "stdin:", 6) == 0) {
    *proto = StdIn;
    arg = "stdin";
  } else if (strncmp(arg, "stdout:", 7) == 0) {
    *proto = StdOut;
    arg = "stdout";
  } else if (strncmp(arg, "stderr:", 7) == 0) {
    *proto = StdErr;
    arg = "stderr";
  } else if (strchr(arg, ':') != 0) {
    *proto = UDP;
  } else if (strcmp(arg, "-") == 0) {
    *proto = is_output ? StdOut : StdIn;
    arg = is_output ? "stdout" : "stdin";
  } else {
    *proto = File;
  }

  if (is_output)
    destination = arg;
  else
    source	= arg;

  switch (*proto) {
    case UDP	:
    case TCP	: return create_IP_socket(arg, is_output, *proto);

    case File	: return create_file(arg, is_output);

    case Eth	: return create_raw_eth_socket(arg, is_output);

    case StdIn	:
    case StdOut:
    case StdErr	: return create_stdio(is_output, *proto);
  }
}
Пример #3
0
int main (int argc, char **argv)
{ FILE *frsp[NRSP];
  OutPktType* outpkt[NELEM] = {NULL,};
  InPktType inpkt;
  int i = 0, fil=0, dip=0, stream_nr=0, pktcnt=0, j=0;
  unsigned int bad = 0;
  int station = 0, rsp = 0;
  char substr[16] = {0,};
  char testdat[128] = {0,};

  // NOTE limit on name size! Also hardcoded addresses and ports.
  int sk_out[NELEM] = {0,};
  int tcpport = 4567;
  char ipport[128] = {0,};
  char tcpsrc[32] = "10.142.5.94", tcpdest[32] = "10.141.5.23"; 
  
  // Write out per dipole data to file
  #if 1 // def DEBUG
  FILE *fout[NELEM] = {NULL,};
  for (i=0; i<NELEM; i++)
  { sprintf(testdat, "%03d.dip", i);
    if ((fout[i]=fopen(testdat, "wb")) == NULL)
     perror ("fopen");
  }
  #endif

  if (argc < 2) {usage(argv[0]); exit (-1);}
  
/* //TODO currently just specify filename on cmdline via shell regex.
  // Get RSP filenames
  DIR *dirp = NULL;
  struct dirent *dp = NULL;
  if ((dirp=opendir(argv[1])) == NULL)
  { perror ("opendir"); return -1;}

  if ((dp = readdir (dirp)) != NULL)
  { if (strcmp (
  }
*/

  // Open files
  for (i=0; i<NRSP; i++)
  { if ((frsp[i]=fopen (argv[1+i], "rb")) == NULL)
    { perror ("fopen"); break;}
	else
      fprintf (stderr, "Opened %s successfully.\n", argv[i+1]);
  }
  if (i<NRSP) { fprintf (stderr, "File problem!\n"); exit (-1);}
  
  fprintf (stderr, "Size of inpacket: %d, outpkt: %d bytes.\n", sizeof (InPktType), sizeof (OutPktType));
  // Create memory buffers
  for (i=0; i<NELEM; i++, tcpport++)
  { if ((outpkt[i]=(OutPktType*)calloc (sizeof (OutPktType), MAX_OUT_PKTS)) == NULL) 
	{ perror ("calloc"); break; }

    // Generate tcp streams to receiver. Starting port number configurable
    fprintf (stderr, "Creating TCP connection to %s:%d\n", tcpdest, tcpport);
    sprintf (ipport, "%s:%d", tcpdest, tcpport);
    sk_out[i] = create_IP_socket (ipport, 1, TCP);
  }
  if (i<NELEM) { fprintf (stderr, "calloc problem!\n"); exit (-1);}

  // For each RSP file, do till end of file:
  for (j=0; j<1; j++)
  for (fil=0; fil<NRSP; fil++)
  { if (fread (&inpkt, 1, sizeof (InPktType), frsp[fil]) < sizeof(InPktType))
	{ perror ("fread"); }
    
  	// Each RSP file packet has 12 streams or dipoles
  	// Check name of this file, set stream_nr accordingly
  	#ifdef DEBUG
  	fprintf (stderr, "%s\n", strstr(argv[fil+1], "CS00"));
  	#endif
  	strncpy (substr, strstr(argv[fil+1], "CS00"), 5); substr[5] = 0; 
  	station = atoi (substr+4);
  
  	strncpy (substr, strstr(argv[fil+1], "RSP"), 4); substr[4] = 0;
      rsp = atoi (substr+3);
  	#if 0 // # DEBUG
  	fprintf (stderr, "Working on station %d, RSP %d. Dip = %d, stream = %d\n",
               station, rsp, (station-2)*48, rsp*12); 
  	
  	#endif 

	// 12 dipole's data within an RSP board packet
	for (dip=(station-2)*48 + rsp*12,stream_nr=0; stream_nr<12; stream_nr++, dip++)
    { 

    #if 1 //def DEBUG
	  fprintf (stderr, "Working on station %d, RSP %d. Dip = %d, stream = %d\n",
             station, rsp, dip, stream_nr); 
	  // fprintf (stderr, "Writing data: %s", testdat);
	  //if (write (sk_out[dip], testdat, strlen(testdat)) < strlen(testdat))
 	  // { perror ("write"); }
	#endif

	  // Split into per dipole structures, fill dedicated buffer
	  memcpy ((void*)&(outpkt[dip]->header), (void*)(&inpkt.header), 
			  sizeof (HdrType));
      for (i=0; i< 1 * 16 * 2 * 2; i++) 
      { char value = inpkt.payload[2*stream_nr][0][0][i]; // Subband 1
  
        if (value < -128) value = -128, ++ bad;
        if (value >  127) value =  127, ++ bad;
  
        outpkt[dip]->payload[0][0][0][i] = value;
  
        value = inpkt.payload[2*stream_nr+1][0][0][i]; // Subband 2
  
        if (value < -128) value = -128, ++ bad;
        if (value >  127) value =  127, ++ bad;
  
        outpkt[dip]->payload[1][0][0][i] = value;
      }
      fwrite (outpkt[dip], 1, sizeof (OutPktType), fout[dip]); 
	  if (write (sk_out[dip], outpkt[dip], sizeof (OutPktType)) < sizeof (OutPktType))
	  { perror ("write"); }
	  else pktcnt++;
    }
  }
  
  fprintf (stderr, "Sent %d packets.\n", pktcnt);
  // Free buffers
  for (i=0; i<NELEM; i++)  if (outpkt[i]) free (outpkt[i]);

  // Close TCP connections

  // close files
  for (i=0; i<NRSP; i++)  if (frsp[i] != NULL) fclose (frsp[i]);

  return 0;
}