OSDN Git Service

HTTPリクエストパーサがフィールド値のクォートで遷移を間違えていた問題を修正。
[wordring-tm/wordring-tm.git] / proxy / tmservice.h
index 28af026..21227d0 100644 (file)
 #define TMSERVICE_H
 
 #include "language.h"
+#include "tmtext.h"
+#include "tmdatabase.h"
 
 #include <QObject>
-#include <QByteArray>
 #include <QString>
+#include <QByteArray>
+#include <QJsonArray>
 #include <QMap>
+#include <QQueue>
+#include <QAbstractSocket>
 
 #include <QIcon>
 
 #include <QMutex>
 
 QT_BEGIN_NAMESPACE
-class QSettings;
+class QWebSocket;
 QT_END_NAMESPACE
 
 class Language;
+class Settings;
 
 namespace TM
 {
 
+class Service;
+class SocketConnection;
+class EditorWidget;
+class CandidateWidget;
+
+class WordringConnection : public QObject
+{
+       Q_OBJECT
+public:
+       WordringConnection(Settings *settings, Service *service);
+
+       bool find_sentence(sentence_data_type const &sdata);
+
+       bool insert_sentence(sentence_data_type const &sdata,
+                                                sentence_data_type const &tdata);
+       bool remove_sentence(
+                       sentence_data_type const &sdata,
+                       sentence_data_type const &tdata);
+
+public slots:
+       void onConnected();
+       void onDisconnected();
+       void onError(QAbstractSocket::SocketError e);
+
+       void onTextMessageReceived(QString const &message);
+       void onBinaryMessageReceived(QByteArray const &message);
+
+private:
+       void open();
+       void send();
+
+       void sentence_found(QJsonObject json);
+       void sentence_not_found(QJsonObject json);
+
+private:
+       Settings *m_settings;
+       Service *m_service;
+       QWebSocket *m_socket;
+
+       QQueue<QByteArray> m_requests;
+       int m_request_limit; /*!< 待機できるリクエストの制限値 */
+};
+
 class Service : public QObject
 {
        Q_OBJECT
+
+       struct dispatcher_data_type
+       {
+               SocketConnection::pointer connection;
+               int segment_id;
+               int index;
+       };
+
 public:
-       Service(QSettings *settings, QObject *parent = 0);
+       Service(Settings *settings, QObject *parent = 0);
+       ~Service();
+
+       // エディタ。
+       EditorWidget* editor_widget();
+       void set_editor_widget(EditorWidget *editor);
+       CandidateWidget* candidate_widget();
+       void set_candidate_widget(CandidateWidget *candidate);
+
+       void change_edit_mode(bool mode);
+
+       // SocketConnection
+       void attach(SocketConnection *connection);
+       void detach(SocketConnection *connection);
+       void set_current_connection(SocketConnection *connection);
 
+       // CRC32
+       void setup_crc_table();
+       quint32 crc32(QString const &string);
+       quint32 crc32(int code, QString const &string);
+
+       // 言語プラグイン
        void load_languages(QString const &path);
 
+       Language* find_language(int code);
+
        Text::pointer divide_into_sentences(int code, QString string);
        Text::pointer divide_into_words(int code, Text::pointer sentence);
 
-       int find_source_id(int code, QString const &source);
+       QString normalize(int code, QString string);
+
+       // データベース
+       quint32 find_site_id(QString host);
+       void insert_sentence(TextSentence::pointer sentence);
+       void remove_sentence(TextSentence::pointer sentence);
+       void find_sentence(TextSentence::pointer sentence);
+
+       void find_candidates(TextSentence::pointer sentence);
 
+private:
+public:
 signals:
+       /*!
+        * \brief 言語プラグインが読み込まれたときに発火するシグナルです。
+        * \param code 言語コードです。
+        * \param name 言語名です。
+        * \param icon 言語を表現するアイコンです。
+        */
        void languageLoaded(int code, QString name, QIcon icon);
 
 public slots:
+       // データベースから
+       void sentence_found(sentence_data_type result, TextSentence::weak_pointer token);
+       void sentence_not_found(TextSentence::weak_pointer token);
+       void sentence_inserted(quint32 source_id, quint32 target_id, TextSentence::weak_pointer token);
 
+       void candidate_found(candidate_data_type candidate,
+                                                TextSentence::weak_pointer token);
 public:
-       QMutex m_mutex;
+       // サーバから
+       void sentence_found(QString sstring, sentence_data_type const &result);
+
+private:
+       Settings *m_settings;
 
-       QSettings *m_settings;
        QMap<int, Language*> m_languages;
+
+       EditorWidget *m_editor_widget;
+       CandidateWidget *m_candidate_widget;
+
+       QThread *m_database_thread;
+       Database *m_database;
+
+       WordringConnection *m_wordring;
+
+       QSet<SocketConnection*> m_connections;
+       SocketConnection *m_current_connection;
+
+       quint32 m_crc_table[256];
 };
 
+Q_DECLARE_METATYPE(sentence_data_type)
+Q_DECLARE_METATYPE(candidate_data_type)
+Q_DECLARE_METATYPE(TextSentence::weak_pointer)
+
 } // namespace TM
 
 #endif // TMSERVICE_H