]> git.lyx.org Git - lyx.git/blob - src/lyxserver.h
9dcdf99d65570e7c8a1861dbb2145992659e4362
[lyx.git] / src / lyxserver.h
1 // -*- C++ -*-
2 /* This file is part of
3 * ======================================================
4
5 *           LyX, The Document Processor
6 *        
7 *           Copyright (C) 1995 Matthias Ettrich
8 *           Copyright (C) 1995-1998 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 {
34 public:
35         /** When we receive a message, we send it to a client.
36           This is one of the small things that would have been a lot
37           cleaner with a Signal/Slot thing.
38          */
39         typedef void (*ClientCallbackfct)(LyXServer *, string const &);
40
41         /// Construct with pipe-basename and callback to receive messages
42         LyXComm(string const &pip, LyXServer * cli, ClientCallbackfct ccb = 0)
43                 :pipename(pip), client(cli), clientcb(ccb)
44         {
45                 ready = false;
46                 openConnection();
47         }
48
49         ///
50         ~LyXComm() {
51                 closeConnection();
52         }
53
54         /// Send message
55         void send(string const &);
56
57         /// We receive messages via XForms through this callback
58         static void callback(int fd, void *v);
59
60 private:
61         /// Open pipes
62         void openConnection();
63         
64         /// Close pipes
65         void closeConnection();
66
67         /// This is -1 if not open
68         int infd;
69
70         /// This is -1 if not open
71         int outfd;
72
73         /// Are we up and running?
74         bool ready;
75
76         /// Base of pipename including path
77         string pipename;
78
79         /// The client
80         LyXServer * client;
81
82         /// The client callback function
83         ClientCallbackfct clientcb;
84 };
85
86
87 /* --- prototypes -------------------------------------------------------- */
88 class LyXServer
89 {
90 public:
91         // FIXME IN 0.13
92         // Hack! This should be changed in 0.13
93
94         /// The lyx server should not take an argument "LyXFunc" but this is
95         // how it will be done for 0.12. In 0.13 we must write a non-gui
96         // bufferview.
97         // IMO lyxserver is atypical, and for the moment the only one, non-gui
98         // bufferview. We just have to find a way to handle situations like if
99         // lyxserver is using a buffer that is being edited with a bufferview.
100         // With a common buffer list this is not a problem, maybe. (Alejandro)
101         LyXServer(LyXFunc *f, string const &pip)
102                 : numclients(0), func(f), pipes(pip, (this), callback)
103         { }
104         /// 
105         ~LyXServer();
106         ///
107         void notifyClient(string const &);
108 private:
109         ///
110         static void callback(LyXServer *, string const & msg);
111         /// Names and number of current clients
112         enum { MAX_CLIENTS = 10 };
113         string clients[MAX_CLIENTS];
114         int numclients;
115         ///
116         LyXFunc *func;
117         ///
118         LyXComm pipes;
119 };
120
121 #endif /* _LYXSERVER_H_ */
122
123 /* === End of File: lyxserver.h ========================================== */