📄 udpwired.cc
字号:
//// UDPwired.cc : UDP Wired Support for Diffusion// Authors : Chalermek Intanagonwiwat and Fabio Silva//// Copyright (C) 2000-2002 by the University of Southern California// $Id: UDPwired.cc,v 1.10 2002/06/27 22:04:31 fabio Exp $//// This program is free software; you can redistribute it and/or// modify it under the terms of the GNU General Public License,// version 2, as published by the Free Software Foundation.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License along// with this program; if not, write to the Free Software Foundation, Inc.,// 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.////#include <stdio.h>#include <stdlib.h>#include "UDPwired.hh"UDPWired::UDPWired(char *config_file) : DiffusionIO(){ TopologyEntry *entry; FILE *cfile; int flag, quality, port; char hname[50]; struct hostent *hent = NULL; // Try to open the config file cfile = fopen(config_file, "r"); if (cfile == NULL){ DiffPrint(DEBUG_ALWAYS, "Can't find configuration file !\n"); exit(-1); } // Parse the config file num_out_descriptors_ = 1; DiffPrint(DEBUG_IMPORTANT, "Parsing config file - %s \n", config_file); flag = fscanf(cfile, "%s - %d - %d\n", hname, &port, &quality); while (flag != EOF){ if (flag == 0){ DiffPrint(DEBUG_ALWAYS, "Error in the config file !\n"); exit(-1); } if (flag == 1){ // Link quality and port not specified port = DEFAULT_DIFFUSION_PORT; quality = RELIABLE_LINK; } else{ if (flag == 2){ // We have either port or quality. Let's figure it out if (port > 0 && port <= RELIABLE_LINK){ // Ok, we have the link's quality quality = port; port = DEFAULT_DIFFUSION_PORT; } else{ if (port < 1024 || port > 65535){ DiffPrint(DEBUG_ALWAYS, "Error parsing config file: Syntax error !\n"); exit(-1); } quality = RELIABLE_LINK; } } else{ // We have port and quality if (quality < 0 || quality > RELIABLE_LINK){ DiffPrint(DEBUG_ALWAYS, "Error parsing config file: Link quality outside range (0-100) !\n"); exit(-1); } if (port < 1024 || port > 65535){ DiffPrint(DEBUG_ALWAYS, "Error parsing config file: Port outside range (1024-65535) !\n"); exit(-1); } } } DiffPrint(DEBUG_IMPORTANT, "Neighbor: %d - %s - Port: %d - Link Quality: %d\n", num_out_descriptors_, hname, port, quality); hent = gethostbyname(hname); if (hent == NULL){ DiffPrint(DEBUG_ALWAYS, "Cannot resolve host name !\n"); exit(-1); } entry = new TopologyEntry; entry->fd_ = socket(AF_INET, SOCK_DGRAM, 0); if (entry->fd_ < 0){ DiffPrint(DEBUG_ALWAYS, "Cannot create socket !\n"); exit(-1); } bzero((char *) &(entry->skt_addr_inet_), sizeof(entry->skt_addr_inet_)); // Configure socket entry->skt_addr_inet_.sin_family = AF_INET; entry->skt_addr_inet_.sin_port = htons((u_int16_t) port); bcopy(hent->h_addr, (char *)&(entry->skt_addr_inet_.sin_addr), hent->h_length); entry->quality_ = quality; neighbors_list_.push_back(entry); num_out_descriptors_++; flag = fscanf(cfile, "%s - %d - %d\n", hname, &port, &quality); } fclose(cfile); num_out_descriptors_--; DiffPrint(DEBUG_IMPORTANT, "\nTotal Neighbors = %d\n", num_out_descriptors_);}void UDPWired::sendPacket(DiffPacket pkt, int len, int dst){ TopologyList::iterator itr; TopologyEntry *entry; int prob_loss; if (len > MAX_UDP_MESSAGE_SIZE){ DiffPrint(DEBUG_ALWAYS, "Warning: Packet larger than maximum UDP datagram size !\n"); exit(-1); } for (itr = neighbors_list_.begin(); itr != neighbors_list_.end(); ++itr){ entry = *itr; if (entry->quality_ == RELIABLE_LINK){ sendto(entry->fd_, pkt, len, 0, (struct sockaddr *)&entry->skt_addr_inet_, sizeof(entry->skt_addr_inet_)); } else{ prob_loss = (int) (RELIABLE_LINK * (GetRand() * 1.0 / RAND_MAX)); if (prob_loss <= entry->quality_) sendto(entry->fd_, pkt, len, 0, (struct sockaddr *)&entry->skt_addr_inet_, sizeof(entry->skt_addr_inet_)); else DiffPrint(DEBUG_NO_DETAILS, "Packet dropped !\n"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -