Beispiel #1
0
    void ReplSet::haveNewConfig(OperationContext* txn, ReplSetConfig& newConfig, bool addComment) {
        bo comment;
        if( addComment )
            comment = BSON( "msg" << "Reconfig set" << "version" << newConfig.version );

        newConfig.saveConfigLocally(txn, comment);

        try {
            BSONObj oldConfForAudit = config().asBson();
            BSONObj newConfForAudit = newConfig.asBson();
            audit::logReplSetReconfig(ClientBasic::getCurrent(),
                                      &oldConfForAudit,
                                      &newConfForAudit);
            if (initFromConfig(txn, newConfig, true)) {
                log() << "replSet replSetReconfig new config saved locally" << rsLog;
            }
        }
        catch (const DBException& e) {
            log() << "replSet error unexpected exception in haveNewConfig() : " << e.toString() << rsLog;
            fassertFailedNoTrace(18755);
        }
        catch (...) {
            std::terminate();
        }
    }
Beispiel #2
0
    // Our own config must be the first one.
    bool ReplSetImpl::_loadConfigFinish(OperationContext* txn, vector<ReplSetConfig*>& cfgs) {
        int v = -1;
        ReplSetConfig *highest = 0;
        int myVersion = -2000;
        int n = 0;
        for (vector<ReplSetConfig*>::iterator i = cfgs.begin(); i != cfgs.end(); i++) {
            ReplSetConfig* cfg = *i;
            DEV { LOG(1) << n+1 << " config shows version " << cfg->version << rsLog; }
            if (++n == 1) myVersion = cfg->version;
            if (cfg->ok() && cfg->version > v) {
                highest = cfg;
                v = cfg->version;
            }
        }
        verify(highest);

        if (!initFromConfig(txn, *highest))
            return false;

        if (highest->version > myVersion && highest->version >= 0) {
            log() << "replSet got config version " << highest->version
                  << " from a remote, saving locally" << rsLog;
            highest->saveConfigLocally(txn, BSONObj());
        }
        return true;
    }
Beispiel #3
0
//Default constructor
CLogger::CLogger() 
{ 
	m_directory=L".";
	m_prefix=L"ZS";
	m_filesize=100000;
	m_filenr=2;
	m_groupinnewfile=false;
	m_maxlevel=LOG_LEVEL_DEFAULT;

	initFromConfig();
}
Beispiel #4
0
/*!
    Constructs a new QUsbStorageGadgetProvider in \a group and attach it to \a parent.
*/
QUsbStorageGadgetProvider::QUsbStorageGadgetProvider(const QString &group, QObject *parent)
    : QUsbStorageGadget(group, parent, Server)
{
    d = new QUsbStorageGadgetProviderPrivate;

    QSettings settings("Trolltech", "Usb");

    QByteArray function = settings.value("PeripheralController/Path").toByteArray() +
                                         "/gadget/function";

    QFile file(function);
    if (file.open(QIODevice::ReadOnly | QIODevice::Text) &&
        file.readLine().trimmed() == "File-backed Storage Gadget") {
        initFromSystem();
    } else {
        initFromConfig();
    }

    setValue("gadget", QByteArray(GADGET_NAME));
}
PlotControlWindow::PlotControlWindow(const int plotId, PlotArea *parent) :
    QMainWindow(parent),
    ui(new Ui::PlotControlWindow)
{
    ui->setupUi(this);
    mPlot = parent->getPlotWidget();
    mPlotId = plotId;

    init();
    initFromConfig();

    // Dialog
    connect(this->ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(this->ui->buttonBox, SIGNAL(accepted()), this, SLOT(close()));
    connect(this->ui->buttonBox, SIGNAL(accepted()), parent, SLOT(plotConfigChanged()));
    connect(this->ui->buttonBox, SIGNAL(rejected()), this, SLOT(close()));
    QPushButton* applyButton = ui->buttonBox->button(QDialogButtonBox::Apply);
    connect(applyButton, SIGNAL(clicked()), this, SLOT(apply()));
    connect(ui->autoOrdinate, SIGNAL(toggled(bool)), this, SLOT(autoOrdinateChecked(bool)));

    // Curve page
    connect(ui->newCurveButton, SIGNAL(clicked()), this, SLOT(newCurve()));
    connect(ui->curveSelection, SIGNAL(doubleClicked ( const QModelIndex &)), this, SLOT(editCurve(const QModelIndex &)));
    connect(ui->deleteCurveButton, SIGNAL(clicked()), this, SLOT(deleteSelectedCurve()));

    // Function page
    connect(ui->functionNew, SIGNAL(clicked()), this, SLOT(newFunction()));
    connect(ui->functionHierarchicalNew, SIGNAL(clicked()), this, SLOT(newHierarachicalFunction()));
    connect(ui->functionIntegralNew, SIGNAL(clicked()), this, SLOT(newIntegralFunction()));
    connect(ui->functionDifferentialNew, SIGNAL(clicked()), this, SLOT(newDifferentialFunction()));
    connect(ui->functionView, SIGNAL(doubleClicked ( const QModelIndex &)), this, SLOT(editFunction(const QModelIndex &)));
    connect(ui->functionDelete, SIGNAL(clicked()), this, SLOT(deleteFunction()));
    connect(ui->functionEditCurve, SIGNAL(clicked()), this, SLOT(editFunctionCurve()));

    setWindowModality(Qt::NonModal);
}
int main (int argc, char * argv[]) {
  if (argc < 7) {
	printf("Usage : server config portnumber ttl infinity period isSplitHorizon\n");
	printf("ttl should be less than 65536\n");
	printf("infinity should be less than 65536\n");
	printf("period should be less than 65536\n");
	printf("isSplitHorizon is 0 or 1, 0 means to disable split horizon\n");
	return 1;
  }
  char *config  = argv[1];
  int portNum   = atoi(argv[2]);
  int ttl       = atoi(argv[3]);
  int infinity  = atoi(argv[4]);
  int period    = atoi(argv[5]);
  int isSH      = atoi(argv[6]);

  //create socket and bind to port
  int sockFd = socket(AF_INET, SOCK_DGRAM, 0);
  struct sockaddr_in addr_in;
  int addrin_len = sizeof(struct sockaddr_in);
  addr_in.sin_family = AF_INET;
  addr_in.sin_port   = htons(portNum);
  addr_in.sin_addr.s_addr   = INADDR_ANY;

  if (bind(sockFd, (struct sockaddr *) &addr_in, (socklen_t)addrin_len) == 0) {
	printf("bind successfully to port %u \n", portNum);
  } else {
	printf("failed to bind to port %u \n", portNum);
	return 1;
  }

  //test ip convert
  //char ipText[256] = "156.56.83.24";
  //uint8_t ipResult[256] = {0};
  //convertIpText(ipText, ipResult);
  //printf("%u, %u, %u, %u\n", ipResult[0], ipResult[1], ipResult[2], ipResult[3]);

  // read config file and initialize 
  Panel dvPanel;
  dvPanel.port      = portNum;
  dvPanel.sockFd    = sockFd;
  dvPanel.ttl       = ttl;
  dvPanel.infinity  = infinity;
  dvPanel.isSH      = isSH;
  dvPanel.period    = period;
  initPanel(&dvPanel);

  if (initFromConfig(&dvPanel, config) == 0) {
	printf("initialize from config %s successfully\n", config);
	echoProfile(&dvPanel);
  } else {
	printf("failed to initialize from config %s\n", config);
	return 1;
  }

  // set up a new thread for periodically sending out update msg
  pthread_t updateThread;
  pthread_create(&updateThread, NULL, periodUpdate, (void *) &dvPanel);

  //set up a new thread for periodically minus ttl of forward records
  pthread_t ttlThread;
  pthread_create(&ttlThread, NULL, ttlCheck, (void *)&dvPanel);

  // recursively receive update msg and send trigger update msg
  while (1) {

	//recv update msg
	struct sockaddr_in tempAddr;
	int addr_len = sizeof(struct sockaddr_in);
	unsigned char buffer[2046] = {0};
	int recvLen = 0;
	if ((recvLen = recvfrom(sockFd, buffer, sizeof buffer,0,  (struct sockaddr *) &tempAddr, (socklen_t *)&addr_len)) <= 0) {
	  printf("recv update msg failed\n");
	  close(sockFd);
	  return 1;
	}
	//process update msg
	if (processUpdateMsg(&dvPanel, buffer, recvLen, (uint32_t) (tempAddr.sin_addr.s_addr)) != 0) {
	  printf("process update msg error\n");
	  close(sockFd);
	  return 1;
	}

	//trigger update msg sending
	if (triggerUpdateMsg(&dvPanel) != 0) {
	  printf("trigger update msg error\n");
	  close(sockFd);
	  return 1;
	}
  }
}
Beispiel #7
0
int main(int argc,char* argv[]){
   int port,sock,serverlen,clientlen;
   struct sockaddr_in server,client;
   struct sockaddr* serverptr;
   struct sockaddr* clientptr;
   struct hostent* rem;
   int        i=0;                               
   config=(char**)malloc(sizeof(char*)*3);
   config[PORT]=(char*)malloc(sizeof(char)*MAX_CONFIG_LINE);
   config[PATH]=(char*)malloc(sizeof(char)*MAX_CONFIG_LINE);
   config[THREADS]=(char*)malloc(sizeof(char)*MAX_CONFIG_LINE);
   initFromConfig(config);
   int        thr_id[atoi(config[THREADS])];      
   pthread_t  p_threads[atoi(config[THREADS])];   	
   mutexes = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t) * atoi(config[THREADS]));
   conditions = (pthread_cond_t *) malloc(sizeof(pthread_cond_t) * atoi(config[THREADS]));
   sockets=(int*)malloc(sizeof(int)*atoi(config[THREADS]));

   if((sock=socket(PF_INET,SOCK_STREAM,0))<0){
      perror("Error in socket().");
      exit(-1);
   }

   port=atoi(config[PORT]);

   server.sin_family=PF_INET;
   server.sin_addr.s_addr=htonl(INADDR_ANY);
   server.sin_port=htons(port);
   
   serverptr=(struct sockaddr*) &server;
   serverlen=sizeof(server);

   if(bind(sock,serverptr,serverlen)<0){
      perror("Error in bind().");
      exit(-1);
   }
   
   if(listen(sock,MAX_QUEUED)){
      perror("Error in listen()");
      exit(-1);      
   }
   printf("Now listening on %d\n",port);
	
	for (i=0; i<atoi(config[THREADS]); i++) {
		thr_id[i] = i;
		sockets[i]=0;
		pthread_create(&p_threads[i], NULL, &handle_requests_loop, (void*)&thr_id[i]);
		pthread_mutex_init(&mutexes[i], NULL);
		pthread_cond_init(&conditions[i], NULL);
   }
	
   while(1){
	   i = 0;
		while (sockets[i] != 0) {
			i++;
			if (i == atoi(config[THREADS]))
				i = 0;
		}
	   
      clientptr=(struct sockaddr*) &client;
      clientlen=sizeof(client);
	
      if((sockets[i]=accept(sock,clientptr,&clientlen))<0){
         perror("Error in accept().");
         exit(-1);
      }

      if((rem=gethostbyaddr((char*)&client.sin_addr.s_addr,sizeof(client.sin_addr.s_addr),client.sin_family))==NULL){
         herror("Error in gethostbyadd()");
         
exit(-1);
      }
      
      pthread_cond_signal(&conditions[i]);
		
		
      /*
      char* req_token; 
      char method[MAX_SOCK];
      char path[MAX_SOCK];
      const char del[3] = "\r\n";
      char klakla[MAX_SOCK];

      strcpy(klakla,buffer);
      req_token=strtok(klakla,del);
      
      strcpy(method,strtok(req_token," "));
      method[strlen(method)]='\0';

      strcpy(path,strtok(NULL," "));
      path[strlen(path)]='\0';
      
      write(new_socket, "HTTP/1.1 200 OK\n", 15);
      write(new_socket, "Content-length: 46\n", 19);
      write(new_socket, "Content-Type: text/html\n\n", 25);
      write(new_socket, "<html><body><H1>",16);
      write(new_socket, method, strlen(method));
      write(new_socket, " ",1);
      write(new_socket, path, strlen(path));
      write(new_socket, "</H1></body></html>",19);
      */
   }
   close(sock);
}
Beispiel #8
0
int main(int argc, char *argv[]) {
	SPConfig config = NULL;						    // hold configuration parameters
	char config_filename[CONFIG_FILE_PATH_SIZE];    // the configuration file name
	int knn;										// the number of similar features in each image to find (spKNN from configuration file)
	int num_of_similar_images_to_find;              // the number of similar images (to the query) to find (from configuration file)
	int split_method;                               // holds an int representing the split method: 0=RANDOM, 1= MAX_SPREAD,  2=INCREMENTAL
	bool minGui = false;                            // value of the system variable MinimalGui
	bool extraction_mode;							// indicates if extraction mode on or off
	int num_of_images = 0;   					    // number of images in the directory given by the user in the configuration file
	char** all_images_paths = NULL;					// array with the paths to all the images
	
	int last_extracted_feature = 0;  				// helper - holds the last feature extracted in order to free all extracted features on error
	SPPoint** features_per_image = NULL;   			// helper - holds the features for each images
	int* num_of_features_per_image = NULL;			// holds number of features extracted for each image
	
	char query_image[CONFIG_FILE_PATH_SIZE];        // the query image 
	SPPoint* query_features = NULL;				    // all query features
	int query_num_of_features;					    // number of features in query image
	
	KDTreeNode kd_tree = NULL;						// array holds a KDTree for the images
	int* closest_images = NULL;  				    // array holds the spNumOfSimilarImages indexes of the closest images to the query image
	int print_result;   							// holds the result of the call to PrintMinGuiFalse
	
	int retval = 0;									 // return value - default 0 on success
	char string_holder[CONFIG_FILE_PATH_SIZE];       // helper to hold strings
	sp::ImageProc *improc = NULL;
	SP_CONFIG_MSG msg;
	int i;
	int j;
	int n;

	// validate command line arguments:
	// cmd line arguments are ok if there was no arguments specified (argc == 1) or two arguments specified ( -c and filname)
	if (argc != 3 && argc != 1) {
		printf(INVALID_CMD_LINE_MSG);
		return -1;
	}

	if (argc == 1) {
		strcpy(config_filename, DEFAULT_CONFIG_FILENAME);
		config = spConfigCreate(config_filename, &msg);
		if (msg == SP_CONFIG_CANNOT_OPEN_FILE) {
			printf(ERROR_OPENING_DEFAULT_CONFIG_FILE_MSG, DEFAULT_CONFIG_FILENAME);
		}

		if (msg != SP_CONFIG_SUCCESS) {
			retval = -1;
			goto err; // error is printed inside spConfigCreate
		}
	}
	else { // argc == 3

		// check that second argument is the -c flag
		if (strcmp(argv[1], CMD_LINE_CONFIG_FILENAME_FLAG) != 0) {
			printf(INVALID_CMD_LINE_MSG);
			retval = -1;
			goto err;
		}
	
		strcpy(config_filename, argv[2]);
		config = spConfigCreate(config_filename, &msg);
		if (msg == SP_CONFIG_CANNOT_OPEN_FILE) {
			printf(ERROR_OPENING_CONFIG_FILE_MSG, config_filename);
		}

		if (msg != SP_CONFIG_SUCCESS) {
			retval = -1;
			goto err; // error is printed inside spConfigCreate
		}
	}
	
	// initiate from config
	if (initFromConfig(config, &num_of_images, &num_of_similar_images_to_find, &knn, &split_method, &extraction_mode, &minGui, &all_images_paths) == -1 ) {
		retval = -1; 
		goto err; // error is printed inside initFromConfig 
	}


	// initiate image proc
	improc = new sp::ImageProc(config);

	// extract images features
	if ((num_of_features_per_image = (int*)malloc(sizeof(*num_of_features_per_image) * num_of_images)) == NULL) {
		spLoggerPrintError(ALLOCATION_FAILURE_MSG, __FILE__, __func__, __LINE__);
		retval = -1;
		goto err;
	}

	spLoggerPrintInfo(CHECK_EXTRACTION_MODE_INFO_LOG);
	if (extraction_mode) {	// extraction mode is chosen
		spLoggerPrintMsg(USE_EXTRACTION_MODE_LOG);
		spLoggerPrintInfo(EXTRACT_IMAGES_FEATURES_INFO_LOG);

		if ((features_per_image = (SPPoint**)malloc(sizeof(*features_per_image) * num_of_images)) == NULL) {
			spLoggerPrintError(ALLOCATION_FAILURE_MSG, __FILE__, __func__, __LINE__);
			retval = -1;
			goto err;
		}

		// extract each image features and write them to file
		for (i=0; i < num_of_images; i++) {	
			// extract image features
			if ((features_per_image[i] = improc->getImageFeatures(all_images_paths[i], i, &(num_of_features_per_image[i]))) == NULL) {
				last_extracted_feature = i;
				retval = -1;
				goto err; // error is printed inside  getImageFeatures
			}
		}

		if (saveToDirectory(config, features_per_image, num_of_features_per_image, num_of_images) == -1) {
			retval = -1;
			goto err; // error is printed inside  saveToDirectory
		}
	}

	else { // not extraction mode
		spLoggerPrintMsg(USE_NOT_EXTRACTION_MODE_LOG);
		spLoggerPrintInfo(READ_FEATURES_FROM_FILE_LOG);

		if ((features_per_image = extractFromFiles(config, num_of_features_per_image, num_of_images)) == NULL) {
			retval = -1;
			goto err; // error is printed inside  extractFromFiles
		}
	}
	
	if ((kd_tree = initiateDataStructures(features_per_image, num_of_features_per_image, num_of_images, split_method)) == NULL) {
		retval = -1;
		goto err; // error is printed inside initiateDataStructures
	}

	query:
	while(1) {
		// get a query image from the user
		printf(ENTER_AN_IMAGE_MSG);
		fflush(NULL);
		scanf("%s",query_image);

		// exit if user asked
		if (strcmp (query_image,EXIT_SIGN) == 0) {
			printf(EXIT_MSG);
			fflush(NULL);
			goto err; // free memory and quit 
		}

		if( access( query_image, F_OK ) == -1 ) {
		    printf(FILE_DOESNT_EXIST, query_image);
			goto query;
		}

		// extract query image features
		spLoggerPrintMsg(EXTRACT_QUERY_IMAGE_FEATURES_LOG);
		if ((query_features = improc->getImageFeatures(query_image, num_of_images, &query_num_of_features)) == NULL) {
			retval = -1;
			goto err_inside_loop; // error log is printed inside getImageFeatures	
		}
		
		// print debug log
		if ((n = sprintf(string_holder, NUM_OF_EXTRACTED_FEATURES_DEBUG_LOG, query_num_of_features)) < 0) {
			spLoggerPrintError(GENERAL_ERROR_MSG, __FILE__, __func__, __LINE__);
			retval = -1;
			goto err_inside_loop;
		}

		spLoggerPrintDebug(string_holder, __FILE__, __func__, __LINE__);
		
		//  print log message
		if ((n = sprintf(string_holder, SEARCING_SIMILAR_IMAGES_MSG, num_of_similar_images_to_find)) < 0) {
			spLoggerPrintError(GENERAL_ERROR_MSG, __FILE__, __func__, __LINE__);
			retval = -1;
			goto err_inside_loop;
		}

		spLoggerPrintMsg(string_holder);

		// find similar images to the query image
		closest_images = getKClosestImages(num_of_similar_images_to_find, knn, query_features,
										   kd_tree, query_num_of_features, num_of_images);

		if (closest_images == NULL) { 
			retval = -1;
			goto err_inside_loop; // error is printed to inside getKClosestImages
		}

		// show (display) closest_images images

		//need to show images
		if (minGui==true){ 
			for (i=0; i<num_of_similar_images_to_find; i++){
				//get file path of the images by the indexes in closest_images
				improc->showImage(all_images_paths[closest_images[i]]);
			}
		}

		// i.e. minGui==false,  just need to print images path
		else{
			print_result = PrintMinGuiFalse(query_image, num_of_similar_images_to_find, all_images_paths, closest_images);
			if (print_result == 0) {
				retval = -1;
				goto err_inside_loop; // error is printed inside 
			}

		}
		// free memory before entering the loop again
		
		free(closest_images);
		if (query_features != NULL) {
			for (i=0; i<query_num_of_features; i++) {
				spPointDestroy(query_features[i]);
			}
			free(query_features);
		}
	}

	err_inside_loop:	
		free(closest_images);
		// free query_features
		if (query_features != NULL) {
			for (i=0; i<query_num_of_features; i++) {
				spPointDestroy(query_features[i]);
			}
			free(query_features);
		}

	// done - destroy logger and free everything 
	err:
		spLoggerDestroy();

		// free the kd tree
		DestroyKDTreeNode(kd_tree);
		spConfigDestroy(config);

		// free all images paths
		if (all_images_paths != NULL) {
			for (i = 0; i < num_of_images; i ++) {
				free(all_images_paths[i]);
			}
			free(all_images_paths);
		}

		if (features_per_image != NULL) {
			// free features_per_image
			for (i = 0; i < last_extracted_feature; i ++) {
				if (features_per_image[i] != NULL) {
					for (j = 0; j < num_of_features_per_image[i]; j++) {
						spPointDestroy(features_per_image[i][j]);
					}
				}
			}
			free(features_per_image);
		}
		free(num_of_features_per_image); // must be freed after features_per_image
		if (improc != NULL) {
			delete improc;
		}

	return retval;
}