This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
/
wmq_structs.erb
341 lines (299 loc) · 12.3 KB
/
wmq_structs.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<%
symbols = {
'descriptor=' => nil,
'headers=' => nil,
'data=' => nil,
'header_type' => nil,
'to_s' => nil,
'xml' => nil,
'name_value' => nil,
}
wmq_structs = [
# Message Descriptor
{ file: 'cmqc.h', struct: 'MQMD' },
# Message Descriptor 1
{ file: 'cmqc.h', struct: 'MQMD1', other_keys: [:remote_q_name, :remote_q_mgr_name] },
# Rules and formatting header2
{ file: 'cmqc.h', struct: 'MQRFH2', header: 'rf_header_2', struct_id: 'MQRFH_STRUC_ID',
other_keys: [:xml],
custom: true }, # Implement custom Message_build_rf_header_2 and Message_deblock_rf_header_2
# Rules and formatting header
{ file: 'cmqc.h', struct: 'MQRFH', header: 'rf_header', other_keys: [:name_value],
custom: true }, # Implement custom Message_build_rf_header and Message_deblock_rf_header
# Dead Letter Header
{ file: 'cmqc.h', struct: 'MQDLH', header: 'dead_letter_header',
defaults: 'MQDLH_DEF.CodedCharSetId = MQCCSI_INHERIT;' },
# CICS bridge header
{ file: 'cmqc.h', struct: 'MQCIH', header: 'cics' },
# Distribution header
{ file: 'cmqc.h', struct: 'MQDH', header: 'dist_header' },
# IMS information header
{ file: 'cmqc.h', struct: 'MQIIH', header: 'ims' },
# Reference message header (send large files over channels)
{ file: 'cmqc.h', struct: 'MQRMH', header: 'ref_msg_header' },
# Trigger Message
{ file: 'cmqc.h', struct: 'MQTM', header: 'trigger', format: false },
# Trigger Message 2 (character format)
{ file: 'cmqc.h', struct: 'MQTMC2' },
# Work Information header
{ file: 'cmqc.h', struct: 'MQWIH', header: 'work_info_header' },
# Transmission-queue header - Todo: Need to deal with MQMDE
{ file: 'cmqc.h', struct: 'MQXQH', header: 'xmit_q_header', format: 'MsgDesc.Format' },
]
wmq_structs.each do |struct|
# Parse WebSphere MQ 'C' Header file and extract elements
elements = extract_struct(@path+'/'+struct[:file], struct[:struct])
struct[:elements] = elements
# Add symbol for each struct name
symbols[struct[:header]]=nil if struct[:header]
# Add symbols for each element in C Struct
elements.each do |item|
symbols[rubyize_name(item[1])] = nil unless @@field_ignore_list.include?(item[1])
end
end
%>
/* --------------------------------------------------------------------------
*
* WARNING: DO NOT MODIFY THIS FILE
*
* This file was generated by generate_structs.rb.
*
* --------------------------------------------------------------------------*/
#include "wmq.h"
/* --------------------------------------------------------------------------
* Static's to hold Symbols
* --------------------------------------------------------------------------*/
<%
header_file = 'cmqc.h'
struct = 'MQMD'
# Define static variables to hold Ruby symbols
key_list = symbols.keys.sort
key_list.each do |key, value|
%><%="static ID ID_#{key.sub('=', '_set')};\n" %><%
end
# initialize symbols
%>
/* --------------------------------------------------------------------------
* Initialize Symbols
* --------------------------------------------------------------------------*/
void wmq_structs_id_init()
{
<% key_list.each do |key, value|
%><%=" ID_%-20s = rb_intern(\"%s\");\n"% [key.sub('=', '_set'), key] %><%
end
%>}
<%
# Generate functions to move to/from a Ruby Hash of the symbols defined above
wmq_structs.each do |struct|
elements = struct[:elements]
struct_name = struct[:struct]
variable = 'p'+struct_name.downcase
%>
/* --------------------------------------------------------------------------
* Convert between <%=struct_name%> and Hash
* --------------------------------------------------------------------------*/
void Message_from_<%=struct_name.downcase%>(VALUE hash, <%=struct_name%>* <%=variable%>)
{
VALUE str;
size_t size;
size_t length;
size_t i;
char* pChar;
<%
elements.each do |item|
type = item[0]
name = item[1]
next if @@field_ignore_list.include?(name) ||
(name=='Format' && struct[:header])
match = /(MQ\D+)/.match(type)
type = "#{match[1]}S" if match[1] != type
%><%= if type == 'MQMDS'
" %-16s(hash, &#{variable}->%s);\n" % ['Message_from_mqmd1', name]
else
" %-16s(hash, %-30s #{variable}->%s)\n" % ["WMQ_#{type}2HASH", rubyize_name(name)+',',name]
end %><%
end
%>}
static int Message_to_<%=struct_name.downcase%>_each (VALUE key, VALUE value, <%=struct_name%>* p<%=struct_name.downcase%>)
{
VALUE str;
size_t size;
size_t length;
VALUE val;
ID id = rb_to_id(key);
<% contains = nil
elements.each do |item|
type = item[0]
name = item[1]
next if @@field_ignore_list.include?(name) ||
(name=='Format' && struct[:header])
match = /(MQ\D+)/.match(type)
type = "#{match[1]}S" if match[1] != type
%><%= if type == 'MQMDS'
contains = 'mqmd1'
""
else
"%-35s { %-16s(value,#{variable}->#{name}) }\n else" % [" if(id == ID_#{rubyize_name(name)})", "WMQ_STR2#{type}"]
end%><%
end
if struct[:other_keys]
struct[:other_keys].each do |key|
%> if(id == ID_<%=key.to_s%>) {}
else<% end
end
%><%=struct[:header]?" if(id != ID_header_type)":""%><%
if contains
%> {
Message_to_<%= contains%>_each(key, value, &pmqxqh->MsgDesc);
}<%
else%>
{
val = rb_funcall(key, ID_to_s, 0);
rb_raise(rb_eArgError, "WMQ::Message#to_<%=struct_name.downcase%> Unknown symbol :%s supplied", RSTRING_PTR(val));
}
<% end%>
return 0;
}
void Message_to_<%=struct_name.downcase%>(VALUE hash, <%=struct_name%>* p<%=struct_name.downcase%>)
{
rb_hash_foreach(hash, Message_to_<%=struct_name.downcase%>_each, (VALUE)p<%=struct_name.downcase%>);
}
<% end # wmq_structs.each
%>
/* --------------------------------------------------------------------------
* Extract message data and headers
* --------------------------------------------------------------------------*/
void Message_deblock(VALUE self, PMQMD pmqmd, PMQBYTE p_buffer, MQLONG total_length, MQLONG trace_level)
{
PMQCHAR p_format = pmqmd->Format; /* Start with format in MQMD */
PMQBYTE p_data = p_buffer; /* Pointer to start of data */
MQLONG data_length= total_length; /* length of data portion */
VALUE headers = rb_ary_new();
VALUE descriptor = rb_hash_new();
MQLONG size = 0;
while (p_format)
{
<%
wmq_structs.each do |struct|
if struct[:header]
%> /* <%=struct[:struct]%>: <%=struct[:header]%> */
if(strncmp(p_format, MQFMT_<%=struct[:header].upcase%>, MQ_FORMAT_LENGTH) == 0)
{
VALUE hash = rb_hash_new();
P<%=struct[:struct]%> p_header = (P<%=struct[:struct]%>)p_data;
if(trace_level>2)
printf("WMQ::Message#deblock Found <%=struct[:header]%>\n");
if(memcmp(p_header->StrucId, <%=struct[:struct_id] || "#{struct[:struct].upcase}_STRUC_ID"%>, sizeof(p_header->StrucId)) != 0)
{
if(trace_level>1)
printf("WMQ::Message#deblock MQFMT_<%=struct[:header].upcase%> received, but message does not contain <%=struct[:struct].upcase%>\n");
break; /* Bad Message received, do not deblock headers */
}
else
{
Message_from_<%=struct[:struct].downcase%>(hash, p_header);
rb_hash_aset(hash, ID2SYM(ID_header_type), ID2SYM(ID_<%=struct[:header]%>));
rb_ary_push(headers, hash);
size = <%=if struct[:custom] then
"Message_deblock_#{struct[:header]} (hash, p_data, data_length);\n"+
" if (!size) break; /* Poison Message */"
elsif struct[:elements].include?(['MQLONG', 'StrucLength'])
'p_header->StrucLength;'
else
"sizeof(#{struct[:struct]});"
end %>
p_data += size;
data_length -= size;
<%=if struct[:format] == nil
'p_format = p_header->Format'
elsif struct[:format] == false
'break'
else
"p_format = p_header->#{struct[:format]}"
end %>;
}
}
else
<%
end
end # wmq_structs.each
%> {
break;
}
}
/* Copy the last recognised header found to the Descriptor */
if(p_format && p_format != pmqmd->Format)
{
strncpy(pmqmd->Format, p_format, MQ_FORMAT_LENGTH);
}
Message_from_mqmd(descriptor, pmqmd);
rb_funcall(self, ID_descriptor_set, 1, descriptor);
rb_funcall(self, ID_headers_set, 1, headers);
rb_funcall(self, ID_data_set, 1, rb_str_new(p_data, data_length));
}
void Message_build_set_format(ID header_type, PMQBYTE p_format)
{
<%
wmq_structs.each do |struct|
if struct[:header]
%> if(header_type == ID_<%=struct[:header]%>) { memcpy(p_format, MQFMT_<%=struct[:header].upcase%>, MQ_FORMAT_LENGTH); return;}
<%
end
end # wmq_structs.each
%>
rb_raise(rb_eArgError, "Invalid/Unknown header_type supplied in WMQ::Message#headers array");
}
/* --------------------------------------------------------------------------
* Build message headers
* --------------------------------------------------------------------------*/
int Message_build_header (VALUE hash, struct Message_build_header_arg* parg)
{
VALUE val = rb_hash_aref(hash, ID2SYM(ID_header_type));
PMQBYTE p_data = 0;
if (!NIL_P(val) && (TYPE(val) == T_SYMBOL))
{
ID header_id = rb_to_id(val);
<%
wmq_structs.each do |struct|
if struct[:header]
%> if (header_id == ID_<%=struct[:header]%>) /* Build <%=struct[:struct]%> */
{ <%
if struct[:custom] %>
Message_build_<%=struct[:header]%> (hash, parg);
<% else %>
static <%=struct[:struct]%> <%=struct[:struct]%>_DEF = {<%=struct[:struct]%>_DEFAULT};
<%=struct[:defaults]%>
if(parg->trace_level>2)
printf ("WMQ::Message#build_header Found <%=struct[:header]%>\n");
p_data = Message_autogrow_data_buffer(parg, sizeof(<%=struct[:struct]%>));
memcpy(p_data, &<%=struct[:struct]%>_DEF, sizeof(<%=struct[:struct]%>));
Message_to_<%=struct[:struct].downcase%>(hash, (P<%=struct[:struct]%>)p_data);
<% if struct[:format] != false%>
if(parg->next_header_id)
{
Message_build_set_format(parg->next_header_id, ((P<%=struct[:struct]%>)p_data)-><%=struct[:format] || 'Format'%>);
}
else
{
memcpy(((P<%=struct[:struct]%>)p_data)-><%=struct[:format] || 'Format'%>, parg->data_format, MQ_FORMAT_LENGTH);
}
<% end %>
*(parg->p_data_offset) += sizeof(<%=struct[:struct]%>);
if(parg->trace_level>2)
printf ("WMQ::Message#build_header data offset:%ld\n", (long)(*(parg->p_data_offset)));
<% end
%> }
else
<%
end
end # wmq_structs.each
%> {
rb_raise(rb_eArgError, "Unknown :header_type supplied in WMQ::Message#headers array");
}
}
else
{
rb_raise(rb_eArgError, "Mandatory parameter :header_type missing from header entry in WMQ::Message#headers array");
}
return 0; /* Continue */
}