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

  1. /*
  2.  * File:     server.cc
  3.  * Purpose:  Server 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 "server.h"
  32.  
  33. MyFrame *frame = NULL;
  34.  
  35. // This statement initialises the whole application
  36. MyApp myApp;
  37.  
  38. char ipc_buffer[4000];
  39. MyConnection *the_connection = NULL;
  40.  
  41. // The `main program' equivalent, creating the windows and returning the
  42. // main frame
  43. wxFrame *MyApp::OnInit(void)
  44. {
  45.   // Create the main frame window
  46.   frame = new MyFrame(NULL, "Server", 0, 0, 400, 500);
  47.  
  48.   // Give it an icon
  49.   wxIcon *icon = new wxIcon("conn_icn");
  50.   frame->SetIcon(icon);
  51.  
  52.   // Make a menubar
  53.   wxMenu *file_menu = new wxMenu;
  54.  
  55.   file_menu->Append(SERVER_QUIT, "Quit");
  56.  
  57.   wxMenuBar *menu_bar = new wxMenuBar;
  58.  
  59.   menu_bar->Append(file_menu, "File");
  60.  
  61.   // Associate the menu bar with the frame
  62.   frame->SetMenuBar(menu_bar);
  63.  
  64.   // Make a panel
  65.   frame->panel = new wxPanel(frame, 0, 0, 400, 250);
  66.   wxListBox *list = new wxListBox(frame->panel, (wxFunction)&list_proc, "A list",
  67.                                   wxSINGLE, -1, -1, 150, 120);
  68.   list->Append("Apple");
  69.   list->Append("Pear");
  70.   list->Append("Orange");
  71.   list->Append("Banana");
  72.   list->Append("Fruit");
  73.  
  74.   frame->SetSize(0, 0, 400, 300);
  75.  
  76.   // Initialize IPC
  77.   wxIPCInitialize();
  78.  
  79.   char *server_name = "4242";
  80.   if (argc > 1)
  81.     server_name = argv[1];
  82.  
  83.   // Create a new server
  84.   MyServer *server = new MyServer;
  85.   server->Create(server_name);
  86.  
  87.   frame->Show(TRUE);
  88.  
  89.   // Return the main frame window
  90.   return frame;
  91. }
  92.  
  93. // Define my frame constructor
  94. MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
  95.   wxFrame(frame, title, x, y, w, h)
  96. {
  97.   panel = NULL;
  98. }
  99.  
  100. // Intercept menu commands
  101. void MyFrame::OnMenuCommand(int id)
  102. {
  103.   switch (id)
  104.   {
  105.     case SERVER_QUIT:
  106.     {
  107.       OnClose();
  108.       delete this;
  109.       break;
  110.     }
  111.   }
  112. }
  113.  
  114. // Define the behaviour for the frame closing
  115. // - must delete all frames except for the main one.
  116. Bool MyFrame::OnClose(void)
  117. {
  118.   return TRUE;
  119. }
  120.  
  121. /*
  122.  * IPC stuff
  123.  *
  124.  */
  125.  
  126. void IPCButtonQuit(wxButton& button, wxEvent& event)
  127. {
  128.   IPCDialogBox *dialog = (IPCDialogBox *)button.GetParent();
  129.   dialog->connection->Disconnect();
  130.   delete dialog->connection;
  131. }
  132.  
  133. IPCDialogBox::IPCDialogBox(wxFrame *Parent, char *Title, Bool Modal, 
  134.                          int x, int y, int width, int height, MyConnection *the_connection):wxDialogBox(Parent, Title,
  135.     Modal, x, y, width, height)
  136. {
  137.   connection = the_connection;
  138.   (void)new wxButton(this, (wxFunction)IPCButtonQuit, "Quit this connection");
  139.   Fit();
  140.   Show(TRUE);
  141. }
  142.  
  143. wxConnection *MyServer::OnAcceptConnection(char *topic)
  144. {
  145.   if (strcmp(topic, "STDIO") != 0 && strcmp(topic, "IPC TEST") == 0)
  146.     return new MyConnection(ipc_buffer, 4000);
  147.   else
  148.     return NULL;
  149. }
  150.  
  151. MyConnection::MyConnection(char *buf, int size):wxConnection(buf, size)
  152. {
  153.   dialog = new IPCDialogBox(frame, "Connection", FALSE, 100, 100, 100, 100, this);
  154.   the_connection = this;
  155. }
  156.  
  157. MyConnection::~MyConnection(void)
  158. {
  159.   delete dialog;
  160.   the_connection = NULL;
  161. }
  162.  
  163. Bool MyConnection::OnExecute(char *topic, char *data, int size, int format)
  164. {
  165.   wxMessageBox(data, "Server: Execute", wxOK);
  166.   return TRUE;
  167. }
  168.  
  169. Bool MyConnection::OnPoke(char *topic, char *item, char *data, int size, int format)
  170. {
  171.   wxMessageBox(data, "Server: Ouch, got a Poke", wxOK);
  172.   return TRUE;
  173. }
  174.  
  175. char *MyConnection::OnRequest(char *topic, char *item, int *size, int format)
  176. {
  177.   return "Here, have your data, client!";
  178. }
  179.  
  180. // Set the client process's listbox to this item
  181. void list_proc(wxListBox& list, wxEvent& event)
  182. {
  183.   char *value = list.GetStringSelection();
  184.   if (the_connection)
  185.   {
  186.     the_connection->Advise("Item", value);
  187.   }
  188. }
  189.