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