]> git.lyx.org Git - lyx.git/blob - src/Server.h
Fixed some lines that were too long. It compiled afterwards.
[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                 : pipename(pip), client(cli), clientcb(ccb)
43         {
44                 ready = false;
45                 openConnection();
46         }
47
48         ///
49         ~LyXComm() { closeConnection(); }
50
51         /// clean up in emergency
52         void emergencyCleanup();
53
54         /// Send message
55         void send(std::string const &);
56
57         /// asynch ready-to-be-read notification
58         void read_ready();
59
60 private:
61         /// the filename of the in pipe
62         std::string const inPipeName() const;
63
64         /// the filename of the out pipe
65         std::string const outPipeName() const;
66
67         /// Open pipes
68         void openConnection();
69
70         /// Close pipes
71         void closeConnection();
72
73         /// start a pipe
74         int startPipe(std::string const &, bool);
75
76         /// finish a pipe
77         void endPipe(int &, std::string const &, bool);
78
79         /// This is -1 if not open
80         int infd;
81
82         /// This is -1 if not open
83         int outfd;
84
85         /// Are we up and running?
86         bool ready;
87
88         /// Base of pipename including path
89         std::string pipename;
90
91         /// The client
92         Server * client;
93
94         /// The client callback function
95         ClientCallbackfct clientcb;
96 };
97
98
99
100 ///
101 class Server {
102 public:
103         // FIXME IN 0.13
104         // Hack! This should be changed in 0.13
105
106         // The lyx server should not take an argument "LyXFunc" but this is
107         // how it will be done for 0.12. In 0.13 we must write a non-gui
108         // bufferview.
109         // IMO lyxserver is atypical, and for the moment the only one, non-gui
110         // bufferview. We just have to find a way to handle situations like if
111         // lyxserver is using a buffer that is being edited with a bufferview.
112         // With a common buffer list this is not a problem, maybe. (Alejandro)
113         ///
114         Server(LyXFunc * f, std::string const & pip)
115                 : numclients(0), func(f), pipes(pip, (this), callback) {}
116         ///
117         ~Server();
118         ///
119         void notifyClient(std::string const &);
120
121         /// whilst crashing etc.
122         void emergencyCleanup() { pipes.emergencyCleanup(); }
123
124 private:
125         ///
126         static void callback(Server *, std::string const & msg);
127         /// Names and number of current clients
128         enum {
129                 ///
130                 MAX_CLIENTS = 10
131         };
132         ///
133         std::string clients[MAX_CLIENTS];
134         ///
135         int numclients;
136         ///
137         LyXFunc * func;
138         ///
139         LyXComm pipes;
140 };
141
142 /// Implementation is in LyX.cpp
143 extern Server & theServer();
144
145
146 } // namespace lyx
147
148 #endif // SERVER_H