Ejemplo n.º 1
0
 // ------------------------------------------------------------------
 bool FileReader::checkEncoding()
 {
     encoding::TextEncoding eEncoding;
     if (! readPreamble(eEncoding))
         return false;
     return setDecoder(encoding::BaseDecoder::getDecoder(eEncoding));
 };
Ejemplo n.º 2
0
void Bridge::checkNewMessages()
{
	unordered_map<string, int> closed_ifaces;
	for (auto it = connected_ifaces.begin(); it != connected_ifaces.end(); ++it)
	{
		
		if (FD_ISSET(it->second, &readset))
		{	
			sockaddr_in client_addr;
			socklen_t calen = sizeof(client_addr);
			char buffer[MSGMAX + 1];
			memset(buffer, 0, MSGMAX + 1);
			if (!readPreamble(it->second, buffer))
			{
				//disconnect
				current_ports--;
				getpeername(it->second, (sockaddr*)&client_addr, &calen);
				cout << "(" << to_string(ntohs(client_addr.sin_port)) << ")" << string(inet_ntoa(client_addr.sin_addr)) << " has disconnected." << endl;
				if (DebugON) cout << "PORTS AVAILABLE: " << max_ports - current_ports << endl;
				closed_ifaces[it->first] = it->second;
			}
			else
			{
				getpeername(it->second, (sockaddr*)&client_addr, &calen);
				char* msg = receivePacket(it->second, buffer); //reads packet into msg  	
				Ethernet_Pkt e(msg);
				mtx.lock();
				connections[e.src] = ConnectionEntry(it->second); //adds AND refreshes entry
				if (e.dst != NOENTRY && connections.count(e.dst) > 0) // it knows the destination
				{
					if (DebugON) cout << "SENDING DIRECT ON FD: " << it->second << ")" << endl;
					sendPacket(e, connections[e.dst].port);
				}
				else //broadcast message
				{	
					for (auto it2 = connected_ifaces.begin(); it2 != connected_ifaces.end(); it2++)
					{
						if (it->second != it2->second)
						{
							if (DebugON) cout << "BROADCASTING TO " << it2->second << "..." << endl;
							sendPacket(e, it2->second);	
						}
					}
				}
				mtx.unlock();				
				delete msg;
			}
		}
	}
	for (auto it = closed_ifaces.begin(); it != closed_ifaces.end(); ++it)
	{
		close(it->second);
		connected_ifaces.erase(it->first);
	}
}
Ejemplo n.º 3
0
void Bin2HMeTiS::convert(const char *filename) {
  ifstream in_stream;
  ofstream out_stream;

  char hmetis_file[512];
  char preAmble[512];

  int i;
  int pin;
  int chunkLength;
  int inData;
  int inSourceFile;
  int numReadHedges;

  in_stream.open(filename, ifstream::in | ifstream::binary);

  if (!in_stream.is_open()) {
    cout << "error opening " << filename << endl;
    in_stream.close();
    exit(1);
  }

  sprintf(hmetis_file, "%s.hgr", filename);
  out_stream.open(hmetis_file, ofstream::out);

  if (!out_stream.is_open()) {
    cout << "error opening " << hmetis_file << endl;
    in_stream.close();
    exit(1);
  }

  readPreamble(in_stream);
  readInVertexWts(in_stream);

  sprintf(&preAmble[0], "%s HMeTiS file representation of %s\n", "%", filename);
  out_stream << &preAmble[0];

  sprintf(&preAmble[0], "%d %d %d\n", numHedges, numVerts, 11);
  out_stream << &preAmble[0];

  numReadHedges = 0;
  inData = 0;
  inSourceFile = in_stream.tellg();

  for (; numReadHedges < numHedges;) {
    if (inData == 0)
      readInHedgeData(in_stream, inSourceFile);

    for (; inData < dataLength;) {
      chunkLength = hEdgeData[inData];
      out_stream << hEdgeData[inData + 1] << " ";

      for (i = 2; i < chunkLength; ++i) {
        pin = hEdgeData[inData + i];

        if (pin < 0 || pin >= numVerts) {
          cout << "pin = " << pin << ", numVerts = " << numVerts << endl;
          in_stream.close();
          out_stream.close();
          exit(1);
        }

        out_stream << (pin + 1) << " ";
      }

      out_stream << "\n";

      inData += chunkLength;
      ++numReadHedges;

      if (numReadHedges == numHedges)
        break;
    }

    if (inData == dataLength)
      inData = 0;
  }

  for (i = 0; i < numVerts; ++i)
    out_stream << vWeights[i] << "\n";

  out_stream.close();
  in_stream.close();
}
Ejemplo n.º 4
0
void HMeTiS2Bin::convert(const char *filename) {
  char bin_file[512];

  int maxPinsRead = TextFileReader::getMaxPinsInChunk();
  int numHedgesRead;
  int numPinsRead;
  int binPreAmble[3];

  ifstream in_stream;
  ofstream out_stream;

  in_stream.open(filename, ifstream::in);

  if (!in_stream.is_open()) {
    cout << "error opening " << filename << endl;
    exit(1);
  }

  sprintf(bin_file, "%s.bin", filename);
  out_stream.open(bin_file, ofstream::out | ofstream::binary);

  if (!out_stream.is_open()) {
    cout << "error opening " << bin_file << endl;
    in_stream.close();
    exit(1);
  }

  readPreamble(in_stream);

  numPins = computeNumPins(in_stream);

  binPreAmble[0] = numVertices;
  binPreAmble[1] = numHedges;
  binPreAmble[2] = numPins;

  out_stream.write((char *)(&binPreAmble), sizeof(int) * 3);

  numPinsRead = 0;
  numHedgesRead = 0;

  while (!in_stream.eof() && numHedgesRead < numHedges) {
    if (numPinsRead == 0) {
      resetConverterParameters();
      addLengthParameter();
    }

    getLine(in_stream);
    numHedgesRead += processHedgeLine(buffer.data(), numPinsRead);

    if (numPinsRead > maxPinsRead || numHedgesRead == numHedges) {
      setLengthParameter();
      out_stream.write((char *)(hEdgeData.data()),
                       sizeof(int) * dataLength);
      numPinsRead = 0;
    }
  }

  if (numHedgesRead < numHedges)
    cout << "warning - could not read " << numHedges << " hyperedges" << endl;

  if (wtsOnVertices) {
    resetConverterParameters();

    while (!in_stream.eof() && numVerts < numVertices) {
      getLine(in_stream);
      processVertLine(buffer.data());
    }

    if (numVerts < numVertices) {
      cout << "warning - do not have all vertex weights" << endl;
      in_stream.close();
      out_stream.close();
      exit(1);
    }
  } else {
    vWeights.reserve(numVertices);
    numVerts = numVertices;

    for (int i = 0; i < numVertices; ++i)
      vWeights[i] = 1;
  }

  out_stream.write((char *)(vWeights.data()), sizeof(int) * numVertices);

  in_stream.close();
  out_stream.close();
}