home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 March / Chip_1999-03_cd.bin / zkuste / delphi / INFO / DI9807BL.ZIP / Intf / Server / ChatConnection.pas < prev    next >
Pascal/Delphi Source File  |  1998-05-08  |  2KB  |  78 lines

  1. { *****************************************************************************
  2.   Implementing COM Component Callbacks in Delphi
  3.   Code written for Delphi Informant publication
  4.  
  5.   Comments, questions, suggestions?
  6.   Binh Ly, Systems Analyst (bly@brickhouse.com)
  7.   Brickhouse Data Systems (http://www.brickhouse.com)
  8.   *****************************************************************************
  9. }
  10. unit ChatConnection;
  11.  
  12. interface
  13.  
  14. uses
  15.   ComObj, ActiveX, ChatServer_TLB, Classes;
  16.  
  17. type
  18.   TChatConnection = class(TAutoObject, IChatConnection)
  19.   protected
  20.     { IChatConnection }
  21.     function Get_ChatChannel: IChatChannel; safecall;
  22.     procedure BroadcastMessage(const UserName, Message: WideString); safecall;
  23.   protected
  24.     procedure Initialize; override;
  25.     destructor Destroy; override;
  26.   end;
  27.  
  28. const
  29.   { used to keep track of the number of active chat connections }
  30.   ChatConnections : integer = 0;
  31.  
  32. implementation
  33.  
  34. uses
  35.   ComServ,
  36.   ChatChannel
  37.   ;
  38.  
  39. { TChatConnection }
  40.  
  41. procedure TChatConnection.Initialize;
  42. begin
  43.   inherited;
  44.  
  45.   { increment count for every new connection }
  46.   ChatConnections := ChatConnections + 1;
  47. end;
  48.  
  49. destructor TChatConnection.Destroy;
  50. begin
  51.   inherited;
  52.  
  53.   { decrement count when connection terminates }
  54.   ChatConnections := ChatConnections - 1;
  55.  
  56.   { when count does down to 0, we don't need the MainChatChannel instance anymore } 
  57.   if (ChatConnections <= 0) then MainChatChannel := NIL;
  58. end;
  59.  
  60. function TChatConnection.Get_ChatChannel: IChatChannel;
  61. begin
  62.   { returns the main chat channel for all chat connections }
  63.   if (MainChatChannel = NIL) then
  64.     MainChatChannel := TChatChannel.Create;
  65.   Result := MainChatChannel;
  66. end;
  67.  
  68. procedure TChatConnection.BroadcastMessage (const UserName, Message: WideString);
  69. begin
  70.   { called by the client when it wants to broadcast a chat message }
  71.   if (MainChatChannel <> NIL) then
  72.     MainChatChannel.BroadcastMessage (UserName, Message);
  73. end;
  74.  
  75. initialization
  76.   TAutoObjectFactory.Create(ComServer, TChatConnection, Class_ChatConnection, ciMultiInstance);
  77. end.
  78.