SVN checkout 11/12/2010
[monav:monav.git] / routingdaemon / signals.h
1 /*
2 Copyright 2010  Christian Vetter veaac.fdirct@gmail.com
3
4 This file is part of MoNav.
5
6 MoNav is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 MoNav is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with MoNav.  If not, see <http://www.gnu.org/licenses/>.
18
19 Alternatively, this file may be used under the terms of the GNU
20 Library General Public License as published by the Free Software
21 Foundation; either version 2 of the License, or (at your option)
22 any later version.
23 */
24
25 #ifndef SIGNALS_H
26 #define SIGNALS_H
27
28 #include <QString>
29 #include <QVector>
30 #include <QDataStream>
31 #include <QStringList>
32 #include <QLocalSocket>
33
34 struct RoutingDaemonNode {
35         double latitude;
36         double longitude;
37
38         friend QDataStream& operator<< ( QDataStream& out, const RoutingDaemonNode& node )
39         {
40                 out << node.latitude;
41                 out << node.longitude;
42                 return out;
43         }
44
45         friend QDataStream& operator>> ( QDataStream& in, RoutingDaemonNode& node )
46         {
47                 in >> node.latitude;
48                 in >> node.longitude;
49                 return in;
50         }
51 };
52
53 struct RoutingDaemonEdge {
54         unsigned length; // length of the edge == number of edges it represents == number of nodes - 1
55         unsigned name; // name ID of the edge
56         unsigned type; // type ID of the edge
57         unsigned seconds; // travel time metric for the edge
58         bool branchingPossible; // is it possible to choose between more than one subsequent edge ( turning around on bidirectional edges does not count )
59
60         friend QDataStream& operator<< ( QDataStream& out, const RoutingDaemonEdge& edge )
61         {
62                 out << edge.length;
63                 out << edge.name;
64                 out << edge.type;
65                 out << edge.seconds;
66                 out << edge.branchingPossible;
67                 return out;
68         }
69
70         friend QDataStream& operator>> ( QDataStream& in, RoutingDaemonEdge& edge )
71         {
72                 in >> edge.length;
73                 in >> edge.name;
74                 in >> edge.type;
75                 in >> edge.seconds;
76                 in >> edge.branchingPossible;
77                 return in;
78         }
79 };
80
81 class RoutingDaemonCommand {
82
83 public:
84
85         RoutingDaemonCommand()
86         {
87                 lookupRadius = 10000; // 10km should suffice for most applications
88                 lookupStrings = false;
89         }
90
91         double lookupRadius;
92         bool lookupStrings;
93         QString dataDirectory;
94         QVector< RoutingDaemonNode > waypoints;
95
96         void post( QIODevice* out )
97         {
98                 QByteArray buffer;
99                 QDataStream stream( &buffer, QIODevice::WriteOnly );
100                 stream << lookupRadius;
101                 stream << lookupStrings;
102                 stream << dataDirectory;
103                 stream << waypoints;
104                 qint32 size = buffer.size();
105                 out->write( ( const char* ) &size, sizeof( qint32 ) );
106                 out->write( buffer.data(), size );
107         }
108
109         bool read( QLocalSocket* in )
110         {
111                 qint32 size;
112                 while ( in->bytesAvailable() < ( int ) sizeof( qint32 ) ) {
113                         if ( in->state() != QLocalSocket::ConnectedState )
114                                 return false;
115                         in->waitForReadyRead( 100 );
116                 }
117
118                 in->read( ( char* ) &size, sizeof( quint32 ) );
119
120                 while ( in->bytesAvailable() < size ) {
121                         if ( in->state() != QLocalSocket::ConnectedState )
122                                 return false;
123                         in->waitForReadyRead( 100 );
124                 }
125
126                 QByteArray buffer= in->read( size );
127                 QDataStream stream( buffer );
128                 stream >> lookupRadius;
129                 stream >> lookupStrings;
130                 stream >> dataDirectory;
131                 stream >> waypoints;
132
133                 return true;
134         }
135
136 };
137
138 class RoutingDaemonResult {
139
140 public:
141
142         enum ResultType {
143                 LoadFailed = 1, RouteFailed = 2, NameLookupFailed = 3, TypeLookupFailed = 4, Success = 5
144         } type;
145
146         double seconds;
147         QVector< RoutingDaemonNode > pathNodes;
148         QVector< RoutingDaemonEdge > pathEdges;
149         QStringList nameStrings;
150         QStringList typeStrings;
151
152         void post( QIODevice* out )
153         {
154                 QByteArray buffer;
155                 QDataStream stream( &buffer, QIODevice::WriteOnly );
156                 stream << qint32( type );
157                 stream << seconds;
158                 stream << pathNodes;
159                 stream << pathEdges;
160                 stream << nameStrings;
161                 stream << typeStrings;
162                 qint32 size = buffer.size();
163                 out->write( ( const char* ) &size, sizeof( qint32 ) );
164                 out->write( buffer.data(), size );
165         }
166
167         bool read( QLocalSocket* in )
168         {
169                 qint32 size;
170                 while ( in->bytesAvailable() < ( int ) sizeof( qint32 ) ) {
171                         if ( in->state() != QLocalSocket::ConnectedState )
172                                 return false;
173                         in->waitForReadyRead( 100 );
174                 }
175
176                 in->read( ( char* ) &size, sizeof( quint32 ) );
177
178                 while ( in->bytesAvailable() < size ) {
179                         if ( in->state() != QLocalSocket::ConnectedState )
180                                 return false;
181                         in->waitForReadyRead( 100 );
182                 }
183
184                 QByteArray buffer= in->read( size );
185                 QDataStream stream( buffer );
186                 qint32 temp;
187                 stream >> temp;
188                 type = ( ResultType ) temp;
189                 stream >> seconds;
190                 stream >> pathNodes;
191                 stream >> pathEdges;
192                 stream >> nameStrings;
193                 stream >> typeStrings;
194
195                 return true;
196         }
197
198 };
199
200 #endif // SIGNALS_H