コード例 #1
0
int main(int argc, char * argv[])
{
  Operations operation;

  
  if (argc < 2 || (operation = GetOperation(argv[1])) == NumOperations || argc < RequiredArgsForOperation[operation]) {
    Operations op;
    fputs("usage: c_api { ", stderr);
    for (op = OpListen; op < NumOperations; op++) {
      if (op > OpListen)
        fputs(" | ", stderr);
      fputs(OperationNames[op], stderr);
    }
    fputs(" } [ A-party [ B-party | file ] ]\n", stderr);
    return 1;
  }

  puts("Initialising.\n");

  if (!InitialiseOPAL())
    return 1;

  switch (operation) {
    case OpListen :
      puts("Listening.\n");
      HandleMessages(60000);
      break;

    case OpCall :
      if (argc > 3) {
        if (!DoCall(argv[2], argv[3]))
          break;
      } else {
        if (!DoCall(NULL, argv[2]))
          break;
      }
      HandleMessages(15000);
      break;

    case OpMute :
      if (!DoCall(NULL, argv[2]))
        break;
      HandleMessages(15000);
      if (!DoMute(1))
        break;
      HandleMessages(15000);
      if (!DoMute(0))
        break;
      HandleMessages(15000);
      break;

    case OpHold :
      if (!DoCall(NULL, argv[2]))
        break;
      HandleMessages(15000);
      if (!DoHold())
        break;
      HandleMessages(15000);
      break;

    case OpTransfer :
      if (!DoCall(NULL, argv[2]))
        break;
      HandleMessages(15000);
      if (!DoTransfer(argv[3]))
        break;
      HandleMessages(15000);
      break;

    case OpConsult :
      if (!DoCall(NULL, argv[2]))
        break;
      HandleMessages(15000);
      if (!DoHold())
        break;
      HandleMessages(15000);
      if (!DoCall(NULL, argv[3]))
        break;
      HandleMessages(15000);
      if (!DoTransfer(HeldCallToken))
        break;
      HandleMessages(15000);
      break;

    case OpRegister :
      if (!DoRegister(argv[2], argv[3]))
        break;
      HandleMessages(15000);
      break;

    case OpSubscribe :
      if (!DoSubscribe(argv[2], argv[3], argv[4]))
        break;
      HandleMessages(INT_MAX); // More or less forever
      break;

    case OpRecord :
      if (!DoRecord(argv[2], argv[3]))
        break;
      HandleMessages(INT_MAX); // More or less forever
      break;

    case OpPlay :
      if (!DoPlay(argv[2], argv[3]))
        break;
      HandleMessages(INT_MAX); // More or less forever
      break;

    default :
      break;
  }

  puts("Exiting.\n");

  ShutDownFunction(hOPAL);
  return 0;
}
コード例 #2
0
/*
** A "local" function of main().  Handles a #messageType# message arrived on
** #sd# accompanied by #dataSize# bytes of data.
*/
static void
ProcessRequest(Socket *sd,
               MessageType messageType,
               size_t dataSize) {

  unsigned long timeOut;
  DataDescriptor timeOutDescriptor = SIMPLE_DATA(UNSIGNED_LONG_TYPE, 1);
  char *matching;
  char *object;
  DataDescriptor objectDescriptor = SIMPLE_DATA(CHAR_TYPE, 0);
  char *pattern;
  DataDescriptor patternDescriptor = SIMPLE_DATA(CHAR_TYPE, 0);

  switch(messageType) {

  case NS_REGISTER:
    objectDescriptor.repetitions =
      dataSize - HomogenousDataSize(UNSIGNED_LONG_TYPE, 1, NETWORK_FORMAT);
    object = (char *)malloc(objectDescriptor.repetitions + 1);
    if(object == NULL) {
      (void)SendMessage(*sd, NS_FAILED, PktTimeOut(*sd));
      ERROR("ProcessRequest: out of memory\n");
    }
    else {
      if(!RecvData(*sd,
                   object,
                   &objectDescriptor,
                   1,
                   PktTimeOut(*sd)) ||
         !RecvData(*sd,
                   &timeOut,
                   &timeOutDescriptor,
                   1,
                   PktTimeOut(*sd))) {
        DROP_SOCKET(sd);
        ERROR("ProcessRequest: receive failed\n");
      }
      else {
        object[objectDescriptor.repetitions] = '\0';
        (void)SendMessage(*sd, NS_REGISTERED, PktTimeOut(*sd));
        /* Change time-out period to an expiration time. */
        if(timeOut != 0) {
          timeOut += (unsigned long)CurrentTime();
        }
        DoRegister(object, timeOut);
      }
      free(object);
    }
    break;

  case NS_SEARCH:
  case NS_UNREGISTER:
    patternDescriptor.repetitions = dataSize;
    pattern = (char *)malloc(dataSize);
    if(pattern == NULL) {
      (void)SendMessage(*sd, NS_FAILED, PktTimeOut(*sd));
      ERROR("ProcessRequest: out of memory\n");
    }
    else {
      if(!RecvData(*sd,
                   pattern,
                   &patternDescriptor,
                   1,
                   PktTimeOut(*sd))) {
        DROP_SOCKET(sd);
        ERROR("ProcessRequest: receive failed\n");
      }
      else if(messageType == NS_SEARCH) {
        matching = DoSearch(pattern);
        if(matching == NULL) {
          (void)SendMessage(*sd, NS_FAILED, PktTimeOut(*sd));
          ERROR("ProcessRequest: out of memory\n");
        }
        else {
          objectDescriptor.repetitions = strlen(matching) + 1;
          (void)SendMessageAndData(*sd,
                                   NS_SEARCHED,
                                   matching,
                                   &objectDescriptor,
                                   1,
                                   PktTimeOut(*sd));
          free(matching);
        }
      }
      else {
        DoUnregister(pattern);
        (void)SendMessage(*sd, NS_UNREGISTERED, PktTimeOut(*sd));
      }
      free(pattern);
    }
    break;

  default:
    ERROR1("ProcessRequest: unknown message %d\n", messageType);

  }

}
コード例 #3
0
bool GlobalShortcutBackend::Register() {
  bool ret = DoRegister();
  if (ret)
    active_ = true;
  return ret;
}