public function testCidr()
 {
     $ipSeg1 = new ipSegment(0, 0);
     $ipSeg1->setCidr("1.0.0.0/14");
     $this->assertEquals("1.0.0.0", $ipSeg1->getStartIpString());
     $this->assertEquals("1.3.255.255", $ipSeg1->getEndIpString());
 }
 public function testReadingPhp()
 {
     $dbData = new dbDataProcessor();
     $hashStep = dbWriter::HASH_STEP;
     $dbData->addCarrierInterval(new ipSegment(1, 4), "carrier1");
     $dbData->addCarrierInterval(new ipSegment(2 * $hashStep, 3 * $hashStep - 1), "carrier2");
     $opts = new optionsMock();
     $dbStream = new dbStream(dirname(__FILE__) . "/tmp/test1.db", false);
     $dbWriter = new dbWriter($opts, $dbData, $dbStream);
     $dbWriter->writeDb();
     $dbReader = new dbReader($dbStream);
     $dbReader->readAll();
     $dbReader->checkConsistency();
     $dbStream->close();
     $dbCarrier = new CarrierDbPhp(dirname(__FILE__) . "/tmp/test1.db");
     $info = $dbCarrier->getDbInfo();
     $this->assertEquals(dbWriter::STRUCT_VERSION, $info['structVersion']);
     $this->assertEquals(1, $info['buildVersion']);
     $this->assertTrue(time() - $info['buildTimestamp'] < 10);
     // меньше 10 секунд назад
     $this->assertEquals(2, $info['recCount']);
     $this->assertEquals(NULL, $dbCarrier->get("0.0.0.0"));
     $this->assertEquals("carrier1", $dbCarrier->get("0.0.0.1"));
     $this->assertEquals("carrier1", $dbCarrier->get("0.0.0.2"));
     $this->assertEquals("carrier1", $dbCarrier->get("0.0.0.4"));
     $this->assertEquals(NULL, $dbCarrier->get("0.0.0.5"));
     $ipSeg = new ipSegment(floor(1.5 * $hashStep), floor(2.5 * $hashStep));
     $this->assertEquals(NULL, $dbCarrier->get($ipSeg->getStartIpString()));
     $this->assertEquals("carrier2", $dbCarrier->get($ipSeg->getEndIpString()));
     $ipSeg = new ipSegment(3 * $hashStep - 1, 3 * $hashStep);
     $this->assertEquals("carrier2", $dbCarrier->get($ipSeg->getStartIpString()));
     $this->assertEquals(NULL, $dbCarrier->get($ipSeg->getEndIpString()));
 }
 private function writeHashIndex()
 {
     $this->hashListPtr = array();
     $curData = $this->dbData->resetData();
     $ipSeg = new ipSegment($curData[dbDataProcessor::IP_START_POS], $curData[dbDataProcessor::IP_END_POS]);
     $ipSeg2 = new ipSegment(0, 0);
     $count = ($this->hashMax - $this->hashMin) / $this->hashStep;
     for ($i = 0; $i < $count; $i++) {
         $hashSegmentStart = $this->hashMin + $this->hashStep * $i;
         $ipSeg2->setStartIp($hashSegmentStart);
         $ipSeg2->setEndIp($hashSegmentStart + $this->hashStep - 1);
         //Проверяем вхождение
         $hashList = array();
         while ($ipSeg->checkIntersection($ipSeg2)) {
             $hashList[] = $this->dbData->getCurrentKey();
             if ($curData[dbDataProcessor::IP_END_POS] > $hashSegmentStart + $this->hashStep - 0.001) {
                 break;
                 // Продлеваем текущий интервал на следующий шаг
             }
             $curData = $this->dbData->getNext();
             $ipSeg->setStartIp($curData[dbDataProcessor::IP_START_POS]);
             $ipSeg->setEndIp($curData[dbDataProcessor::IP_END_POS]);
             if ($curData === false) {
                 break;
             }
         }
         $hashListPtr[] = $this->dbStream->tell();
         $this->writeInt(count($hashList));
         foreach ($hashList as $dataKey) {
             $this->writeInt($this->dbData->getStartIpByKey($dataKey));
             $this->writeInt($this->dbData->getEndIpByKey($dataKey));
             $this->writeInt($this->dataFilePtr[$dataKey]);
         }
         unset($hashList);
         if ($curData === false) {
             break;
         }
     }
     //Записываем указатели на Хэши
     foreach ($hashListPtr as $hashPtr) {
         $this->writeInt($hashPtr);
     }
     //Записываем параметры хэш функции
     $this->writeInt($this->hashMin);
     $this->writeInt($this->hashMax);
     $this->writeInt($this->hashStep);
 }
 private function getNextCellular()
 {
     while ($connRow = $this->connCsvParser->getNext(1)) {
         if ($connRow[0]['connection_type'] === 'Cellular') {
             if (!isset($connRow[0]['network']) && !isset($connRow[0]['network_start_ip'], $connRow[0]['network_last_ip'])) {
                 throw new Exception("ERROR: Не найдено поле network");
             }
             if (isset($connRow[0]['network_start_ip'])) {
                 $cellSeg = new ipSegment($connRow[0]['network_start_ip'], $connRow[0]['network_last_ip']);
             } else {
                 $cellSeg = new ipSegment(0, 0);
                 $cellSeg->setCidr($connRow[0]['network']);
             }
             return $cellSeg;
         }
     }
     return false;
 }