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