<commit_before>//
// This file is part of the Marble Virtual Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2010      Dennis Nienhüser <earthwings@gentoo.org>
//

#include "RoutingModel.h"

#include "MarbleDebug.h"
#include "MarbleDirs.h"
#include "MarbleMath.h"
#include "GeoDataCoordinates.h"
#include "GeoDataDocument.h"
#include "GeoDataGeometry.h"
#include "GeoDataFolder.h"
#include "GeoDataPlacemark.h"
#include "RouteRequest.h"
#include "PositionTracking.h"
#include "MarbleModel.h"
#include "MarbleWidget.h"
#include "MarbleGlobal.h"
#include "GeoDataExtendedData.h"

#include <QBuffer>
#include <QPointer>
#include <QRegExp>
#include <QVector>
#include <QHash>
#include <QByteArray>
#include <QMessageBox>
#include <QPixmap>
#include <QDomDocument>

namespace Marble
{

class RoutingModelPrivate
{
public:
    enum RouteDeviation
    {
        Unknown,
        OnRoute,
        OffRoute
    };

    RoutingModelPrivate( RouteRequest* request );

    Route m_route;

    RouteDeviation m_deviation;
    PositionTracking* m_positionTracking;
    RouteRequest* const m_request;
    GeoDataCoordinates m_position;
#if QT_VERSION >= 0x050000
    QHash<int, QByteArray> m_roleNames;
#endif

    void importPlacemark( RouteSegment &outline, QVector<RouteSegment> &segments, const GeoDataPlacemark *placemark );

    void updateViaPoints( const GeoDataCoordinates &position );
};

RoutingModelPrivate::RoutingModelPrivate( RouteRequest* request )
    : m_deviation( Unknown ),
      m_positionTracking( 0 ),
      m_request( request )
{
    // nothing to do
}

void RoutingModelPrivate::updateViaPoints( const GeoDataCoordinates &position )
{
    // Mark via points visited after approaching them in a range of 500m or less
    qreal const threshold = 500 / EARTH_RADIUS;
    for( int i=0; i<m_request->size(); ++i ) {
        if ( !m_request->visited( i ) ) {
            if ( distanceSphere( position, m_request->at( i ) ) < threshold ) {
                m_request->setVisited( i, true );
            }
        }
    }
}

void RoutingModelPrivate::importPlacemark( RouteSegment &outline, QVector<RouteSegment> &segments, const GeoDataPlacemark *placemark )
{
    GeoDataGeometry* geometry = placemark->geometry();
    GeoDataLineString* lineString = dynamic_cast<GeoDataLineString*>( geometry );
    QStringList blacklist = QStringList() << "" << "Route" << "Tessellated";
    RouteSegment segment;
    bool isOutline = true;
    if ( !blacklist.contains( placemark->name() ) ) {
        if( lineString ) {
            Maneuver maneuver;
            maneuver.setInstructionText( placemark->name() );
            maneuver.setPosition( lineString->at( 0 ) );

            if ( placemark->extendedData().contains( "turnType" ) ) {
                QVariant turnType = placemark->extendedData().value( "turnType" ).value();
                // The enum value is converted to/from an int in the QVariant
                // because only a limited set of data types can be serialized with QVariant's
                // toString() method (which is used to serialize <ExtendedData>/<Data> values)
                maneuver.setDirection( Maneuver::Direction( turnType.toInt() ) );
            }

            if ( placemark->extendedData().contains( "roadName" ) ) {
                QVariant roadName = placemark->extendedData().value( "roadName" ).value();
                maneuver.setRoadName( roadName.toString() );
            }

            segment.setManeuver( maneuver );
            isOutline = false;
        }
    }

    if ( lineString ) {
        segment.setPath( *lineString );

        if ( isOutline ) {
            outline = segment;
        } else {
            segments.push_back( segment );
        }
    }
}

RoutingModel::RoutingModel( RouteRequest* request, MarbleModel *model, QObject *parent ) :
        QAbstractListModel( parent ), d( new RoutingModelPrivate( request ) )
{
   if( model )
    {
        d->m_positionTracking = model->positionTracking();
        QObject::connect( d->m_positionTracking, SIGNAL(gpsLocation(GeoDataCoordinates,qreal)),
                 this, SLOT(updatePosition(GeoDataCoordinates,qreal)) );
    }

   QHash<int, QByteArray> roles;
   roles.insert( RoutingModel::TurnTypeIconRole, "turnTypeIcon" );
   roles.insert( RoutingModel::LongitudeRole, "longitude" );
   roles.insert( RoutingModel::LatitudeRole, "latitude" );
#if QT_VERSION < 0x050000
   setRoleNames( roles );
#else
   d->m_roleNames = roles;
#endif
}

RoutingModel::~RoutingModel()
{
    delete d;
}

int RoutingModel::rowCount ( const QModelIndex &parent ) const
{
    return parent.isValid() ? 0 : d->m_route.turnPoints().size();
}

QVariant RoutingModel::headerData ( int section, Qt::Orientation orientation, int role ) const
{
    if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0 ) {
        return QString( "Instruction" );
    }

    return QAbstractListModel::headerData( section, orientation, role );
}

QVariant RoutingModel::data ( const QModelIndex & index, int role ) const
{
    if ( !index.isValid() ) {
        return QVariant();
    }

    if ( index.row() < d->m_route.turnPoints().size() && index.column() == 0 ) {
        const RouteSegment &segment = d->m_route.at( index.row() );
        switch ( role ) {
        case Qt::DisplayRole:
        case Qt::ToolTipRole:
            return segment.maneuver().instructionText();
            break;
        case Qt::DecorationRole:
            {
                bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
                if ( segment.maneuver().hasWaypoint() ) {
                    int const size = smallScreen ? 64 : 32;
                    return d->m_request->pixmap( segment.maneuver().waypointIndex(), size, size/4 );
                } else {
                    QPixmap const pixmap = segment.maneuver().directionPixmap();
                    return smallScreen ? pixmap : pixmap.scaled( 32, 32 );
                }
            }
            break;
        case Qt::SizeHintRole:
            {
                bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
                int const size = smallScreen ? 64 : 32;
                return QSize( size, size );
            }
            break;
        case RoutingModel::CoordinateRole:
            return QVariant::fromValue( segment.maneuver().position() );
            break;
        case RoutingModel::LongitudeRole:
            return QVariant::fromValue( segment.maneuver().position().longitude( GeoDataCoordinates::Degree ) );
            break;
        case RoutingModel::LatitudeRole:
            return QVariant::fromValue( segment.maneuver().position().latitude( GeoDataCoordinates::Degree ) );
            break;
        case RoutingModel::TurnTypeIconRole:
            return segment.maneuver().directionPixmap();
            break;
        default:
            return QVariant();
        }
    }

    return QVariant();
}

#if QT_VERSION >= 0x050000
QHash<int, QByteArray> RoutingModel::roleNames() const
{
    return d->m_roleNames;
}
#endif

bool RoutingModel::setCurrentRoute( GeoDataDocument* document )
{
    d->m_route = Route();
    QVector<RouteSegment> segments;
    RouteSegment outline;

    QVector<GeoDataFolder*> folders = document->folderList();
    foreach( const GeoDataFolder *folder, folders ) {
        foreach( const GeoDataPlacemark *placemark, folder->placemarkList() ) {
            d->importPlacemark( outline, segments, placemark );
        }
    }

    foreach( const GeoDataPlacemark *placemark, document->placemarkList() ) {
        d->importPlacemark( outline, segments, placemark );
    }

    if ( segments.isEmpty() ) {
        segments << outline;
    }

    // Map via points onto segments
    if ( d->m_request->size() > 1 && segments.size() > 1 ) {
        int index = 0;
        for ( int j=0; j<d->m_request->size(); ++j ) {
            QPair<int, qreal> minimum( -1, -1.0 );
            int viaIndex = -1;
            for ( int i=index; i<segments.size(); ++i ) {
                RouteSegment const & segment = segments[i];
                GeoDataCoordinates closest;
                qreal const distance = segment.distanceTo( d->m_request->at( j ), closest, closest );
                if ( minimum.first < 0 || distance < minimum.second ) {
                    minimum.first = i;
                    minimum.second = distance;
                    viaIndex = j;
                }
            }

            if ( minimum.first >= 0 ) {
                index = minimum.first;
                Maneuver viaPoint = segments[ minimum.first ].maneuver();
                viaPoint.setWaypoint( d->m_request->at( viaIndex ), viaIndex );
                segments[ minimum.first ].setManeuver( viaPoint );
            }
        }
    }

    if ( segments.size() > 0 ) {
        foreach( const RouteSegment &segment, segments ) {
            d->m_route.addRouteSegment( segment );
        }
    }

    d->m_deviation = RoutingModelPrivate::Unknown;

    beginResetModel();
    endResetModel();
    emit currentRouteChanged();
    return true;
}

void RoutingModel::exportGpx( QIODevice *device ) const
{
    QString content( "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n" );
    content += "<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" creator=\"Marble\" version=\"1.1\" ";
    content += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ";
    content += "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 ";
    content += "http://www.topografix.com/GPX/1/1/gpx.xsd\">\n";
    content += "<metadata>\n  <link href=\"http://edu.kde.org/marble\">\n    ";
    content += "<text>Marble Virtual Globe</text>\n  </link>\n</metadata>\n";

    content += "  <rte>\n    <name>Route</name>\n";
    for ( int i=0; i<d->m_route.size(); ++i ) {
        const RouteSegment &segment = d->m_route.at( i );
        qreal lon = segment.maneuver().position().longitude( GeoDataCoordinates::Degree );
        qreal lat = segment.maneuver().position().latitude( GeoDataCoordinates::Degree );
        QString const text = segment.maneuver().instructionText();
        content += QString( "    <rtept lat=\"%1\" lon=\"%2\"><name>%3</name></rtept>\n" ).arg( lat, 0, 'f', 7 ).arg( lon, 0, 'f', 7 ).arg( text );
    }
    content += "  </rte>\n";

    content += "<trk>\n  <name>Route</name>\n    <trkseg>\n";
    GeoDataLineString points = d->m_route.path();
    for ( int i=0; i<points.size(); ++i ) {
        qreal lon = points[i].longitude( GeoDataCoordinates::Degree );
        qreal lat = points[i].latitude( GeoDataCoordinates::Degree );
        content += QString( "      <trkpt lat=\"%1\" lon=\"%2\"></trkpt>\n" ).arg( lat, 0, 'f', 7 ).arg( lon, 0, 'f', 7 );
    }
    content += "    </trkseg>\n  </trk>\n";
    content += "</gpx>\n";

    device->write( content.toUtf8() );
}

void RoutingModel::clear()
{
    d->m_route = Route();
    beginResetModel();
    endResetModel();
    emit currentRouteChanged();
}

int RoutingModel::rightNeighbor( const GeoDataCoordinates &position, RouteRequest const *const route ) const
{
    Q_ASSERT( route && "Must not pass a null route ");

    // Quick result for trivial cases
    if ( route->size() < 3 ) {
        return route->size() - 1;
    }

    // Generate an ordered list of all waypoints
    GeoDataLineString points = d->m_route.path();
    QMap<int,int> mapping;

    // Force first mapping point to match the route start
    mapping[0] = 0;

    // Calculate the mapping between waypoints and via points
    // Need two for loops to avoid getting stuck in local minima
    for ( int j=1; j<route->size()-1; ++j ) {
        qreal minDistance = -1.0;
        for ( int i=mapping[j-1]; i<points.size(); ++i ) {
            qreal distance = distanceSphere( points[i], route->at(j) );
            if (minDistance < 0.0 || distance < minDistance ) {
                mapping[j] = i;
                minDistance = distance;
            }
        }
    }

    // Determine waypoint with minimum distance to the provided position
    qreal minWaypointDistance = -1.0;
    int waypoint=0;
    for ( int i=0; i<points.size(); ++i ) {
        qreal waypointDistance = distanceSphere( points[i], position );
        if ( minWaypointDistance < 0.0 || waypointDistance < minWaypointDistance ) {
            minWaypointDistance = waypointDistance;
            waypoint = i;
        }
    }

    // Force last mapping point to match the route destination
    mapping[route->size()-1] = points.size()-1;

    // Determine neighbor based on the mapping
    QMap<int, int>::const_iterator iter = mapping.constBegin();
    for ( ; iter != mapping.constEnd(); ++iter ) {
        if ( iter.value() > waypoint ) {
            int index = iter.key();
            Q_ASSERT( index >= 0 && index <= route->size() );
            return index;
        }
    }

    return route->size()-1;
}

void RoutingModel::updatePosition( GeoDataCoordinates location, qreal /*speed*/ )
{
    d->m_position = location;
    d->m_route.setPosition( location );

    d->updateViaPoints( d->m_position );
    qreal distance = EARTH_RADIUS * distanceSphere( location, d->m_route.positionOnRoute() );
    emit positionChanged();

    qreal deviation = 0.0;
    if ( d->m_positionTracking && d->m_positionTracking->accuracy().vertical > 0.0 ) {
        deviation = qMax<qreal>( d->m_positionTracking->accuracy().vertical, d->m_positionTracking->accuracy().horizontal );
    }
    qreal const threshold = deviation + 100.0;

    RoutingModelPrivate::RouteDeviation const deviated = distance < threshold ? RoutingModelPrivate::OnRoute : RoutingModelPrivate::OffRoute;
    if ( d->m_deviation != deviated ) {
        d->m_deviation = deviated;
        emit deviatedFromRoute( deviated == RoutingModelPrivate::OffRoute );
    }
}

bool RoutingModel::deviatedFromRoute() const
{
    return d->m_deviation == RoutingModelPrivate::OffRoute;
}

const Route & RoutingModel::route() const
{
    return d->m_route;
}

} // namespace Marble

#include "RoutingModel.moc"
<commit_msg>Fix missing display role name<commit_after>//
// This file is part of the Marble Virtual Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2010      Dennis Nienhüser <earthwings@gentoo.org>
//

#include "RoutingModel.h"

#include "MarbleDebug.h"
#include "MarbleDirs.h"
#include "MarbleMath.h"
#include "GeoDataCoordinates.h"
#include "GeoDataDocument.h"
#include "GeoDataGeometry.h"
#include "GeoDataFolder.h"
#include "GeoDataPlacemark.h"
#include "RouteRequest.h"
#include "PositionTracking.h"
#include "MarbleModel.h"
#include "MarbleWidget.h"
#include "MarbleGlobal.h"
#include "GeoDataExtendedData.h"

#include <QBuffer>
#include <QPointer>
#include <QRegExp>
#include <QVector>
#include <QHash>
#include <QByteArray>
#include <QMessageBox>
#include <QPixmap>
#include <QDomDocument>

namespace Marble
{

class RoutingModelPrivate
{
public:
    enum RouteDeviation
    {
        Unknown,
        OnRoute,
        OffRoute
    };

    RoutingModelPrivate( RouteRequest* request );

    Route m_route;

    RouteDeviation m_deviation;
    PositionTracking* m_positionTracking;
    RouteRequest* const m_request;
    GeoDataCoordinates m_position;
#if QT_VERSION >= 0x050000
    QHash<int, QByteArray> m_roleNames;
#endif

    void importPlacemark( RouteSegment &outline, QVector<RouteSegment> &segments, const GeoDataPlacemark *placemark );

    void updateViaPoints( const GeoDataCoordinates &position );
};

RoutingModelPrivate::RoutingModelPrivate( RouteRequest* request )
    : m_deviation( Unknown ),
      m_positionTracking( 0 ),
      m_request( request )
{
    // nothing to do
}

void RoutingModelPrivate::updateViaPoints( const GeoDataCoordinates &position )
{
    // Mark via points visited after approaching them in a range of 500m or less
    qreal const threshold = 500 / EARTH_RADIUS;
    for( int i=0; i<m_request->size(); ++i ) {
        if ( !m_request->visited( i ) ) {
            if ( distanceSphere( position, m_request->at( i ) ) < threshold ) {
                m_request->setVisited( i, true );
            }
        }
    }
}

void RoutingModelPrivate::importPlacemark( RouteSegment &outline, QVector<RouteSegment> &segments, const GeoDataPlacemark *placemark )
{
    GeoDataGeometry* geometry = placemark->geometry();
    GeoDataLineString* lineString = dynamic_cast<GeoDataLineString*>( geometry );
    QStringList blacklist = QStringList() << "" << "Route" << "Tessellated";
    RouteSegment segment;
    bool isOutline = true;
    if ( !blacklist.contains( placemark->name() ) ) {
        if( lineString ) {
            Maneuver maneuver;
            maneuver.setInstructionText( placemark->name() );
            maneuver.setPosition( lineString->at( 0 ) );

            if ( placemark->extendedData().contains( "turnType" ) ) {
                QVariant turnType = placemark->extendedData().value( "turnType" ).value();
                // The enum value is converted to/from an int in the QVariant
                // because only a limited set of data types can be serialized with QVariant's
                // toString() method (which is used to serialize <ExtendedData>/<Data> values)
                maneuver.setDirection( Maneuver::Direction( turnType.toInt() ) );
            }

            if ( placemark->extendedData().contains( "roadName" ) ) {
                QVariant roadName = placemark->extendedData().value( "roadName" ).value();
                maneuver.setRoadName( roadName.toString() );
            }

            segment.setManeuver( maneuver );
            isOutline = false;
        }
    }

    if ( lineString ) {
        segment.setPath( *lineString );

        if ( isOutline ) {
            outline = segment;
        } else {
            segments.push_back( segment );
        }
    }
}

RoutingModel::RoutingModel( RouteRequest* request, MarbleModel *model, QObject *parent ) :
        QAbstractListModel( parent ), d( new RoutingModelPrivate( request ) )
{
   if( model )
    {
        d->m_positionTracking = model->positionTracking();
        QObject::connect( d->m_positionTracking, SIGNAL(gpsLocation(GeoDataCoordinates,qreal)),
                 this, SLOT(updatePosition(GeoDataCoordinates,qreal)) );
    }

   QHash<int, QByteArray> roles;
   roles.insert( Qt::DisplayRole, "display" );
   roles.insert( RoutingModel::TurnTypeIconRole, "turnTypeIcon" );
   roles.insert( RoutingModel::LongitudeRole, "longitude" );
   roles.insert( RoutingModel::LatitudeRole, "latitude" );
#if QT_VERSION < 0x050000
   setRoleNames( roles );
#else
   d->m_roleNames = roles;
#endif
}

RoutingModel::~RoutingModel()
{
    delete d;
}

int RoutingModel::rowCount ( const QModelIndex &parent ) const
{
    return parent.isValid() ? 0 : d->m_route.turnPoints().size();
}

QVariant RoutingModel::headerData ( int section, Qt::Orientation orientation, int role ) const
{
    if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0 ) {
        return QString( "Instruction" );
    }

    return QAbstractListModel::headerData( section, orientation, role );
}

QVariant RoutingModel::data ( const QModelIndex & index, int role ) const
{
    if ( !index.isValid() ) {
        return QVariant();
    }

    if ( index.row() < d->m_route.turnPoints().size() && index.column() == 0 ) {
        const RouteSegment &segment = d->m_route.at( index.row() );
        switch ( role ) {
        case Qt::DisplayRole:
        case Qt::ToolTipRole:
            return segment.maneuver().instructionText();
            break;
        case Qt::DecorationRole:
            {
                bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
                if ( segment.maneuver().hasWaypoint() ) {
                    int const size = smallScreen ? 64 : 32;
                    return d->m_request->pixmap( segment.maneuver().waypointIndex(), size, size/4 );
                } else {
                    QPixmap const pixmap = segment.maneuver().directionPixmap();
                    return smallScreen ? pixmap : pixmap.scaled( 32, 32 );
                }
            }
            break;
        case Qt::SizeHintRole:
            {
                bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
                int const size = smallScreen ? 64 : 32;
                return QSize( size, size );
            }
            break;
        case RoutingModel::CoordinateRole:
            return QVariant::fromValue( segment.maneuver().position() );
            break;
        case RoutingModel::LongitudeRole:
            return QVariant::fromValue( segment.maneuver().position().longitude( GeoDataCoordinates::Degree ) );
            break;
        case RoutingModel::LatitudeRole:
            return QVariant::fromValue( segment.maneuver().position().latitude( GeoDataCoordinates::Degree ) );
            break;
        case RoutingModel::TurnTypeIconRole:
            return segment.maneuver().directionPixmap();
            break;
        default:
            return QVariant();
        }
    }

    return QVariant();
}

#if QT_VERSION >= 0x050000
QHash<int, QByteArray> RoutingModel::roleNames() const
{
    return d->m_roleNames;
}
#endif

bool RoutingModel::setCurrentRoute( GeoDataDocument* document )
{
    d->m_route = Route();
    QVector<RouteSegment> segments;
    RouteSegment outline;

    QVector<GeoDataFolder*> folders = document->folderList();
    foreach( const GeoDataFolder *folder, folders ) {
        foreach( const GeoDataPlacemark *placemark, folder->placemarkList() ) {
            d->importPlacemark( outline, segments, placemark );
        }
    }

    foreach( const GeoDataPlacemark *placemark, document->placemarkList() ) {
        d->importPlacemark( outline, segments, placemark );
    }

    if ( segments.isEmpty() ) {
        segments << outline;
    }

    // Map via points onto segments
    if ( d->m_request->size() > 1 && segments.size() > 1 ) {
        int index = 0;
        for ( int j=0; j<d->m_request->size(); ++j ) {
            QPair<int, qreal> minimum( -1, -1.0 );
            int viaIndex = -1;
            for ( int i=index; i<segments.size(); ++i ) {
                RouteSegment const & segment = segments[i];
                GeoDataCoordinates closest;
                qreal const distance = segment.distanceTo( d->m_request->at( j ), closest, closest );
                if ( minimum.first < 0 || distance < minimum.second ) {
                    minimum.first = i;
                    minimum.second = distance;
                    viaIndex = j;
                }
            }

            if ( minimum.first >= 0 ) {
                index = minimum.first;
                Maneuver viaPoint = segments[ minimum.first ].maneuver();
                viaPoint.setWaypoint( d->m_request->at( viaIndex ), viaIndex );
                segments[ minimum.first ].setManeuver( viaPoint );
            }
        }
    }

    if ( segments.size() > 0 ) {
        foreach( const RouteSegment &segment, segments ) {
            d->m_route.addRouteSegment( segment );
        }
    }

    d->m_deviation = RoutingModelPrivate::Unknown;

    beginResetModel();
    endResetModel();
    emit currentRouteChanged();
    return true;
}

void RoutingModel::exportGpx( QIODevice *device ) const
{
    QString content( "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n" );
    content += "<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" creator=\"Marble\" version=\"1.1\" ";
    content += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ";
    content += "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 ";
    content += "http://www.topografix.com/GPX/1/1/gpx.xsd\">\n";
    content += "<metadata>\n  <link href=\"http://edu.kde.org/marble\">\n    ";
    content += "<text>Marble Virtual Globe</text>\n  </link>\n</metadata>\n";

    content += "  <rte>\n    <name>Route</name>\n";
    for ( int i=0; i<d->m_route.size(); ++i ) {
        const RouteSegment &segment = d->m_route.at( i );
        qreal lon = segment.maneuver().position().longitude( GeoDataCoordinates::Degree );
        qreal lat = segment.maneuver().position().latitude( GeoDataCoordinates::Degree );
        QString const text = segment.maneuver().instructionText();
        content += QString( "    <rtept lat=\"%1\" lon=\"%2\"><name>%3</name></rtept>\n" ).arg( lat, 0, 'f', 7 ).arg( lon, 0, 'f', 7 ).arg( text );
    }
    content += "  </rte>\n";

    content += "<trk>\n  <name>Route</name>\n    <trkseg>\n";
    GeoDataLineString points = d->m_route.path();
    for ( int i=0; i<points.size(); ++i ) {
        qreal lon = points[i].longitude( GeoDataCoordinates::Degree );
        qreal lat = points[i].latitude( GeoDataCoordinates::Degree );
        content += QString( "      <trkpt lat=\"%1\" lon=\"%2\"></trkpt>\n" ).arg( lat, 0, 'f', 7 ).arg( lon, 0, 'f', 7 );
    }
    content += "    </trkseg>\n  </trk>\n";
    content += "</gpx>\n";

    device->write( content.toUtf8() );
}

void RoutingModel::clear()
{
    d->m_route = Route();
    beginResetModel();
    endResetModel();
    emit currentRouteChanged();
}

int RoutingModel::rightNeighbor( const GeoDataCoordinates &position, RouteRequest const *const route ) const
{
    Q_ASSERT( route && "Must not pass a null route ");

    // Quick result for trivial cases
    if ( route->size() < 3 ) {
        return route->size() - 1;
    }

    // Generate an ordered list of all waypoints
    GeoDataLineString points = d->m_route.path();
    QMap<int,int> mapping;

    // Force first mapping point to match the route start
    mapping[0] = 0;

    // Calculate the mapping between waypoints and via points
    // Need two for loops to avoid getting stuck in local minima
    for ( int j=1; j<route->size()-1; ++j ) {
        qreal minDistance = -1.0;
        for ( int i=mapping[j-1]; i<points.size(); ++i ) {
            qreal distance = distanceSphere( points[i], route->at(j) );
            if (minDistance < 0.0 || distance < minDistance ) {
                mapping[j] = i;
                minDistance = distance;
            }
        }
    }

    // Determine waypoint with minimum distance to the provided position
    qreal minWaypointDistance = -1.0;
    int waypoint=0;
    for ( int i=0; i<points.size(); ++i ) {
        qreal waypointDistance = distanceSphere( points[i], position );
        if ( minWaypointDistance < 0.0 || waypointDistance < minWaypointDistance ) {
            minWaypointDistance = waypointDistance;
            waypoint = i;
        }
    }

    // Force last mapping point to match the route destination
    mapping[route->size()-1] = points.size()-1;

    // Determine neighbor based on the mapping
    QMap<int, int>::const_iterator iter = mapping.constBegin();
    for ( ; iter != mapping.constEnd(); ++iter ) {
        if ( iter.value() > waypoint ) {
            int index = iter.key();
            Q_ASSERT( index >= 0 && index <= route->size() );
            return index;
        }
    }

    return route->size()-1;
}

void RoutingModel::updatePosition( GeoDataCoordinates location, qreal /*speed*/ )
{
    d->m_position = location;
    d->m_route.setPosition( location );

    d->updateViaPoints( d->m_position );
    qreal distance = EARTH_RADIUS * distanceSphere( location, d->m_route.positionOnRoute() );
    emit positionChanged();

    qreal deviation = 0.0;
    if ( d->m_positionTracking && d->m_positionTracking->accuracy().vertical > 0.0 ) {
        deviation = qMax<qreal>( d->m_positionTracking->accuracy().vertical, d->m_positionTracking->accuracy().horizontal );
    }
    qreal const threshold = deviation + 100.0;

    RoutingModelPrivate::RouteDeviation const deviated = distance < threshold ? RoutingModelPrivate::OnRoute : RoutingModelPrivate::OffRoute;
    if ( d->m_deviation != deviated ) {
        d->m_deviation = deviated;
        emit deviatedFromRoute( deviated == RoutingModelPrivate::OffRoute );
    }
}

bool RoutingModel::deviatedFromRoute() const
{
    return d->m_deviation == RoutingModelPrivate::OffRoute;
}

const Route & RoutingModel::route() const
{
    return d->m_route;
}

} // namespace Marble

#include "RoutingModel.moc"
<|endoftext|>


<commit_before>#include "DiagnosticMacros.hpp"

BEGIN_IOKIT_INCLUDE;
#include <IOKit/IOLib.h>
#include <IOKit/IOUserClient.h>
END_IOKIT_INCLUDE;

#include "UserClient.hpp"

#define super IOUserClient

OSDefineMetaClassAndStructors(org_pqrs_driver_VirtualHIDManager_UserClient, IOUserClient);

IOExternalMethodDispatch org_pqrs_driver_VirtualHIDManager_UserClient::methods_[static_cast<size_t>(virtual_hid_manager_user_client_method::end_)] = {
    {
        // keyboard_input_report
        reinterpret_cast<IOExternalMethodAction>(&staticKeyboardInputReportCallback), // Method pointer.
        0,                                                                            // One scalar input value.
        sizeof(hid_report::keyboard_input),                                           // No struct input value.
        0,                                                                            // No scalar output value.
        0                                                                             // No struct output value.
    },
    {
        // pointing_input_report
        reinterpret_cast<IOExternalMethodAction>(&staticPointingInputReportCallback), // Method pointer.
        0,                                                                            // One scalar input value.
        sizeof(hid_report::pointing_input),                                           // No struct input value.
        0,                                                                            // No scalar output value.
        0                                                                             // No struct output value.
    },
};

bool org_pqrs_driver_VirtualHIDManager_UserClient::initWithTask(task_t owningTask, void* securityToken, UInt32 type) {
  if (clientHasPrivilege(owningTask, kIOClientPrivilegeAdministrator) != KERN_SUCCESS) {
    IOLog("%s Error: clientHasPrivilege failed.\n", __PRETTY_FUNCTION__);
    return false;
  }

  if (!super::initWithTask(owningTask, securityToken, type)) {
    return false;
  }

  owner_ = owningTask;
  provider_ = nullptr;

  return true;
}

IOReturn org_pqrs_driver_VirtualHIDManager_UserClient::clientClose(void) {
  IOLog("%s\n", __PRETTY_FUNCTION__);

  // clear input events.
  hid_report::keyboard_input report;
  keyboardInputReportCallback(report);

  return super::clientClose();
}

bool org_pqrs_driver_VirtualHIDManager_UserClient::start(IOService* provider) {
  provider_ = OSDynamicCast(org_pqrs_driver_VirtualHIDManager, provider);
  if (!provider_) {
    IOLog("%s Error: provider_ == nullptr\n", __PRETTY_FUNCTION__);
    return false;
  }

  if (!super::start(provider)) {
    return false;
  }

  return true;
}

IOReturn org_pqrs_driver_VirtualHIDManager_UserClient::externalMethod(uint32_t selector, IOExternalMethodArguments* arguments,
                                                                      IOExternalMethodDispatch* dispatch, OSObject* target, void* reference) {
  if (selector >= static_cast<uint32_t>(virtual_hid_manager_user_client_method::end_)) {
    return kIOReturnUnsupported;
  }

  dispatch = &(methods_[selector]);
  if (!target) {
    target = this;
  }

  return super::externalMethod(selector, arguments, dispatch, target, reference);
}

#pragma mark - keyboard_input_report

IOReturn org_pqrs_driver_VirtualHIDManager_UserClient::staticKeyboardInputReportCallback(org_pqrs_driver_VirtualHIDManager_UserClient* target, void* reference, IOExternalMethodArguments* arguments) {
  if (!target) {
    return kIOReturnBadArgument;
  }
  if (!arguments) {
    return kIOReturnBadArgument;
  }

  if (auto input = static_cast<const hid_report::keyboard_input*>(arguments->structureInput)) {
    return target->keyboardInputReportCallback(*input);
  }

  return kIOReturnBadArgument;
}

IOReturn org_pqrs_driver_VirtualHIDManager_UserClient::keyboardInputReportCallback(const hid_report::keyboard_input& input) {
  if (!provider_) {
    return kIOReturnError;
  }

  if (auto keyboard = provider_->getVirtualHIDKeyboard()) {
    if (auto report = IOBufferMemoryDescriptor::withBytes(&input, sizeof(input), kIODirectionNone)) {
      auto result = keyboard->handleReport(report, kIOHIDReportTypeInput, kIOHIDOptionsTypeNone);
      report->release();
      return result;
    }
  }

  return kIOReturnError;
}

#pragma mark - consumer_input_report

IOReturn org_pqrs_driver_VirtualHIDManager_UserClient::staticPointingInputReportCallback(org_pqrs_driver_VirtualHIDManager_UserClient* target, void* reference, IOExternalMethodArguments* arguments) {
  if (!target) {
    return kIOReturnBadArgument;
  }
  if (!arguments) {
    return kIOReturnBadArgument;
  }

  if (auto input = static_cast<const hid_report::pointing_input*>(arguments->structureInput)) {
    return target->pointingInputReportCallback(*input);
  }

  return kIOReturnBadArgument;
}

IOReturn org_pqrs_driver_VirtualHIDManager_UserClient::pointingInputReportCallback(const hid_report::pointing_input& input) {
  if (!provider_) {
    return kIOReturnError;
  }

  if (auto pointing = provider_->getVirtualHIDPointing()) {
    if (auto report = IOBufferMemoryDescriptor::withBytes(&input, sizeof(input), kIODirectionNone)) {
      auto result = pointing->handleReport(report, kIOHIDReportTypeInput, kIOHIDOptionsTypeNone);
      report->release();
      return result;
    }
  }

  return kIOReturnError;
}
<commit_msg>add logs<commit_after>#include "DiagnosticMacros.hpp"

BEGIN_IOKIT_INCLUDE;
#include <IOKit/IOLib.h>
#include <IOKit/IOUserClient.h>
END_IOKIT_INCLUDE;

#include "UserClient.hpp"

#define super IOUserClient

OSDefineMetaClassAndStructors(org_pqrs_driver_VirtualHIDManager_UserClient, IOUserClient);

IOExternalMethodDispatch org_pqrs_driver_VirtualHIDManager_UserClient::methods_[static_cast<size_t>(virtual_hid_manager_user_client_method::end_)] = {
    {
        // keyboard_input_report
        reinterpret_cast<IOExternalMethodAction>(&staticKeyboardInputReportCallback), // Method pointer.
        0,                                                                            // One scalar input value.
        sizeof(hid_report::keyboard_input),                                           // No struct input value.
        0,                                                                            // No scalar output value.
        0                                                                             // No struct output value.
    },
    {
        // pointing_input_report
        reinterpret_cast<IOExternalMethodAction>(&staticPointingInputReportCallback), // Method pointer.
        0,                                                                            // One scalar input value.
        sizeof(hid_report::pointing_input),                                           // No struct input value.
        0,                                                                            // No scalar output value.
        0                                                                             // No struct output value.
    },
};

bool org_pqrs_driver_VirtualHIDManager_UserClient::initWithTask(task_t owningTask, void* securityToken, UInt32 type) {
  IOLog("%s\n", __PRETTY_FUNCTION__);

  if (clientHasPrivilege(owningTask, kIOClientPrivilegeAdministrator) != KERN_SUCCESS) {
    IOLog("%s Error: clientHasPrivilege failed.\n", __PRETTY_FUNCTION__);
    return false;
  }

  if (!super::initWithTask(owningTask, securityToken, type)) {
    IOLog("%s Error: initWithTask failed.\n", __PRETTY_FUNCTION__);
    return false;
  }

  owner_ = owningTask;
  provider_ = nullptr;

  return true;
}

IOReturn org_pqrs_driver_VirtualHIDManager_UserClient::clientClose(void) {
  IOLog("%s\n", __PRETTY_FUNCTION__);

  // clear input events.
  hid_report::keyboard_input report;
  keyboardInputReportCallback(report);

  return super::clientClose();
}

bool org_pqrs_driver_VirtualHIDManager_UserClient::start(IOService* provider) {
  provider_ = OSDynamicCast(org_pqrs_driver_VirtualHIDManager, provider);
  if (!provider_) {
    IOLog("%s Error: provider_ == nullptr\n", __PRETTY_FUNCTION__);
    return false;
  }

  if (!super::start(provider)) {
    return false;
  }

  return true;
}

IOReturn org_pqrs_driver_VirtualHIDManager_UserClient::externalMethod(uint32_t selector, IOExternalMethodArguments* arguments,
                                                                      IOExternalMethodDispatch* dispatch, OSObject* target, void* reference) {
  if (selector >= static_cast<uint32_t>(virtual_hid_manager_user_client_method::end_)) {
    return kIOReturnUnsupported;
  }

  dispatch = &(methods_[selector]);
  if (!target) {
    target = this;
  }

  return super::externalMethod(selector, arguments, dispatch, target, reference);
}

#pragma mark - keyboard_input_report

IOReturn org_pqrs_driver_VirtualHIDManager_UserClient::staticKeyboardInputReportCallback(org_pqrs_driver_VirtualHIDManager_UserClient* target, void* reference, IOExternalMethodArguments* arguments) {
  if (!target) {
    return kIOReturnBadArgument;
  }
  if (!arguments) {
    return kIOReturnBadArgument;
  }

  if (auto input = static_cast<const hid_report::keyboard_input*>(arguments->structureInput)) {
    return target->keyboardInputReportCallback(*input);
  }

  return kIOReturnBadArgument;
}

IOReturn org_pqrs_driver_VirtualHIDManager_UserClient::keyboardInputReportCallback(const hid_report::keyboard_input& input) {
  if (!provider_) {
    return kIOReturnError;
  }

  if (auto keyboard = provider_->getVirtualHIDKeyboard()) {
    if (auto report = IOBufferMemoryDescriptor::withBytes(&input, sizeof(input), kIODirectionNone)) {
      auto result = keyboard->handleReport(report, kIOHIDReportTypeInput, kIOHIDOptionsTypeNone);
      report->release();
      return result;
    }
  }

  return kIOReturnError;
}

#pragma mark - consumer_input_report

IOReturn org_pqrs_driver_VirtualHIDManager_UserClient::staticPointingInputReportCallback(org_pqrs_driver_VirtualHIDManager_UserClient* target, void* reference, IOExternalMethodArguments* arguments) {
  if (!target) {
    return kIOReturnBadArgument;
  }
  if (!arguments) {
    return kIOReturnBadArgument;
  }

  if (auto input = static_cast<const hid_report::pointing_input*>(arguments->structureInput)) {
    return target->pointingInputReportCallback(*input);
  }

  return kIOReturnBadArgument;
}

IOReturn org_pqrs_driver_VirtualHIDManager_UserClient::pointingInputReportCallback(const hid_report::pointing_input& input) {
  if (!provider_) {
    return kIOReturnError;
  }

  if (auto pointing = provider_->getVirtualHIDPointing()) {
    if (auto report = IOBufferMemoryDescriptor::withBytes(&input, sizeof(input), kIODirectionNone)) {
      auto result = pointing->handleReport(report, kIOHIDReportTypeInput, kIOHIDOptionsTypeNone);
      report->release();
      return result;
    }
  }

  return kIOReturnError;
}
<|endoftext|>


<commit_before><commit_msg>Minor change.<commit_after><|endoftext|>


<commit_before>#include "samples.h"

#include <tbb/parallel_for.h>

#include <cassert>

namespace lightfields {

Samples::Samples() {
}

Samples::~Samples() {
}

void Samples::offset(float uvOffset) {
	tbb::parallel_for(std::size_t(0), m_samples.size(), [&](std::size_t i) {
		auto& s = m_samples[i];
		s.xy += s.uv * uvOffset;
	});
}

void Samples::threshold(float uvThreshold) {
	auto tgt = m_samples.begin();
	auto src = m_samples.begin();

	uvThreshold *= uvThreshold;
	for(; src != m_samples.end(); ++src) {
		if(src->uv.length2() < uvThreshold) {
			*tgt = *src;
			++tgt;
		}
	}
	m_samples.resize(tgt - m_samples.begin());
}

void Samples::filterInvalid() {
	auto tgt = m_samples.begin();
	auto src = m_samples.begin();

	const float widthf = m_size[0];
	const float heightf = m_size[1];
	for(; src != m_samples.end(); ++src) {
		if(src->xy[0] >= 0.0f && src->xy[1] >= 0.0f && src->xy[0] < widthf && src->xy[1] < heightf) {
			*tgt = *src;
			++tgt;
		}
	}
	m_samples.resize(tgt - m_samples.begin());
}

void Samples::scale(float xy_scale) {
	const Imath::V2f center(m_size[0] / 2, m_size[1] / 2);

	tbb::parallel_for(std::size_t(0), m_samples.size(), [&](std::size_t i) {
		auto& sample = m_samples[i];
		sample.xy = (sample.xy - center) * xy_scale + center;
	});
}

Samples::const_iterator Samples::begin() const {
	return m_samples.begin();
}

Samples::const_iterator Samples::end() const {
	return m_samples.end();
}

std::size_t Samples::size() const {
	return m_samples.size();
}

bool Samples::empty() const {
	return m_samples.empty();
}

const Imath::V2i Samples::sensorSize() const {
	return m_size;
}

/////////

Samples Samples::fromPattern(const Pattern& pattern, const cv::Mat& m) {
	assert(m.rows == pattern.sensorResolution().y && m.cols == pattern.sensorResolution().x);
	assert(m.type() == CV_32FC1);

	// if(data.type() != CV_32FC1 && data.type() != CV_32FC3)
	// 	throw std::runtime_error("Only 32-bit single-float or 32-bit 3 channel float format supported on input.");

	Samples result;

	result.m_size = pattern.sensorResolution();
	result.m_samples.resize(result.m_size[0] * result.m_size[1]);

	// assemble the samples
	tbb::parallel_for(std::size_t(0), (std::size_t)result.m_size[0], [&](std::size_t y) {
		for(std::size_t x = 0; x < (std::size_t)result.m_size[1]; ++x) {
			auto& sample = result.m_samples[y * result.m_size[0] + x];

			const Pattern::Sample coords = pattern.sample(Imath::V2i(x, y));

			// and UV coordinates, -1..1
			sample.uv = coords.offset;

			// target pixel position, normalized (0..1) - adding the UV offset to handle displacement
			sample.xy = coords.lensCenter;

			// hardcoded bayer pattern, for now - generates kRed, kGreen or kBlue only
			sample.color = Color((x % 2) + (y % 2));

			// and the value
			sample.value = Imath::V3f(0, 0, 0);
			sample.value[sample.color] = *m.ptr<float>(y, x);
		}
	});

	return result;
}

/////////

std::ostream& operator<<(std::ostream& out, const Samples& f) {
	out << "(lightfield samples - " << f.size() << " samples)";

	return out;
}

}  // namespace lightfields
<commit_msg>Fixed Samples::fromPattern() to handle RGB data<commit_after>#include "samples.h"

#include <tbb/parallel_for.h>

#include <cassert>

namespace lightfields {

Samples::Samples() {
}

Samples::~Samples() {
}

void Samples::offset(float uvOffset) {
	tbb::parallel_for(std::size_t(0), m_samples.size(), [&](std::size_t i) {
		auto& s = m_samples[i];
		s.xy += s.uv * uvOffset;
	});
}

void Samples::threshold(float uvThreshold) {
	auto tgt = m_samples.begin();
	auto src = m_samples.begin();

	uvThreshold *= uvThreshold;
	for(; src != m_samples.end(); ++src) {
		if(src->uv.length2() < uvThreshold) {
			*tgt = *src;
			++tgt;
		}
	}
	m_samples.resize(tgt - m_samples.begin());
}

void Samples::filterInvalid() {
	auto tgt = m_samples.begin();
	auto src = m_samples.begin();

	const float widthf = m_size[0];
	const float heightf = m_size[1];
	for(; src != m_samples.end(); ++src) {
		if(src->xy[0] >= 0.0f && src->xy[1] >= 0.0f && src->xy[0] < widthf && src->xy[1] < heightf) {
			*tgt = *src;
			++tgt;
		}
	}
	m_samples.resize(tgt - m_samples.begin());
}

void Samples::scale(float xy_scale) {
	const Imath::V2f center(m_size[0] / 2, m_size[1] / 2);

	tbb::parallel_for(std::size_t(0), m_samples.size(), [&](std::size_t i) {
		auto& sample = m_samples[i];
		sample.xy = (sample.xy - center) * xy_scale + center;
	});
}

Samples::const_iterator Samples::begin() const {
	return m_samples.begin();
}

Samples::const_iterator Samples::end() const {
	return m_samples.end();
}

std::size_t Samples::size() const {
	return m_samples.size();
}

bool Samples::empty() const {
	return m_samples.empty();
}

const Imath::V2i Samples::sensorSize() const {
	return m_size;
}

/////////

namespace {

template <int CV_TYPE>
struct AssignSample;

template <>
struct AssignSample<CV_32FC1> {
	static void assign(Samples::Sample& sample, std::size_t x, std::size_t y, const cv::Mat& m) {
		// hardcoded bayer pattern, for now - generates kRed, kGreen or kBlue only
		sample.color = Samples::Color((x % 2) + (y % 2));

		// and the value
		sample.value = Imath::V3f(0, 0, 0);
		sample.value[sample.color] = *m.ptr<float>(y, x);
	}
};

template <>
struct AssignSample<CV_32FC3> {
	static void assign(Samples::Sample& sample, std::size_t x, std::size_t y, const cv::Mat& m) {
		// hardcoded bayer pattern, for now - generates kRed, kGreen or kBlue only
		sample.color = Samples::kRGB;

		// and the value
		const float* ptr = m.ptr<float>(y, x);
		sample.value = Imath::V3f(ptr[0], ptr[1], ptr[2]);
	}
};

template <int CV_TYPE>
std::vector<Samples::Sample> makeSamples(const Pattern& pattern, const cv::Mat& m) {
	std::vector<Samples::Sample> result;
	result.resize(m.rows * m.cols);

	// assemble the samples
	tbb::parallel_for(0, m.rows, [&](int y) {
		for(int x = 0; x < m.cols; ++x) {
			auto& sample = result[y * m.cols + x];

			const Pattern::Sample coords = pattern.sample(Imath::V2i(x, y));

			// and UV coordinates, -1..1
			sample.uv = coords.offset;

			// target pixel position, normalized (0..1) - adding the UV offset to handle displacement
			sample.xy = coords.lensCenter;

			// and assign the value
			AssignSample<CV_TYPE>::assign(sample, x, y, m);
		}
	});

	return result;
}

}  // namespace

Samples Samples::fromPattern(const Pattern& pattern, const cv::Mat& m) {
	assert(m.rows == pattern.sensorResolution().y && m.cols == pattern.sensorResolution().x);
	assert(m.type() == CV_32FC1 || m.type() == CV_32FC3);

	// if(data.type() != CV_32FC1 && data.type() != CV_32FC3)
	// 	throw std::runtime_error("Only 32-bit single-float or 32-bit 3 channel float format supported on input.");

	Samples result;

	result.m_size = pattern.sensorResolution();

	switch(m.type()) {
		case CV_32FC1:
			result.m_samples = makeSamples<CV_32FC1>(pattern, m);
			break;

		case CV_32FC3:
			result.m_samples = makeSamples<CV_32FC3>(pattern, m);
			break;
	}

	return result;
}

/////////

std::ostream& operator<<(std::ostream& out, const Samples& f) {
	out << "(lightfield samples - " << f.size() << " samples)";

	return out;
}

}  // namespace lightfields
<|endoftext|>


<commit_before><commit_msg>don't use SC_DLLPUBLIC for class + method<commit_after><|endoftext|>


<commit_before>/******************************************************************************
 *  Copyright (c) 2015 Jamis Hoo
 *  Distributed under the MIT license
 *  (See accompanying file LICENSE or copy at http://opensource.org/licenses/MIT)
 *
 *  Project:
 *  Filename: k_means_iteration.cc
 *  Version: 1.0
 *  Author: Jamis Hoo
 *  E-mail: hjm211324@gmail.com
 *  Date: Jul 19, 2015
 *  Time: 20:55:38
 *  Description:
 *****************************************************************************/
#include <iostream>
#include <fstream>
#include <cassert>
#include <unordered_map>
#include <algorithm>

#include "hadoop/Pipes.hh"
#include "hadoop/TemplateFactory.hh"
#include "hadoop/StringUtils.hh"

#include "netflix_movie.h"

constexpr size_t canopy_threshold = 2;
const std::string canopy_centers_path = "canopy_centers";
const std::string k_means_center_path = "k_means_centers";

inline std::string to_hex_string(const size_t x) {
    char buff[32] = { 0 };
    sprintf(buff, "%zx", x);

    return buff;
}

inline std::vector<uint32_t> string_split(const std::string& str) {
    std::vector<uint32_t> numbers;

    char* offset;

    const char* end = str.data() + str.length();
    const char* tmp = str.data();
    while (tmp < end) {
        uint32_t movie_id = strtoul(tmp, &offset, 16);
        numbers.emplace_back(movie_id);
        tmp = offset + 1;
    }

    return numbers;
}

class kMeansMapper: public HadoopPipes::Mapper {
public:
    kMeansMapper(HadoopPipes::TaskContext& /* context */) {
        load_canopy_centers();
        load_kmeans_centers();

        // no use anymore
        canopy_centers.clear();
    }

    void map(HadoopPipes::MapContext& context) {
        std::string input_value = context.getInputValue();

        std::string movie_string =
            input_value.substr(0, input_value.find_first_of('\t')) + ':' +
            input_value.substr(input_value.find_first_of(';') + 1);

        Movie movie = movie_string;

        // std::cout << "Load input movie: " << movie.to_string() << std::endl;

        size_t start_pos = input_value.find_first_of('\t');
        size_t end_pos = input_value.find_first_of(';', start_pos);
        std::vector<uint32_t> canopy_ids = string_split(input_value.substr(start_pos + 1, end_pos - start_pos - 1));

        // std::cout << "canopy_ids: "; for (const auto i: canopy_ids) std::cout << i << ' '; std::cout << std::endl;

        float max_distance = -1;
        const Movie* max_distance_movie = nullptr;
        for (const auto canopy_id: canopy_ids) {
            if (canopy_id == movie.movie_id()) continue;
            for (const auto k_means_center: centers[canopy_id]) {
                float distance = movie.cos_distance(k_means_centers[k_means_center]);
                // std::cout << "Distance with " << k_means_center.to_string() << " is " << distance;
                if (distance > max_distance) {
                    // std::cout << " is max. ";
                    max_distance = distance;
                    max_distance_movie = &k_means_centers[k_means_center];
                }
                // std::cout << std::endl;
            }
        }

        if (max_distance_movie == nullptr) return;

        std::string emit_key = max_distance_movie->to_string();
        std::string emit_value = movie_string;

        // std::cout << "emit_key = " << emit_key << std::endl;
        // std::cout << "emit_value = " << emit_value << std::endl;

        context.emit(emit_key, emit_value);
    }

private:
    void load_canopy_centers() {
        std::ifstream fin(canopy_centers_path);

        std::string line;
        while (std::getline(fin, line)) {
            canopy_centers.emplace_back(line);
            // std::cout << "Load cannopy center: " << canopy_centers.back().to_string() << std::endl;
        }

    }

    void load_kmeans_centers() {
        std::ifstream fin(k_means_center_path);

        std::string line;
        while (std::getline(fin, line)) {
            k_means_centers.push_back(line);
            Movie& k_means_center = k_means_centers.back();

            // std::cout << "k-means center: " << k_means_center.to_string() << " ";
            for (const auto& canopy_center: canopy_centers)
                if (k_means_center.user_match_count(canopy_center) > canopy_threshold) {
                    // std::cout << "applied to canopy " << canopy_center.to_string() << " ";
                    auto ite = centers.emplace(canopy_center.movie_id(), std::vector<uint32_t>()).first;
                    ite->second.push_back(k_means_centers.size() - 1);
                }
            // std::cout << std::endl;
        }
    }

    std::vector<Movie> k_means_centers;
    std::vector<Movie> canopy_centers;
    // canopy movie_id, vector of k_means_centers index
    std::unordered_map< uint32_t, std::vector<uint32_t> > centers;
};

class kMeansReducer: public HadoopPipes::Reducer {
public:
    kMeansReducer(const HadoopPipes::TaskContext& /* context */) { }

    void reduce(HadoopPipes::ReduceContext& context) {
        Movie k_means_center(context.getInputKey());
#ifndef LAST_ITERATION
        // <user id, < number of users, total user ratings > >
        std::unordered_map<uint32_t, std::pair<uint32_t, uint32_t> > new_features;

        while (context.nextValue()) {
            Movie movie(context.getInputValue());

            for (size_t i = 0; i < movie.num_users(); ++i) {
                auto ite = new_features.emplace(movie.user_id(i), std::make_pair(0, 0)).first;

                ite->second.first += 1;
                ite->second.second += movie.rating(i);
            }
        }

        std::vector< std::pair<uint32_t, uint32_t> > new_features_vec;
        for (const auto i: new_features)
            new_features_vec.push_back({ i.first, i.second.second / i.second.first });

        sort(new_features_vec.begin(), new_features_vec.end());

        std::string emit_key = to_hex_string(k_means_center.movie_id());

        std::string emit_value;
        for (const auto i: new_features_vec)
            emit_value += to_hex_string(i.first) + ',' + to_hex_string(i.second) + ',';

        if (emit_value.size()) {
            emit_value.pop_back();
            context.emit(emit_key, emit_value);
        }
#else
        std::string emit_value;
        while (context.nextValue()) {
            Movie movie(context.getInputValue());
            emit_value += to_hex_string(movie.movie_id());
        }
        context.emit(emit_value, "");
#endif
    }
};

int main(int, char**) {
    return HadoopPipes::runTask(HadoopPipes::TemplateFactory<kMeansMapper, kMeansReducer>());
}

<commit_msg>slight xif<commit_after>/******************************************************************************
 *  Copyright (c) 2015 Jamis Hoo
 *  Distributed under the MIT license
 *  (See accompanying file LICENSE or copy at http://opensource.org/licenses/MIT)
 *
 *  Project:
 *  Filename: k_means_iteration.cc
 *  Version: 1.0
 *  Author: Jamis Hoo
 *  E-mail: hjm211324@gmail.com
 *  Date: Jul 19, 2015
 *  Time: 20:55:38
 *  Description:
 *****************************************************************************/
#include <iostream>
#include <fstream>
#include <cassert>
#include <unordered_map>
#include <algorithm>

#include "hadoop/Pipes.hh"
#include "hadoop/TemplateFactory.hh"
#include "hadoop/StringUtils.hh"

#include "netflix_movie.h"

constexpr size_t canopy_threshold = 2;
const std::string canopy_centers_path = "canopy_centers";
const std::string k_means_center_path = "k_means_centers";

inline std::string to_hex_string(const size_t x) {
    char buff[32] = { 0 };
    sprintf(buff, "%zx", x);

    return buff;
}

inline std::vector<uint32_t> string_split(const std::string& str) {
    std::vector<uint32_t> numbers;

    char* offset;

    const char* end = str.data() + str.length();
    const char* tmp = str.data();
    while (tmp < end) {
        uint32_t movie_id = strtoul(tmp, &offset, 16);
        numbers.emplace_back(movie_id);
        tmp = offset + 1;
    }

    return numbers;
}

class kMeansMapper: public HadoopPipes::Mapper {
public:
    kMeansMapper(HadoopPipes::TaskContext& /* context */) {
        load_canopy_centers();
        load_kmeans_centers();

        // no use anymore
        canopy_centers.clear();
    }

    void map(HadoopPipes::MapContext& context) {
        std::string input_value = context.getInputValue();

        std::string movie_string =
            input_value.substr(0, input_value.find_first_of('\t')) + ':' +
            input_value.substr(input_value.find_first_of(';') + 1);

        Movie movie = movie_string;

        // std::cout << "Load input movie: " << movie.to_string() << std::endl;

        size_t start_pos = input_value.find_first_of('\t');
        size_t end_pos = input_value.find_first_of(';', start_pos);
        std::vector<uint32_t> canopy_ids = string_split(input_value.substr(start_pos + 1, end_pos - start_pos - 1));

        // std::cout << "canopy_ids: "; for (const auto i: canopy_ids) std::cout << i << ' '; std::cout << std::endl;

        float max_distance = -1;
        const Movie* max_distance_movie = nullptr;
        for (const auto canopy_id: canopy_ids) {
            if (canopy_id == movie.movie_id()) continue;
            for (const auto k_means_center: centers[canopy_id]) {
                float distance = movie.cos_distance(k_means_centers[k_means_center]);
                // std::cout << "Distance with " << k_means_center.to_string() << " is " << distance;
                if (distance > max_distance) {
                    // std::cout << " is max. ";
                    max_distance = distance;
                    max_distance_movie = &k_means_centers[k_means_center];
                }
                // std::cout << std::endl;
            }
        }

        if (max_distance_movie == nullptr) return;

        std::string emit_key = max_distance_movie->to_string();
        std::string emit_value = movie_string;

        // std::cout << "emit_key = " << emit_key << std::endl;
        // std::cout << "emit_value = " << emit_value << std::endl;

        context.emit(emit_key, emit_value);
    }

private:
    void load_canopy_centers() {
        std::ifstream fin(canopy_centers_path);

        std::string line;
        while (std::getline(fin, line)) {
            canopy_centers.emplace_back(line);
            // std::cout << "Load cannopy center: " << canopy_centers.back().to_string() << std::endl;
        }

    }

    void load_kmeans_centers() {
        std::ifstream fin(k_means_center_path);

        std::string line;
        while (std::getline(fin, line)) {
            k_means_centers.push_back(line);
            Movie& k_means_center = k_means_centers.back();

            // std::cout << "k-means center: " << k_means_center.to_string() << " ";
            for (const auto& canopy_center: canopy_centers)
                if (k_means_center.user_match_count(canopy_center) > canopy_threshold) {
                    // std::cout << "applied to canopy " << canopy_center.to_string() << " ";
                    auto ite = centers.emplace(canopy_center.movie_id(), std::vector<uint32_t>()).first;
                    ite->second.push_back(k_means_centers.size() - 1);
                }
            // std::cout << std::endl;
        }
    }

    std::vector<Movie> k_means_centers;
    std::vector<Movie> canopy_centers;
    // canopy movie_id, vector of k_means_centers index
    std::unordered_map< uint32_t, std::vector<uint32_t> > centers;
};

class kMeansReducer: public HadoopPipes::Reducer {
public:
    kMeansReducer(const HadoopPipes::TaskContext& /* context */) { }

    void reduce(HadoopPipes::ReduceContext& context) {
        Movie k_means_center(context.getInputKey());
#ifndef LAST_ITERATION
        // <user id, < number of users, total user ratings > >
        std::unordered_map<uint32_t, std::pair<uint32_t, uint32_t> > new_features;

        while (context.nextValue()) {
            Movie movie(context.getInputValue());

            for (size_t i = 0; i < movie.num_users(); ++i) {
                auto ite = new_features.emplace(movie.user_id(i), std::make_pair(0, 0)).first;

                ite->second.first += 1;
                ite->second.second += movie.rating(i);
            }
        }

        std::vector< std::pair<uint32_t, uint32_t> > new_features_vec;
        for (const auto i: new_features)
            new_features_vec.push_back({ i.first, i.second.second / i.second.first });

        sort(new_features_vec.begin(), new_features_vec.end());

        std::string emit_key = to_hex_string(k_means_center.movie_id());

        std::string emit_value;
        for (const auto i: new_features_vec)
            emit_value += to_hex_string(i.first) + ',' + to_hex_string(i.second) + ',';

        if (emit_value.size()) {
            emit_value.pop_back();
            context.emit(emit_key, emit_value);
        }
#else
        std::string emit_value;
        while (context.nextValue()) {
            Movie movie(context.getInputValue());
            emit_value += to_hex_string(movie.movie_id()) + ',';
        }
        if (emit_value.length()) emit_value.pop_back();
        context.emit(emit_value, "");
#endif
    }
};

int main(int, char**) {
    return HadoopPipes::runTask(HadoopPipes::TemplateFactory<kMeansMapper, kMeansReducer>());
}

<|endoftext|>


<commit_before>#include "packettrains_ma.h"
#include <QUdpSocket>
#include <QElapsedTimer>
#include <iostream>
using namespace std;
#include <iomanip>
#include "../../log/logger.h"
#include "../../network/networkmanager.h"

#ifdef Q_OS_WIN
#include <WinSock2.h>
#else
#include <arpa/inet.h>
#endif

#include <chrono>
#include <thread>

LOGGER(PacketTrainsMA);

PacketTrainsMA::PacketTrainsMA()
{
}

bool PacketTrainsMA::start()
{
    QByteArray buffer;
    buffer.resize(definition->packetSize);

    struct msg* message = reinterpret_cast<msg*>(buffer.data());

    // calculate disperson
    quint64* disp = new quint64[definition->iterations];
    quint64 R_MIN = 10485760;
    quint64 R_MAX = 262144000;
    qint64 delay = 200000000;

    for(int i = 0; i < definition->iterations; i++)
    {
        disp[i] = (quint64) (definition->packetSize * 1000000000.0 / ((R_MAX - R_MIN) / definition->iterations * i + R_MIN)); // Linear Rate
    }

    QElapsedTimer timer;
    timer.start();

    // send trains
    for(int i = 0; i < definition->trainLength * definition->iterations; i++)
    {
        message->iter = htons(i / definition->trainLength);
        message->id = i % definition->trainLength;

        message->otime = timer.nsecsElapsed();

        m_udpSocket->writeDatagram(buffer, QHostAddress(definition->host), definition->port);

        std::this_thread::sleep_for(std::chrono::nanoseconds(i / definition->trainLength));
        if(i % definition->trainLength == definition->trainLength -1)
        {
            std::this_thread::sleep_for(std::chrono::nanoseconds(delay));
        }
    }

    delete disp;

    emit finished();
    return true;
}

void PacketTrainsMA::handleError(QAbstractSocket::SocketError socketError)
{
    if(socketError == QAbstractSocket::RemoteHostClosedError)
        return;

    QAbstractSocket* socket = qobject_cast<QAbstractSocket*>(sender());
    cout<<"Socket Error: "<<socket->errorString().toStdString()<<endl;
}

Measurement::Status PacketTrainsMA::status() const
{
    return Unknown;
}

bool PacketTrainsMA::prepare(NetworkManager *networkManager, const MeasurementDefinitionPtr &measurementDefinition)
{
    definition = measurementDefinition.dynamicCast<PacketTrainsDefinition>();
    if ( definition.isNull() ) {
        LOG_WARNING("Definition is empty");
    }

    QString hostname = QString("%1:%2").arg(definition->host).arg(definition->port);

    m_udpSocket = qobject_cast<QUdpSocket*>(networkManager->establishConnection(hostname, "packettrains_mp", definition->toVariant(), NetworkManager::UdpSocket));
    if (!m_udpSocket) {
        LOG_ERROR("Preparation failed");
        return false;
    }

    m_udpSocket->setParent(this);

    // Signal for errors
    connect(m_udpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(handleError(QAbstractSocket::SocketError)));

    return true;
}

bool PacketTrainsMA::stop()
{
    return true;
}

ResultPtr PacketTrainsMA::result() const
{
    return ResultPtr();
}
<commit_msg>Use POSIX nanosleep everywhere except on Windows - there we allow c++11.<commit_after>#include "packettrains_ma.h"
#include <QUdpSocket>
#include <QElapsedTimer>
#include <iostream>
using namespace std;
#include <iomanip>
#include "../../log/logger.h"
#include "../../network/networkmanager.h"

#ifdef Q_OS_WIN
#include <WinSock2.h>
#else
#include <arpa/inet.h>
#endif

#include <chrono>
#include <thread>

LOGGER(PacketTrainsMA);

namespace help
{
#ifdef Q_OS_WIN
inline void nanosleep(qint64 ns)
{
    std::this_thread::sleep_for(std::chrono::nanoseconds(ns));
}
#else
inline void nanosleep(qint64 ns)
{
    struct timespec delay;
    delay.tv_sec = 0;
    delay.tv_nsec = ns;
    ::nanosleep(&delay, NULL);
}
#endif
}

PacketTrainsMA::PacketTrainsMA()
{
}

bool PacketTrainsMA::start()
{
    QByteArray buffer;
    buffer.resize(definition->packetSize);

    struct msg* message = reinterpret_cast<msg*>(buffer.data());

    // calculate disperson
    quint64* disp = new quint64[definition->iterations];
    quint64 R_MIN = 10485760;
    quint64 R_MAX = 262144000;
    qint64 delay = 200000000;

    for(int i = 0; i < definition->iterations; i++)
    {
        disp[i] = (quint64) (definition->packetSize * 1000000000.0 / ((R_MAX - R_MIN) / definition->iterations * i + R_MIN)); // Linear Rate
    }

    QElapsedTimer timer;
    timer.start();

    // send trains
    for(int i = 0; i < definition->trainLength * definition->iterations; i++)
    {
        message->iter = htons(i / definition->trainLength);
        message->id = i % definition->trainLength;

        message->otime = timer.nsecsElapsed();

        m_udpSocket->writeDatagram(buffer, QHostAddress(definition->host), definition->port);

        help::nanosleep(i / definition->trainLength);
        if(i % definition->trainLength == definition->trainLength -1)
        {
            help::nanosleep(delay);
        }
    }

    delete disp;

    emit finished();
    return true;
}

void PacketTrainsMA::handleError(QAbstractSocket::SocketError socketError)
{
    if(socketError == QAbstractSocket::RemoteHostClosedError)
        return;

    QAbstractSocket* socket = qobject_cast<QAbstractSocket*>(sender());
    cout<<"Socket Error: "<<socket->errorString().toStdString()<<endl;
}

Measurement::Status PacketTrainsMA::status() const
{
    return Unknown;
}

bool PacketTrainsMA::prepare(NetworkManager *networkManager, const MeasurementDefinitionPtr &measurementDefinition)
{
    definition = measurementDefinition.dynamicCast<PacketTrainsDefinition>();
    if ( definition.isNull() ) {
        LOG_WARNING("Definition is empty");
    }

    QString hostname = QString("%1:%2").arg(definition->host).arg(definition->port);

    m_udpSocket = qobject_cast<QUdpSocket*>(networkManager->establishConnection(hostname, "packettrains_mp", definition->toVariant(), NetworkManager::UdpSocket));
    if (!m_udpSocket) {
        LOG_ERROR("Preparation failed");
        return false;
    }

    m_udpSocket->setParent(this);

    // Signal for errors
    connect(m_udpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(handleError(QAbstractSocket::SocketError)));

    return true;
}

bool PacketTrainsMA::stop()
{
    return true;
}

ResultPtr PacketTrainsMA::result() const
{
    return ResultPtr();
}
<|endoftext|>


<commit_before>/*
   Copyright (c) 2011, The Mineserver Project
   All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
  * Neither the name of the The Mineserver Project nor the
    names of its contributors may be used to endorse or promote products
    derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <fstream>
#include <iostream>
//#include <iomanip>
#include <limits>
#include <ctime>

#include "random.h"

seed_type prng_seed = 0;

MyRNG prng;

std::tr1::uniform_int<MyRNG::result_type> m_uniformUINT(0, std::numeric_limits<MyRNG::result_type>::max());

void initPRNG()
{
  std::ifstream urandom("/dev/urandom");
  if (urandom)
  {
    urandom.read(reinterpret_cast<char*>(&prng_seed), sizeof(prng_seed));
  }
  else
  {
    prng_seed = std::time(NULL);
  }

  //std::cout << "Seeding the PRNG with: 0x" << std::hex << std::uppercase << std::setfill('0')
  //          << std::setw(2 * sizeof(MyRNG::result_type)) << prng_seed << std::endl;

  prng.seed(prng_seed);
}

double uniform01()
{
  return double(m_uniformUINT(prng)) / double(std::numeric_limits<MyRNG::result_type>::max());
}
<commit_msg>Added good random seeding for Win32 (using CryptGenRandom()).<commit_after>/*
   Copyright (c) 2011, The Mineserver Project
   All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
  * Neither the name of the The Mineserver Project nor the
    names of its contributors may be used to endorse or promote products
    derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//#include <iostream>
//#include <iomanip>
#include <limits>
#include <ctime>

#ifdef WIN32
#include <wincrypt.h>
#else
#include <fstream>
#endif

#include "random.h"

seed_type prng_seed = 0;

MyRNG prng;

std::tr1::uniform_int<MyRNG::result_type> m_uniformUINT(0, std::numeric_limits<MyRNG::result_type>::max());

void initPRNG()
{
  bool seedsuccess = false;

#ifdef WIN32
  HCRYPTPROV hCryptProv;
  BYTE* pbData = reinterpret_cast<BYTE*>(&prng_seed);

  if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0))
  {
    if (CryptGenRandom(hCryptProv, sizeof(prng_seed), pbData))
    {
      seedsuccess = true;
    }
    CryptReleaseContext(hCryptProv, 0);
  }
#else
  std::ifstream urandom("/dev/urandom");
  if (urandom)
  {
    urandom.read(reinterpret_cast<char*>(&prng_seed), sizeof(prng_seed));
    seedsuccess = true;
  }
#endif

  if (!seedsuccess)
  {
    prng_seed = std::time(NULL);
  }

  //std::cout << "Seeding the PRNG with: 0x" << std::hex << std::uppercase << std::setfill('0')
  //          << std::setw(2 * sizeof(MyRNG::result_type)) << prng_seed << std::endl;

  prng.seed(prng_seed);
}

double uniform01()
{
  return double(m_uniformUINT(prng)) / double(std::numeric_limits<MyRNG::result_type>::max());
}
<|endoftext|>


<commit_before>// The MIT License(MIT)
// Copyright(c) <2016> <Juan Gonzalez Burgos>
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "qjsonwebtoken.h"

#include <QDebug>

QJsonWebToken::QJsonWebToken()
{
	// create the header with default algorithm
	setAlgorithmStr("HS256");
	m_jdocPayload = QJsonDocument::fromJson("{}");
    // default for random generation
    m_intRandLength   = 10;
    m_strRandAlphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
}

QJsonWebToken::QJsonWebToken(const QJsonWebToken &other)
{
	this->m_jdocHeader    = other.m_jdocHeader;
	this->m_jdocPayload   = other.m_jdocPayload;
	this->m_byteSignature = other.m_byteSignature;
	this->m_strSecret     = other.m_strSecret;
	this->m_strAlgorithm  = other.m_strAlgorithm;
}

QJsonDocument QJsonWebToken::getHeaderJDoc()
{
	return m_jdocHeader;
}

QString QJsonWebToken::getHeaderQStr(QJsonDocument::JsonFormat format /*= QJsonDocument::JsonFormat::Indented*/)
{
	return m_jdocHeader.toJson(format);
}

bool QJsonWebToken::setHeaderJDoc(QJsonDocument jdocHeader)
{
	if (jdocHeader.isEmpty() || jdocHeader.isNull() || !jdocHeader.isObject())
	{
		return false;
	}

	// check if supported algorithm
	QString strAlgorithm = jdocHeader.object().value("alg").toString("");
	if (!isAlgorithmSupported(strAlgorithm))
	{
		return false;
	}

	m_jdocHeader = jdocHeader;

	// set also new algorithm
	m_strAlgorithm = strAlgorithm;

	return true;
}

bool QJsonWebToken::setHeaderQStr(QString strHeader)
{
	QJsonParseError error;
	QJsonDocument tmpHeader = QJsonDocument::fromJson(strHeader.toUtf8(), &error);

	// validate and set header
	if (error.error != QJsonParseError::NoError || !setHeaderJDoc(tmpHeader))
	{
		return false;
	}

	return true;
}

QJsonDocument QJsonWebToken::getPayloadJDoc()
{
	return m_jdocPayload;
}

QString QJsonWebToken::getPayloadQStr(QJsonDocument::JsonFormat format /*= QJsonDocument::JsonFormat::Indented*/)
{
	return m_jdocPayload.toJson(format);
}

bool QJsonWebToken::setPayloadJDoc(QJsonDocument jdocPayload)
{
	if (jdocPayload.isEmpty() || jdocPayload.isNull() || !jdocPayload.isObject())
	{
		return false;
	}

	m_jdocPayload = jdocPayload;

	return true;
}

bool QJsonWebToken::setPayloadQStr(QString strPayload)
{
	QJsonParseError error;
	QJsonDocument tmpPayload = QJsonDocument::fromJson(strPayload.toUtf8(), &error);

	// validate and set payload
	if (error.error != QJsonParseError::NoError || !setPayloadJDoc(tmpPayload))
	{
		return false;
	}

	return true;
}

QByteArray QJsonWebToken::getSignature()
{
	// recalculate
	// get header in compact mode and base64 encoded
	QByteArray byteHeaderBase64  = getHeaderQStr(QJsonDocument::JsonFormat::Compact).toUtf8().toBase64();
	// get payload in compact mode and base64 encoded
	QByteArray bytePayloadBase64 = getPayloadQStr(QJsonDocument::JsonFormat::Compact).toUtf8().toBase64();
	// calculate signature based on chosen algorithm and secret
	m_byteAllData = byteHeaderBase64 + "." + bytePayloadBase64;
	if (m_strAlgorithm.compare("HS256", Qt::CaseInsensitive) == 0)      // HMAC using SHA-256 hash algorithm
	{
		m_byteSignature = QMessageAuthenticationCode::hash(m_byteAllData, m_strSecret.toUtf8(), QCryptographicHash::Sha256);
	}
	else if (m_strAlgorithm.compare("HS384", Qt::CaseInsensitive) == 0) // HMAC using SHA-384 hash algorithm
	{
		m_byteSignature = QMessageAuthenticationCode::hash(m_byteAllData, m_strSecret.toUtf8(), QCryptographicHash::Sha384);
	}
	else if (m_strAlgorithm.compare("HS512", Qt::CaseInsensitive) == 0) // HMAC using SHA-512 hash algorithm
	{
		m_byteSignature = QMessageAuthenticationCode::hash(m_byteAllData, m_strSecret.toUtf8(), QCryptographicHash::Sha512);
	}
	// TODO : support other algorithms
	else
	{
		m_byteSignature = QByteArray();
	}
	// return recalculated
	return m_byteSignature;
}

QByteArray QJsonWebToken::getSignatureBase64()
{
	// note we return through getSignature() to force recalculation
	return getSignature().toBase64();
}

QString QJsonWebToken::getSecret()
{
	return m_strSecret;
}

bool QJsonWebToken::setSecret(QString strSecret)
{
	if (strSecret.isEmpty() || strSecret.isNull())
	{
		return false;
	}

	m_strSecret = strSecret;

    return true;
}

void QJsonWebToken::setRandomSecret()
{
    m_strSecret.resize(m_intRandLength);
    for (int i = 0; i < m_intRandLength; ++i)
    {
        m_strSecret[i] = m_strRandAlphanum.at(rand() % (m_strRandAlphanum.length() - 1));
    }
}

QString QJsonWebToken::getAlgorithmStr()
{
	return m_strAlgorithm;
}

bool QJsonWebToken::setAlgorithmStr(QString strAlgorithm)
{
	// check if supported algorithm
	if (!isAlgorithmSupported(strAlgorithm))
	{
		return false;
	}
	// set algorithm
	m_strAlgorithm = strAlgorithm;
	// modify header
	m_jdocHeader = QJsonDocument::fromJson(QObject::trUtf8("{\"typ\": \"JWT\", \"alg\" : \"").toUtf8()
		                                 + m_strAlgorithm.toUtf8()
		                                 + QObject::trUtf8("\"}").toUtf8());

	return true;
}

QString QJsonWebToken::getToken()
{
	// important to execute first to update m_byteAllData which contains header + "." + payload in base64
	QByteArray byteSignatureBase64 = getSignatureBase64();
	// compose token and return it
	return m_byteAllData + "." + byteSignatureBase64;
}

bool QJsonWebToken::setToken(QString strToken)
{
	// assume base64 encoded at first, if not try decoding
	bool isBase64Encoded = true;
	QStringList listJwtParts = strToken.split(".");
	// check correct size
	if (listJwtParts.count() != 3)
	{
		return false;
	}
	// check all parts are valid using another instance,
	// so we dont overwrite this instance in case of error
	QJsonWebToken tempTokenObj;
	if ( !tempTokenObj.setHeaderQStr(QByteArray::fromBase64(listJwtParts.at(0).toUtf8())) ||
		 !tempTokenObj.setPayloadQStr(QByteArray::fromBase64(listJwtParts.at(1).toUtf8())) )
	{
		// try unencoded
		if (!tempTokenObj.setHeaderQStr(listJwtParts.at(0)) ||
			!tempTokenObj.setPayloadQStr(listJwtParts.at(1)))
		{
			return false;
		}
		else
		{
			isBase64Encoded = false;
		}
	}
	// set parts on this instance
	setHeaderQStr(tempTokenObj.getHeaderQStr());
	setPayloadQStr(tempTokenObj.getPayloadQStr());
	if (isBase64Encoded)
	{ // unencode
		m_byteSignature = QByteArray::fromBase64(listJwtParts.at(2).toUtf8());
	}
	else
	{
		m_byteSignature = listJwtParts.at(2).toUtf8();
	}
	// allData not valid anymore
	m_byteAllData.clear();
	// success
    return true;
}

QString QJsonWebToken::getRandAlphanum()
{
    return m_strRandAlphanum;
}

void QJsonWebToken::setRandAlphanum(QString strRandAlphanum)
{
    if(strRandAlphanum.isNull())
    {
        return;
    }
    m_strRandAlphanum = strRandAlphanum;
}

int QJsonWebToken::getRandLength()
{
    return m_intRandLength;
}

void QJsonWebToken::setRandLength(int intRandLength)
{
    if(intRandLength < 0 || intRandLength > 1e6)
    {
        return;
    }
    m_intRandLength = intRandLength;
}

bool QJsonWebToken::isValid()
{
	// calculate token on other instance,
	// so we dont overwrite this instance's signature
	QJsonWebToken tempTokenObj = *this;
	if (m_byteSignature != tempTokenObj.getSignature())
	{
		return false;
	}
	return true;
}

QJsonWebToken QJsonWebToken::fromTokenAndSecret(QString strToken, QString srtSecret)
{
	QJsonWebToken tempTokenObj;
	// set Token
	tempTokenObj.setToken(strToken);
	// set Secret
	tempTokenObj.setSecret(srtSecret);
	// return
	return tempTokenObj;
}

void QJsonWebToken::appendClaim(QString strClaimType, QString strValue)
{
	// have to make a copy of the json object, modify the copy and then put it back, sigh
	QJsonObject jObj = m_jdocPayload.object();
	jObj.insert(strClaimType, strValue);
	m_jdocPayload = QJsonDocument(jObj);
}

void QJsonWebToken::removeClaim(QString strClaimType)
{
	// have to make a copy of the json object, modify the copy and then put it back, sigh
	QJsonObject jObj = m_jdocPayload.object();
	jObj.remove(strClaimType);
	m_jdocPayload = QJsonDocument(jObj);
}

bool QJsonWebToken::isAlgorithmSupported(QString strAlgorithm)
{
	// TODO : support other algorithms
	if (strAlgorithm.compare("HS256", Qt::CaseInsensitive) != 0 && // HMAC using SHA-256 hash algorithm
		strAlgorithm.compare("HS384", Qt::CaseInsensitive) != 0 && // HMAC using SHA-384 hash algorithm
		strAlgorithm.compare("HS512", Qt::CaseInsensitive) != 0 /*&& // HMAC using SHA-512 hash algorithm
		strAlgorithm.compare("RS256", Qt::CaseInsensitive) != 0 && // RSA using SHA-256 hash algorithm
		strAlgorithm.compare("RS384", Qt::CaseInsensitive) != 0 && // RSA using SHA-384 hash algorithm
		strAlgorithm.compare("RS512", Qt::CaseInsensitive) != 0 && // RSA using SHA-512 hash algorithm
		strAlgorithm.compare("ES256", Qt::CaseInsensitive) != 0 && // ECDSA using P-256 curve and SHA-256 hash algorithm
		strAlgorithm.compare("ES384", Qt::CaseInsensitive) != 0 && // ECDSA using P-384 curve and SHA-384 hash algorithm
		strAlgorithm.compare("ES512", Qt::CaseInsensitive) != 0*/)  // ECDSA using P-521 curve and SHA-512 hash algorithm
	{
		return false;
	}
	return true;
}

QStringList QJsonWebToken::supportedAlgorithms()
{
	// TODO : support other algorithms
	return QStringList() << "HS256" << "HS384" << "HS512";
}
<commit_msg>qjsonwebtoken: Remove unused empty string<commit_after>// The MIT License(MIT)
// Copyright(c) <2016> <Juan Gonzalez Burgos>
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "qjsonwebtoken.h"

#include <QDebug>

QJsonWebToken::QJsonWebToken()
{
	// create the header with default algorithm
	setAlgorithmStr("HS256");
	m_jdocPayload = QJsonDocument::fromJson("{}");
    // default for random generation
    m_intRandLength   = 10;
    m_strRandAlphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
}

QJsonWebToken::QJsonWebToken(const QJsonWebToken &other)
{
	this->m_jdocHeader    = other.m_jdocHeader;
	this->m_jdocPayload   = other.m_jdocPayload;
	this->m_byteSignature = other.m_byteSignature;
	this->m_strSecret     = other.m_strSecret;
	this->m_strAlgorithm  = other.m_strAlgorithm;
}

QJsonDocument QJsonWebToken::getHeaderJDoc()
{
	return m_jdocHeader;
}

QString QJsonWebToken::getHeaderQStr(QJsonDocument::JsonFormat format /*= QJsonDocument::JsonFormat::Indented*/)
{
	return m_jdocHeader.toJson(format);
}

bool QJsonWebToken::setHeaderJDoc(QJsonDocument jdocHeader)
{
	if (jdocHeader.isEmpty() || jdocHeader.isNull() || !jdocHeader.isObject())
	{
		return false;
	}

	// check if supported algorithm
	QString strAlgorithm = jdocHeader.object().value("alg").toString();
	if (!isAlgorithmSupported(strAlgorithm))
	{
		return false;
	}

	m_jdocHeader = jdocHeader;

	// set also new algorithm
	m_strAlgorithm = strAlgorithm;

	return true;
}

bool QJsonWebToken::setHeaderQStr(QString strHeader)
{
	QJsonParseError error;
	QJsonDocument tmpHeader = QJsonDocument::fromJson(strHeader.toUtf8(), &error);

	// validate and set header
	if (error.error != QJsonParseError::NoError || !setHeaderJDoc(tmpHeader))
	{
		return false;
	}

	return true;
}

QJsonDocument QJsonWebToken::getPayloadJDoc()
{
	return m_jdocPayload;
}

QString QJsonWebToken::getPayloadQStr(QJsonDocument::JsonFormat format /*= QJsonDocument::JsonFormat::Indented*/)
{
	return m_jdocPayload.toJson(format);
}

bool QJsonWebToken::setPayloadJDoc(QJsonDocument jdocPayload)
{
	if (jdocPayload.isEmpty() || jdocPayload.isNull() || !jdocPayload.isObject())
	{
		return false;
	}

	m_jdocPayload = jdocPayload;

	return true;
}

bool QJsonWebToken::setPayloadQStr(QString strPayload)
{
	QJsonParseError error;
	QJsonDocument tmpPayload = QJsonDocument::fromJson(strPayload.toUtf8(), &error);

	// validate and set payload
	if (error.error != QJsonParseError::NoError || !setPayloadJDoc(tmpPayload))
	{
		return false;
	}

	return true;
}

QByteArray QJsonWebToken::getSignature()
{
	// recalculate
	// get header in compact mode and base64 encoded
	QByteArray byteHeaderBase64  = getHeaderQStr(QJsonDocument::JsonFormat::Compact).toUtf8().toBase64();
	// get payload in compact mode and base64 encoded
	QByteArray bytePayloadBase64 = getPayloadQStr(QJsonDocument::JsonFormat::Compact).toUtf8().toBase64();
	// calculate signature based on chosen algorithm and secret
	m_byteAllData = byteHeaderBase64 + "." + bytePayloadBase64;
	if (m_strAlgorithm.compare("HS256", Qt::CaseInsensitive) == 0)      // HMAC using SHA-256 hash algorithm
	{
		m_byteSignature = QMessageAuthenticationCode::hash(m_byteAllData, m_strSecret.toUtf8(), QCryptographicHash::Sha256);
	}
	else if (m_strAlgorithm.compare("HS384", Qt::CaseInsensitive) == 0) // HMAC using SHA-384 hash algorithm
	{
		m_byteSignature = QMessageAuthenticationCode::hash(m_byteAllData, m_strSecret.toUtf8(), QCryptographicHash::Sha384);
	}
	else if (m_strAlgorithm.compare("HS512", Qt::CaseInsensitive) == 0) // HMAC using SHA-512 hash algorithm
	{
		m_byteSignature = QMessageAuthenticationCode::hash(m_byteAllData, m_strSecret.toUtf8(), QCryptographicHash::Sha512);
	}
	// TODO : support other algorithms
	else
	{
		m_byteSignature = QByteArray();
	}
	// return recalculated
	return m_byteSignature;
}

QByteArray QJsonWebToken::getSignatureBase64()
{
	// note we return through getSignature() to force recalculation
	return getSignature().toBase64();
}

QString QJsonWebToken::getSecret()
{
	return m_strSecret;
}

bool QJsonWebToken::setSecret(QString strSecret)
{
	if (strSecret.isEmpty() || strSecret.isNull())
	{
		return false;
	}

	m_strSecret = strSecret;

    return true;
}

void QJsonWebToken::setRandomSecret()
{
    m_strSecret.resize(m_intRandLength);
    for (int i = 0; i < m_intRandLength; ++i)
    {
        m_strSecret[i] = m_strRandAlphanum.at(rand() % (m_strRandAlphanum.length() - 1));
    }
}

QString QJsonWebToken::getAlgorithmStr()
{
	return m_strAlgorithm;
}

bool QJsonWebToken::setAlgorithmStr(QString strAlgorithm)
{
	// check if supported algorithm
	if (!isAlgorithmSupported(strAlgorithm))
	{
		return false;
	}
	// set algorithm
	m_strAlgorithm = strAlgorithm;
	// modify header
	m_jdocHeader = QJsonDocument::fromJson(QObject::trUtf8("{\"typ\": \"JWT\", \"alg\" : \"").toUtf8()
		                                 + m_strAlgorithm.toUtf8()
		                                 + QObject::trUtf8("\"}").toUtf8());

	return true;
}

QString QJsonWebToken::getToken()
{
	// important to execute first to update m_byteAllData which contains header + "." + payload in base64
	QByteArray byteSignatureBase64 = getSignatureBase64();
	// compose token and return it
	return m_byteAllData + "." + byteSignatureBase64;
}

bool QJsonWebToken::setToken(QString strToken)
{
	// assume base64 encoded at first, if not try decoding
	bool isBase64Encoded = true;
	QStringList listJwtParts = strToken.split(".");
	// check correct size
	if (listJwtParts.count() != 3)
	{
		return false;
	}
	// check all parts are valid using another instance,
	// so we dont overwrite this instance in case of error
	QJsonWebToken tempTokenObj;
	if ( !tempTokenObj.setHeaderQStr(QByteArray::fromBase64(listJwtParts.at(0).toUtf8())) ||
		 !tempTokenObj.setPayloadQStr(QByteArray::fromBase64(listJwtParts.at(1).toUtf8())) )
	{
		// try unencoded
		if (!tempTokenObj.setHeaderQStr(listJwtParts.at(0)) ||
			!tempTokenObj.setPayloadQStr(listJwtParts.at(1)))
		{
			return false;
		}
		else
		{
			isBase64Encoded = false;
		}
	}
	// set parts on this instance
	setHeaderQStr(tempTokenObj.getHeaderQStr());
	setPayloadQStr(tempTokenObj.getPayloadQStr());
	if (isBase64Encoded)
	{ // unencode
		m_byteSignature = QByteArray::fromBase64(listJwtParts.at(2).toUtf8());
	}
	else
	{
		m_byteSignature = listJwtParts.at(2).toUtf8();
	}
	// allData not valid anymore
	m_byteAllData.clear();
	// success
    return true;
}

QString QJsonWebToken::getRandAlphanum()
{
    return m_strRandAlphanum;
}

void QJsonWebToken::setRandAlphanum(QString strRandAlphanum)
{
    if(strRandAlphanum.isNull())
    {
        return;
    }
    m_strRandAlphanum = strRandAlphanum;
}

int QJsonWebToken::getRandLength()
{
    return m_intRandLength;
}

void QJsonWebToken::setRandLength(int intRandLength)
{
    if(intRandLength < 0 || intRandLength > 1e6)
    {
        return;
    }
    m_intRandLength = intRandLength;
}

bool QJsonWebToken::isValid()
{
	// calculate token on other instance,
	// so we dont overwrite this instance's signature
	QJsonWebToken tempTokenObj = *this;
	if (m_byteSignature != tempTokenObj.getSignature())
	{
		return false;
	}
	return true;
}

QJsonWebToken QJsonWebToken::fromTokenAndSecret(QString strToken, QString srtSecret)
{
	QJsonWebToken tempTokenObj;
	// set Token
	tempTokenObj.setToken(strToken);
	// set Secret
	tempTokenObj.setSecret(srtSecret);
	// return
	return tempTokenObj;
}

void QJsonWebToken::appendClaim(QString strClaimType, QString strValue)
{
	// have to make a copy of the json object, modify the copy and then put it back, sigh
	QJsonObject jObj = m_jdocPayload.object();
	jObj.insert(strClaimType, strValue);
	m_jdocPayload = QJsonDocument(jObj);
}

void QJsonWebToken::removeClaim(QString strClaimType)
{
	// have to make a copy of the json object, modify the copy and then put it back, sigh
	QJsonObject jObj = m_jdocPayload.object();
	jObj.remove(strClaimType);
	m_jdocPayload = QJsonDocument(jObj);
}

bool QJsonWebToken::isAlgorithmSupported(QString strAlgorithm)
{
	// TODO : support other algorithms
	if (strAlgorithm.compare("HS256", Qt::CaseInsensitive) != 0 && // HMAC using SHA-256 hash algorithm
		strAlgorithm.compare("HS384", Qt::CaseInsensitive) != 0 && // HMAC using SHA-384 hash algorithm
		strAlgorithm.compare("HS512", Qt::CaseInsensitive) != 0 /*&& // HMAC using SHA-512 hash algorithm
		strAlgorithm.compare("RS256", Qt::CaseInsensitive) != 0 && // RSA using SHA-256 hash algorithm
		strAlgorithm.compare("RS384", Qt::CaseInsensitive) != 0 && // RSA using SHA-384 hash algorithm
		strAlgorithm.compare("RS512", Qt::CaseInsensitive) != 0 && // RSA using SHA-512 hash algorithm
		strAlgorithm.compare("ES256", Qt::CaseInsensitive) != 0 && // ECDSA using P-256 curve and SHA-256 hash algorithm
		strAlgorithm.compare("ES384", Qt::CaseInsensitive) != 0 && // ECDSA using P-384 curve and SHA-384 hash algorithm
		strAlgorithm.compare("ES512", Qt::CaseInsensitive) != 0*/)  // ECDSA using P-521 curve and SHA-512 hash algorithm
	{
		return false;
	}
	return true;
}

QStringList QJsonWebToken::supportedAlgorithms()
{
	// TODO : support other algorithms
	return QStringList() << "HS256" << "HS384" << "HS512";
}
<|endoftext|>