File indexing completed on 2024-04-28 03:50:34

0001 /*
0002     SPDX-FileCopyrightText: 2010 Christian Vetter veaac.fdirct @gmail.com
0003     SPDX-License-Identifier: GPL-3.0-or-later OR LGPL-2.0-or-later
0004 
0005     This file is part of MoNav.
0006 */
0007 
0008 #ifndef SIGNALS_H
0009 #define SIGNALS_H
0010 
0011 #include <QString>
0012 #include <QVector>
0013 #include <QDataStream>
0014 #include <QStringList>
0015 #include <QLocalSocket>
0016 
0017 namespace MoNav {
0018 
0019     // has to be send before each command to identify the following command type
0020     struct CommandType {
0021         enum Type{
0022             RoutingCommand = 0,
0023             UnpackCommand = 1
0024         } value;
0025 
0026         void post( QIODevice* out )
0027         {
0028             qint32 temp = value;
0029             out->write( ( const char* ) &temp, sizeof( qint32 ) );
0030         }
0031 
0032         bool read( QLocalSocket* in )
0033         {
0034             while ( in->bytesAvailable() < ( int ) sizeof( qint32 ) ) {
0035                 if ( in->state() != QLocalSocket::ConnectedState )
0036                     return false;
0037                 in->waitForReadyRead( 100 );
0038             }
0039 
0040             qint32 temp;
0041             in->read( ( char* ) &temp, sizeof( qint32 ) );
0042             value = ( Type ) temp;
0043 
0044             return true;
0045         }
0046     };
0047 
0048     struct Node {
0049         double latitude;
0050         double longitude;
0051 
0052         friend QDataStream& operator<< ( QDataStream& out, const Node& node )
0053         {
0054             out << node.latitude;
0055             out << node.longitude;
0056             return out;
0057         }
0058 
0059         friend QDataStream& operator>> ( QDataStream& in, Node& node )
0060         {
0061             in >> node.latitude;
0062             in >> node.longitude;
0063             return in;
0064         }
0065     };
0066 
0067     struct Edge {
0068         unsigned length; // length of the edge == number of edges it represents == number of nodes - 1
0069         unsigned name; // name ID of the edge
0070         unsigned type; // type ID of the edge
0071         unsigned seconds; // travel time metric for the edge
0072         bool branchingPossible; // is it possible to choose between more than one subsequent edge ( turning around on bidirectional edges does not count )
0073 
0074         friend QDataStream& operator<< ( QDataStream& out, const Edge& edge )
0075         {
0076             out << edge.length;
0077             out << edge.name;
0078             out << edge.type;
0079             out << edge.seconds;
0080             out << edge.branchingPossible;
0081             return out;
0082         }
0083 
0084         friend QDataStream& operator>> ( QDataStream& in, Edge& edge )
0085         {
0086             in >> edge.length;
0087             in >> edge.name;
0088             in >> edge.type;
0089             in >> edge.seconds;
0090             in >> edge.branchingPossible;
0091             return in;
0092         }
0093     };
0094 
0095     class RoutingCommand {
0096 
0097     public:
0098 
0099         RoutingCommand()
0100         {
0101             lookupRadius = 10000; // 10km should suffice for most applications
0102             lookupStrings = false;
0103         }
0104 
0105         // waypoint edge lookup radius in meters
0106         double lookupRadius;
0107         // lookup street name / type strings?
0108         bool lookupStrings;
0109         // a valid  routing module directory
0110         QString dataDirectory;
0111         // waypoints of the route
0112         QVector< Node > waypoints;
0113 
0114         void post( QIODevice* out )
0115         {
0116             QByteArray buffer;
0117             QDataStream stream( &buffer, QIODevice::WriteOnly );
0118             stream << lookupRadius;
0119             stream << lookupStrings;
0120             stream << dataDirectory;
0121             stream << waypoints;
0122             qint32 size = buffer.size();
0123             out->write( ( const char* ) &size, sizeof( qint32 ) );
0124             out->write( buffer.data(), size );
0125         }
0126 
0127         bool read( QLocalSocket* in )
0128         {
0129             qint32 size;
0130             while ( in->bytesAvailable() < ( int ) sizeof( qint32 ) ) {
0131                 if ( in->state() != QLocalSocket::ConnectedState )
0132                     return false;
0133                 in->waitForReadyRead( 100 );
0134             }
0135 
0136             in->read( ( char* ) &size, sizeof( quint32 ) );
0137 
0138             while ( in->bytesAvailable() < size ) {
0139                 if ( in->state() != QLocalSocket::ConnectedState )
0140                     return false;
0141                 in->waitForReadyRead( 100 );
0142             }
0143 
0144             QByteArray buffer= in->read( size );
0145             QDataStream stream( buffer );
0146             stream >> lookupRadius;
0147             stream >> lookupStrings;
0148             stream >> dataDirectory;
0149             stream >> waypoints;
0150 
0151             return true;
0152         }
0153 
0154     };
0155 
0156     class RoutingResult {
0157 
0158     public:
0159 
0160         enum ResultType {
0161             LoadFailed = 1, RouteFailed = 2, NameLookupFailed = 3, TypeLookupFailed = 4, Success = 5
0162         } type;
0163 
0164         double seconds;
0165         QVector< Node > pathNodes;
0166         QVector< Edge > pathEdges;
0167         QStringList nameStrings;
0168         QStringList typeStrings;
0169 
0170         void post( QIODevice* out )
0171         {
0172             QByteArray buffer;
0173             QDataStream stream( &buffer, QIODevice::WriteOnly );
0174             stream << qint32( type );
0175             stream << seconds;
0176             stream << pathNodes;
0177             stream << pathEdges;
0178             stream << nameStrings;
0179             stream << typeStrings;
0180             qint32 size = buffer.size();
0181             out->write( ( const char* ) &size, sizeof( qint32 ) );
0182             out->write( buffer.data(), size );
0183         }
0184 
0185         bool read( QLocalSocket* in )
0186         {
0187             qint32 size;
0188             while ( in->bytesAvailable() < ( int ) sizeof( qint32 ) ) {
0189                 if ( in->state() != QLocalSocket::ConnectedState )
0190                     return false;
0191                 in->waitForReadyRead( 100 );
0192             }
0193 
0194             in->read( ( char* ) &size, sizeof( quint32 ) );
0195 
0196             while ( in->bytesAvailable() < size ) {
0197                 if ( in->state() != QLocalSocket::ConnectedState )
0198                     return false;
0199                 in->waitForReadyRead( 100 );
0200             }
0201 
0202             QByteArray buffer= in->read( size );
0203             QDataStream stream( buffer );
0204             qint32 temp;
0205             stream >> temp;
0206             type = ( ResultType ) temp;
0207             stream >> seconds;
0208             stream >> pathNodes;
0209             stream >> pathEdges;
0210             stream >> nameStrings;
0211             stream >> typeStrings;
0212 
0213             return true;
0214         }
0215 
0216     };
0217 
0218     class UnpackCommand
0219     {
0220     public:
0221 
0222         UnpackCommand()
0223         {
0224             deleteFile = false;
0225         }
0226 
0227         // MoNav Map Module file to be unpacked
0228         // it will be unpacked in the directory of the same name
0229         // e.g. test.mmm -> test/
0230         QString mapModuleFile;
0231         // delete file after unpacking?
0232         bool deleteFile;
0233 
0234         void post( QIODevice* out )
0235         {
0236             QByteArray buffer;
0237             QDataStream stream( &buffer, QIODevice::WriteOnly );
0238             stream << mapModuleFile;
0239             stream << deleteFile;
0240             qint32 size = buffer.size();
0241             out->write( ( const char* ) &size, sizeof( qint32 ) );
0242             out->write( buffer.data(), size );
0243         }
0244 
0245         bool read( QLocalSocket* in )
0246         {
0247             qint32 size;
0248             while ( in->bytesAvailable() < ( int ) sizeof( qint32 ) ) {
0249                 if ( in->state() != QLocalSocket::ConnectedState )
0250                     return false;
0251                 in->waitForReadyRead( 100 );
0252             }
0253 
0254             in->read( ( char* ) &size, sizeof( quint32 ) );
0255 
0256             while ( in->bytesAvailable() < size ) {
0257                 if ( in->state() != QLocalSocket::ConnectedState )
0258                     return false;
0259                 in->waitForReadyRead( 100 );
0260             }
0261 
0262             QByteArray buffer= in->read( size );
0263             QDataStream stream( buffer );
0264             stream >> mapModuleFile;
0265             stream >> deleteFile;
0266 
0267             return true;
0268         }
0269     };
0270 
0271     class UnpackResult
0272     {
0273     public:
0274 
0275         enum ResultType {
0276             FailUnpacking = 1, Success = 2
0277         } type;
0278 
0279         void post( QIODevice* out )
0280         {
0281             qint32 temp = type;
0282             out->write( ( const char* ) &temp, sizeof( qint32 ) );
0283         }
0284 
0285         bool read( QLocalSocket* in )
0286         {
0287             while ( in->bytesAvailable() < ( int ) sizeof( qint32 ) ) {
0288                 if ( in->state() != QLocalSocket::ConnectedState )
0289                     return false;
0290                 in->waitForReadyRead( 100 );
0291             }
0292 
0293             qint32 temp;
0294             in->read( ( char* ) &temp, sizeof( qint32 ) );
0295             type = ResultType( temp );
0296 
0297             return true;
0298         }
0299     };
0300 
0301 }
0302 
0303 #endif // SIGNALS_H