#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <time.h>
#include <sys/socket.h>
#include <math.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <sched.h>
#include <stdlib.h>
#include <string.h>
#include "futils.h"
#include "dada_def.h"
#include "dada_hdu.h"
#include "multilog.h"
#include "ipcio.h"
#include "ascii_header.h"
void usage()
{
fprintf (stdout,
"example_dada_writer [options] header_file\n"
" -k key shared memory key [default %x]\n"
" -h print help text\n",
DADA_DEFAULT_BLOCK_KEY);
}
int main (int argc, char **argv)
{
multilog_t * log = 0;
key_t dada_key = DADA_DEFAULT_BLOCK_KEY;
dada_hdu_t * hdu = 0;
int arg = 0;
while ((arg=getopt(argc,argv,"hk:")) != -1)
{
switch (arg)
{
case 'h':
usage();
return 0;
case 'k':
if (sscanf (optarg, "%x", &dada_key) != 1)
{
fprintf (stderr, "dada_db: could not parse key from %s\n", optarg);
return -1;
}
default:
usage ();
return 0;
}
}
if ((argc - optind) != 1)
{
fprintf (stderr, "ERROR: header must be specified\n");
usage();
return (EXIT_FAILURE);
}
char * header_file = strdup(argv[optind]);
char * obs_header = (char *) malloc(sizeof(char) * DADA_DEFAULT_HEADER_SIZE);
if (!obs_header)
{
fprintf (stderr, "ERROR: could not allocate memory\n");
return (EXIT_FAILURE);
}
if (fileread (header_file, obs_header, DADA_DEFAULT_HEADER_SIZE) < 0)
{
free (obs_header);
fprintf (stderr, "ERROR: could not read ASCII header from %s\n", header_file);
return (EXIT_FAILURE);
}
log = multilog_open ("example_dada_writer", 0);
multilog_add (log, stderr);
hdu = dada_hdu_create (log);
dada_hdu_set_key (hdu, dada_key);
if (dada_hdu_connect (hdu) < 0)
{
multilog (log, LOG_ERR, "could not connect to HDU\n");
return EXIT_FAILURE;
}
if (dada_hdu_lock_write (hdu) < 0)
{
multilog (log, LOG_ERR, "could not lock write on HDU\n");
return EXIT_FAILURE;
}
{
uint64_t header_size = ipcbuf_get_bufsz (hdu->header_block);
char * header = ipcbuf_get_next_write (hdu->header_block);
memcpy (header, obs_header, header_size);
if (ipcbuf_mark_filled (hdu->header_block, header_size) < 0)
{
multilog (log, LOG_ERR, "could not mark filled Header Block\n");
return EXIT_FAILURE;
}
}
uint64_t block_size = ipcbuf_get_bufsz ((ipcbuf_t*) hdu->data_block);
{
uint64_t block_id;
char * block = ipcio_open_block_write (hdu->data_block, &block_id);
if (!block)
{
multilog (log, LOG_ERR, "ipcio_open_block_write failed\n");
return EXIT_FAILURE;
}
memset (block, 0, block_size);
if (ipcio_close_block_write (hdu->data_block, block_size) < 0)
{
multilog (log, LOG_ERR, "ipcio_close_block_write failed\n");
return EXIT_FAILURE;
}
}
{
uint64_t local_buffer_size = (block_size * 3) / 2;
char * local_buffer = (char *) malloc (local_buffer_size);
if (!local_buffer)
{
multilog (log, LOG_ERR, "failed to allocate %lu bytes\n", local_buffer_size);
return EXIT_FAILURE;
}
memset (local_buffer, 0, local_buffer_size);
ipcio_write (hdu->data_block, local_buffer, local_buffer_size);
free (local_buffer);
}
if (dada_hdu_unlock_write (hdu) < 0)
{
multilog (log, LOG_ERR, "dada_hdu_unlock_write failed\n");
return -1;
}
if (dada_hdu_disconnect (hdu) < 0)
multilog (log, LOG_ERR, "could not unlock write on hdu\n");
return EXIT_SUCCESS;
}