////////////////////////////////////////////////////////////////////////////////
// Simulation::init_terminals                                                 //
//                                                                            //
// initializes terminals for a new iteration                                  //
////////////////////////////////////////////////////////////////////////////////
void Simulation::init_terminals(){

	adapt_struct adapt (sim_par.get_TxMode(), sim_par.get_AdaptMode(),
			sim_par.get_TxPowerMax(), sim_par.get_TxPowerMin(),
			sim_par.get_TxPowerStepUp(),sim_par.get_TxPowerStepDown(),
			sim_par.get_TargetPER(),sim_par.get_LAMaxSucceedCounter(),
			sim_par.get_LAFailLimit(), sim_par.get_UseRxMode());

	mac_struct mac(sim_par.get_RetryLimit(), sim_par.get_RTSThreshold(),
			sim_par.get_FragmentationThresh(), sim_par.get_QueueSize(),
			sim_par.get_set_BA_agg());

	PHY_struct phy(sim_par.get_NoiseVariance(), sim_par.get_CCASensitivity());

	traffic_struct tr_dl(sim_par.get_DataRateDL(), sim_par.get_PacketLength(),
			sim_par.get_ArrivalTime());

	traffic_struct tr_ul(sim_par.get_DataRateUL(), sim_par.get_PacketLength(),
			sim_par.get_ArrivalTime());

	timestamp tr_time = sim_par.get_TransientTime();

	for (unsigned i = 0; i < sim_par.get_NumberAPs(); i++) {
		AccessPoint* ap = new AccessPoint(sim_par.get_APPosition(i), &main_sch, ch,
				&randgent, &log, mac, phy, tr_time);
		term_vector.push_back(ap);

		if (log(log_type::setup))
			log << *ap << " created at position " << sim_par.get_APPosition(i)
			<< '\n' << endl;
	}


	double cell_radius = sim_par.get_Radius();

	// Array containing the amount of connections belonging to each AC
	unsigned ppArray[5] = {round(2*sim_par.get_ppAC_BK()*sim_par.get_NumberStas()),
					  	   round(2*sim_par.get_ppAC_BE()*sim_par.get_NumberStas()),
						   round(2*sim_par.get_ppAC_VI()*sim_par.get_NumberStas()),
						   round(2*sim_par.get_ppAC_VO()*sim_par.get_NumberStas()),
						   round(2*sim_par.get_ppLegacy()*sim_par.get_NumberStas())};
	unsigned sum = 0;
	for(int k = 0; k < 5; k++) {
		sum = sum + ppArray[k];
	}
	int diff = sum - 2*sim_par.get_NumberStas();
	if(diff > 0) {
		for(int k = 0; k < 5; k++) {
			while(ppArray[k] > 0 && diff > 0){
				ppArray[k]--;
				diff--;
			}
		}
	}
	if(diff < 0)	{
		ppArray[0] = (-1)*diff;
	}
	vector<int> noZe_ppArray;
	accCat MS_AC = AC_BK;
	accCat AP_AC = AC_BK;
	int idx = 0;

	for (unsigned i = 0; i < sim_par.get_NumberStas(); i++) {

		// Vector containing elements of ppArray that are non-zero
		noZe_ppArray.clear();
		for(int k = 0; k < 5; k++) {
			if(ppArray[k] != 0) noZe_ppArray.push_back(k);
		}
		// Choose one access category randomly
		if(noZe_ppArray.size() != 0) {
			idx = randgent.from_vec(noZe_ppArray);
			MS_AC = allACs[idx];
			ppArray[idx]--;
		}

		// if just one mobile station, then distance = cell radius
		Position pos(cell_radius,0);
		// else Stas are uniformly distributed
		if (sim_par.get_NumberStas() > 1) {
			do {
				pos = Position (randgent.uniform(-cell_radius,cell_radius),
						randgent.uniform(-cell_radius,cell_radius));
			} while(pos.distance() > cell_radius);
		}

		MobileStation* ms = new MobileStation(pos, &main_sch, ch, &randgent,
				&log, mac, phy, tr_time);
		term_vector.push_back(ms);

		double min_dist = HUGE_VAL;
		int min_index = -1;
		for (unsigned count = 0; count < sim_par.get_NumberAPs(); count++) {
			double dist = pos.distance(sim_par.get_APPosition(count));
			if (dist < min_dist) {
				min_dist = dist;
				min_index = count;
			}
		}

		// Vector containing elements of ppArray that are non-zero
		noZe_ppArray.clear();
		for(int k = 0; k < 5; k++) {
			if(ppArray[k] != 0) noZe_ppArray.push_back(k);
		}
		// Choose one access category randomly
		if(noZe_ppArray.size() != 0) {
			idx = randgent.from_vec(noZe_ppArray);
			AP_AC = allACs[idx];
			ppArray[idx]--;
		}

		// Connect mobile terminal to closest AP
		connect_two(term_vector[min_index], AP_AC, ms, MS_AC, ch, adapt, tr_dl, tr_ul);

		if (log(log_type::setup))
			log << *ms << " created at position " << pos << " with distance "
			<< min_dist << " m to " << *term_vector[min_index]
		    << "\nMobile station AC: "<< MS_AC << ". Access Point AC: "
			<< AP_AC << "." <<endl;
	}


	if (log(log_type::setup)) log_connections();
}
Ejemplo n.º 2
0
static int cws_response (void * cls, struct MHD_Connection * connection, const char * url,
                  const char * method, const char * version, 
                  const char * upload_data, size_t * upload_data_size, void ** ptr)
{
    int ret = MHD_NO;
    static int dummy;
    const char *webfile;
    char *page;
    char *basedir = ((cws_conn *) cls)->basedir;
    cws_astring *pathfile;
    struct MHD_Response *response;
    cws_handler *hdlr = ((cws_conn *) cls)->hdlr;
    const union MHD_ConnectionInfo *info;
    /*
    if (0 != strcmp(method, "GET")) return MHD_NO;
    */
    webfile = url;
    if (&dummy != *ptr)
    {
        *ptr = &dummy;
        return MHD_YES;
    }
    *ptr = NULL;
    response = NULL;
    if (0 != *upload_data_size)
    {
        cws_handler *h = cws_handler_lookup (hdlr, webfile); 
        if (h != NULL)
        {
            cws_dict *dict = NULL;
            struct MHD_PostProcessor *pp = MHD_create_post_processor(connection, 65536,
                                                                 post_processor,
                                                                 &dict);
            MHD_post_process(pp, upload_data, *upload_data_size);
            MHD_destroy_post_processor(pp);
            page = h->f(dict);
            printf("Page: %s\n", page);
            response = MHD_create_response_from_data (strlen (page), page, MHD_NO, MHD_YES);
            if (response)
            {
                printf("sending response\n");
                MHD_add_response_header (response, "Content-Type", h->mimetype);
                ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
                printf("if? %d\n", ret == MHD_YES ? 1 : 0);
            }
            MHD_destroy_response (response);
            free (page);
            cws_dict_free (&dict);
        }
        return MHD_NO;
    }
    info = MHD_get_connection_info (connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
    log_connections (inet_ntoa(((struct sockaddr_in *) info->client_addr)->sin_addr), url);
    if (strcmp (webfile, "/") == 0) webfile = "/index.html";
    pathfile = cws_astring_new(basedir);
    cws_astring_cat(pathfile, webfile);
    if (access (CWS_ASTRING(pathfile), F_OK) != -1)
    {
        int fd = open (CWS_ASTRING(pathfile), O_RDONLY);
        struct stat sbuf;
        fstat (fd, &sbuf);
        response = MHD_create_response_from_fd (sbuf.st_size, fd);
        MHD_add_response_header (response, "Content-Type", cws_mime_type(CWS_ASTRING(pathfile)));
    }
    else 
    {
        cws_handler *h = cws_handler_lookup (hdlr, webfile); 
        if (h != NULL)
        {
            cws_dict *dict = NULL;
            MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND, get_keys, &dict);
            page = h->f(dict);
            response = MHD_create_response_from_data (strlen (page), page, MHD_NO, MHD_YES);
            MHD_add_response_header (response, "Content-Type", h->mimetype);
            free (page);
            cws_dict_free (&dict);
        }
        else
        {
            response = MHD_create_response_from_data (strlen (ERROR_ANSWER), ERROR_ANSWER, MHD_NO, MHD_YES);
            MHD_add_response_header (response, "Content-Type", cws_mime_type("html"));
        }
    }    
    if (response != NULL)
    {
        ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
        MHD_destroy_response (response);
    }
    cws_astring_free(pathfile);
    return ret;
}