shared_code.h

Go to the documentation of this file.
00001 #ifndef RPC_SHARED_CODE_GROUP
00002 #define RPC_SHARED_CODE_GROUP
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : shared code                                                       *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1995-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include <basis/array.h>
00019 #include <basis/istring.h>
00020 #include <basis/portable.h>
00021 #include <geometric/screen_rectangle.h>
00022 #include <mechanisms/semaphore.h>
00023 
00025 
00026 #define DEBUG_RPC
00027   // comment this out for non-debugging version or leave in for debugging.
00028 
00030 
00031 #define RPC_DEBUG_METHOD debugger::delayed
00032   // delayed means that messages will wait in the queue until time can be
00033   // spared to print them.  immediate means they will print when sent.
00034 
00036 
00037 typedef geometric::point<int> rpc_point;
00038   // type of point used here (same size under 16 and 32 bit platforms).
00039 
00041 
00042 // This class is used as a base for classes who want to be alerted to
00043 // the mouse being clicked.
00044 
00045 class mouserciser
00046 {
00047 public:
00048   virtual void mouse_hit(const rpc_point &position) = 0;
00049     // called by the drawing window when the mouse has been
00050     // clicked somewhere on the window.
00051   virtual void update() = 0;
00052     // called by the drawing window when it's time for the points to be updated
00053     // to all clients (only if this is a server).
00054 };
00055 
00057 
00058 // this class is used to capture mouse events and route them to the parent.
00059 
00060 class drawing_window : public CView
00061 {
00062 public:
00063   drawing_window(mouserciser &mouse_alert);
00064     // the "mouse_alert" object will receive notifications when mouse events
00065     // occur.  
00066   virtual ~drawing_window();
00067 
00068   bool Create(CFrameWnd *parent, const istring &title, window_handle alert,
00069         bool locking);
00070     // the "parent" window manages the drawing window.  the "title" is used for
00071     // the window's title.  "alert" is a window to direct debugging messages
00072     // at; it's only used if DEBUG_RPC is turned on.  if "locking" is true,
00073     // then the list will be protected using a semaphore.
00074 
00075   IMPLEMENT_CLASS_NAME("drawing_window");
00076 
00077   void be_gone();
00078     // gets rid of the window in preparation for the controller dying.
00079 
00080   void add_dot(const rpc_point &draw_at);
00081     // plots a blob at the position to "draw_at".
00082 
00083   int length();
00084     // returns the current size of the mouse list.
00085 
00086   void get_mouse_list(array<rpc_point> &current_list);
00087     // returns the "current_list" of points where the user has clicked
00088     // the mouse.
00089   void set_mouse_list(const array<rpc_point> &new_mice);
00090      // resets the mouse list to "new_mice".
00091 
00092   void pull_out_mice(const byte_array &flattened, array<rpc_point> &original);
00093     // returns a list of mouse positions from their flattened form.
00094   void push_in_mice(const array<rpc_point> &to_flatten, byte_array &flattened);
00095     // stuffs a mouse list into an array of bytes.
00096 
00097   const geometric::screen_point dot_size;
00098     // the dots we draw are this big in magnitude.
00099 
00100   void start_timer(UINT interval);
00101     // starts the timer to go off after interval milliseconds.
00102 
00103   void stop_timer();
00104     // stops the operation of the timer.
00105 
00106   CFrameWnd *parent() const { return my_parent; };
00107     // returns the parent frame window.
00108 
00109   // these protect access to the lists.  every lock must be followed by an
00110   // unlock or the program will deadlock.
00111   void lock();
00112   void unlock();
00113 
00114 private:
00115   CFrameWnd *my_parent;  // controller of this drawing window.
00116   window_handle alert;  // debugging window console.
00117   mouserciser &mouse_alert;  // alerted when user adds a point.
00118   array<rpc_point> mice;  // our list of mice.
00119   semaphore locker;  // protects our lists.
00120   bool locking_mode;  // true if semaphore is really used.
00121 
00122   // mfc mechanisms:
00123   DECLARE_MESSAGE_MAP()
00124 
00125   //{{AFX_MSG(drawing_window)
00126   afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
00127   afx_msg void OnClose();
00128   afx_msg void OnTimer(u_int timer_id);
00129     // invoked by a timer message from the example server to help test the
00130     // send_and_receive() function's extended rpc protocol; it sends out the
00131     // point to be updated.
00132   afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
00133   afx_msg void OnPaint();
00134   afx_msg void OnDraw(CDC *pDC);
00135   //}}AFX_MSG
00136 };
00137 
00139 
00140 // strings used for rpc communication.
00141 
00142 #define REGISTER_CLIENT istring("regis")
00143   // in: window handle, out: none.
00144 
00145 #define UNREGISTER_CLIENT istring("kathylee")
00146   // in: window handle, out: none.
00147 
00148 #define NEW_POINT istring("add")
00149   // in: array<rpc_point>, out: none.
00150 
00151 #define GET_LIST istring("get")
00152   // in: none, out: array<rpc_point>.
00153 
00154 #define GET_PARTIAL istring("getfrom")
00155   // in: index, out: array<rpc_point> from index to end. 
00156 
00158 
00159 const int MAXIMUM_POINT_EXCHANGE = 150000;
00160   // the largest chunk of bytes that can hold a list of points.
00161 
00162 const int THREAD_STACK_SIZE = 20000;
00163   // the maximum amount of data allowed on an rpc thread's stack, for both
00164   // client and servers.
00165 
00167 
00168 u_int POINT_ALERT();
00169   // provides a registered message for alerting clients to updates to the
00170   // points list.
00171 
00173 
00174 #define INSTANTIATE_RPC_EXAMPLE \
00175   void crunkerfnurt() { const array<rpc_point> dummy; }
00176 
00178 
00179 #endif
00180 

Generated on Fri Nov 28 04:29:33 2008 for HOOPLE Libraries by  doxygen 1.5.1