$search
00001 /* 00002 Aseba - an event-based framework for distributed robot control 00003 Copyright (C) 2007--2012: 00004 Stephane Magnenat <stephane at magnenat dot net> 00005 (http://stephane.magnenat.net) 00006 and other contributors, see authors.txt for details 00007 00008 This program is free software: you can redistribute it and/or modify 00009 it under the terms of the GNU Lesser General Public License as published 00010 by the Free Software Foundation, version 3 of the License. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. 00019 */ 00020 00021 #ifndef ASEBA_MEDULLA 00022 #define ASEBA_MEDULLA 00023 00024 #include <dashel/dashel.h> 00025 #include <QThread> 00026 #include <QStringList> 00027 #include <QDBusObjectPath> 00028 #include <QDBusAbstractAdaptor> 00029 #include <QDBusConnection> 00030 #include <QDBusMessage> 00031 #include <QMetaType> 00032 #include <QList> 00033 #include "../msg/msg.h" 00034 #include "../msg/descriptions-manager.h" 00035 00036 typedef QList<qint16> Values; 00037 00038 namespace Aseba 00039 { 00044 00045 class Hub; 00046 class AsebaNetworkInterface; 00047 00049 class EventFilterInterface: public QObject 00050 { 00051 Q_OBJECT 00052 Q_CLASSINFO("D-Bus Interface", "ch.epfl.mobots.EventFilter") 00053 00054 public: 00055 EventFilterInterface(AsebaNetworkInterface* network) : network(network) { ListenEvent(0); } 00056 void emitEvent(const quint16 id, const QString& name, const Values& data); 00057 00058 public slots: 00059 Q_SCRIPTABLE Q_NOREPLY void ListenEvent(const quint16 event); 00060 Q_SCRIPTABLE Q_NOREPLY void ListenEventName(const QString& name, const QDBusMessage &message); 00061 Q_SCRIPTABLE Q_NOREPLY void IgnoreEvent(const quint16 event); 00062 Q_SCRIPTABLE Q_NOREPLY void IgnoreEventName(const QString& name, const QDBusMessage &message); 00063 Q_SCRIPTABLE Q_NOREPLY void Free(); 00064 00065 signals: 00066 Q_SCRIPTABLE void Event(const quint16, const QString& name, const Values& values); 00067 Q_SCRIPTABLE void Test0(const quint16); 00068 Q_SCRIPTABLE void Test1(const QString& ); 00069 Q_SCRIPTABLE void Test2(const Values& ); 00070 Q_SCRIPTABLE void Test0_1(const quint16, const QString& ); 00071 Q_SCRIPTABLE void Test0_2(const quint16, const Values& ); 00072 Q_SCRIPTABLE void Test1_2(const QString&, const Values& ); 00073 00074 protected: 00075 AsebaNetworkInterface* network; 00076 }; 00077 00079 class AsebaNetworkInterface: public QDBusAbstractAdaptor, public DescriptionsManager 00080 { 00081 Q_OBJECT 00082 Q_CLASSINFO("D-Bus Interface", "ch.epfl.mobots.AsebaNetwork") 00083 00084 protected: 00085 struct RequestData 00086 { 00087 unsigned nodeId; 00088 unsigned pos; 00089 QDBusMessage reply; 00090 }; 00091 00092 public: 00093 AsebaNetworkInterface(Hub* hub, bool systemBus); 00094 00095 private slots: 00096 friend class Hub; 00097 void processMessage(Message *message, Dashel::Stream* sourceStream); 00098 friend class EventFilterInterface; 00099 void sendEventOnDBus(const quint16 event, const Values& data); 00100 void listenEvent(EventFilterInterface* filter, quint16 event); 00101 void ignoreEvent(EventFilterInterface* filter, quint16 event); 00102 void filterDestroyed(EventFilterInterface* filter); 00103 00104 public slots: 00105 Q_NOREPLY void LoadScripts(const QString& fileName, const QDBusMessage &message); 00106 QStringList GetNodesList() const; 00107 qint16 GetNodeId(const QString& node, const QDBusMessage &message) const; 00108 QStringList GetVariablesList(const QString& node) const; 00109 Q_NOREPLY void SetVariable(const QString& node, const QString& variable, const Values& data, const QDBusMessage &message) const; 00110 Values GetVariable(const QString& node, const QString& variable, const QDBusMessage &message); 00111 Q_NOREPLY void SendEvent(const quint16 event, const Values& data); 00112 Q_NOREPLY void SendEventName(const QString& name, const Values& data, const QDBusMessage &message); 00113 QDBusObjectPath CreateEventFilter(); 00114 00115 protected: 00116 virtual void nodeDescriptionReceived(unsigned nodeId); 00117 QDBusConnection DBusConnectionBus() const; 00118 00119 protected: 00120 Hub* hub; 00121 CommonDefinitions commonDefinitions; 00122 typedef QMap<QString, unsigned> NodesNamesMap; 00123 NodesNamesMap nodesNames; 00124 typedef QMap<QString, Compiler::VariablesMap> UserDefinedVariablesMap; 00125 UserDefinedVariablesMap userDefinedVariablesMap; 00126 typedef QList<RequestData*> RequestsList; 00127 RequestsList pendingReads; 00128 typedef QMultiMap<quint16, EventFilterInterface*> EventsFiltersMap; 00129 EventsFiltersMap eventsFilters; 00130 bool systemBus; 00131 unsigned eventsFiltersCounter; 00132 }; 00133 00141 class Hub: public QThread, public Dashel::Hub 00142 { 00143 Q_OBJECT 00144 00145 public: 00153 Hub(unsigned port, bool verbose, bool dump, bool forward, bool rawTime, bool systemBus); 00154 00160 void sendMessage(Message *message, Dashel::Stream* sourceStream = 0); 00164 void sendMessage(Message& message, Dashel::Stream* sourceStream = 0); 00165 00166 signals: 00167 void firstConnectionCreated(); 00168 void messageAvailable(Message *message, Dashel::Stream* sourceStream); 00169 00170 protected slots: 00172 void firstConnectionAvailable(); 00174 void requestDescription(); 00175 00176 private: 00177 virtual void run(); 00178 virtual void connectionCreated(Dashel::Stream *stream); 00179 virtual void incomingData(Dashel::Stream *stream); 00180 virtual void connectionClosed(Dashel::Stream *stream, bool abnormal); 00181 00182 private: 00183 bool verbose; 00184 bool dump; 00185 bool forward; 00186 bool rawTime; 00187 }; 00188 00190 }; 00191 00192 Q_DECLARE_METATYPE(Values); 00193 00194 #endif