ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/branches/udr/client/FBEvents.pas
Revision: 371
Committed: Wed Jan 5 15:21:22 2022 UTC (2 years, 3 months ago) by tony
Content type: text/x-pascal
File size: 12469 byte(s)
Log Message:
Beta Release 0.1

File Contents

# User Rev Content
1 tony 45 (*
2     * Firebird Interface (fbintf). The fbintf components provide a set of
3     * Pascal language bindings for the Firebird API. Although predominantly
4     * a new development they include source code taken from IBX and may be
5     * considered a derived product. This software thus also includes the copyright
6     * notice and license conditions from IBX.
7     *
8     * Except for those parts dervied from IBX, contents of this file are subject
9     * to the Initial Developer's Public License Version 1.0 (the "License"); you
10     * may not use this file except in compliance with the License. You may obtain a
11     * copy of the License here:
12     *
13     * http://www.firebirdsql.org/index.php?op=doc&id=idpl
14     *
15     * Software distributed under the License is distributed on an "AS
16     * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17     * implied. See the License for the specific language governing rights
18     * and limitations under the License.
19     *
20     * The Initial Developer of the Original Code is Tony Whyman.
21     *
22     * The Original Code is (C) 2016 Tony Whyman, MWA Software
23     * (http://www.mwasoftware.co.uk).
24     *
25     * All Rights Reserved.
26     *
27     * Contributor(s): ______________________________________.
28     *
29     *)
30     {************************************************************************}
31     { }
32     { Borland Delphi Visual Component Library }
33     { InterBase Express core components }
34     { }
35     { Copyright (c) 1998-2000 Inprise Corporation }
36     { }
37     { InterBase Express is based in part on the product }
38     { Free IB Components, written by Gregory H. Deatz for }
39     { Hoagland, Longo, Moran, Dunst & Doukas Company. }
40     { Free IB Components is used under license. }
41     { }
42     { The contents of this file are subject to the InterBase }
43     { Public License Version 1.0 (the "License"); you may not }
44     { use this file except in compliance with the License. You }
45     { may obtain a copy of the License at http://www.Inprise.com/IPL.html }
46     { Software distributed under the License is distributed on }
47     { an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either }
48     { express or implied. See the License for the specific language }
49     { governing rights and limitations under the License. }
50     { The Original Code was created by InterBase Software Corporation }
51     { and its successors. }
52     { Portions created by Inprise Corporation are Copyright (C) Inprise }
53     { Corporation. All Rights Reserved. }
54     { Contributor(s): Jeff Overcash }
55     { }
56     { IBX For Lazarus (Firebird Express) }
57     { Contributor: Tony Whyman, MWA Software http://www.mwasoftware.co.uk }
58     { Portions created by MWA Software are copyright McCallum Whyman }
59     { Associates Ltd 2011 - 2015 }
60     { }
61     {************************************************************************}
62     unit FBEvents;
63 tony 56 {$IFDEF MSWINDOWS}
64     {$DEFINE WINDOWS}
65     {$ENDIF}
66 tony 45
67     {$IFDEF FPC}
68 tony 56 {$mode delphi}
69 tony 45 {$interfaces COM}
70     {$ENDIF}
71    
72     interface
73    
74     uses
75     Classes, SysUtils, IB, FBClientAPI, syncobjs, FBActivityMonitor;
76    
77     type
78    
79     { TFBEvents }
80    
81 tony 371 {Firebird Event and Result buffer syntax is:
82    
83     record
84     version: byte;
85     event: array of packed record
86     strlen: byte;
87     strchars: array of AnsiChar; //no of chars given by strlen
88     EventCounts: long;
89     end;
90     end;
91    
92     }
93    
94 tony 45 TFBEvents = class(TActivityReporter)
95     private
96     FEvents: TStringList;
97     FAttachment: IAttachment;
98 tony 47 FEventCounts: TEventCounts;
99 tony 263 FFirebirdClientAPI: TFBClientAPI;
100 tony 45 protected
101 tony 56 FEventBuffer: PByte;
102 tony 45 FEventBufferLen: integer;
103 tony 56 FResultBuffer: PByte;
104 tony 45 FEventHandler: TEventHandler;
105     FCriticalSection: TCriticalSection;
106     FInWaitState: boolean;
107     procedure CreateEventBlock;
108     procedure CancelEvents(Force: boolean = false); virtual;
109     procedure EventSignaled;
110     function GetIEvents: IEvents; virtual; abstract;
111 tony 47 procedure ProcessEventCounts;
112 tony 45 public
113 tony 371 const EPB_version1 = 1;
114     public
115 tony 45 constructor Create(DBAttachment: IAttachment; aMonitor: IActivityMonitor; Events: TStrings);
116     destructor Destroy; override;
117    
118     {IEvents}
119     procedure GetEvents(EventNames: TStrings);
120     procedure SetEvents(EventNames: TStrings); overload;
121 tony 56 procedure SetEvents(Event: AnsiString); overload;
122 tony 45 procedure Cancel;
123     function ExtractEventCounts: TEventCounts;
124     function GetAttachment: IAttachment;
125 tony 47 procedure AsyncWaitForEvent(EventHandler: TEventHandler); virtual; abstract;
126 tony 45 end;
127    
128    
129     implementation
130    
131     uses FBMessages, IBExternals;
132    
133     const
134     MaxEvents = 15;
135    
136     { TFBEvents }
137    
138 tony 371 (* Original Firebird 'C' code
139    
140     SLONG API_ROUTINE_VARARG isc_event_block(UCHAR** event_buffer,
141     UCHAR** result_buffer,
142     USHORT count, ...)
143     {
144     /**************************************
145     *
146     * i s c _ e v e n t _ b l o c k
147     *
148     **************************************
149     *
150     * Functional description
151     * Create an initialized event parameter block from a
152     * variable number of input arguments.
153     * Return the size of the block.
154     *
155     * Return 0 if any error occurs.
156     *
157     **************************************/
158     va_list ptr;
159    
160     va_start(ptr, count);
161    
162     // calculate length of event parameter block, setting initial length to include version
163     // and counts for each argument
164    
165     SLONG length = 1;
166     USHORT i = count;
167     while (i--)
168     {
169     const char* q = va_arg(ptr, SCHAR * );
170     length += static_cast<SLONG>(strlen(q)) + 5;
171     }
172     va_end(ptr);
173    
174     UCHAR* p = *event_buffer = (UCHAR * ) gds__alloc((SLONG) length);
175     // FREE: apparently never freed
176     if (!*event_buffer) // NOMEM:
177     return 0;
178     if ((*result_buffer = (UCHAR * ) gds__alloc((SLONG) length)) == NULL)
179     {
180     // NOMEM:
181     // FREE: apparently never freed
182     gds__free(*event_buffer);
183     *event_buffer = NULL;
184     return 0;
185     }
186    
187     // initialize the block with event names and counts
188    
189     *p++ = EPB_version1;
190    
191     va_start(ptr, count);
192    
193     i = count;
194     while (i--)
195     {
196     const char* q = va_arg(ptr, SCHAR * );
197    
198     // Strip the blanks from the ends
199     const char* end = q + strlen(q);
200     while (--end >= q && *end == ' ')
201     ;
202     *p++ = end - q + 1;
203     while (q <= end)
204     *p++ = *q++;
205     *p++ = 0;
206     *p++ = 0;
207     *p++ = 0;
208     *p++ = 0;
209     }
210     va_end(ptr);
211    
212     return static_cast<SLONG>(p - *event_buffer);
213     }
214     *)
215    
216     {CreateEventBlock effectively replaces isc_event_block}
217    
218 tony 45 procedure TFBEvents.CreateEventBlock;
219 tony 371 var i: integer;
220     P: PByte;
221 tony 45 begin
222 tony 371 {calculate length of event parameter block, setting initial length to include version
223     and counts for each argument}
224    
225     FEventBufferLen := 1;
226     for i := 0 to FEvents.Count - 1 do
227     FEventBufferLen := FEventBufferLen + length(FEvents[i]) + 1 + sizeof(Long);
228    
229 tony 263 with FFirebirdClientAPI do
230 tony 45 begin
231 tony 371 IBAlloc(FEventBuffer,0,FEventBufferLen);
232     if FEventBuffer = nil then Exit;
233     {$if declared(FillByte)}
234     FillByte(FEventBuffer^,FEventBufferLen,0);
235     {$else}
236     FillChar(FEventBuffer^,FEventBufferLen,0);
237     {$ifend}
238     IBAlloc(FResultBuffer,0,FEventBufferLen);
239     if FResultBuffer = nil then
240     begin
241     FreeMem(FEventBuffer);
242     Exit;
243     end;
244 tony 45
245 tony 371 P := FEventBuffer;
246     P^ := EPB_version1;
247     Inc(P);
248     SetLength(FEventCounts,FEvents.Count);
249 tony 45
250 tony 371 for i := 0 to FEvents.Count - 1 do
251     begin
252     P^ := Length(FEvents[i]);
253     Inc(P);
254     Move(FEvents[i][1],P^,Length(FEvents[i]));
255     Inc(P,Length(FEvents[i])+sizeof(Long));
256     FEventCounts[i].EventName := FEvents[i];
257 tony 45 end;
258     end;
259 tony 371
260     { for i := 0 to FEventBufferLen - 1 do
261     write(Format('%x ', [FEventBuffer[i]]));
262     writeln;}
263 tony 45 end;
264    
265     procedure TFBEvents.CancelEvents(Force: boolean);
266     begin
267     FEventHandler := nil;
268     end;
269    
270     procedure TFBEvents.EventSignaled;
271     var Handler: TEventHandler;
272     begin
273 tony 47 Handler := nil;
274 tony 45 FCriticalSection.Enter;
275     try
276     if not FInWaitState then Exit;
277     FInWaitState := false;
278 tony 47 ProcessEventCounts;
279 tony 45 if assigned(FEventHandler) then
280     begin
281     Handler := FEventHandler;
282     FEventHandler := nil;
283     end;
284     finally
285 tony 47 FCriticalSection.Leave;
286 tony 45 end;
287 tony 47 if assigned(Handler) then
288     Handler(GetIEvents);
289 tony 45 end;
290    
291 tony 371 (*
292     Original Firebird 'C' code for isc_event_counts
293    
294     void API_ROUTINE isc_event_counts(ULONG* result_vector,
295     SSHORT buffer_length,
296     UCHAR* event_buffer,
297     const UCHAR* result_buffer)
298     {
299     /**************************************
300     *
301     * g d s _ $ e v e n t _ c o u n t s
302     *
303     **************************************
304     *
305     * Functional description
306     * Get the delta between two events in an event
307     * parameter block. Used to update gds_events
308     * for GPRE support of events.
309     *
310     **************************************/
311     ULONG* vec = result_vector;
312     const UCHAR* p = event_buffer;
313     const UCHAR* q = result_buffer;
314     USHORT length = buffer_length;
315     const UCHAR* const end = p + length;
316    
317     // analyze the event blocks, getting the delta for each event
318    
319     p++;
320     q++;
321     while (p < end)
322     {
323     // skip over the event name
324    
325     const USHORT i = (USHORT)* p++;
326     p += i;
327     q += i + 1;
328    
329     // get the change in count
330    
331     const ULONG initial_count = gds__vax_integer(p, sizeof(SLONG));
332     p += sizeof(SLONG);
333     const ULONG new_count = gds__vax_integer(q, sizeof(SLONG));
334     q += sizeof(SLONG);
335     *vec++ = new_count - initial_count;
336     }
337    
338     // copy over the result to the initial block to prepare
339     // for the next call to gds__event_wait
340    
341     memcpy(event_buffer, result_buffer, length);
342     }
343     *)
344    
345     {ProcessEventCounts effectively replaces isc_event_counts}
346    
347 tony 47 procedure TFBEvents.ProcessEventCounts;
348 tony 371
349     var i: integer;
350     P, Q: PByte;
351     initial_count: Long;
352     new_count: Long;
353     len: byte;
354 tony 47 begin
355 tony 371 P := FEventBuffer;
356     Q := FResultBuffer;
357     Inc(P); {skip past version byte}
358     Inc(Q);
359     for i := 0 to Length(FEventCounts) - 1 do
360 tony 263 with FFirebirdClientAPI do
361 tony 47 begin
362 tony 371 {skip over the event name}
363     len := P^;
364     P := P + len + 1;
365     Q := Q + len + 1; {event name length in P^}
366     initial_count := DecodeInteger(P,sizeof(Long));
367     Inc(P,sizeof(Long));
368     new_count := DecodeInteger(Q,sizeof(Long));
369     Inc(Q,sizeof(Long));
370     FEventCounts[i].Count := new_count - initial_count;
371 tony 47 end;
372 tony 371 Move(FResultBuffer^,FEventBuffer^,FEventBufferLen);
373 tony 47 end;
374    
375 tony 45 constructor TFBEvents.Create(DBAttachment: IAttachment;
376     aMonitor: IActivityMonitor; Events: TStrings);
377     begin
378     inherited Create(aMonitor);
379     FAttachment := DBAttachment;
380 tony 263 FFirebirdClientAPI := DBAttachment.getFirebirdAPI as TFBClientAPI;
381 tony 45 if Events.Count > MaxEvents then
382     IBError(ibxeMaximumEvents, [nil]);
383    
384     FCriticalSection := TCriticalSection.Create;
385     FEvents := TStringList.Create;
386     FEvents.Assign(Events);
387     CreateEventBlock;
388     end;
389    
390     destructor TFBEvents.Destroy;
391     begin
392     if assigned(FCriticalSection) then FCriticalSection.Free;
393     if assigned(FEvents) then FEvents.Free;
394 tony 263 with FFirebirdClientAPI do
395 tony 45 begin
396     if FEventBuffer <> nil then
397 tony 371 FreeMem( FEventBuffer);
398 tony 45 if FResultBuffer <> nil then
399 tony 371 FreeMem( FResultBuffer);
400 tony 45 end;
401     inherited Destroy;
402     end;
403    
404     procedure TFBEvents.GetEvents(EventNames: TStrings);
405     begin
406     EventNames.Assign(FEvents)
407     end;
408    
409     procedure TFBEvents.SetEvents(EventNames: TStrings);
410 tony 371 var i: integer;
411 tony 45 begin
412 tony 263 {$ifdef Unix}
413 tony 217 if (EventNames.Count > 0) and not IsMultiThread then
414 tony 221 IBError(ibxeMultiThreadRequired,['Firebird Events Handling']);
415 tony 263 {$endif}
416 tony 45 if EventNames.Text <> FEvents.Text then
417     begin
418     Cancel;
419 tony 371 for i := 0 to EventNames.Count - 1 do
420     FEvents[i] := Trim(EventNames[i]);
421 tony 45 CreateEventBlock;
422     end;
423     end;
424    
425 tony 56 procedure TFBEvents.SetEvents(Event: AnsiString);
426 tony 45 var S: TStringList;
427     begin
428     S := TStringList.Create;
429     try
430     S.Add(Event);
431     SetEvents(S);
432     finally
433     S.Free;
434     end;
435     end;
436    
437     procedure TFBEvents.Cancel;
438     begin
439     if assigned(FEventHandler) then
440     CancelEvents;
441     end;
442    
443     function TFBEvents.ExtractEventCounts: TEventCounts;
444     begin
445 tony 47 Result := FEventCounts;
446 tony 45 end;
447    
448     function TFBEvents.GetAttachment: IAttachment;
449     begin
450     Result := FAttachment;
451     end;
452    
453     end.
454