]> git.lyx.org Git - lyx.git/blob - src/lyxserver.h
next step...
[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         /// Open pipes
63         void openConnection();
64         
65         /// Close pipes
66         void closeConnection();
67
68         /// start a pipe
69         int startPipe(string const &);
70  
71         /// finish a pipe
72         void endPipe(int, string const &);
73  
74         /// This is -1 if not open
75         int infd;
76
77         /// This is -1 if not open
78         int outfd;
79
80         /// Are we up and running?
81         bool ready;
82
83         /// Base of pipename including path
84         string pipename;
85
86         /// The client
87         LyXServer * client;
88
89         /// The client callback function
90         ClientCallbackfct clientcb;
91 };
92
93
94 /* --- prototypes -------------------------------------------------------- */
95 ///
96 class LyXServer {
97 public:
98         // FIXME IN 0.13
99         // Hack! This should be changed in 0.13
100
101         // The lyx server should not take an argument "LyXFunc" but this is
102         // how it will be done for 0.12. In 0.13 we must write a non-gui
103         // bufferview.
104         // IMO lyxserver is atypical, and for the moment the only one, non-gui
105         // bufferview. We just have to find a way to handle situations like if
106         // lyxserver is using a buffer that is being edited with a bufferview.
107         // With a common buffer list this is not a problem, maybe. (Alejandro)
108         ///
109         LyXServer(LyXFunc * f, string const & pip)
110                 : numclients(0), func(f), pipes(pip, (this), callback) {}
111         /// 
112         ~LyXServer();
113         ///
114         void notifyClient(string const &);
115         
116         /// whilst crashing etc.
117         void emergencyCleanup() {
118                 pipes.emergencyCleanup();
119         }
120  
121 private:
122         ///
123         static void callback(LyXServer *, string const & msg);
124         /// Names and number of current clients
125         enum {
126                 ///
127                 MAX_CLIENTS = 10
128         };
129         ///
130         string clients[MAX_CLIENTS];
131         ///
132         int numclients;
133         ///
134         LyXFunc * func;
135         ///
136         LyXComm pipes;
137 };
138
139 #endif /* _LYXSERVER_H_ */
140
141 /* === End of File: lyxserver.h ========================================== */