Пример #1
0
void ServerInstance::handleCommitReady(PacketBase &pb)
{
    if(pb.opCode == OPCODE_COMMIT) {
        /* Flush blocks to disk (sort by client order) */
        PRINT("[%s] FileID[%d] flushing writes...\n", N->hostname, curFd);
        for(mapit it = blocks.begin(); it != blocks.end(); ++it) {
            PacketWriteBlock &blk = it->second;
            if(newFile) {
                curFile = fopen(filepath.c_str(), "wb");
                if(curFile == NULL) {
                    throw FSException("Error creating empty file.");
                }
                newFile = false;
            }
            fseek(curFile, blk.offset, SEEK_SET);
            fwrite(blk.payload.str().c_str(), 1, blk.size, curFile);
            if(fflush(curFile)<0){
                PRINT("Error flushing writes: %s\n", strerror(errno));
            }
        }
        PRINT("[%s] FileID[%d] %lu writes committed!\n", 
                N->hostname, curFd, blocks.size());
        PacketCommitSuccess pcs;
        pcs.fileID = curFd;
        N->send(pcs);
        /* Clear block cache and wait for more writes */
        blocks.clear();
        transit(Write);
    } else if(pb.opCode == OPCODE_WRITEBLOCK) {
        /* Mostly like other servers' resend response */
        PacketWriteBlock p;
        p.deserialize(pb.buf);
        if(p.fileID != curFd) {
            throw FSException("Unknown file descriptor");
        }
        blocks[p.blockID] = p;
        //transit(Write);
    } else if(pb.opCode == OPCODE_COMMITPREPARE) {
        PacketCommitReady pcr;
        pcr.fileID = curFd;
        N->send(pcr);
        PRINT("Ready to commit\n");
    } else if(pb.opCode == OPCODE_CLOSE) {
        /* Close file; Clear block cache; Go to Idle */
        blocks.clear();
        if(curFile) fclose(curFile);
        curFile = NULL;
        transit(Idle);
        PacketCloseAck pca;
        pca.fileID = closedFd = curFd;
        N->send(pca);
        PRINT("FileID[%d] closed.\n", curFd);
    }
}
Пример #2
0
void ServerInstance::handleIdle(PacketBase &pb)
{
    if(pb.opCode == OPCODE_OPENFILE) {
        PacketOpenFile p;
        p.deserialize(pb.buf);
        curFd = p.fileID;
        filepath = mount + "/" + p.filename;

        PacketOpenFileAck pr;
        pr.fileID = curFd;

        curFile = fopen(filepath.c_str(), "r+b");
        if(curFile == NULL) {
            newFile = true;
        } else {
            newFile = false;
        }
        PRINT("Opening file: %s\n", filepath.c_str());
        pr.status = FILEOPENACK_OK;
        transit(Write);
        N->send(pr);
    } else if(pb.opCode == OPCODE_CLOSE) {
        /* Missed Close request */
        PacketClose p;
        p.deserialize(pb.buf);
        if(p.fileID == closedFd) {
            PacketCloseAck pca;
            pca.fileID = closedFd;
            N->send(pca);
        }
    }
}
int main() {
    mapnik::projection merc("+init=epsg:3857",true);
    mapnik::proj_transform prj_trans(merc,merc); // no-op
    unsigned tile_size = 256;
    mapnik::vector_tile_impl::spherical_mercator merc_tiler(tile_size);
    double minx,miny,maxx,maxy;
    merc_tiler.xyz(9664,20435,15,minx,miny,maxx,maxy);
    mapnik::box2d<double> z15_extent(minx,miny,maxx,maxy);
    mapnik::view_transform tr(tile_size,tile_size,z15_extent,0,0);
    std::string geojson_file("./test/data/poly.geojson");
    mapnik::util::file input(geojson_file);
    if (!input.open())
    {
        throw std::runtime_error("failed to open geojson");
    }
    mapnik::geometry::geometry<double> geom;
    std::string json_string(input.data().get(), input.size());
    if (!mapnik::json::from_geojson(json_string, geom))
    {
        throw std::runtime_error("failed to parse geojson");
    }
    mapnik::geometry::correct(geom);

    unsigned count = 0;
    unsigned count2 = 0;
    {
        mapnik::vector_tile_impl::vector_tile_strategy vs(prj_trans, tr, 16);
        mapnik::progress_timer __stats__(std::clog, "boost::geometry::transform");
        for (unsigned i=0;i<10000;++i)
        {
            mapnik::geometry::geometry<std::int64_t> new_geom = mapnik::geometry::transform<std::int64_t>(geom, vs);
            auto const& poly = mapnik::util::get<mapnik::geometry::multi_polygon<std::int64_t>>(new_geom);
            count += poly.size();
        }
    }
    {
        mapnik::vector_tile_impl::vector_tile_strategy vs(prj_trans, tr, 16);
        mapnik::progress_timer __stats__(std::clog, "transform_visitor with reserve");
        mapnik::vector_tile_impl::transform_visitor transit(vs);
        for (unsigned i=0;i<10000;++i)
        {
            mapnik::geometry::geometry<std::int64_t> new_geom = mapnik::util::apply_visitor(transit,geom);        
            auto const& poly = mapnik::util::get<mapnik::geometry::multi_polygon<std::int64_t>>(new_geom);
            count2 += poly.size();
        }
        if (count != count2)
        {
            std::clog << "tests did not run as expected!\n";
            return -1;
        }
    }
    return 0;
}
static void stateTransit(radio_token_t token) {
  setState(transit(regs.STATE, token));

  info("state change with token = %d", token);

  if (token == READY && (regs.SHORTS & 1) != 0) {
    info("SHORTS trigger START");
    regs.TASKS_START = 1;
    stateTransit(START); // start state
  } else if (token == END && (regs.SHORTS & 2) != 0) {
    info("SHORTS trigger DISABLE");
    regs.TASKS_DISABLE = 1;
    regs.TASKS_TXEN = 0;
    stateTransit(DISABLE); // disable state
  } else if (token == FULLY_DISABLED && (regs.SHORTS & 4) != 0) { // not tested yet
    info("SHORTS trigger TXEN");
    regs.TASKS_DISABLE = 0;
    regs.TASKS_TXEN = 1;
    stateTransit(TXEN); // txen state
  } else if (token == FULLY_DISABLED && (regs.SHORTS & 8) != 0) { // not tested yet
    info("SHORTS trigger RXEN");
    regs.TASKS_DISABLE = 0;
    regs.TASKS_RXEN = 1;
    stateTransit(RXEN); // rxen state
  } else if (token == ADDRESS && (regs.SHORTS & 0x10) != 0) { // not tested yet
    info("SHORTS trigger RSSISTART");
    regs.TASKS_RSSISTART = 1;
    regs.TASKS_RSSISTOP = 0;
  } else if (token == END && (regs.SHORTS & 0x20) != 0) { // not tested yet
    info("SHORTS trigger START");
    regs.TASKS_START = 1;
    regs.TASKS_TXEN = 0;
    stateTransit(START); // start state
  } else if (token == ADDRESS && (regs.SHORTS & 0x40) != 0) { // not tested yet
    info("SHORTS trigger BCSTART");
    regs.TASKS_BCSTART = 1;
    regs.TASKS_BCSTOP = 0;
    //stateTransit(RXEN); // rxen state
  } else if (token == ADDRESS && (regs.SHORTS & 0x100) != 0) { // not tested yet
    info("SHORTS trigger RSSISTOP");
    regs.TASKS_RSSISTOP = 1;
    regs.TASKS_RSSISTART = 0;
  }

  if (irqPredicate(token, regs.INTENSET)) {
    triggerIrq();
  }
}
Пример #5
0
int main()
{
	int i=0, j;
	while (i<20) {
		for (j=0; j<NUM_T; j++) {
			if (pre_satisfy(j)) {
				transit(j);
				break;
			}
		}
		i++;
	}
	for (i=0; i<NUM_P; i++) {
		printf("P%d: %d\n", i+1, current[i]);
	}
	return 0;
}
Пример #6
0
void FSM::VM<X>::createStates(std::istream &stream)
{
  boost::regex    state("state: *([a-zA-Z][a-zA-Z0-9_-]*)");
  boost::regex    transit("transit: *\\[([A-Z]+)] *-> *([a-zA-Z][a-zA-Z0-9_-]*)");
  boost::regex    link("([a-zA-Z][a-zA-Z0-9_-]*) *\\[([A-Z]+)] *-> *([a-zA-Z][a-zA-Z0-9_-]*)");
  std::string     line;

  while (std::getline(stream, line))
    {
      boost::match_results<std::string::iterator>  what;

#ifdef DEBUG
      std::cout << "line: " << line << std::endl;
#endif
      if (boost::regex_search(line.begin(), line.end(), what, state,
                          boost::regex_constants::match_default))
        {
#ifdef DEBUG
          std::cout << "MATCHED with " << state.expression() << std::endl;
          std::cout << "Extracted: " << what[1] << std::endl;
#endif
          addState(what[1]);
        }
      else if (boost::regex_search(line.begin(), line.end(), what, link,
                                   boost::regex_constants::match_default))
        {
#ifdef DEBUG
          std::cout << "MATCHED with " << link.expression() << std::endl;
          std::cout << "Extracted: " << what[1] << " && " << what[2] << " && " << what[3] << std::endl;
#endif
          addLink(what[1], what[2], what[3]);
        }
      else if (boost::regex_search(line.begin(), line.end(), what, transit,
                                   boost::regex_constants::match_default))
        {

        }
#ifdef DEBUG
      else
        std::cout << "DID NOT MATCHED" << std::endl;
#endif
    }
}
Пример #7
0
void State::update(float deltaSec) {
    if (!m_currentSubState) {
        if (m_initialSubState) {
            m_currentSubState = m_initialSubState;
            m_currentSubState->onEntered();
		}
    }

    if (m_currentSubState) {
        for (Transition* transition : m_currentSubState->transitions()) {
            if (transition->isPossible()) {
                transit(transition->to());
                transition->onPerformed();
            }
        }
    }

    if (m_currentSubState) {
        m_currentSubState->update(deltaSec);
    }
}
Пример #8
0
int main()
{
	initialize();
	double minDistance = DBL_MAX; // The very largest allowed value for double type.
	for (int i = 0; i < iteration; ++i)
	{
		iterationInitialize();
		printf("Iterating %d ...\n", i + 1);

		for (int j = 0; j < nrOfAnts; ++j)
		{
			for (int iIndex = 0; iIndex < nrOfCities; ++iIndex)
			{
				routeIndex[iIndex].cumP = 0;
				routeIndex[iIndex].visited = 0;
			}

			int temp = rand() % nrOfCities; // Randomly choose a city as the very starting point for the ant j.
			route[j][0] = temp;
			routeIndex[temp].visited = 1;

			// Determine the next city given the current city, and a complete route covering all cities is built for the ant j afterwards.
			for (int k = 1; k < nrOfCities - 1; ++k)
			{
				route[j][k] = transit(routeIndex, route[j][k - 1]);
			}

			// Set the only unvisited city as the last city.
			for (int iIndex = 0; iIndex < nrOfCities; ++iIndex)
			{
				if (!routeIndex[iIndex].visited)
				{
					route[j][nrOfCities - 1] = iIndex;
				}
			}

			distance[j] = getDistance(route[j], nrOfCities);
		}

		// The following 2 double-loop update the pheromone matrix.
		for (int j = 0; j < nrOfAnts; ++j)
		{
			for (int k = 0; k < nrOfCities - 1; ++k)
			{
				deltaTau[route[j][k]][route[j][k + 1]] = Q / distance[j];
			}
		}

		for (int j = 0; j < nrOfCities; ++j)
		{
			for (int k = 0; k < nrOfCities; ++k)
			{
				tau[j][k] += (1 - rho) * tau[j][k] + deltaTau[j][k];
			}
		}

		int minIdx = minIndex(distance, nrOfAnts);
		int minRoute[nrOfCities];
		if (distance[minIdx] < minDistance)
		{
			minDistance = distance[minIdx];
			copyArrayElement(route[minIdx], minRoute, nrOfCities);
		}

		printf("The minimum distance: %lf\n", minDistance);
		printf("Corresponding route:\n");
		for (int i = 0; i < nrOfCities - 1; ++i)
		{
			printf("%d->", route[minIdx][i]+1);
		}
		printf("%d\n", route[minIdx][nrOfCities - 1]+1);
	}

	return 0;
}
Пример #9
0
void ServerInstance::handleWrite(PacketBase &pb)
{
    if(pb.opCode == OPCODE_OPENFILE) {
        if(pb.fileID == curFd && lastState == Idle) {
            PacketOpenFileAck pr;
            pr.fileID = curFd;
            pr.status = FILEOPENACK_OK;
            N->send(pr);
        }
    } else if(pb.opCode == OPCODE_WRITEBLOCK) {
        PacketWriteBlock p;
        p.deserialize(pb.buf);
        if(p.fileID != curFd) {
            throw FSException("Unknown file descriptor");
        }
        blocks[p.blockID] = p;
    } else if(pb.opCode == OPCODE_COMMITPREPARE) {
        PacketCommitPrepare p;
        p.deserialize(pb.buf);
        if(p.fileID != curFd) {
            throw FSException("Unknown file descriptor");
        }
        /* Clean up extra blocks due to missed abort */
        mapit mit = blocks.begin();
        while(mit != blocks.end()) {
            if(p.blockIDs.count(mit->first) == 0) {
                mapit toErase = mit;
                ++mit;
                blocks.erase(toErase);
            } else {
                ++mit;
            }
        }
        /* Find missing blocks */
        PacketResendBlock prb;
        for(blockIDit it = p.blockIDs.begin(); it != p.blockIDs.end(); ++it) {
            if(blocks.find(*it) == blocks.end()) {
                prb.blockIDs.insert(*it);
            }
        }
        if(prb.blockIDs.size() > 0) {
            /* Missing blocks; ask for resend */
            prb.fileID = curFd;
            N->send(prb);
            PRINT("[%s] Missing %lu blocks\n", N->hostname, prb.blockIDs.size());
        } else {
            /* I'm ready */
            PacketCommitReady pcr;
            pcr.fileID = curFd;
            N->send(pcr);
            transit(CommitReady);
            PRINT("[%s] Ready to commit\n", N->hostname);
        }
    } else if(pb.opCode == OPCODE_ABORT) {
        PRINT("FileID[%d] %lu writes aborted!\n", curFd, blocks.size());
        blocks.clear();
    } else if(pb.opCode == OPCODE_CLOSE) {
        /* Close file; Clear block cache; Go to Idle */
        blocks.clear();
        if(curFile) fclose(curFile);
        curFile = NULL;
        transit(Idle);
        PacketCloseAck pca;
        pca.fileID = closedFd = curFd;
        N->send(pca);
        PRINT("FileID[%d] closed.\n", curFd);
    } else if(pb.opCode == OPCODE_COMMIT) {
        if(lastState == CommitReady) {
            PRINT("[%s] Re-ack commitSuccess\n", N->hostname);
            PacketCommitSuccess pcs;
            pcs.fileID = curFd;
            N->send(pcs);
        }
    }
}
Пример #10
0
void StudentWindow::showLessonWidget(int i)
{
    LessonsDB *lessons = LessonsDBController::getDB();
    Lesson lesson = lessons->getLessons().at(i);
    LessonsDBController::setIndex(i);
    this->lessonWidget->prepare(&lesson);
    
    connect(this->lessonWidget->menuWidget->lessonButton, SIGNAL(clicked()), this->lessonWidget, SLOT(transit()));
    connect(this->lessonWidget->menuWidget->logoutButton, SIGNAL(clicked()), this->lessonWidget, SLOT(exit()));

    connect(this->lessonWidget->menuWidget, SIGNAL(selectTest(bool)), this->testWidget, SLOT(prepare(bool)));
    connect(this->testWidget, SIGNAL(prepared()), this->testWidget, SLOT(show()));
    connect(this->testWidget, SIGNAL(prepared()), this->lessonWidget, SLOT(hide()));

    connect(this->testWidget, SIGNAL(transitLessonStudent()), this->testWidget, SLOT(hide()));
    connect(this->testWidget, SIGNAL(transitLessonStudent()), this->lessonWidget, SLOT(show()));
}