Back to index

texmacs  1.0.7.15
tm_link.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : tm_link.hpp
00004 * DESCRIPTION: Links between TeXmacs and extern programs
00005 * COPYRIGHT  : (C) 2000  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #ifndef TM_LINK_H
00013 #define TM_LINK_H
00014 #include "tree.hpp"
00015 #include "command.hpp"
00016 
00017 #define CONNECTION_DEAD    0
00018 #define CONNECTION_DYING   1
00019 #define WAITING_FOR_INPUT  2
00020 #define WAITING_FOR_OUTPUT 3
00021 
00022 #define DATA_BEGIN   ((char) 2)
00023 #define DATA_END     ((char) 5)
00024 #define DATA_COMMAND ((char) 16)
00025 #define DATA_ESCAPE  ((char) 27)
00026 
00027 #define LINK_IN   0
00028 #define LINK_OUT  0
00029 #define LINK_ERR  1
00030 
00031 #define SOCKET_DEFAULT  0
00032 #define SOCKET_CLIENT   1
00033 #define SOCKET_SERVER   2
00034 
00035 /******************************************************************************
00036 * The tm_link class
00037 ******************************************************************************/
00038 
00039 struct tm_link_rep: abstract_struct {
00040   bool   alive;   // link is alive
00041   string secret;  // empty string or secret key for encrypted connections
00042 
00043   command feed_cmd; // called when async data available
00044   
00045 public:
00046   inline tm_link_rep () {}
00047   inline virtual ~tm_link_rep () {}
00048 
00049   virtual string  start () = 0;
00050   virtual void    write (string s, int channel) = 0;
00051   virtual string& watch (int channel) = 0;
00052   virtual string  read (int channel) = 0;
00053   virtual void    listen (int msecs) = 0;
00054   virtual void    interrupt () = 0;
00055   virtual void    stop () = 0;
00056 
00057   void write_packet (string s, int channel);
00058   bool complete_packet (int channel);
00059   string read_packet (int channel, int timeout, bool& success);
00060   void secure_server (string cmd);
00061   void secure_client ();
00062 
00063   void set_command (command _cmd) { feed_cmd = _cmd; }
00064   void apply_command () { if (!is_nil (feed_cmd)) feed_cmd->apply (); }
00065   
00066   friend class tm_link;
00067 };
00068 
00069 class tm_link {
00070 public:
00071   ABSTRACT_NULL(tm_link);
00072   inline bool operator == (tm_link l);
00073   inline bool operator != (tm_link l);
00074 };
00075 
00076 ABSTRACT_NULL_CODE(tm_link);
00077 inline bool tm_link::operator == (tm_link l) { return rep == l.rep; }
00078 inline bool tm_link::operator != (tm_link l) { return rep != l.rep; }
00079 
00080 tm_link make_pipe_link (string cmd);
00081 tm_link make_dynamic_link (string lib, string symb, string init, string ses);
00082 tm_link make_socket_link (string h, int p, int t= SOCKET_DEFAULT, int fd= -1);
00083 tm_link make_socket_server (int port);
00084 tm_link find_socket_link (int fd);
00085 
00086 void close_all_pipes ();
00087 void process_all_pipes ();
00088 void close_all_sockets ();
00089 void close_all_servers ();
00090 int  number_of_servers ();
00091 
00092 #endif // TM_LINK_H