00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #include <sstream>
00025
00026 #include "SimpleNodeEntry.h"
00027 #include "IPAddressResolver.h"
00028
00029
00030 uint32_t NodeRecord::dim;
00031
00032 NodeRecord::NodeRecord(uint32_t dim)
00033 {
00034 this->dim = dim;
00035 coords = new double[dim];
00036 }
00037
00038 NodeRecord::~NodeRecord()
00039 {
00040 delete[] coords;
00041 coords = NULL;
00042 }
00043
00044 NodeRecord::NodeRecord(const NodeRecord& nodeRecord)
00045 {
00046 coords = new double[dim];
00047 for (uint32_t i = 0; i < dim; ++i)
00048 coords[i] = nodeRecord.coords[i];
00049 }
00050
00051 NodeRecord& NodeRecord::operator=(const NodeRecord& nodeRecord)
00052 {
00053 delete[] coords;
00054 coords = new double[dim];
00055 for (uint32_t i = 0; i < dim; ++i)
00056 coords[i] = nodeRecord.coords[i];
00057
00058 return *this;
00059 }
00060
00061
00062 SimpleNodeEntry::SimpleNodeEntry(cModule* node,
00063 cChannelType* type,
00064 uint32_t fieldSize,
00065 uint32_t sendQueueLength)
00066 {
00067 ingate = node->getSubmodule("udp")->gate("network_in");
00068
00069 nodeRecord = new NodeRecord(2);
00070 index = -1;
00071
00072
00073 nodeRecord->coords[0] = uniform(0, fieldSize) - fieldSize / 2;
00074 nodeRecord->coords[1] = uniform(0, fieldSize) - fieldSize / 2;
00075
00076 cDatarateChannel* temp = dynamic_cast<cDatarateChannel*>(type->create("temp"));
00077
00078 bandwidth = temp->par("datarate");
00079 errorRate = temp->par("ber");
00080 accessDelay = temp->par("delay");
00081
00082 txMaxQueueTime = ((float)sendQueueLength * 8) / bandwidth;
00083 txFinished = simTime();
00084
00085 delete temp;
00086 }
00087
00088 SimpleNodeEntry::SimpleNodeEntry(cModule* node,
00089 cChannelType* type,
00090 uint32_t sendQueueLength,
00091 NodeRecord* nodeRecord, int index)
00092 {
00093 ingate = node->getSubmodule("udp")->gate("network_in");
00094
00095
00096
00097 this->nodeRecord = nodeRecord;
00098 this->index = index;
00099
00100 cDatarateChannel* temp = dynamic_cast<cDatarateChannel*>(type->create("temp"));
00101
00102 bandwidth = temp->par("datarate");
00103 errorRate = temp->par("ber");
00104 accessDelay = temp->par("delay");
00105
00106 txMaxQueueTime = ((float)sendQueueLength * 8) / bandwidth;
00107 txFinished = simTime();
00108
00109 delete temp;
00110 }
00111
00112 float SimpleNodeEntry::operator-(const SimpleNodeEntry& entry) const
00113 {
00114 double sum_of_squares = 0;
00115 for (uint32_t i = 0; i < nodeRecord->dim; i++) {
00116 sum_of_squares += pow(nodeRecord->coords[i] -
00117 entry.nodeRecord->coords[i], 2);
00118 }
00119 return sqrt(sum_of_squares);
00120 }
00121
00122 SimpleNodeEntry::SimpleDelay SimpleNodeEntry::calcDelay(SimpleUDPPacket* msg, const SimpleNodeEntry& dest)
00123 {
00124 if ((pow(1 - errorRate, msg->getByteLength() * 8) <= uniform(0, 1))
00125 || (pow(1 - dest.errorRate, msg->getByteLength() * 8) <= uniform(0, 1))) {
00126 msg->setBitError(true);
00127 }
00128
00129 simtime_t now = simTime();
00130 simtime_t bandwidthDelay= ((msg->getByteLength() * 8) / bandwidth);
00131 simtime_t newTxFinished = std::max(txFinished, now) + bandwidthDelay;
00132
00133
00134 if ((newTxFinished > now + txMaxQueueTime) && (txMaxQueueTime != 0)) {
00135 EV << "[SimpleNodeEntry::calcDelay()]\n"
00136 << " Send queue overrun"
00137 << "\n newTxFinished = fmax(txFinished, now) + bandwidthDelay"
00138 << "\n newTxFinished = " << newTxFinished
00139 << "\n txFinished = " << txFinished
00140 << "\n now = " << now
00141 << "\n bandwidthDelay = " << bandwidthDelay
00142 << "\n (newTxFinished > now + txMaxQueueTime) == true"
00143 << "\n txMaxQueueTime = " << txMaxQueueTime
00144 << endl;
00145 return SimpleDelay(0, false);
00146 }
00147
00148 txFinished = newTxFinished;
00149
00150 simtime_t destBandwidthDelay = (msg->getByteLength() * 8) / dest.bandwidth;
00151 simtime_t coordDelay = 0.001 * (*this - dest);
00152
00153 return SimpleDelay(txFinished - now
00154 + accessDelay
00155 + coordDelay
00156 + destBandwidthDelay + dest.accessDelay, true);
00157 }
00158
00159 std::string SimpleNodeEntry::info() const
00160 {
00161 std::ostringstream str;
00162 str << *this;
00163 return str.str();
00164 }
00165
00166 std::ostream& operator<<(std::ostream& out, const SimpleNodeEntry& entry)
00167 {
00168 out << "(x:" << entry.nodeRecord->coords[0]
00169 << ", y:" << entry.nodeRecord->coords[1]
00170 << ")\nbandwidth = " << entry.bandwidth
00171 << ",\ndelay = " << entry.accessDelay
00172 << ",\nerrorRate = " << entry.errorRate
00173 << ",\ntxMaxQueueTime = " << entry.txMaxQueueTime
00174 << ",\ntxFinished = " << entry.txFinished;
00175
00176 return out;
00177 }