home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / wxwin140 / samples / ipc / client.cc next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  4.4 KB  |  184 lines

  1. /*
  2.  * File:     client.cc
  3.  * Purpose:  Client demo for wxWindows class library
  4.  *
  5.  *                       wxWindows 1.40
  6.  * Copyright (c) 1993 Artificial Intelligence Applications Institute,
  7.  *                   The University of Edinburgh
  8.  *
  9.  *                     Author: Julian Smart
  10.  *                        Date: 18-4-93
  11.  *
  12.  * Permission to use, copy, modify, and distribute this software and its
  13.  * documentation for any purpose is hereby granted without fee, provided
  14.  * that the above copyright notice, author statement and this permission
  15.  * notice appear in all copies of this software and related documentation.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
  18.  * IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  19.  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * IN NO EVENT SHALL THE ARTIFICIAL INTELLIGENCE APPLICATIONS INSTITUTE OR THE
  22.  * UNIVERSITY OF EDINBURGH BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR
  23.  * CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM
  24.  * LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF
  25.  * DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH
  26.  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  */
  28.  
  29. #include <windows.h>
  30. #include "wx.h"
  31. #include "client.h"
  32.  
  33. MyFrame *frame = NULL;
  34.  
  35. // This statement initialises the whole application
  36. MyApp myApp;
  37.  
  38. char ipc_buffer[4000];
  39. wxListBox *the_list = NULL;
  40.  
  41. MyConnection *the_connection = NULL;
  42.  
  43. // The `main program' equivalent, creating the windows and returning the
  44. // main frame
  45. wxFrame *MyApp::OnInit(void)
  46. {
  47.   // Create the main frame window
  48.   frame = new MyFrame(NULL, "Client", 400, 0, 400, 300);
  49.  
  50.   // Give it an icon
  51.   wxIcon *icon = new wxIcon("conn_icn");
  52.   frame->SetIcon(icon);
  53.  
  54.   // Make a menubar
  55.   wxMenu *file_menu = new wxMenu;
  56.  
  57.   file_menu->Append(CLIENT_EXECUTE, "Execute");
  58.   file_menu->Append(CLIENT_REQUEST, "Request");
  59.   file_menu->Append(CLIENT_POKE, "Poke");
  60.   file_menu->Append(CLIENT_QUIT, "Quit");
  61.  
  62.   wxMenuBar *menu_bar = new wxMenuBar;
  63.  
  64.   menu_bar->Append(file_menu, "File");
  65.  
  66.   // Associate the menu bar with the frame
  67.   frame->SetMenuBar(menu_bar);
  68.  
  69.   // Make a panel
  70.   frame->panel = new wxPanel(frame, 0, 0, 400, 250);
  71.   the_list = new wxListBox(frame->panel, (wxFunction)NULL, "A list",
  72.                                   wxSINGLE, -1, -1, 150, 120);
  73.   the_list->Append("Apple");
  74.   the_list->Append("Pear");
  75.   the_list->Append("Orange");
  76.   the_list->Append("Banana");
  77.   the_list->Append("Fruit");
  78.  
  79.   frame->SetSize(400, 0, 400, 300);
  80.  
  81.   // Initialize IPC
  82.   wxIPCInitialize();
  83.  
  84.   char *server = "4242";
  85.   char *host = "bute";
  86.   if (argc > 1)
  87.     server = argv[1];
  88.   if (argc > 2)
  89.     host = argv[2];
  90.  
  91.   // Create a new client
  92.   MyClient *client = new MyClient;
  93.   the_connection = (MyConnection *)client->MakeConnection(host, server, "IPC TEST");
  94.   the_connection->StartAdvise("Item");
  95.  
  96.   frame->Show(TRUE);
  97.  
  98.   // Essential - return the main frame window
  99.   return frame;
  100. }
  101.  
  102. // Define my frame constructor
  103. MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
  104.   wxFrame(frame, title, x, y, w, h)
  105. {
  106.   panel = NULL;
  107. }
  108.  
  109. // Intercept menu commands
  110. void MyFrame::OnMenuCommand(int id)
  111. {
  112.   switch (id)
  113.   {
  114.     case CLIENT_EXECUTE:
  115.     {
  116.       if (the_connection)
  117.         the_connection->Execute("Hello from the client!");
  118.       break;
  119.     }
  120.     case CLIENT_POKE:
  121.     {
  122.       if (the_connection)
  123.         the_connection->Poke("An item", "Some data to poke at the server!");
  124.       break;
  125.     }
  126.     case CLIENT_REQUEST:
  127.     {
  128.       if (the_connection)
  129.       {
  130.         char *data = the_connection->Request("An item");
  131.         if (data)
  132.           wxMessageBox(data, "Client: Request", wxOK);
  133.       }
  134.       break;
  135.     }
  136.     case CLIENT_QUIT:
  137.     {
  138.       OnClose();
  139.       delete this;
  140.       break;
  141.     }
  142.   }
  143. }
  144.  
  145. // Define the behaviour for the frame closing
  146. Bool MyFrame::OnClose(void)
  147. {
  148.   if (the_connection)
  149.   {
  150.     the_connection->Disconnect();
  151.   }
  152.   return TRUE;
  153. }
  154.  
  155. MyClient::MyClient(void)
  156. {
  157. }
  158.  
  159. wxConnection *MyClient::OnMakeConnection(void)
  160. {
  161.   return new MyConnection;
  162. }
  163.  
  164. MyConnection::MyConnection(void):wxConnection(ipc_buffer, 3999)
  165. {
  166. }
  167.  
  168. MyConnection::~MyConnection(void)
  169. {
  170.   the_connection = NULL;
  171. }
  172.  
  173. Bool MyConnection::OnAdvise(char *topic, char *item, char *data, int size, int format)
  174. {
  175.   if (the_list)
  176.   {
  177.     int n = the_list->FindString(data);
  178.     if (n > -1)
  179.       the_list->SetSelection(n);
  180.   }
  181.   return TRUE;
  182. }
  183.  
  184.