IdIPMCastBase.pas 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 10219: IdIPMCastBase.pas
  11. {
  12. { Rev 1.1 4/20/03 1:45:18 PM RLebeau
  13. { Updated IsValidMulticastGroup() to use new TIdStack::GetIPInfo() method.
  14. }
  15. {
  16. { Rev 1.0 2002.11.12 10:43:06 PM czhower
  17. }
  18. unit IdIPMCastBase;
  19. interface
  20. uses
  21. Classes,
  22. IdComponent, IdException, IdGlobal, IdSocketHandle, IdStack {$IFDEF LINUX} ,Libc {$ENDIF};
  23. const
  24. IPMCastLo = 224;
  25. IPMCastHi = 239;
  26. type
  27. TMultiCast = record
  28. IMRMultiAddr : TIdInAddr; // IP multicast address of group */
  29. IMRInterface : TIdInAddr; // local IP address of interface */
  30. end;
  31. TIdIPMCastBase = class(TIdComponent)
  32. protected
  33. FDsgnActive: Boolean;
  34. FMulticastGroup: String;
  35. FPort: Integer;
  36. //
  37. procedure CloseBinding; virtual; abstract;
  38. function GetActive: Boolean; virtual;
  39. function GetBinding: TIdSocketHandle; virtual; abstract;
  40. procedure Loaded; override;
  41. procedure SetActive(const Value: Boolean); virtual;
  42. procedure SetMulticastGroup(const Value: string); virtual;
  43. procedure SetPort(const Value: integer); virtual;
  44. //
  45. property Active: Boolean read GetActive write SetActive Default False;
  46. property MulticastGroup: string read FMulticastGroup write SetMulticastGroup;
  47. property Port: Integer read FPort write SetPort;
  48. public
  49. constructor Create(AOwner: TComponent); override;
  50. function IsValidMulticastGroup(Value: string): Boolean;
  51. published
  52. end;
  53. EIdMCastException = Class(EIdException);
  54. EIdMCastNoBindings = class(EIdMCastException);
  55. EIdMCastNotValidAddress = class(EIdMCastException);
  56. implementation
  57. uses
  58. IdAssignedNumbers,
  59. IdResourceStrings, IdStackConsts,
  60. SysUtils;
  61. { TIdIPMCastBase }
  62. constructor TIdIPMCastBase.Create(AOwner: TComponent);
  63. begin
  64. inherited Create(AOwner);
  65. FMultiCastGroup := Id_IPMC_All_Systems;
  66. end;
  67. function TIdIPMCastBase.GetActive: Boolean;
  68. begin
  69. Result := FDsgnActive;
  70. end;
  71. function TIdIPMCastBase.IsValidMulticastGroup(Value: string): Boolean;
  72. var
  73. ip1: Byte;
  74. begin
  75. Result := false;
  76. if not GStack.GetIPInfo(Value, @ip1) then
  77. Exit;
  78. if ((ip1 < IPMCastLo) or (ip1 > IPMCastHi)) then
  79. Exit;
  80. Result := true;
  81. end;
  82. procedure TIdIPMCastBase.Loaded;
  83. var
  84. b: Boolean;
  85. begin
  86. inherited Loaded;
  87. b := FDsgnActive;
  88. FDsgnActive := False;
  89. Active := b;
  90. end;
  91. procedure TIdIPMCastBase.SetActive(const Value: Boolean);
  92. begin
  93. if Active <> Value then begin
  94. if not ((csDesigning in ComponentState) or (csLoading in ComponentState)) then begin
  95. if Value then begin
  96. GetBinding;
  97. end
  98. else begin
  99. CloseBinding;
  100. end;
  101. end
  102. else begin // don't activate at designtime (or during loading of properties) {Do not Localize}
  103. FDsgnActive := Value;
  104. end;
  105. end;
  106. end;
  107. procedure TIdIPMCastBase.SetMulticastGroup(const Value: string);
  108. begin
  109. if (FMulticastGroup <> Value) then begin
  110. if IsValidMulticastGroup(Value) then
  111. begin
  112. Active := False;
  113. FMulticastGroup := Value;
  114. end
  115. else
  116. begin
  117. Raise EIdMCastNotValidAddress.Create(RSIPMCastInvalidMulticastAddress);
  118. end;
  119. end;
  120. end;
  121. procedure TIdIPMCastBase.SetPort(const Value: integer);
  122. begin
  123. if FPort <> Value then begin
  124. Active := False;
  125. FPort := Value;
  126. end;
  127. end;
  128. end.