librime: service.h Source File

| 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
18 using SessionId = uintptr_t;
19
20static const SessionId kInvalidSessionId = 0;
21
22 using 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
31 class 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();
41 void ApplySchema(Schema* schema);
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
59 class RIME_DLL Service {
60 public:
61 ~Service();
62
63 void StartService();
64 void StopService();
65
66 SessionId CreateSession();
67 an<Session> GetSession(SessionId session_id);
68 bool DestroySession(SessionId session_id);
69 void CleanupStaleSessions();
70 void CleanupAllSessions();
71
72 void SetNotificationHandler(const NotificationHandler& handler);
73 void ClearNotificationHandler();
74 void Notify(SessionId session_id,
75 const string& message_type,
76 const string& message_value);
77
78 ResourceResolver* CreateResourceResolver(const ResourceType& type);
79 ResourceResolver* CreateUserSpecificResourceResolver(
80 const ResourceType& type);
81 ResourceResolver* CreateDeployedResourceResolver(const ResourceType& type);
82 ResourceResolver* CreateStagingResourceResolver(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_
deployer.h
common.h
RIME_DLL
#define RIME_DLL
Definition rime_api.h:33
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::the
std::unique_ptr< T > the
Definition common.h:58
rime::SessionId
uintptr_t SessionId
Definition service.h:18
rime::NotificationHandler
function< void(SessionId session_id, const char *message_type, const char *message_value)> NotificationHandler
Definition service.h:22
rime::Context
Definition context.h:19
rime::Deployer
Definition deployer.h:32
rime::Engine
Definition engine.h:20
rime::KeyEvent
Definition key_event.h:17
rime::ResourceType
Definition resource.h:16
rime::ResourceResolver
Definition resource.h:22
rime::Schema
Definition schema.h:15
rime::Session::CommitComposition
bool CommitComposition()
Definition service.cc:38
rime::Session::Session
Session()
Definition service.cc:17
rime::Session::context
Context * context() const
Definition service.cc:59
rime::Session::schema
Schema * schema() const
Definition service.cc:63
rime::Session::Activate
void Activate()
Definition service.cc:30
rime::Session::ClearComposition
void ClearComposition()
Definition service.cc:45
rime::Session::ProcessKey
bool ProcessKey(const KeyEvent &key_event)
Definition service.cc:26
rime::Session::ResetCommitText
void ResetCommitText()
Definition service.cc:34
rime::Session::last_active_time
time_t last_active_time() const
Definition service.h:45
rime::Session::commit_text
const string & commit_text() const
Definition service.h:46
rime::Session::kLifeSpan
static const int kLifeSpan
Definition service.h:33
rime::Session::ApplySchema
void ApplySchema(Schema *schema)
Definition service.cc:51
rime::Service
Definition service.h:59
rime::Service::DestroySession
bool DestroySession(SessionId session_id)
Definition service.cc:120
rime::Service::CreateUserSpecificResourceResolver
ResourceResolver * CreateUserSpecificResourceResolver(const ResourceType &type)
Definition service.cc:174
rime::Service::CreateResourceResolver
ResourceResolver * CreateResourceResolver(const ResourceType &type)
Definition service.cc:167
rime::Service::GetSession
an< Session > GetSession(SessionId session_id)
Definition service.cc:108
rime::Service::CreateSession
SessionId CreateSession()
Definition service.cc:85
rime::Service::disabled
bool disabled()
Definition service.h:85
rime::Service::StopService
void StopService()
Definition service.cc:80
rime::Service::SetNotificationHandler
void SetNotificationHandler(const NotificationHandler &handler)
Definition service.cc:149
rime::Service::StartService
void StartService()
Definition service.cc:76
rime::Service::CreateStagingResourceResolver
ResourceResolver * CreateStagingResourceResolver(const ResourceType &type)
Definition service.cc:189
rime::Service::deployer
Deployer & deployer()
Definition service.h:84
rime::Service::CreateDeployedResourceResolver
ResourceResolver * CreateDeployedResourceResolver(const ResourceType &type)
Definition service.cc:181
rime::Service::Notify
void Notify(SessionId session_id, const string &message_type, const string &message_value)
Definition service.cc:157
rime::Service::ClearNotificationHandler
void ClearNotificationHandler()
Definition service.cc:153
rime::Service::CleanupAllSessions
void CleanupAllSessions()
Definition service.cc:145
rime::Service::CleanupStaleSessions
void CleanupStaleSessions()
Definition service.cc:128