Gaia
cyclopsmaster.h
1 /*
2  * Copyright (C) 2006-2013 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Gaia
5  *
6  * Gaia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef GAIA_CYCLOPSMASTER_H
21 #define GAIA_CYCLOPSMASTER_H
22 
23 #include <QNetworkReply>
24 #include <QHostAddress>
25 #include <QBuffer>
26 #include "cyclops.h"
27 
29  public:
30 
31  gaia2::yaml::Mapping request;
32  QList<gaia2::yaml::Mapping> replies;
33 
34  // for requests that need multiple roundtrips (ie: nnSearchById), this is used to keep
35  // a state for the request being processed (ie: progress state)
36  int state;
37  // and this can be used as a container for additional info (kind of a void*, but cleaner)
38  QVariant info;
39 
40  PendingRequest(const gaia2::yaml::Mapping& request = gaia2::yaml::Mapping()) : request(request), state(0) {}
41 
42 };
43 
44 class CyclopsMaster : public YamlRPCServer {
45 Q_OBJECT
46  public:
47 
48  CyclopsMaster();
49  virtual ~CyclopsMaster();
50 
51  void setupClients(const QString& configFilename);
52 
53  protected:
57  QList<QPair<QHostAddress, int> > _slaves;
58 
63  QMap<int, QTcpSocket*> _slaveToClient;
64 
68  QMap<int, QBuffer*> _slaveReplies;
69 
75  QMap<QTcpSocket*, PendingRequest> _pendingRequests;
76 
77  virtual void readClient();
78 
82  void sendReadyReply(QTcpSocket* client);
83 
87  void sendReplyToClient(QTcpSocket* client, const gaia2::yaml::Mapping& reply);
88 
89  // need to rewrite this to be called only once and return all the split queries at once
90  gaia2::yaml::Mapping split(const gaia2::yaml::Mapping& query, int idx, int total, PendingRequest& preq);
91  gaia2::yaml::Mapping reduce(const PendingRequest& results);
92 
93  void distributeClientRequest(QTcpSocket* client, const gaia2::yaml::Mapping& q);
94  int sendSlaveRequest(const QPair<QHostAddress, int>& slave, const gaia2::yaml::Mapping& request);
95 
96  void setup(const QString& filename);
97 
98  protected slots:
99  void replyFromSlave(int id, bool error);
100 
101 };
102 
103 
104 #endif // GAIA_CYCLOPSMASTER_H
QMap< int, QBuffer * > _slaveReplies
Maintains a list of QHttp connections incoming data.
Definition: cyclopsmaster.h:68
Definition: yamlrpcserver.h:59
QMap< int, QTcpSocket * > _slaveToClient
Maintains a list of QHttp connection ids to client socket that originated the spawning of this QHttp...
Definition: cyclopsmaster.h:63
QList< QPair< QHostAddress, int > > _slaves
Maintains a list of connected slaves, as pairs (IP, port).
Definition: cyclopsmaster.h:57
Definition: cyclopsmaster.h:44
QMap< QTcpSocket *, PendingRequest > _pendingRequests
Maintains the list of slave replies already obtained for a client query.
Definition: cyclopsmaster.h:75
Definition: cyclopsmaster.h:28