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