home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / arts / dispatcher.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  7.0 KB  |  266 lines

  1.     /*
  2.  
  3.     Copyright (C) 2000 Stefan Westerfeld
  4.                        stefan@space.twc.de
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License as published by the Free Software Foundation; either
  9.     version 2 of the License, or (at your option) any later version.
  10.   
  11.     This library is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.     Library General Public License for more details.
  15.    
  16.     You should have received a copy of the GNU Library General Public License
  17.     along with this library; see the file COPYING.LIB.  If not, write to
  18.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19.     Boston, MA 02111-1307, USA.
  20.  
  21.     */
  22.  
  23. #ifndef DISPATCHER_H
  24. #define DISPATCHER_H
  25.  
  26. #include "common.h"
  27. #include "pool.h"
  28. #include "iomanager.h"
  29. #include "tcpserver.h"
  30. #include "unixserver.h"
  31. #include "connection.h"
  32. #include "notification.h"
  33.  
  34. #include <deque>
  35. #include <stack>
  36. #include <vector>
  37. #include <list>
  38. #include "arts_export.h"
  39.  
  40. /*
  41.  * BC - Status (2002-03-08): Dispatcher
  42.  *
  43.  * This is part of the public API (and interacts with generated code) and
  44.  * has to be kept binary compatible.
  45.  */
  46.  
  47. namespace Arts {
  48.  
  49. class ObjectReference;
  50. class FlowSystem_impl;
  51. class GlobalComm;
  52. class InterfaceRepo;
  53. class ObjectManager;
  54. class Object_skel;
  55. class ReferenceClean;
  56. class DelayedReturn;
  57.  
  58. extern "C" {
  59.   typedef void (*mcop_sighandler)(int); 
  60. }
  61.  
  62. class ARTS_EXPORT Dispatcher {
  63. private:
  64.     class DispatcherPrivate *d;
  65.  
  66. protected:
  67.     static Dispatcher *_instance;
  68.  
  69.     Pool<Buffer> requestResultPool;
  70.     Pool<Object_skel> objectPool;
  71.     std::list<Connection *> connections;
  72.  
  73.     std::string serverID;
  74.     void generateServerID();    // helper function
  75.  
  76.     bool deleteIOManagerOnExit;
  77.  
  78.     TCPServer *tcpServer;
  79.     UnixServer *unixServer;
  80.     IOManager *_ioManager;
  81.     FlowSystem_impl *_flowSystem;
  82.     ObjectManager *objectManager;
  83.     ReferenceClean *referenceClean;
  84.     NotificationManager *notificationManager;
  85.  
  86.     mcop_sighandler orig_sigpipe;        // original signal handler for SIG_PIPE
  87.     Connection *_activeConnection;        // internal use only (for refcounting)
  88.  
  89.     /**
  90.      * connects to a given URL
  91.      *
  92.      * @returns a valid connection, or a 0 pointer on error
  93.      */
  94.     Connection *connectUrl(const std::string& url);
  95.  
  96. public:
  97.     enum StartServer {
  98.         noServer = 0,                    // don't be a server
  99.         startUnixServer = 1,            // listen on a unix domain socket
  100.         startTCPServer = 2,                // listen on a tcp port
  101.         noAuthentication = 4            // don't require authentication
  102.     };
  103.  
  104.     Dispatcher(IOManager *ioManager = 0, StartServer startServer = noServer);
  105.     ~Dispatcher();
  106.  
  107.     static Dispatcher *the();
  108.     inline IOManager *ioManager() { return _ioManager; };
  109.     InterfaceRepo interfaceRepo();
  110.     FlowSystem_impl *flowSystem();
  111.     GlobalComm globalComm();
  112.     void setFlowSystem(FlowSystem_impl *fs);
  113.  
  114.     void refillRequestIDs();
  115.  
  116.     // blocking wait for result
  117.     Buffer *waitForResult(long requestID,Connection *connection);
  118.  
  119.     // request creation for oneway and twoway requests
  120.     Buffer *createRequest(long& requestID, long objectID, long methodID);
  121.     Buffer *createOnewayRequest(long objectID, long methodID);
  122.  
  123.     // processes messages
  124.     void handle(Connection *conn, Buffer *buffer, long messageType);
  125.  
  126.     /*
  127.      * special hook to handle corrupt messages
  128.      */
  129.     void handleCorrupt(Connection *conn);
  130.  
  131.     /**
  132.      * object registration
  133.      * 
  134.      * do not call manually, this is used by the Object_skel constructor
  135.      * to register itself
  136.      */
  137.     long addObject(Object_skel *object);
  138.  
  139.     /**
  140.      * object deregistration
  141.      *
  142.      * do not call manually, this is called by the Object_skel destructor
  143.      * as soon as the object is deleted
  144.      */
  145.     void removeObject(long objectID);
  146.  
  147.     /**
  148.      * connects to a local object, returning a readily casted "interface" *
  149.      * if success, or NULL if this object is not local
  150.      */
  151.     void *connectObjectLocal(ObjectReference& reference, const std::string& interface);
  152.  
  153.     /**
  154.      * connects to a remote object, establishing a connection if necessary
  155.      *
  156.      * returns NULL if connecting fails or object isn't present or whatever
  157.      * else could go wrong
  158.      */
  159.     Connection *connectObjectRemote(ObjectReference& reference);
  160.  
  161.     /**
  162.      * stringification of objects (only used by the object implementations,
  163.      * which in turn provide a _toString method)
  164.      */
  165.     std::string objectToString(long objectID);
  166.  
  167.     /**
  168.      * destringification of objects (mainly used by the object implementations,
  169.      * which in turn provide a _fromString method)
  170.      */
  171.     bool stringToObjectReference(ObjectReference& r, const std::string& s);
  172.  
  173.     /**
  174.      * main loop
  175.      */
  176.     void run();
  177.  
  178.     /**
  179.      * this function quits the main loop (it must be running, of course)
  180.      * and causes the run() function to return.
  181.      */
  182.     void terminate();
  183.  
  184.     /**
  185.      * Is called by the transport services as soon as a new connection is
  186.      * created - this will send authentication request, etc.
  187.      */
  188.     void initiateConnection(Connection *connection);
  189.  
  190.     /**
  191.      * Is called by the Connection as soon as the connection goes down,
  192.      * which may happen due to a normal cause (client exits), and may
  193.      * happen if an error occurs as well (network down)
  194.      */
  195.     void handleConnectionClose(Connection *connection);
  196.  
  197.     /**
  198.      * Locks the dispatcher. Whenever you want to do anything with any kind
  199.      * of aRts object, you will hold a lock on Arts::Dispatcher. There is
  200.      * only one exception to the rule, and that is: you don't have to lock
  201.      * the dispatcher when the lock is already held.
  202.      *
  203.      * Generally, that is very often the case. Typical situations where you
  204.      * don't need to lock() the Dispatcher are:
  205.      *
  206.      *  @li you receive a callback from the IOManager (timer or fd)
  207.      *  @li you get call due to some MCOP request
  208.      *  @li you are called from the NotificationManager
  209.      *  @li you are called from the FlowSystem (calculateBlock)
  210.      */
  211.     static void lock();
  212.  
  213.     /**
  214.      * Unlocks the dispatcher. Do this to release a lock you hold on
  215.      * the Arts::Dispatcher.
  216.      */
  217.     static void unlock();
  218.  
  219.     /**
  220.      * Wakes the dispatcher - normally, the dispatcher will go to sleep in
  221.      * it's event loop, when nothing is to be done. If you change things from
  222.      * another thread, like: add a new Timer, write something to a connection,
  223.      * and so on, the dispatcher will not notice it. To wake the dispatcher
  224.      * up, this call is provided.
  225.      */
  226.     static void wakeUp();
  227.  
  228.     /**
  229.      * - internal usage only -
  230.      *
  231.      * this will return the Connection the last request came from
  232.      */
  233.     Connection *activeConnection();
  234.  
  235.     /**
  236.      * - internal usage only -
  237.      *
  238.      * this will return a loopback Connection for sending requests to myself
  239.      */
  240.     Connection *loopbackConnection();
  241.  
  242.     /**
  243.      * - internal usage only -
  244.      *
  245.      * this will cause a function to return from a request later
  246.      * @see DelayedReturn
  247.      */
  248.     DelayedReturn *delayReturn();    
  249.  
  250.     /**
  251.      * - internal usage only -
  252.      *
  253.      * gets object with a given ID
  254.      * @returns the object if successful, 0 otherwise
  255.      */
  256.     Object_skel *getLocalObject(long ID);
  257.  
  258.     /**
  259.      * reloads the trader data (do this if things have been added/changed
  260.      * there)
  261.      */
  262.     void reloadTraderData();
  263. };
  264. }
  265. #endif
  266.