Ejemplo n.º 1
0
Archivo: options.cpp Proyecto: hyps/DGT
void OptionHost::init(std::string addrPort)
{
  std::string origAddrPort(addrPort);
  size_t pos = addrPort.find_first_of("[");

  if(pos != std::string::npos && pos != 0) {
    throw syntax_error(origAddrPort, pos);  // an [ was found but not at the beginning;
  }

  bool hasPort = false;
  if(pos != std::string::npos) {
    addrPort.erase(pos, 1);
    pos = addrPort.find_first_of("]");

    if(pos == std::string::npos) {
      throw syntax_error(origAddrPort, origAddrPort.length());  //no trailing ] although an leading [ was found
    }

    if(pos < addrPort.length()-2) {
      if(addrPort[pos+1] != ':') {
        throw syntax_error(origAddrPort, pos+2);  // wrong port delimieter
      }

      addrPort[pos+1] = '/';
      hasPort = true;
    } else if(pos != addrPort.length()-1) {
      throw syntax_error(origAddrPort, pos+2);  // too few characters left
    }

    addrPort.erase(pos, 1);
  } else {
    pos = addrPort.find_first_of(":");
    if(pos != std::string::npos && pos == addrPort.find_last_of(":")) {
      // an ':' has been found and it is the only one -> assuming port present
      hasPort = true;
      addrPort[pos] = '/';
    }
  }

  if(hasPort) {
    std::stringstream tmp_stream(addrPort);

    getline(tmp_stream, addr, '/');
    if(!tmp_stream.good()) {
      throw syntax_error(origAddrPort, addr.length());
    }

    tmp_stream >> port;
  } else {
Ejemplo n.º 2
0
/*
	- result data is sent as a string which ends with "\n\r"
	- the Teamspeak3 server can send multiple strings
	- the end of a result set is always an error result string
*/
void CNetwork::OnRead(const boost::system::error_code &error_code)
{
	if (error_code.value() == 0)
	{
		static vector<string> captured_data;
		std::istream tmp_stream(&m_ReadStreamBuf);
		string read_data;
		std::getline(tmp_stream, read_data, '\r');

#ifdef _DEBUG
		string dbg_read_data(read_data);
		bool first_line = true;
		do
		{
			logprintf("%s> %s", 
				first_line == true ? ">>>" : "   ",
				dbg_read_data.substr(0, 512).c_str());
			dbg_read_data.erase(0, 512);
			first_line = false;
		} while (dbg_read_data.empty() == false);
#endif

		//regex: parse error
		//if this is an error message, it means that no other result data will come
		static const boost::regex error_rx("error id=([0-9]+) msg=([^ \n]+)");
		boost::smatch error_rx_result;
		if (boost::regex_search(read_data, error_rx_result, error_rx))
		{
			if (error_rx_result[1].str() == "0")
			{
				for (auto i = captured_data.begin(); i != captured_data.end(); ++i)
				{
					string &data = *i;
					if (data.find('|') == string::npos) 
						continue;

					//we have multiple data rows with '|' as delimiter here,
					//split them up and re-insert every single row
					vector<string> result_set;
					size_t delim_pos = 0;
					do
					{
						size_t old_delim_pos = delim_pos;
						delim_pos = data.find('|', delim_pos);
						string row = data.substr(old_delim_pos, delim_pos - old_delim_pos);
						result_set.push_back(row);
					} while (delim_pos != string::npos && ++delim_pos);

					i = captured_data.erase(i);
					for (auto j = result_set.begin(), jend = result_set.end(); j != jend; ++j)
						i = captured_data.insert(i, *j);
				}
				
				//call callback and send next command
				m_CmdQueueMutex.lock();
				if (m_CmdQueue.empty() == false)
				{
					ReadCallback_t &callback = m_CmdQueue.front().get<1>();
					if (callback)
					{
						m_CmdQueueMutex.unlock();
						callback(captured_data); //calls the callback
						m_CmdQueueMutex.lock();
					}
					m_CmdQueue.pop();

					if (m_CmdQueue.empty() == false)
						AsyncWrite(m_CmdQueue.front().get<0>());
				}
				m_CmdQueueMutex.unlock();
			}
			else
			{
				string error_str(error_rx_result[2].str());
				unsigned int error_id = 0;

				CUtils::Get()->UnEscapeString(error_str);
				CUtils::Get()->ConvertStringToInt(error_rx_result[1].str(), error_id);

				m_CmdQueueMutex.lock();

				CCallbackHandler::Get()->ForwardError(
					EErrorType::TEAMSPEAK_ERROR, error_id,
					fmt::format("error while executing \"{}\": {}", m_CmdQueue.front().get<0>(), error_str));

				m_CmdQueue.pop();

				if (m_CmdQueue.empty() == false)
					AsyncWrite(m_CmdQueue.front().get<0>());

				m_CmdQueueMutex.unlock();
			}

			captured_data.clear();
		}
		else if (read_data.find("notify") == 0)
		{
			//check if notify is duplicate
			static string last_notify_data;
			static const vector<string> duplicate_notifies{ 
				"notifyclientmoved", 
				"notifycliententerview", 
				"notifyclientleftview" 
			};
			bool is_duplicate = false;
			
			for (auto &s : duplicate_notifies)
			{
				if (read_data.find(s) == 0)
				{
					if (last_notify_data == read_data)
						is_duplicate = true;
					
					break;
				}
			}
			
			if (is_duplicate == false)
			{
				//notify event
				boost::smatch event_result;
				for (auto &event : m_EventList)
				{
					if (boost::regex_search(read_data, event_result, event.get<0>()))
					{
						event.get<1>()(event_result);
						break;
					}
				}
			}

			last_notify_data = read_data;
		}
		else
		{
			//stack the result data if it is not an error or notification message
			captured_data.push_back(read_data);
		}

		AsyncRead();
	}
	else //error
	{
		CCallbackHandler::Get()->ForwardError(
			EErrorType::CONNECTION_ERROR, error_code.value(),
			fmt::format("error while reading: {}", error_code.message()));

		//"disable" the plugin, since calling Disconnect() or
		//destroying CNetwork here is not very smart
		CServer::CSingleton::Destroy();
		m_Connected = false; //we're not _really_ connected, are we?
	}
}
Ejemplo n.º 3
0
int load_trainfile(const char* fname,HashMap* wl,REAL** xbin,REAL** ybin)
{
	CONLLReader* reader = new CONLLReader();
	reader->startReading(fname);

	cout << "------2.Opening train file '" << fname << "'" << endl;
	HashMap all_features(CONF_train_feat_map_size);
	vector<int> all_feat_freq;
	vector<int> all_feat_ashead;
	vector<string* > all_feat_str;
	//vector<vector<int>* > all_feat_windex;

	//read all
	DependencyInstance* x = reader->getNext();
	int sentence_count=0,feat_count=0,tokens_count=0;
	while(x != NULL){
		sentence_count++;
		if(sentence_count % 1000 == 0)
			cout << "--Reading sentence " << sentence_count << endl;
		int length = x->forms->size();
		tokens_count += length;
		//--deal with all pairs
		for(int i=0;i<length;i++){
			for(int j=i+1;j<length;j++){
				for(int lr=0;lr<2;lr++){
					//seperated by space
					string *tmp_str = 0;
					if(lr==E_LEFT)
						tmp_str = get_feature(x,j,i);
					else
						tmp_str = get_feature(x,i,j);
					//add it or already there
					HashMap::iterator iter = all_features.find(tmp_str);
					if(iter == all_features.end()){
						all_features.insert(pair<string*, int>(tmp_str, feat_count));
						all_feat_str.push_back(tmp_str);
						all_feat_freq.push_back(1);
						all_feat_ashead.push_back(0);
						feat_count++;
					}
					else{
						int where = iter->second;
						all_feat_freq[where] += 1;
						delete tmp_str;
					}
				}
			}
		}
		//--add positive samples
		for(int i=1;i<length;i++){
			//add some scores first
			int head = (*x->heads)[i];
			string* tmp = get_feature(x,head,i);
			HashMap::iterator iter = all_features.find(tmp);
			all_feat_ashead[iter->second] += 1;
			delete tmp;
		}
		//next
		delete x;
		x = reader->getNext();
	}
	reader->finishReading();
	delete reader;
	cout << "-Training data:\n"
			<< "--Sentences: " << sentence_count << '\n'
			<< "--Tokens: " << tokens_count << '\n'
			<< "--Feats: " << feat_count << '\n';

	//maybe calculating
	cout << "-Transfering as binarys." << endl;
	(*xbin) = new REAL[CONF_X_dim*feat_count];
	(*ybin) = new REAL[CONF_Y_dim*feat_count];
	string unknown_token = UNKNOW_WORD;
	for(int i=0;i<feat_count;i++){
		//get x
		istringstream tmp_stream(*(all_feat_str.at(i)));
		for(int j=0;j<CONF_X_dim;j++){
			string tmp_find;
			tmp_stream >> tmp_find;
			REAL the_index=0;
			HashMap::iterator iter = wl->find(&tmp_find);
			if(iter == wl->end()){
				cout << "--Strange word not find " << tmp_find << endl;
				the_index = wl->find(&unknown_token)->second;
			}
			else
				the_index = iter->second;
			(*xbin)[j+i*CONF_X_dim] = the_index;
		}
		//get y
		for(int j=0;j<CONF_Y_dim;j++){
			//scoring method
			int it = i*CONF_Y_dim+j;
			int how_many = all_feat_ashead[it];
			REAL tmp = SCO_INIT;
			for(int k=0;k<how_many;k++){
				tmp += (SCO_STEP/2)*(1+((double)rand()/RAND_MAX));
				if(tmp>SCO_MAX){
					tmp = SCO_MAX;
					break;
				}
			}
			(*ybin)[it] = tmp;
		}
	}

	//additional debug
#ifdef DEBUG_PRETRAINING
	extern void debug_pretraining_write(REAL* ,REAL* ,int );
	extern void debug_pretraining_evaluate(int ,REAL*,HashMap*);
	//debug_pretraining_write(*xbin,*ybin,feat_count);
	debug_pretraining_evaluate(feat_count,*ybin,&all_features);
#endif

	//cleanup
	for(int i=0;i<feat_count;i++){
		delete all_feat_str[i];
		//delete all_feat_windex[i];
	}
	cout << "------2.Done with train file '" << fname << "'" << endl;
	return feat_count;
}
Ejemplo n.º 4
0
  Invocation_Status
  DII_Invocation::handle_user_exception (TAO_InputCDR &cdr)
  {
    Reply_Guard mon (this, TAO_INVOKE_FAILURE);

    if (TAO_debug_level > 3)
      {
        TAOLIB_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) - DII_Invocation::"
                    "handle_user_exception\n"));
      }

    // Match the exception interface repository id with the
    // exception in the exception list.
    // This is important to decode the exception.
    CORBA::String_var buf;

    TAO_InputCDR tmp_stream (cdr,
                             cdr.start ()->length (),
                             0);

    // Pull the exception ID out of the marshaling buffer.
    if (tmp_stream.read_string (buf.inout ()) == 0)
      {
        throw ::CORBA::MARSHAL (TAO::VMCID, CORBA::COMPLETED_YES);
      }

    for (CORBA::ULong i = 0;
         this->excp_list_ != 0 && i < this->excp_list_->count ();
         i++)
      {
          CORBA::TypeCode_var tc = this->excp_list_->item (i);

          const char *xid = tc->id ();

          if (ACE_OS::strcmp (buf.in (), xid) != 0)
            {
              continue;
            }

          CORBA::Any any;
          TAO::Unknown_IDL_Type *unk = 0;
          ACE_NEW_RETURN (unk,
                          TAO::Unknown_IDL_Type (
                              tc.in (),
                              cdr
                            ),
                          TAO_INVOKE_FAILURE);

          any.replace (unk);

          mon.set_status (TAO_INVOKE_USER_EXCEPTION);

          throw ::CORBA::UnknownUserException (any);
        }

    // If we couldn't find the right exception, report it as
    // CORBA::UNKNOWN.

    // But first, save the user exception in case we
    // are being used in a TAO gateway.
    this->host_->raw_user_exception (cdr);

    mon.set_status (TAO_INVOKE_USER_EXCEPTION);

    // @@ It would seem that if the remote exception is a
    //    UserException we can assume that the request was
    //    completed.
    throw ::CORBA::UNKNOWN (TAO::VMCID, CORBA::COMPLETED_YES);

  }
Ejemplo n.º 5
0
int main(int argc, const char** argv) {
  // start logs
  printf("clDeviceQuery Starting...\n\n");
  bool bPassed = true;
  std::string sProfileString = "clDeviceQuery, Platform Name = ";

  // Get OpenCL platform ID for NVIDIA if avaiable, otherwise default
  char cBuffer[1024];
  cl_platform_id clSelectedPlatformID = NULL;
  cl_platform_id* clPlatformIDs;

  cl_uint num_platforms;
  cl_int ciErrNum = clGetPlatformIDs(0, NULL, &num_platforms);
  if (ciErrNum != CL_SUCCESS) {
    printf(" Error %i in clGetPlatformIDs Call!\n\n", ciErrNum);
    bPassed = false;
  } else {
    if (num_platforms == 0) {
      printf("No OpenCL platform found!\n\n");
      bPassed = false;
    } else {
      // if there's one platform or more, make space for ID's
      if ((clPlatformIDs = (cl_platform_id*)malloc(num_platforms * sizeof(cl_platform_id))) == NULL) {
	printf("Failed to allocate memory for cl_platform ID's!\n\n");
	bPassed = false;
      }

      printf("%d OpenCL Platforms found\n\n", num_platforms);
      // get platform info for each platform
      ciErrNum = clGetPlatformIDs (num_platforms, clPlatformIDs, NULL);
      for(cl_uint i = 0; i < num_platforms; ++i) {
	ciErrNum = clGetPlatformInfo (clPlatformIDs[i], CL_PLATFORM_NAME, 1024, &cBuffer, NULL);
	if(ciErrNum == CL_SUCCESS) {
	  clSelectedPlatformID = clPlatformIDs[i];
	  // Get OpenCL platform name and version
	  ciErrNum = clGetPlatformInfo (clSelectedPlatformID, CL_PLATFORM_NAME, sizeof(cBuffer), cBuffer, NULL);
	  if (ciErrNum == CL_SUCCESS) {
	    printf(" CL_PLATFORM_NAME: \t%s\n", cBuffer);
	    sProfileString += cBuffer;
	  } else {
	    printf(" Error %i in clGetPlatformInfo Call !!!\n\n", ciErrNum);
    bPassed = false;
  }
  sProfileString += ", Platform Version = ";

  ciErrNum = clGetPlatformInfo (clSelectedPlatformID, CL_PLATFORM_VERSION, sizeof(cBuffer), cBuffer, NULL);
  if (ciErrNum == CL_SUCCESS) {
    printf(" CL_PLATFORM_VERSION: \t%s\n", cBuffer);
    sProfileString += cBuffer;
  } else {
    printf(" Error %i in clGetPlatformInfo Call !!!\n\n", ciErrNum);
    bPassed = false;
  }

  // Log OpenCL SDK Version # (for convenience:  not specific to OpenCL)
  sProfileString += ", NumDevs = ";

  // Get and log OpenCL device info
  cl_uint ciDeviceCount;
  cl_device_id *devices;
  printf("OpenCL Device Info:\n\n");
  ciErrNum = clGetDeviceIDs (clSelectedPlatformID, CL_DEVICE_TYPE_ALL, 0, NULL, &ciDeviceCount);

  // check for 0 devices found or errors...
  if (ciDeviceCount == 0) {
    printf(" No devices found supporting OpenCL (return code %i)\n\n", ciErrNum);
    bPassed = false;
    sProfileString += "0";
  } else if (ciErrNum != CL_SUCCESS) {
    printf(" Error %i in clGetDeviceIDs call !!!\n\n", ciErrNum);
    bPassed = false;
  } else {
    // Get and log the OpenCL device ID's
    ciErrNum = clGetPlatformInfo (clSelectedPlatformID, CL_PLATFORM_NAME, sizeof(cBuffer), cBuffer, NULL);
    printf(" %u devices found supporting OpenCL on: %s\n\n", ciDeviceCount, cBuffer);
    char cTemp[2];
    sprintf(cTemp, "%u", ciDeviceCount);
    sProfileString += cTemp;
    if ((devices = (cl_device_id*)malloc(sizeof(cl_device_id) * ciDeviceCount)) == NULL) {
      printf(" Failed to allocate memory for devices !!!\n\n");
      bPassed = false;
    }
    ciErrNum = clGetDeviceIDs (clSelectedPlatformID, CL_DEVICE_TYPE_ALL, ciDeviceCount, devices, &ciDeviceCount);
    if (ciErrNum == CL_SUCCESS) {
      for(unsigned int i = 0; i < ciDeviceCount; ++i )  {
        printf(" ----------------------------------\n");
clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(cBuffer), &cBuffer, NULL);
printf(" Device %s\n", cBuffer);
printf(" ---------------------------------\n");
clPrintDevInfo(devices[i]);
sProfileString += ", Device = ";
sProfileString += cBuffer;
      }
            } else {
      printf(" Error %i in clGetDeviceIDs call !!!\n\n", ciErrNum);
      bPassed = false;
    }
  }

  // masterlog info
  sProfileString += "\n";
  printf("%s", sProfileString.c_str());
}
free(clPlatformIDs);
      }
    }
  }

  // Log system info(for convenience:  not specific to OpenCL)
  printf( "\nSystem Info: \n\n");
  char timestr[255];
  time_t now = time(NULL);
  struct tm  *ts;

  ts = localtime(&now);

  strftime(timestr, 255, " %H:%M:%S, %m/%d/%Y",ts);

  // write time and date to logs
  printf(" Local Time/Date = %s\n", timestr);
  // write proc and OS info to logs
  // parse /proc/cpuinfo
  std::ifstream cpuinfo( "/proc/cpuinfo" ); // open the file in /proc
  std::string tmp;

  int cpu_num = 0;
  std::string cpu_name = "none";
  do {
    cpuinfo >> tmp;

    if( tmp == "processor" )
      cpu_num++;

    if( tmp == "name" ) {
      cpuinfo >> tmp; // skip :

      std::stringstream tmp_stream("");
      do {
	cpuinfo >> tmp;
	if (tmp != std::string("stepping")) {
	  tmp_stream << tmp.c_str() << " ";
	}

      }
      while (tmp != std::string("stepping"));

      cpu_name = tmp_stream.str();
    }
  }
  while ( (! cpuinfo.eof()) );

  // Linux version
  std::ifstream version( "/proc/version" );
  char versionstr[255];

  version.getline(versionstr, 255);

  printf(" CPU Name: %s\n # of CPU processors: %u\n %s\n\n\n",
	 cpu_name.c_str(),cpu_num,versionstr);

  // finish
  printf("TEST %s\n\n", bPassed ? "PASSED" : "FAILED !!!");
}