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