void ZeroMQEngine::Initialize(const string endPoint, const string instanceName, const string instanceID)
{
    zcert_t* localCertificate = nullptr;

    // Handle common security initialization for server and client instances
    if (SecurityEnabled())
    {
        if (!zsys_has_curve())
            throw runtime_error("Failed to locate needed curve security libraries, cannot initialize ZeroMQ security");
        
        string localCertDirectory = CURVECERTLOCALDIR "/";
        string publicCertFileName;
        string privateCertFileName;
        
        // Make sure certificate store directories exist
        if (zsys_dir_create(CURVECERTLOCALDIR) != 0)
            throw runtime_error("Failed to create local curve security store, cannot initialize ZeroMQ security");

        if (zsys_dir_create(CURVECERTREMOTEDIR) != 0)
            throw runtime_error("Failed to create remote curve security store, cannot initialize ZeroMQ security");
        
        // Derive certificate file names based on engine mode
        if (ServerMode())
        {
            publicCertFileName = localCertDirectory + SVRPUBCERTFILENAME;
            privateCertFileName = localCertDirectory + SVRPVTCERTFILENAME;
        }
        else
        {
            publicCertFileName = localCertDirectory + instanceName + PUBCERTFILENAMEEXT;
            privateCertFileName = localCertDirectory + instanceName + PVTCERTFILENAMEEXT;
        }
        
        // See if private certificate already exists
        if (zsys_file_exists(privateCertFileName.c_str()))
        {
            // Load existing local certificate
            localCertificate = zcert_load(privateCertFileName.c_str());
            
            if (localCertificate == nullptr)
                throw runtime_error("Failed to load local curve certificate, cannot initialize ZeroMQ security");
        }
        else
        {
            // Create a new full certificate (public + private)
            localCertificate = zcert_new();
            
            if (localCertificate == nullptr)
                throw runtime_error("Failed to create local curve certificate, cannot initialize ZeroMQ security");

            zcert_set_meta(localCertificate, "name", instanceName.c_str());
            zcert_set_meta(localCertificate, "id", instanceID.c_str());
            zcert_set_meta(localCertificate, "type", ServerMode() ? "server" : "client");
            
            // Persist certificates for future runs
            if (zcert_save_public(localCertificate, publicCertFileName.c_str()) != 0)
                throw runtime_error("Failed to save local curve public certificate, cannot initialize ZeroMQ security");
            
            if (zcert_save_secret(localCertificate, privateCertFileName.c_str()) != 0)
                throw runtime_error("Failed to save local curve private certificate, cannot initialize ZeroMQ security");
        }        
    }
    
    // Create new ZeroMQ engine instance
    if (ServerMode())
        m_instance = new ZeroMQServer(m_bufferReceivedCallback, SecurityEnabled(), InactiveClientTimeout(), VerboseOutput(), ConnectionID(), localCertificate);
    else
        m_instance = new ZeroMQClient(m_bufferReceivedCallback, SecurityEnabled(), InactiveClientTimeout(), VerboseOutput(), ConnectionID(), localCertificate);

    m_instance->Initialize(endPoint, instanceName, instanceID);

    log_info("\nEstablished ZeroMQ socket [%s]\n", ConnectionID().ToString().c_str());
}
Example #2
0
int
main(int argc, char *argv[])
{
  int opt;
  char *queryWord = NULL;
  char *queryFile = NULL;
  int discardDupFiles = 1;
  int cacheSizeInKB = 0;

  while ((opt = getopt(argc, argv, "ql:d:uw:f:bc:s:")) != -1) {
    switch (opt) {
      case 'q':
        quietFlag = 1;
        break;
      case 'l':
        diskLatency = atoi(optarg);
        break;
      case 'c':
        cacheSizeInKB = atoi(optarg);
        break;
      case 'b':
        diskBusyWaitEnable = 1;
        break;
      case 'u':
        discardDupFiles = 0;
        break;
      case 'w':
        queryWord = strdup(optarg);
        break;
      case 'f':
        queryFile = strdup(optarg);
        break;
      case 's':
        serverAckFd = atoi(optarg);
	diskLatency = 0;  /* Default for assignment 3 is zero-latency */
        break;
      case 'd': {
        char *c = optarg;
        while (*c) {
          Debug_SetFlag(*c, 1);
          c++;
        }
        break;
      }
      default:
        PrintUsageAndExit(argv[0]);
    }
  }
  if (optind != argc-1) {
    PrintUsageAndExit(argv[0]);
  }

  /*
   * Allocate Memory for any caching of data. 0 means infinite cache.
   */
  if (cacheSizeInKB == 0) {
    cacheSizeInKB = INFINITE_CACHE_SIZE/1024;  
  }
  int retVal = CacheMem_Init(cacheSizeInKB);
  if (retVal < 0) {
    fprintf(stderr, "Can't allocate memory for cache\n");
    return 1;
  }

  char *diskpath = diskpath = argv[optind];
  BuildDiskIndex(diskpath, discardDupFiles);

  if (queryWord) {
    TestServiceBySingleWord(queryWord);
  }
  if (queryFile) {
    TestServiceByFileOfWords(queryFile);
  }

  if (serverAckFd) {
    ServerMode(serverAckFd);
  }
  if (!quietFlag) {
    printf("************ Stats ***************\n");
    DumpStats(stdout);
    DumpUsageStats(stdout);
  }

  exit(EXIT_SUCCESS);
  return 0;
}