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