00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef ASEBA_MEDULLA
00025 #define ASEBA_MEDULLA
00026
00027 #include <dashel/dashel.h>
00028 #include <QThread>
00029 #include <QStringList>
00030 #include <QDBusObjectPath>
00031 #include <QDBusAbstractAdaptor>
00032 #include <QDBusConnection>
00033 #include <QDBusMessage>
00034 #include <QMetaType>
00035 #include <QList>
00036 #include "../msg/msg.h"
00037 #include "../msg/descriptions-manager.h"
00038
00039 typedef QList<qint16> Values;
00040
00041 namespace Aseba
00042 {
00047
00048 class Hub;
00049 class AsebaNetworkInterface;
00050
00052 class EventFilterInterface: public QObject
00053 {
00054 Q_OBJECT
00055 Q_CLASSINFO("D-Bus Interface", "ch.epfl.mobots.EventFilter")
00056
00057 public:
00058 EventFilterInterface(AsebaNetworkInterface* network) : network(network) { ListenEvent(0); }
00059 void emitEvent(const quint16 id, const QString& name, const Values& data);
00060
00061 public slots:
00062 Q_SCRIPTABLE Q_NOREPLY void ListenEvent(const quint16 event);
00063 Q_SCRIPTABLE Q_NOREPLY void ListenEventName(const QString& name, const QDBusMessage &message);
00064 Q_SCRIPTABLE Q_NOREPLY void IgnoreEvent(const quint16 event);
00065 Q_SCRIPTABLE Q_NOREPLY void IgnoreEventName(const QString& name, const QDBusMessage &message);
00066 Q_SCRIPTABLE Q_NOREPLY void Free();
00067
00068 signals:
00069 Q_SCRIPTABLE void Event(const quint16, const QString& name, const Values& values);
00070 Q_SCRIPTABLE void Test0(const quint16);
00071 Q_SCRIPTABLE void Test1(const QString& );
00072 Q_SCRIPTABLE void Test2(const Values& );
00073 Q_SCRIPTABLE void Test0_1(const quint16, const QString& );
00074 Q_SCRIPTABLE void Test0_2(const quint16, const Values& );
00075 Q_SCRIPTABLE void Test1_2(const QString&, const Values& );
00076
00077 protected:
00078 AsebaNetworkInterface* network;
00079 };
00080
00082 class AsebaNetworkInterface: public QDBusAbstractAdaptor, public DescriptionsManager
00083 {
00084 Q_OBJECT
00085 Q_CLASSINFO("D-Bus Interface", "ch.epfl.mobots.AsebaNetwork")
00086
00087 protected:
00088 struct RequestData
00089 {
00090 unsigned nodeId;
00091 unsigned pos;
00092 QDBusMessage reply;
00093 };
00094
00095 public:
00096 AsebaNetworkInterface(Hub* hub, bool systemBus);
00097
00098 private slots:
00099 friend class Hub;
00100 void processMessage(Message *message, Dashel::Stream* sourceStream);
00101 friend class EventFilterInterface;
00102 void sendEventOnDBus(const quint16 event, const Values& data);
00103 void listenEvent(EventFilterInterface* filter, quint16 event);
00104 void ignoreEvent(EventFilterInterface* filter, quint16 event);
00105 void filterDestroyed(EventFilterInterface* filter);
00106
00107 public slots:
00108 Q_NOREPLY void LoadScripts(const QString& fileName, const QDBusMessage &message);
00109 QStringList GetNodesList() const;
00110 qint16 GetNodeId(const QString& node, const QDBusMessage &message) const;
00111 QStringList GetVariablesList(const QString& node) const;
00112 Q_NOREPLY void SetVariable(const QString& node, const QString& variable, const Values& data, const QDBusMessage &message) const;
00113 Values GetVariable(const QString& node, const QString& variable, const QDBusMessage &message);
00114 Q_NOREPLY void SendEvent(const quint16 event, const Values& data);
00115 Q_NOREPLY void SendEventName(const QString& name, const Values& data, const QDBusMessage &message);
00116 QDBusObjectPath CreateEventFilter();
00117
00118 protected:
00119 virtual void nodeDescriptionReceived(unsigned nodeId);
00120 QDBusConnection DBusConnectionBus() const;
00121
00122 protected:
00123 Hub* hub;
00124 CommonDefinitions commonDefinitions;
00125 typedef QMap<QString, unsigned> NodesNamesMap;
00126 NodesNamesMap nodesNames;
00127 typedef QMap<QString, Compiler::VariablesMap> UserDefinedVariablesMap;
00128 UserDefinedVariablesMap userDefinedVariablesMap;
00129 typedef QList<RequestData*> RequestsList;
00130 RequestsList pendingReads;
00131 typedef QMultiMap<quint16, EventFilterInterface*> EventsFiltersMap;
00132 EventsFiltersMap eventsFilters;
00133 bool systemBus;
00134 unsigned eventsFiltersCounter;
00135 };
00136
00144 class Hub: public QThread, public Dashel::Hub
00145 {
00146 Q_OBJECT
00147
00148 public:
00155 Hub(unsigned port, bool verbose, bool dump, bool forward, bool rawTime, bool systemBus);
00156
00162 void sendMessage(Message *message, Dashel::Stream* sourceStream = 0);
00166 void sendMessage(Message& message, Dashel::Stream* sourceStream = 0);
00167
00168 signals:
00169 void firstConnectionCreated();
00170 void messageAvailable(Message *message, Dashel::Stream* sourceStream);
00171
00172 protected slots:
00174 void firstConnectionAvailable();
00176 void requestDescription();
00177
00178 private:
00179 virtual void run();
00180 virtual void connectionCreated(Dashel::Stream *stream);
00181 virtual void incomingData(Dashel::Stream *stream);
00182 virtual void connectionClosed(Dashel::Stream *stream, bool abnormal);
00183
00184 private:
00185 bool verbose;
00186 bool dump;
00187 bool forward;
00188 bool rawTime;
00189 };
00190
00192 };
00193
00194 Q_DECLARE_METATYPE(Values);
00195
00196 #endif