From dc28dfefd6b665ec5a08c4393cd69a7033cb23f2 Mon Sep 17 00:00:00 2001 From: Srivats P Date: Sun, 29 Nov 2015 21:18:31 +0530 Subject: [PATCH] Feature (contd.): Device Emulation - sort the device list returned by getDeviceList() RPC --- server/device.cpp | 15 +++++++++++++++ server/device.h | 5 ++++- server/devicemanager.cpp | 10 ++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/server/device.cpp b/server/device.cpp index 804bf0d..c174f05 100644 --- a/server/device.cpp +++ b/server/device.cpp @@ -457,3 +457,18 @@ void Device::sendArpRequest(PacketBuffer *pktBuf) qPrintable(QHostAddress(srcIp).toString()), qPrintable(QHostAddress(tgtIp).toString())); } + +bool operator<(const DeviceKey &a1, const DeviceKey &a2) +{ + int i = 0; + + while (i < a1.size()) { + if (uchar(a1.at(i)) < uchar(a2.at(i))) + return true; + if (uchar(a1.at(i)) > uchar(a2.at(i))) + return false; + i++; + } + + return false; +} diff --git a/server/device.h b/server/device.h index a18c940..4ef0192 100644 --- a/server/device.h +++ b/server/device.h @@ -29,7 +29,9 @@ along with this program. If not, see class DeviceManager; class PacketBuffer; -typedef QByteArray DeviceKey; +class DeviceKey: public QByteArray +{ +}; class Device { @@ -80,5 +82,6 @@ private: // data QHash arpTable; }; +bool operator<(const DeviceKey &a1, const DeviceKey &a2); #endif diff --git a/server/devicemanager.cpp b/server/devicemanager.cpp index e4bf9ff..bff84b4 100644 --- a/server/devicemanager.cpp +++ b/server/devicemanager.cpp @@ -25,6 +25,7 @@ along with this program. If not, see #include "../common/emulproto.pb.h" +#include #include #define __STDC_FORMAT_MACROS @@ -138,7 +139,16 @@ int DeviceManager::deviceCount() void DeviceManager::getDeviceList( OstProto::PortDeviceList *deviceList) { + // We want to return a sorted deviceList. However, deviceList_ + // is a QHash (unsorted) instead of a QMap (sorted) because + // QHash is faster. So here we use a temporary QMap to sort the + // list that will be returned + QMap list; foreach(Device *device, deviceList_) { + list.insert(device->key(), device); + } + + foreach(Device *device, list) { OstEmul::Device *dev = deviceList->AddExtension(OstEmul::port_device); device->getConfig(dev);