//	===========================================================================
//	METHOD: authdclient::installfile
//	===========================================================================
int authdclient::installfile (const string &source, const string &dest)
{
	string command;

	command.printf ("installfile \"%S\" \"%S\"\n", source.str(), dest.str());
	
	return executecmd (command);
}
//	===========================================================================
//	METHOD: authdclient::adduser
//	===========================================================================
int authdclient::adduser (const string &username,
				  		const string &shell, const string &passwd)
{
	string command;

	command.printf ("createuser \"%S\" \"%S\"\n", username.str(), passwd.str());
	
	return executecmd (command);
}
//	===========================================================================
//	METHOD: authdclient::setusershell
//	===========================================================================
int authdclient::setusershell (const string &username, const string &shell)
{
	string command;

	command.printf ("setusershell \"%S\" \"%S\"\n", username.str(), 
				    shell.str());
			  
	return executecmd (command);
}
//	===========================================================================
//	METHOD: authdclient::setusershell
//	===========================================================================
int authdclient::setuserpass (const string &username, const string &password)
{
	string command;

	command.printf ("setuserpass \"%S\" \"%S\"\n", username.str(), 
				    password.str());
			  
	return executecmd (command);
}
Example #5
0
void readEdges(Graph<int> &g, GraphViewer *gv, string s) {
	ifstream inFile;

	//Ler o ficheiro nos.txt
	inFile.open(s.c_str());

	if (!inFile) {
		cerr << "Unable to open file " << s << endl;
		exit(1);   // call system to stop
	}

	string line;

	int idAresta=0;
	int peso=0;
	int idNoOrigem=0;
	int idNoDestino=0;

	while(std::getline(inFile, line))
	{
		std::stringstream linestream(line);
		std::string data;

		std::stringstream s;

		linestream >> idAresta;

		std::getline(linestream, data, ';');  // read up-to the first ; (discard ;).
		linestream >> peso;
		std::getline(linestream, data, ';');  // read up-to the first ; (discard ;).
		linestream >> idNoOrigem;
		std::getline(linestream, data, ';');  // read up-to the first ; (discard ;).
		linestream >> idNoDestino;
		gv->addEdge(idAresta,idNoOrigem,idNoDestino, EdgeType::UNDIRECTED);

		s << peso;
		gv->setEdgeLabel(idAresta, s.str());

		s.str("");

		g.addEdge(idNoOrigem,idNoDestino,peso);

		//cout << "Edge: (" << idNoOrigem << ", " << idNoDestino << ") - " << peso << endl;

	}

	inFile.close();
}
//	===========================================================================
//	METHOD: authdclient::deletefile
//	===========================================================================
int authdclient::deletefile  (const string &conffile)
{
	string command;

	command.printf ("deletefile \"%S\"\n", conffile.str());
	
	return executecmd (command);
}
//	===========================================================================
//	METHOD: authdclient::setserviceonboot
//	===========================================================================
int authdclient::setserviceonboot (const string &servicename, bool enabled)
{
	string command;

	command.printf ("setonboot \"%S\" %d\n", servicename.str(), (int) enabled);
			  
	return executecmd (command);
}
//	===========================================================================
//	METHOD: authdclient::reloadservice
//	===========================================================================
int authdclient::reloadservice (const string &servicename)
{	
	string command;

	command.printf ("reloadservice \"%S\"\n", servicename.str());
			  
	return executecmd (command);
}
//	===========================================================================
//	METHOD: authdclient::deluser
//	===========================================================================
int authdclient::deluser (const string &username)
{
	string command;

	command.printf ("deleteuser \"%S\"\n", username.str());
	
	return executecmd (command);
}
//	===========================================================================
//	METHOD: authdclient::runscript
//	===========================================================================
int authdclient::runscript (const string &scriptname, const value &params)
{
	string command;

	command.printf ("runscript \"%S\"", scriptname.str());
	
	foreach (p, params)
	{
		command.printf (" \"%S\"", p.cval());
	}
//	===========================================================================
//	METHOD: authdclient::setquota
//	===========================================================================
int authdclient::setquota (const string &username, unsigned int softlimit, 
						 unsigned int hardlimit)
{
	string command;

	command.printf ("setquota \"%S\" %u %u\n", username.str(), 
				    softlimit, hardlimit);
			  
	return executecmd (command);
}
Example #12
0
// ==========================================================================
// METHOD udpsocket::sendto
// ==========================================================================
bool udpsocket::sendto (ipaddress addr, int port, const string &data)
{
	struct sockaddr_in6 remote_addr;

	memset(&remote_addr, 0, sizeof(remote_addr));	

	remote_addr.sin6_addr = addr;
	remote_addr.sin6_port = htons (port);
	remote_addr.sin6_family = AF_INET6;
	
	if (::sendto (sock, data.str (), data.strlen (), 0,
				(struct sockaddr *) &remote_addr, sizeof (remote_addr)) <0)
	{
		return false;
	}
	
	return true;
}
Example #13
0
bool sqlitehandle::query (const string &sql, value &into,
						  const statstring &indexby)
{
	sqlite3_stmt *qhandle;
	int qres;
	int rowcount=0;
	int colcount;
	int i;
	statstring curidx;
	bool done = false;
	
	into.clear ();
	
	if (sqlite3_prepare (hdl, sql.str(), sql.strlen(), &qhandle, 0) != SQLITE_OK)
	{
		errcode = 1;
		errstr = "Could not prepare: %s" %format (sqlite3_errmsg(hdl));
		return false;
	}
	
	if (! (qres = sqlite3_step (qhandle)))
	{
		errcode = 1;
		errstr = "Error making first step: %s" %format (sqlite3_errmsg(hdl));
		sqlite3_finalize (qhandle);
		return false;
	}
	
	colcount = sqlite3_column_count (qhandle);
	if (colcount == 0)
	{
		into("rowschanged") = sqlite3_changes (hdl);
		sqlite3_finalize (qhandle);
		return true;
	}
	
	statstring colnames[colcount];
	value colidx;
	
	for (i=0; i<colcount; ++i)
	{
		colnames[i] = sqlite3_column_name (qhandle, i);
		colidx[colnames[i]] = i;
	}
	
	int indexfield = -1;
	if (colidx.exists (indexby)) indexfield = colidx[indexby];
	
	if (! done) do
	{
		switch (qres)
		{
			case SQLITE_BUSY:
				sleep (1);
				sqlite3_reset(qhandle);
				continue;
			
			case SQLITE_MISUSE: // achtung, fallthrough
			case SQLITE_ERROR:
				errcode = 1;
				errstr = "Error in sqlite3_step: %s" %format (sqlite3_errmsg(hdl));
				done = true;
				break;

			case SQLITE_DONE:
				done = true;
				break;
				
			case SQLITE_ROW:
				{
					if (indexfield>=0)
					{
						curidx = sqlite3_column_text (qhandle, indexfield);
					}
					
					value &myrow = (indexfield<0) ? into.newval() : into[curidx];
					
					for (int i=0; i<colcount; i++)
					{
						int ctype = sqlite3_column_type (qhandle, i);
						statstring &curcol = colnames[i];
						
						switch (ctype)
						{
							case SQLITE_INTEGER:
								myrow[curcol] = sqlite3_column_int (qhandle, i);
								break;
							
							case SQLITE_FLOAT:
								myrow[curcol] = sqlite3_column_double (qhandle, i);
								break;
							
							case SQLITE_BLOB:
								// FIXME: use sqlite3_column_blob
							case SQLITE_TEXT:
								myrow[curcol] = sqlite3_column_text (qhandle, i);
								break;
							
							default:
								break;
						}
					}
				}
				break;
				
		}
		rowcount++;
	} while ((qres = sqlite3_step (qhandle)) && !done);
	
	int finalize_result = sqlite3_finalize (qhandle);
	if (finalize_result != SQLITE_OK && finalize_result != SQLITE_SCHEMA)
	{
		errcode = 1;
		errstr = "Error finalizing: %s" %format (sqlite3_errmsg(hdl));
		return false;
	}
	
	into ("insertid") = sqlite3_last_insert_rowid (hdl);
	into.type (t_dict);
	string tmp = into.tojson();
	return true;
}
Example #14
0
//*****************************************************************************
void BObject::LoadObjectFromFile(string sFilename, string sSection, bool bOnlyShape) {

  // General
  string sTmp;

  if(!bOnlyShape) {
    FileHelpers::GetKeyStringFromINIFile(sSection, "Name", sSection, m_sName, sFilename);
    FileHelpers::GetKeyStringFromINIFile(sSection, "Type", "USER_DEF", sTmp, sFilename);
    m_type = USER_DEF;

    FileHelpers::GetKeyStringFromINIFile(sSection, "ObjectFile", "default", m_sObjectFilename, sFilename);
    FileHelpers::GetKeyVectorFromINIFile(sSection, "Location", BVector(0, 0, 0), m_vLocation, sFilename);
    FileHelpers::GetKeyDoubleFromINIFile(sSection, "Scale", 1, m_dScale2, sFilename);
    FileHelpers::GetKeyDoubleFromINIFile(sSection, "ZRotation", 0, m_dZRotation, sFilename);
    FileHelpers::GetKeyStringFromINIFile(sSection, "Shadow", "True", sTmp, sFilename);
    m_bHasShadow = StringTools::compareNoCase(sTmp, "True");
    FileHelpers::GetKeyDoubleFromINIFile(sSection, "ActiveRadius", 1.0, m_dActiveRadius, sFilename);
  }

  FileHelpers::GetKeyStringFromINIFile("Properties", "CollisionDetection", "Accurate", sTmp, m_sObjectFilename);
  if(sTmp.compare("Accurate") == 0) {
    m_collisionDetection = ACCURATE;
  } else if(sTmp.compare("BoundingSphere") == 0) {
    m_collisionDetection = BOUNDING_SPHERE;
  } else {
    m_collisionDetection = BOUNDING_BOX;
  }

  // Geometry
  FileHelpers::GetKeyStringFromINIFile("Geometry", "RightDir", "+X", m_pOBJData->m_sRightDir, m_sObjectFilename);
  FileHelpers::GetKeyStringFromINIFile("Geometry", "ForwardDir", "+Y", m_pOBJData->m_sForwardDir, m_sObjectFilename);
  FileHelpers::GetKeyStringFromINIFile("Geometry", "DownDir", "+Z", m_pOBJData->m_sDownDir, m_sObjectFilename);
  FileHelpers::GetKeyDoubleFromINIFile("Geometry", "Scale", 1.0, m_pOBJData->m_dScale, m_sObjectFilename);

  // Load in the Parts
  // First count how many there are
  m_nParts = 0;
  do {
    string sHasSection;
    stringstream sSection;
    sSection << "Part" << m_nParts + 1;
    FileHelpers::GetKeyStringFromINIFile(sSection.str(), "", "default", sHasSection, m_sObjectFilename);
    if(sHasSection.compare("default") != 0) {
      ++m_nParts;
    } else {
      break;
    }
  } while(m_nParts < 10000); // just a sanity check to break the loop eventually

  if(m_pPart) {
    delete [] m_pPart;
  }
  m_pPart = new BPart[m_nParts];

  // Read parts
  for(int nPart = 0; nPart < m_nParts; ++nPart) {
    stringstream sSection;
    sSection << "Part" << nPart + 1;
    m_pPart[nPart].SetOBJData(m_pOBJData);
    m_pPart[nPart].LoadPartFromFile(m_sObjectFilename, sSection.str(), false, false);
  }

  if(m_collisionDetection == ACCURATE) {
    // Read/Setup collision detection parts

    if(m_pCollDetPart) {
      delete [] m_pCollDetPart;
    }

    m_nCollDetParts = m_nParts;
    m_pCollDetPart = new BObjectPart[m_nCollDetParts];

    for(int nPart = 0; nPart < m_nParts; ++nPart) {
      stringstream sSection;
      sSection << "Part" << nPart + 1;
      LoadCollisionPartFromFile(m_sObjectFilename, sSection.str(), m_pPart[nPart], m_pCollDetPart[nPart]);
    }
  }
}
Example #15
0
//Perform almost every function of the original main.cpp, initialization of booksim
BooksimInterface::BooksimInterface(string name, SystemConfig* sysCon, Fwk::Log* log, int id):
  Interface(name,sysCon,log,id){

  booksimconfig = new BookSimConfig();
  booksimconfig->ParseFile(sysCon->booksimConfig);


  InitializeRoutingMap(*booksimconfig );


  string topo;

  topo = booksimconfig->GetStr( "topology");
  clock_ratio = booksimconfig->GetInt( "clock_ratio");

  int networks  = booksimconfig->GetInt("subnets");
  /*To include a new network, must register the network here
   *add an else if statement with the name of the network
   */
  net.resize(networks);
  for (int i = 0; i < networks; ++i) {
    ostringstream name;
    name << "network_" << i;
    if ( topo == "torus" ) {
      KNCube::RegisterRoutingFunctions() ;
      net[i] = new KNCube( *booksimconfig, name.str(), false );
    } else if ( topo == "mesh" ) {
      KNCube::RegisterRoutingFunctions() ;
      net[i] = new KNCube( *booksimconfig, name.str(), true );
    } else if ( topo == "cmesh" ) {
      CMesh::RegisterRoutingFunctions() ;
      net[i] = new CMesh( *booksimconfig, name.str() );
    }else if ( topo == "fly" ) {
      KNFly::RegisterRoutingFunctions() ;
      net[i] = new KNFly( *booksimconfig, name.str() );
    } else if ( topo == "qtree" ) {
      QTree::RegisterRoutingFunctions() ;
      net[i] = new QTree( *booksimconfig, name.str() );
    } else if ( topo == "tree4" ) {
      Tree4::RegisterRoutingFunctions() ;
      net[i] = new Tree4( *booksimconfig, name.str() );
    } else if ( topo == "fattree" ) {
      FatTree::RegisterRoutingFunctions() ;
      net[i] = new FatTree( *booksimconfig, name.str() );
    } else if ( topo == "flatfly" ) {
      FlatFlyOnChip::RegisterRoutingFunctions() ;
      net[i] = new FlatFlyOnChip( *booksimconfig, name.str() );
    } else if ( topo == "anynet"){
      AnyNet::RegisterRoutingFunctions() ;
      net[i] = new AnyNet(*booksimconfig, name.str());
    } else if ( topo == "dragonflynew"){
      DragonFlyNew::RegisterRoutingFunctions() ;
      net[i] = new DragonFlyNew(*booksimconfig, name.str());
    }else {
      cerr << "Unknown topology " << topo << endl;
      exit(-1);
    }
  }

  manager = new SSTrafficManager(*booksimconfig, net);
  trafficManager = manager;
}
// ==========================================================================
// METHOD RPCRequestHandler::run
// ==========================================================================
int RPCRequestHandler::run (string &uri, string &postbody, value &inhdr,
                 		    string &out, value &outhdr, value &env,
                		    tcpsocket &s)
{
	try
	{
		DEBUG.storeFile ("RPCRequestHandler","postbody", postbody, "run");
		CORE->log (log::debug, "RPC", "handle: %S %!" %format (uri, inhdr));
		value indata;
		value res;
		string origin = "rpc";
		uid_t uid = 0;
		RPCHandler hdl (sdb);
	
		indata.fromjson (postbody);
		if (inhdr.exists ("X-OpenCORE-Origin"))
		{
			origin = inhdr["X-OpenCORE-Origin"];
		}
		
		CORE->log (log::debug, "RPC", "body: %!" %format (indata));
		
		// Set up credentials if available
		s.getcredentials();
		CORE->log (log::debug, "RPC", "credentials: %d %d %d", s.peer_uid,
														s.peer_gid, s.peer_pid);
		if (s.peer_pid == 0)
		{
			string peer_name = s.peer_name;
			if (peer_name == "127.0.0.1")
			{
				if (inhdr.exists ("X-Forwarded-For"))
				{
					peer_name = inhdr["X-Forwarded-For"];
				}
			}
				
			if (origin.strchr ('/') >0) origin = origin.cutat ('/');
			if (! origin) origin = "RPC";

			origin.strcat ("/src=%s" %format (peer_name));
			env["ip"] = s.peer_name = peer_name;
		}

		if (indata.exists ("header") && indata["header"].exists ("command"))
		{
			uri.strcat ("/%s" %format (indata["header"]["command"]));
		}
	
		res = hdl.handle (indata, s.peer_uid, origin);	
		out = res.tojson ();
		
		if (inhdr.exists ("Accept-Encoding"))
		{
			string ae = inhdr["Accept-Encoding"];
			if (ae.strstr ("deflate") >= 0)
			{
				unsigned long reslen = (out.strlen() * 1.05) + 12;
				char buf[reslen];
				
				if (compress2 ((Bytef*) buf, &reslen,
							   (const Bytef*) out.str(), out.strlen(), 4) == Z_OK)
				{
					outhdr["Content-Encoding"] = "deflate";
					out.strcpy (buf+2, reslen-2);
				}
				else
				{
					log::write (log::warning, "RPC", "Compress error");
				}
			}
		}
		
		outhdr["Content-type"] = "application/json";
	}
	catch (...)
	{
		log::write (log::error, "RPC", "Exception caught");
	}
	return HTTP_OK;
}