]> git.lyx.org Git - lyx.git/blob - src/Server.h
31e7c4b7c4307c37cd13ca71c361fed164196970
[lyx.git] / src / Server.h
1 // -*- C++ -*-
2 /**
3  * \file Server.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Enrico Forestieri
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef SERVER_H
15 #define SERVER_H
16
17 #include <boost/signals/trackable.hpp>
18
19 #ifdef _WIN32
20 #include <windows.h>
21 #include <QObject>
22 #include <QEvent>
23 #endif
24
25
26 namespace lyx {
27
28 class LyXFunc;
29 class Server;
30
31
32 /** This class managed the pipes used for communicating with clients.
33  Usage: Initialize with pipe-filename-base, client class to receive
34  messages, and callback-function that will be called with the messages.
35  When you want to send, use "send()".
36  This class encapsulates all the dirty communication and thus provides
37  a clean string interface.
38  */
39 #ifndef _WIN32
40 class LyXComm : public boost::signals::trackable {
41 #else
42 class LyXComm : public QObject {
43         Q_OBJECT
44 #endif
45 public:
46         /** When we receive a message, we send it to a client.
47           This is one of the small things that would have been a lot
48           cleaner with a Signal/Slot thing.
49          */
50         typedef void (*ClientCallbackfct)(Server *, std::string const &);
51
52         /// Construct with pipe-basename and callback to receive messages
53         LyXComm(std::string const & pip, Server * cli, ClientCallbackfct ccb = 0);
54
55         ///
56         ~LyXComm() { closeConnection(); }
57
58         /// clean up in emergency
59         void emergencyCleanup();
60
61         /// Send message
62         void send(std::string const &);
63
64         /// asynch ready-to-be-read notification
65 #ifndef _WIN32
66         void read_ready();
67 #else
68         void read_ready(HANDLE);
69
70         /// The pipe server
71         void pipeServer();
72 #endif
73
74 private:
75         /// the filename of the in pipe
76         std::string const inPipeName() const;
77
78         /// the filename of the out pipe
79         std::string const outPipeName() const;
80
81         /// Open pipes
82         void openConnection();
83
84         /// Close pipes
85         void closeConnection();
86
87 #ifndef _WIN32
88         /// start a pipe
89         int startPipe(std::string const &, bool);
90
91         /// finish a pipe
92         void endPipe(int &, std::string const &, bool);
93
94         /// This is -1 if not open
95         int infd_;
96
97         /// This is -1 if not open
98         int outfd_;
99 #else
100         HANDLE outpipe_;
101 #endif
102
103         /// Are we up and running?
104         bool ready_;
105
106         /// Base of pipename including path
107         std::string pipename_;
108
109         /// The client
110         Server * client_;
111
112         /// The client callback function
113         ClientCallbackfct clientcb_;
114
115 #ifdef _WIN32
116         /// Catch pipe ready-to-be-read notification
117         bool event(QEvent *);
118
119         /// Check whether the pipe server must be stopped
120         BOOL checkStopServerEvent();
121
122         /// Windows event for stopping the pipe server
123         HANDLE stopserver_;
124 #endif
125 };
126
127
128 ///
129 class Server {
130 public:
131         // FIXME IN 0.13
132         // Hack! This should be changed in 0.13
133
134         // The lyx server should not take an argument "LyXFunc" but this is
135         // how it will be done for 0.12. In 0.13 we must write a non-gui
136         // bufferview.
137         // IMO lyxserver is atypical, and for the moment the only one, non-gui
138         // bufferview. We just have to find a way to handle situations like if
139         // lyxserver is using a buffer that is being edited with a bufferview.
140         // With a common buffer list this is not a problem, maybe. (Alejandro)
141         ///
142         Server(LyXFunc * f, std::string const & pip);
143         ///
144         ~Server();
145         ///
146         void notifyClient(std::string const &);
147
148         /// whilst crashing etc.
149         void emergencyCleanup() { pipes_.emergencyCleanup(); }
150         ///
151         void callback(std::string const & msg);
152
153 private:
154         /// Names and number of current clients
155         enum { MAX_CLIENTS = 10 };
156         ///
157         std::string clients_[MAX_CLIENTS];
158         ///
159         int numclients_;
160         ///
161         LyXFunc * func_;
162         ///
163         LyXComm pipes_;
164 };
165
166 /// Implementation is in LyX.cpp
167 extern Server & theServer();
168
169
170 } // namespace lyx
171
172 #endif // SERVER_H