]> git.lyx.org Git - lyx.git/blob - src/LyXServer.h
merge LyXTabular.{cpp,h} and insets/InsetTabular.{cpp,h}. More reorganization necessa...
[lyx.git] / src / LyXServer.h
1 // -*- C++ -*-
2 /**
3  * \file LyXServer.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  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef LYXSERVER_H
14 #define LYXSERVER_H
15
16 #include <boost/signals/trackable.hpp>
17
18
19 namespace lyx {
20
21 class LyXFunc;
22 class LyXServer;
23
24 /* --- i/o pipes --------------------------------------------------------- */
25
26 /** This class managed the pipes used for communicating with clients.
27  Usage: Initialize with pipe-filename-base, client class to receive
28  messages, and callback-function that will be called with the messages.
29  When you want to send, use "send()".
30  This class encapsulates all the dirty communication and thus provides
31  a clean string interface.
32  */
33 class LyXComm : public boost::signals::trackable {
34 public:
35         /** When we receive a message, we send it to a client.
36           This is one of the small things that would have been a lot
37           cleaner with a Signal/Slot thing.
38          */
39         typedef void (*ClientCallbackfct)(LyXServer *, std::string const &);
40
41         /// Construct with pipe-basename and callback to receive messages
42         LyXComm(std::string const & pip, LyXServer * cli, ClientCallbackfct ccb = 0)
43                 : pipename(pip), client(cli), clientcb(ccb) {
44                 ready = false;
45                 openConnection();
46         }
47
48         ///
49         ~LyXComm() {
50                 closeConnection();
51         }
52
53         /// clean up in emergency
54         void emergencyCleanup();
55
56         /// Send message
57         void send(std::string const &);
58
59         /// asynch ready-to-be-read notification
60         void read_ready();
61
62 private:
63         /// the filename of the in pipe
64         std::string const inPipeName() const;
65
66         /// the filename of the out pipe
67         std::string const outPipeName() const;
68
69         /// Open pipes
70         void openConnection();
71
72         /// Close pipes
73         void closeConnection();
74
75         /// start a pipe
76         int startPipe(std::string const &, bool);
77
78         /// finish a pipe
79         void endPipe(int &, std::string const &, bool);
80
81         /// This is -1 if not open
82         int infd;
83
84         /// This is -1 if not open
85         int outfd;
86
87         /// Are we up and running?
88         bool ready;
89
90         /// Base of pipename including path
91         std::string pipename;
92
93         /// The client
94         LyXServer * client;
95
96         /// The client callback function
97         ClientCallbackfct clientcb;
98 };
99
100
101 /* --- prototypes -------------------------------------------------------- */
102 ///
103 class LyXServer {
104 public:
105         // FIXME IN 0.13
106         // Hack! This should be changed in 0.13
107
108         // The lyx server should not take an argument "LyXFunc" but this is
109         // how it will be done for 0.12. In 0.13 we must write a non-gui
110         // bufferview.
111         // IMO lyxserver is atypical, and for the moment the only one, non-gui
112         // bufferview. We just have to find a way to handle situations like if
113         // lyxserver is using a buffer that is being edited with a bufferview.
114         // With a common buffer list this is not a problem, maybe. (Alejandro)
115         ///
116         LyXServer(LyXFunc * f, std::string const & pip)
117                 : numclients(0), func(f), pipes(pip, (this), callback) {}
118         ///
119         ~LyXServer();
120         ///
121         void notifyClient(std::string const &);
122
123         /// whilst crashing etc.
124         void emergencyCleanup() {
125                 pipes.emergencyCleanup();
126         }
127
128 private:
129         ///
130         static void callback(LyXServer *, std::string const & msg);
131         /// Names and number of current clients
132         enum {
133                 ///
134                 MAX_CLIENTS = 10
135         };
136         ///
137         std::string clients[MAX_CLIENTS];
138         ///
139         int numclients;
140         ///
141         LyXFunc * func;
142         ///
143         LyXComm pipes;
144 };
145
146 /// Implementation is in LyX.cpp
147 extern LyXServer & theLyXServer();
148
149
150 } // namespace lyx
151
152 #endif /* _LYXSERVER_H_ */
153
154 /* === End of File: LyXServer.h ========================================== */