librime 1.2
Rime Input Method Engine, the core library
service.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2011-08-08 GONG Chen <chen.sst@gmail.com>
6//
7#ifndef RIME_SERVICE_H_
8#define RIME_SERVICE_H_
9
10#include <stdint.h>
11#include <time.h>
12#include <mutex>
13#include <rime/common.h>
14#include <rime/deployer.h>
15
16namespace rime {
17
18using SessionId = uintptr_t;
19
20static const SessionId kInvalidSessionId = 0;
21
22using NotificationHandler = function<void(SessionId session_id,
23 const char* message_type,
24 const char* message_value)>;
25
26class Context;
27class Engine;
28class KeyEvent;
29class Schema;
30
31class Session {
32 public:
33 static const int kLifeSpan = 5 * 60; // seconds
34
35 Session();
36 bool ProcessKey(const KeyEvent& key_event);
37 void Activate();
38 void ResetCommitText();
39 bool CommitComposition();
40 void ClearComposition();
42
43 Context* context() const;
44 Schema* schema() const;
45 time_t last_active_time() const { return last_active_time_; }
46 const string& commit_text() const { return commit_text_; }
47
48 private:
49 void OnCommit(const string& commit_text);
50
51 the<Engine> engine_;
52 time_t last_active_time_ = 0;
53 string commit_text_;
54};
55
56class ResourceResolver;
57struct ResourceType;
58
59class RIME_DLL Service {
60 public:
61 ~Service();
62
63 void StartService();
64 void StopService();
65
68 bool DestroySession(SessionId session_id);
70 void CleanupAllSessions();
71
74 void Notify(SessionId session_id,
75 const string& message_type,
76 const string& message_value);
77
80 const ResourceType& type);
83
84 Deployer& deployer() { return deployer_; }
85 bool disabled() { return !started_ || deployer_.IsMaintenanceMode(); }
86
87 static Service& instance();
88
89 private:
90 Service();
91
92 using SessionMap = map<SessionId, an<Session>>;
93 SessionMap sessions_;
94 Deployer deployer_;
95 NotificationHandler notification_handler_;
96 std::mutex mutex_;
97 bool started_ = false;
98};
99
100} // namespace rime
101
102#endif // RIME_SERVICE_H_
#define RIME_DLL
Definition rime_api.h:33
Definition algebra.cc:12
std::shared_ptr< T > an
Definition common.h:60
std::unique_ptr< T > the
Definition common.h:58
uintptr_t SessionId
Definition service.h:18
function< void(SessionId session_id, const char *message_type, const char *message_value)> NotificationHandler
Definition service.h:22
Definition context.h:19
Definition deployer.h:32
Definition engine.h:20
Definition key_event.h:17
Definition resource.h:16
Definition resource.h:22
Definition schema.h:15
bool CommitComposition()
Definition service.cc:38
Session()
Definition service.cc:17
Context * context() const
Definition service.cc:59
Schema * schema() const
Definition service.cc:63
void Activate()
Definition service.cc:30
void ClearComposition()
Definition service.cc:45
bool ProcessKey(const KeyEvent &key_event)
Definition service.cc:26
void ResetCommitText()
Definition service.cc:34
time_t last_active_time() const
Definition service.h:45
const string & commit_text() const
Definition service.h:46
static const int kLifeSpan
Definition service.h:33
void ApplySchema(Schema *schema)
Definition service.cc:51
Definition service.h:59
bool DestroySession(SessionId session_id)
Definition service.cc:120
ResourceResolver * CreateUserSpecificResourceResolver(const ResourceType &type)
Definition service.cc:174
ResourceResolver * CreateResourceResolver(const ResourceType &type)
Definition service.cc:167
an< Session > GetSession(SessionId session_id)
Definition service.cc:108
SessionId CreateSession()
Definition service.cc:85
bool disabled()
Definition service.h:85
void StopService()
Definition service.cc:80
void SetNotificationHandler(const NotificationHandler &handler)
Definition service.cc:149
void StartService()
Definition service.cc:76
ResourceResolver * CreateStagingResourceResolver(const ResourceType &type)
Definition service.cc:189
Deployer & deployer()
Definition service.h:84
ResourceResolver * CreateDeployedResourceResolver(const ResourceType &type)
Definition service.cc:181
void Notify(SessionId session_id, const string &message_type, const string &message_value)
Definition service.cc:157
void ClearNotificationHandler()
Definition service.cc:153
void CleanupAllSessions()
Definition service.cc:145
void CleanupStaleSessions()
Definition service.cc:128