/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : TaskInterface.cc                                              */
/* Author   : Tom O'Reilly                                                  */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/proxy.h>
#include <sys/kernel.h>
#include "TaskInterface.h"
#include "TaskServer.h"
#include "Syslog.h"

TaskInterface::TaskInterface(const char *name, const char *serverName,
			     int serverConnectTimeout)
  : SharedObjectClient(name, serverName, serverConnectTimeout)
{
}


TaskInterface::TaskInterface(const char *name)
  : SharedObjectClient(name)
{
}


TaskInterface::~TaskInterface()
{
}


void TaskInterface::subscribe(EventCode eventCode)
{
  if (!connected())
    findServer();

  Boolean debug = False;

  // Send subscription message to server. 
  Subscription subscription;
  subscription.subscriberPid = getpid();
  subscription.eventCode = eventCode;
  SubscriptionReply reply;

  dprintf("TaskInterface::subscribe() - "
	  "send subscription msg, subscriber=%d\n",
	  subscription.subscriberPid);

  while (True) {
    try {
      send(&subscription, sizeof(Subscription), 
	   &reply, sizeof(SubscriptionReply));

    } catch (...) {
      // Wait, then keep trying
      sleep(1);
      continue;
    }
    // Successfully sent
    break;
  }

  dprintf("TaskInterface::subscribe() - sent.\n");

  // Subscription rejected for some reason
  if (!reply.confirmed) {
    throw Error("Subscription rejected");
  }
}


EventCode TaskInterface::waitForEvent()
{
  Boolean debug = False;
  while (True) {

    TaskServer::Event event;

    dprintf("TaskInterface::waitForEvent() - wait for msg\n");

    pid_t sender = Receive(AnyPid, (void *)&event, sizeof(TaskServer::Event));

    // If message is not actually an event generated by server,
    // it will be discarded, and we'll wait for the next one.
    if (event.marker == EventMarkerCode) {
      // This is a bonafide Event message

      // Remove the proxy
      if (qnx_proxy_detach(sender) == -1) {
	Syslog::write("TaskInterface::waitForEvent() - %s\n", strerror(errno));
      }

      return event.eventCode;
    }
  }
}


void TaskInterface::shutdown()
{
}


void TaskInterface::sendExternalMsg(ExternalMethodMessage *message)
{
}
