* lyx_1_6.py:
[lyx.git] / src / Server.h
blob0c595c861f5c687776603b3c82a02a4fe5605c3f
1 // -*- C++ -*-
2 /**
3 * \file Server.h
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
13 #ifndef SERVER_H
14 #define SERVER_H
16 #include <boost/signals/trackable.hpp>
19 namespace lyx {
21 class LyXFunc;
22 class Server;
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.
32 class LyXComm : public boost::signals::trackable {
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.
38 typedef void (*ClientCallbackfct)(Server *, std::string const &);
40 /// Construct with pipe-basename and callback to receive messages
41 LyXComm(std::string const & pip, Server * cli, ClientCallbackfct ccb = 0);
43 ///
44 ~LyXComm() { closeConnection(); }
46 /// clean up in emergency
47 void emergencyCleanup();
49 /// Send message
50 void send(std::string const &);
52 /// asynch ready-to-be-read notification
53 void read_ready();
55 private:
56 /// the filename of the in pipe
57 std::string const inPipeName() const;
59 /// the filename of the out pipe
60 std::string const outPipeName() const;
62 /// Open pipes
63 void openConnection();
65 /// Close pipes
66 void closeConnection();
68 /// start a pipe
69 int startPipe(std::string const &, bool);
71 /// finish a pipe
72 void endPipe(int &, std::string const &, bool);
74 /// This is -1 if not open
75 int infd_;
77 /// This is -1 if not open
78 int outfd_;
80 /// Are we up and running?
81 bool ready_;
83 /// Base of pipename including path
84 std::string pipename_;
86 /// The client
87 Server * client_;
89 /// The client callback function
90 ClientCallbackfct clientcb_;
94 ///
95 class Server {
96 public:
97 // FIXME IN 0.13
98 // Hack! This should be changed in 0.13
100 // The lyx server should not take an argument "LyXFunc" but this is
101 // how it will be done for 0.12. In 0.13 we must write a non-gui
102 // bufferview.
103 // IMO lyxserver is atypical, and for the moment the only one, non-gui
104 // bufferview. We just have to find a way to handle situations like if
105 // lyxserver is using a buffer that is being edited with a bufferview.
106 // With a common buffer list this is not a problem, maybe. (Alejandro)
108 Server(LyXFunc * f, std::string const & pip);
110 ~Server();
112 void notifyClient(std::string const &);
114 /// whilst crashing etc.
115 void emergencyCleanup() { pipes_.emergencyCleanup(); }
117 void callback(std::string const & msg);
119 private:
120 /// Names and number of current clients
121 enum { MAX_CLIENTS = 10 };
123 std::string clients_[MAX_CLIENTS];
125 int numclients_;
127 LyXFunc * func_;
129 LyXComm pipes_;
132 /// Implementation is in LyX.cpp
133 extern Server & theServer();
136 } // namespace lyx
138 #endif // SERVER_H