Index: trunk/Ohana/src/addstar/src/DatasetOps.c
===================================================================
--- trunk/Ohana/src/addstar/src/DatasetOps.c	(revision 6498)
+++ trunk/Ohana/src/addstar/src/DatasetOps.c	(revision 6498)
@@ -0,0 +1,69 @@
+# include "addstar.h"
+
+/* the init function is an alternative */
+/* pthread_mutex_init(&mutex, const pthread_mutexattr_t *mutexattr); */
+pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;
+
+static int lock = FALSE;
+static int Ndataset = 0;
+static int NDATASET = 0;
+static DVO_DATA **dataset = NULL;
+
+int InitDataset () {
+
+  int i;
+
+
+  Ndataset = 0;
+  NDATASET = 100;
+  ALLOCATE (dataset, DVO_DATA *, NDATASET);
+  for (i = 0; i < NDATASET; i++) {
+    dataset[i] = NULL;
+  }
+  return (TRUE);
+}
+
+/* data a set dataset to the end of the stack */
+int PushDataset (DVO_DATA *data) {
+
+  int N;
+
+  pthread_mutex_lock(&mutex);
+
+  N = Ndataset;
+  Ndataset ++;
+  CHECK_REALLOCATE (dataset, DVO_DATA, NDATASET, Ndataset, 100);
+
+  dataset[N] = data;
+
+  pthread_mutex_unlock(&mutex);
+  return (TRUE);
+}
+
+/* remove and return first dataset */
+DVO_DATA *PopDataset (void) {
+
+  DVO_DATA *data;
+
+  pthread_mutex_lock(&mutex);
+
+  if (Ndataset == 0) {
+    pthread_mutex_unlock(&mutex);
+    return (NULL);
+  }
+
+  data = dataset[0];
+  
+  for (i = 0; i < Ndataset - 1; i++) {
+    dataset[i] = dataset[i+1];
+  }
+  Ndataset --;
+  if ((Ndataset < NDATASET / 2) && (Ndataset > 50)) {
+    NDATASET = Ndataset / 2;
+    REALLOCATE (dataset, DVO_DATA, NDATASET);
+  }
+
+  pthread_mutex_unlock(&mutex);
+  return (data);
+}
+
Index: trunk/Ohana/src/addstar/src/ListenClients_Thread.c
===================================================================
--- trunk/Ohana/src/addstar/src/ListenClients_Thread.c	(revision 6498)
+++ trunk/Ohana/src/addstar/src/ListenClients_Thread.c	(revision 6498)
@@ -0,0 +1,55 @@
+# include "addstar.h"
+
+/* wait for incoming messages from clients */
+int ListenClients_Thread () {
+
+  int status, InitSocket, BindSocket;
+  SockAddress Address;
+  IOBuffer message;
+  AddstarClientOptions options;
+  
+  /* if we have multiple threads, each one creates its own socket? */
+  InitSocket = InitServerSocket (&Address);
+  
+  while (1) {
+
+    /* wait for clients to make connection */
+    BindSocket = WaitServerSocket (InitSocket, &Address, VALID_IP, NVALID);
+    if (BindSocket == -1) continue;
+
+    /* validate : wait for password */
+    if (!CheckPassword (BindSocket)) continue;
+    
+    /* accept command : XXX EAM : long-enough timeout? */
+    status = ExpectCommand (BindSocket, 5, 0.1, &message);
+    if (status != 0) {
+      if (VERBOSE) fprintf (stderr, "failed connection\n");
+      FreeIOBuffer (&message);
+      close (BindSocket);
+      continue;
+    }
+
+    /* message options */
+    if (!strcmp (message.buffer, "IMAGE")) {
+      fprintf (stderr, "Image\n");
+      NewImage_Thread (BindSocket);
+      continue;
+    }
+    if (!strcmp (message.buffer, "REFLS")) {
+      fprintf (stderr, "Reflist\n");
+      NewReflist_Thread (BindSocket);
+      continue;
+    }
+    if (!strcmp (message.buffer, "REFCT")) {
+      fprintf (stderr, "Refcat\n");
+      NewRefcat_Thread (BindSocket);
+      continue;
+    }
+    if (!strcmp (message.buffer, "EXIT")) {
+      /* need to send this signal to the main thread */
+      fprintf (stderr, "Exit\n");
+      exit (2);
+    }
+  }    
+  exit (1);
+}
Index: trunk/Ohana/src/addstar/src/NewImage_Thread.c
===================================================================
--- trunk/Ohana/src/addstar/src/NewImage_Thread.c	(revision 6498)
+++ trunk/Ohana/src/addstar/src/NewImage_Thread.c	(revision 6498)
@@ -0,0 +1,68 @@
+# include "addstar.h"
+
+int NewImage_Thread (int BindSocket) {
+
+  int N, Nstars;
+  Stars *stars;
+  Image *image;
+  Coords *mosaic;
+  AddstarClientOptions *options;
+  DVO_DATA *dataset;
+
+  /* accept incoming data set */
+  if (!Recv_AddstarClientOptions (BindSocket, &options, &N)) {
+    fprintf (stderr, "error: problem receiving options\n");
+    goto reject;
+  }
+  if (N != 1) {
+    fprintf (stderr, "error: too many option sets (%d)\n", N);
+    goto reject;
+  }
+
+  if (!Recv_Image (BindSocket, &image, &N)) {
+    fprintf (stderr, "error: problem receiving image data\n");
+    goto reject;
+  }
+  if (N != 1) {
+    fprintf (stderr, "error: invalid number of images (%d)\n", N);
+    goto reject;
+  }
+
+  if (options[0].mosaic) {
+    if (!Recv_Coords (BindSocket, &mosaic, &N)) {
+      fprintf (stderr, "error: problem receiving mosaic coordinates\n");
+      goto reject;
+    }
+    if (N != 1) {
+      fprintf (stderr, "error: invalid number of mosaic coords (%d)\n", N);
+      goto reject;
+    }
+  }    
+
+  if (!Recv_Stars (BindSocket, &stars, &Nstars)) {
+    fprintf (stderr, "error: problem receiving star data\n");
+    goto reject;
+  }
+  fprintf (stderr, "accepted %s, %d stars\n", image[0].name, Nstars);
+
+  /* create new dataset to store the incoming data */
+  ALLOCATE (dataset, DVO_DATA, 1);
+  dataset[0].options = options;
+  dataset[0].patch   = NULL;
+  dataset[0].refcat  = NULL;
+  dataset[0].image   = image;
+  dataset[0].mosaic  = mosaic;
+  dataset[0].stars   = stars;
+  dataset[0].Nstars  = Nstars;
+
+  /* place on dataset stack */
+  PushDataset (dataset);
+
+  /* close connection, return */
+  close (BindSocket);
+  return (TRUE);
+
+reject:
+  close (BindSocket);
+  return (FALSE);
+}
Index: trunk/Ohana/src/addstar/src/NewRefcat_Thread.c
===================================================================
--- trunk/Ohana/src/addstar/src/NewRefcat_Thread.c	(revision 6498)
+++ trunk/Ohana/src/addstar/src/NewRefcat_Thread.c	(revision 6498)
@@ -0,0 +1,58 @@
+# include "addstar.h"
+
+int NewRefcat_Thread (int BindSocket) {
+
+  int N, status;
+  AddstarClientOptions *options;
+  IOBuffer message;
+  SkyRegion *patch;
+
+  /* accept incoming data set */
+  if (!Recv_AddstarClientOptions (BindSocket, &options, &N)) {
+    fprintf (stderr, "error: problem receiving options\n");
+    goto reject;
+  }
+  if (N != 1) {
+    fprintf (stderr, "error: too many option sets (%d)\n", N);
+    goto reject;
+  }
+
+  if (!Recv_SkyRegion (BindSocket, &patch, &N)) {
+    fprintf (stderr, "error: problem receiving patch\n");
+    goto reject;
+  }
+  if (N != 1) {
+    fprintf (stderr, "error: too many patches (%d)\n", N);
+    goto reject;
+  }
+
+  status = ExpectMessage (BindSocket, 0.25, &message);
+  if (status != 0) {
+    if (VERBOSE) fprintf (stderr, "failed connection\n");
+    FreeIOBuffer (&message);
+    goto reject;
+  }
+
+  /* add to db */
+  UpdateDatabase_Refcat (options, patch, message.buffer);
+
+  /* create new dataset to store the incoming data */
+  ALLOCATE (dataset, DVO_DATA, 1);
+  dataset[0].options = options;
+  dataset[0].patch   = patch;
+  dataset[0].refcat  = message.buffer;
+  dataset[0].image   = NULL;
+  dataset[0].mosaic  = NULL;
+  dataset[0].stars   = NULL;
+  dataset[0].Nstars  = 0;
+
+  /* place on dataset stack */
+  PushDataset (dataset);
+
+  close (BindSocket);
+  return (TRUE);
+
+reject:
+  close (BindSocket);
+  return (FALSE);
+}
Index: trunk/Ohana/src/addstar/src/NewReflist_Thread.c
===================================================================
--- trunk/Ohana/src/addstar/src/NewReflist_Thread.c	(revision 6498)
+++ trunk/Ohana/src/addstar/src/NewReflist_Thread.c	(revision 6498)
@@ -0,0 +1,44 @@
+# include "addstar.h"
+
+int NewReflist_Thread (int BindSocket) {
+
+  int N, Nstars;
+  Stars *stars;
+  AddstarClientOptions *options;
+
+  /* accept incoming data set */
+  if (!Recv_AddstarClientOptions (BindSocket, &options, &N)) {
+    fprintf (stderr, "error: problem receiving options\n");
+    goto reject;
+  }
+  if (N != 1) {
+    fprintf (stderr, "error: too many option sets (%d)\n", N);
+    goto reject;
+  }
+
+  if (!Recv_Stars (BindSocket, &stars, &Nstars)) {
+    fprintf (stderr, "error: problem receiving star data\n");
+    goto reject;
+  }
+  fprintf (stderr, "accepted %d stars\n", Nstars);
+
+  /* create new dataset to store the incoming data */
+  ALLOCATE (dataset, DVO_DATA, 1);
+  dataset[0].options = options;
+  dataset[0].patch   = NULL;
+  dataset[0].refcat  = NULL;
+  dataset[0].image   = NULL;
+  dataset[0].mosaic  = NULL;
+  dataset[0].stars   = stars;
+  dataset[0].Nstars  = Nstars;
+
+  /* place on dataset stack */
+  PushDataset (dataset);
+
+  close (BindSocket);
+  return (TRUE);
+
+reject:
+  close (BindSocket);
+  return (FALSE);
+}
Index: trunk/Ohana/src/addstar/src/addstart.c
===================================================================
--- trunk/Ohana/src/addstar/src/addstart.c	(revision 6498)
+++ trunk/Ohana/src/addstar/src/addstart.c	(revision 6498)
@@ -0,0 +1,55 @@
+# include "addstar.h"
+
+int main (int argc, char **argv) {
+
+  int status, InitSocket, BindSocket;
+  SockAddress Address;
+  IOBuffer message;
+  AddstarClientOptions options;
+
+  options = ConfigInit (&argc, argv);
+  args_server (argc, argv);
+
+  /* store the sky table in a global for internal use */
+  ServerSky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, SKY_DEPTH, VERBOSE);
+  SkyTableSetFilename (ServerSky, CATDIR, "cpt");
+
+  VERBOSE = TRUE;
+
+  /* if we separate the incoming data from db update, spawn db thread here */
+  /* this needs to be launched with pthreads
+   * we can probably launch multiple copies of dbthread */
+
+  InitDataset ();
+
+  /* decide if we need an argument to dbthread */
+  pthread_create (thread, attr, ListenClients_Thread, NULL);
+
+  while (1) {
+
+    /* need to watch for shutdown message */
+
+    dataset = PopDataset ();
+    if (dataset == NULL) {
+      usleep (50000);
+    }
+
+    switch (dataset[0].options[0].mode) {
+      case M_IMAGE:
+	UpdateDatabase_Image (dataset[0].options, dataset[0].image, dataset[0].mosaic, dataset[0].stars, dataset[0].Nstars);
+	continue;
+
+      case M_REFLIST:
+	UpdateDatabase_Reflist (dataset[0].options, dataset[0].stars, dataset[0].Nstars);
+	continue;
+
+      case M_REFCAT:
+	UpdateDatabase_Refcat (dataset[0].options, dataset[0].patch, dataset[0].refcat);
+	continue;
+
+      default:
+	fprintf (stderr, "error: unexpected dataset\n");
+    }
+  }    
+  exit (1);
+}
