OSDN Git Service

dtl追加。
[wordring-tm/wordring-tm.git] / proxy / tmservice.h
1 #ifndef TMSERVICE_H
2 #define TMSERVICE_H
3
4 #include "language.h"
5 #include "tmtext.h"
6 #include "tmdatabase.h"
7
8 #include <QObject>
9 #include <QString>
10 #include <QByteArray>
11 #include <QJsonArray>
12 #include <QMap>
13 #include <QQueue>
14 #include <QAbstractSocket>
15
16 #include <QIcon>
17
18 #include <QMutex>
19
20 QT_BEGIN_NAMESPACE
21 class QWebSocket;
22 QT_END_NAMESPACE
23
24 class Language;
25 class Settings;
26
27 namespace TM
28 {
29
30 class Service;
31 class SocketConnection;
32
33 class WordringConnection : public QObject
34 {
35         Q_OBJECT
36 public:
37         WordringConnection(Settings *settings, Service *service);
38
39         bool find_sentence(sentence_data_type const &sdata);
40
41         bool insert_sentence(sentence_data_type const &sdata,
42                                                  sentence_data_type const &tdata);
43         bool remove_sentence(
44                         sentence_data_type const &sdata,
45                         sentence_data_type const &tdata);
46
47 public slots:
48         void onConnected();
49         void onDisconnected();
50         void onError(QAbstractSocket::SocketError e);
51
52         void onTextMessageReceived(QString const &message);
53         void onBinaryMessageReceived(QByteArray const &message);
54
55 private:
56         void open();
57         void send();
58
59         void sentence_found(QJsonObject json);
60         void sentence_not_found(QJsonObject json);
61
62 private:
63         Settings *m_settings;
64         Service *m_service;
65         QWebSocket *m_socket;
66
67         QQueue<QByteArray> m_requests;
68         int m_request_limit; /*!< 待機できるリクエストの制限値 */
69 };
70
71 class Service : public QObject
72 {
73         Q_OBJECT
74
75         struct dispatcher_data_type
76         {
77                 SocketConnection::pointer connection;
78                 int segment_id;
79                 int index;
80         };
81
82 public:
83         Service(Settings *settings, QObject *parent = 0);
84         ~Service();
85
86         // SocketConnection
87         void attach(SocketConnection *connection);
88         void detach(SocketConnection *connection);
89
90         // CRC32
91         void setup_crc_table();
92         quint32 crc32(QString const &string);
93         quint32 crc32(int code, QString const &string);
94
95         // 言語プラグイン
96         void load_languages(QString const &path);
97
98         Text::pointer divide_into_sentences(int code, QString string);
99         Text::pointer divide_into_words(int code, Text::pointer sentence);
100
101         QString normalize(int code, QString string);
102
103         // データベース
104         quint32 find_site_id(QString host);
105
106         void insert_sentence(TextSentence::pointer sentence);
107         void remove_sentence(TextSentence::pointer sentence);
108
109         void find_sentence(TextSentence::pointer sentence);
110 private:
111 public:
112 signals:
113         /*!
114          * \brief 言語プラグインが読み込まれたときに発火するシグナルです。
115          * \param code 言語コードです。
116          * \param name 言語名です。
117          * \param icon 言語を表現するアイコンです。
118          */
119         void languageLoaded(int code, QString name, QIcon icon);
120
121 public slots:
122         // データベースから
123         void sentence_found(sentence_data_type result, TextSentence::weak_pointer token);
124         void sentence_not_found(TextSentence::weak_pointer token);
125         void sentence_inserted(quint32 source_id, quint32 target_id, TextSentence::weak_pointer token);
126
127 public:
128         // サーバから
129         void sentence_found(QString sstring, sentence_data_type const &result);
130
131 private:
132         Settings *m_settings;
133
134         QMap<int, Language*> m_languages;
135
136         QThread *m_database_thread;
137         Database *m_database;
138
139         WordringConnection *m_wordring;
140
141         QSet<SocketConnection*> m_connections;
142         quint32 m_crc_table[256];
143 };
144
145 Q_DECLARE_METATYPE(sentence_data_type)
146 Q_DECLARE_METATYPE(TextSentence::weak_pointer)
147
148 } // namespace TM
149
150 #endif // TMSERVICE_H