service.cc
14 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
* "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
* (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
* aplicativos mainframe. Registro no INPI sob o nome G3270.
*
* Copyright (C) <2008> <Banco do Brasil S.A.>
*
* Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
* os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
* Free Software Foundation.
*
* Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
* GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
* A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
* obter mais detalhes.
*
* Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
* programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
* St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Este programa está nomeado como service.cc e possui - linhas de código.
*
* Contatos:
*
* perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
*
*/
#include <config.h>
#ifdef HAVE_DBUS
#include <iostream>
#include <stdio.h>
#include <dbus/dbus.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <unistd.h>
#include <limits.h>
#ifndef DBUS_TIMEOUT_INFINITE
#define DBUS_TIMEOUT_INFINITE ((int) 0x7fffffff)
#endif // !DBUS_TIMEOUT_INFINITE
#include <pw3270cpp.h>
#include <lib3270/log.h>
/*--[ Implement ]--------------------------------------------------------------------------------------------------*/
namespace PW3270_NAMESPACE
{
DBusConnection * conn;
class client : public session
{
private:
#define DBUS_DESTINATION "br.com.bb.pw3270.service"
#define DBUS_PATH "/br/com/bb/pw3270/service"
#define DBUS_INTERFACE "br.com.bb.pw3270.service"
DBusConnection * conn;
string name;
const char * id;
DBusMessage * createMessage(const char *method)
{
DBusMessage * msg = dbus_message_new_method_call( DBUS_DESTINATION, // Destination
DBUS_PATH, // Path
DBUS_INTERFACE, // Interface
method); // method
if (!msg)
throw exception("Error creating DBUS message for method %s",method);
return msg;
}
DBusMessage * call(DBusMessage *msg)
{
DBusMessage * reply;
DBusError error;
dbus_error_init(&error);
reply = dbus_connection_send_with_reply_and_block(conn,msg,10000,&error);
dbus_message_unref(msg);
if(dbus_error_is_set(&error))
{
string msg = error.message;
dbus_error_free(&error);
throw exception("%s",msg.c_str());
}
if(!reply)
{
throw exception("No reply for %s message","DBUS");
}
return reply;
}
string getString(DBusMessage *msg)
{
string rc;
if(msg)
{
DBusMessageIter iter;
if(dbus_message_iter_init(msg, &iter))
{
if(dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
{
const char * str;
dbus_message_iter_get_basic(&iter, &str);
trace("Response: [%s]",str);
rc.assign(str);
dbus_message_unref(msg);
return rc;
}
exception e = exception("DBUS Return type was %c, expecting %c",dbus_message_iter_get_arg_type(&iter),DBUS_TYPE_INT32);
dbus_message_unref(msg);
throw e;
}
}
return rc;
}
int getInteger(DBusMessage *msg) {
if(msg)
{
DBusMessageIter iter;
if(dbus_message_iter_init(msg, &iter))
{
if(dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_INT32)
{
dbus_int32_t iSigned;
dbus_message_iter_get_basic(&iter, &iSigned);
dbus_message_unref(msg);
return (int) iSigned;
}
exception e = exception("DBUS Return type was %c, expecting %c",dbus_message_iter_get_arg_type(&iter),DBUS_TYPE_INT32);
dbus_message_unref(msg);
throw e;
}
dbus_message_unref(msg);
}
return -1;
}
string getString(const char *method)
{
return getString(call(createMessage(method)));
}
int getInteger(const char *method)
{
return getInteger(call(createMessage(method)));
}
int getInteger(const char *method, int first_arg_type, ...)
{
va_list var_args;
DBusMessage * msg = createMessage(method);
va_start(var_args, first_arg_type);
dbus_message_append_args_valist(msg,first_arg_type,var_args);
va_end(var_args);
return getInteger(call(msg));
}
string getString(const char *method, int first_arg_type, ...)
{
va_list var_args;
DBusMessage * msg = createMessage(method);
va_start(var_args, first_arg_type);
dbus_message_append_args_valist(msg,first_arg_type,var_args);
va_end(var_args);
return getString(call(msg));
}
protected:
virtual string get_text(int baddr = 0, size_t len = 1, bool lf = true)
{
dbus_int32_t a = (dbus_int32_t) baddr;
dbus_int32_t s = (dbus_int32_t) len;
char c = lf ? '\n' : 0;
return getString( "getText",
DBUS_TYPE_STRING, &this->id,
DBUS_TYPE_INT32, &a,
DBUS_TYPE_INT32, &s,
DBUS_TYPE_BYTE, &c,
DBUS_TYPE_INVALID);
}
virtual string get_text_at(int row, int col, size_t sz, bool lf = true)
{
dbus_int32_t r = (dbus_int32_t) row;
dbus_int32_t c = (dbus_int32_t) col;
dbus_int32_t s = (dbus_int32_t) sz;
char l = lf ? '\n' : 0;
return getString( "getTextAt",
DBUS_TYPE_STRING, &this->id,
DBUS_TYPE_INT32, &r,
DBUS_TYPE_INT32, &c,
DBUS_TYPE_INT32, &s,
DBUS_TYPE_BYTE, &l,
DBUS_TYPE_INVALID);
}
virtual int set_text_at(int row, int col, const char *str)
{
dbus_int32_t r = (dbus_int32_t) row;
dbus_int32_t c = (dbus_int32_t) col;
return getInteger( "setTextAt",
DBUS_TYPE_STRING, &this->id,
DBUS_TYPE_INT32, &r,
DBUS_TYPE_INT32, &c,
DBUS_TYPE_STRING, &str,
DBUS_TYPE_INVALID);
}
virtual int cmp_text_at(int row, int col, const char *str, bool lf)
{
dbus_int32_t r = (dbus_int32_t) row;
dbus_int32_t c = (dbus_int32_t) col;
char l = lf ? '\n' : 0;
return getInteger( "cmpTextAt",
DBUS_TYPE_STRING, &this->id,
DBUS_TYPE_INT32, &r,
DBUS_TYPE_INT32, &c,
DBUS_TYPE_STRING, &str,
DBUS_TYPE_BYTE, &l,
DBUS_TYPE_INVALID);
}
virtual int emulate_input(const char *str)
{
return getInteger( "input",
DBUS_TYPE_STRING, &this->id,
DBUS_TYPE_STRING, &str,
DBUS_TYPE_INVALID);
}
public:
client(const char *session)
{
DBusError err;
dbus_error_init(&err);
#ifdef DEBUG
conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
#else
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
#endif
if(dbus_error_is_set(&err))
{
exception e = exception("DBUS Connection Error (%s)", err.message);
dbus_error_free(&err);
throw e;
}
if(!conn)
{
throw exception("%s", "DBUS Connection failed");
}
if(*session != '?')
{
// Já tem sessão definida, usa.
int rc = getInteger("chkId",DBUS_TYPE_STRING, &session, DBUS_TYPE_INVALID);
if(rc) {
throw exception("%s",strerror(rc));
}
this->name = session;
}
else
{
// Obter um ID de sessão no serviço
this->name = getString("createSession");
}
trace("Session=%s",this->name.c_str());
this->id = this->name.c_str();
}
virtual ~client()
{
}
virtual string get_session_name(void) const
{
string rc = "service://";
rc += this->name;
return rc;
}
virtual bool is_connected(void)
{
return getInteger("isConnected", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual bool is_ready(void)
{
return getInteger("isReady", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual int close(void)
{
return getInteger("destroySession", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
void set_timeout(time_t timeout) {
dbus_int32_t val = (dbus_int32_t) timeout;
getInteger("setTimeout", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
}
void set_autoclose(time_t timeout) {
dbus_int32_t val = (dbus_int32_t) timeout;
getInteger("setAutoClose", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
}
virtual LIB3270_CSTATE get_cstate(void)
{
return (LIB3270_CSTATE) getInteger("getConnectionState", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual LIB3270_MESSAGE get_program_message(void)
{
return (LIB3270_MESSAGE) getInteger("getProgramMessage", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual LIB3270_SSL_STATE get_secure(void)
{
return (LIB3270_SSL_STATE) getInteger("getSecureState", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual int get_width(void)
{
return getInteger("getScreenWidth", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual int get_height(void)
{
return getInteger("getScreenHeight", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual int get_length(void)
{
return getInteger("getScreenLength", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual void set_unlock_delay(unsigned short ms)
{
dbus_int32_t val = (dbus_int32_t) ms;
getInteger("setUnlockDelay", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
}
virtual int set_host_charset(const char *charset)
{
return getInteger("setHostCharset", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_STRING, &charset, DBUS_TYPE_INVALID);
}
virtual string get_host_charset(void)
{
return getString("getHostCharset", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual int connect()
{
return connect("",0);
}
virtual int connect(const char *url, time_t wait)
{
int rc = getInteger("connect", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_STRING, &url, DBUS_TYPE_INVALID);
debug("connect(%s) rc=%d (%s)",url,rc,strerror(rc));
if(!rc && wait) {
time_t end = time(0) + wait;
while(!is_connected()) {
if(time(0) > end) {
debug("%s: Timeout",__FUNCTION__);
return ETIMEDOUT;
}
usleep(500);
}
}
debug("connect(%s) rc=%d (%s)",url,rc,strerror(rc));
return rc;
}
virtual int set_url(const char *hostname)
{
return getInteger("setURL", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_STRING, &hostname, DBUS_TYPE_INVALID);
}
virtual string get_url()
{
return getString("getUrl", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual int disconnect(void)
{
return getInteger("disconnect", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual int wait_for_ready(int seconds)
{
time_t end = time(0)+seconds;
debug("%s(%d)",__FUNCTION__,seconds);
while(time(0) < end)
{
if(!is_connected()) {
debug("%s: %s",__FUNCTION__,strerror(ENOTCONN));
return ENOTCONN;
}
if(is_ready())
return 0;
usleep(500);
}
debug("%s: Timeout",__FUNCTION__);
return ETIMEDOUT;
}
virtual int wait(int seconds)
{
time_t end = time(0)+seconds;
while(time(0) < end)
{
if(!is_connected())
return ENOTCONN;
usleep(500);
}
return 0;
}
virtual int iterate(bool wait)
{
if(wait)
usleep(100);
return 0;
}
virtual const char * asc2ebc(unsigned char *str, int sz = -1)
{
return (const char *) str;
}
virtual const char * ebc2asc(unsigned char *str, int sz = -1)
{
return (const char *) str;
}
virtual int set_cursor_position(int row, int col)
{
dbus_int32_t r = (dbus_int32_t) row;
dbus_int32_t c = (dbus_int32_t) col;
return getInteger( "setCursorAt",
DBUS_TYPE_STRING, &this->id,
DBUS_TYPE_INT32, &r,
DBUS_TYPE_INT32, &c,
DBUS_TYPE_INVALID);
}
virtual int set_cursor_addr(int addr)
{
dbus_int32_t a = (dbus_int32_t) addr;
return getInteger("setCursorAddr", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INT32, &addr, DBUS_TYPE_INVALID);
}
virtual int get_cursor_addr(void)
{
return getInteger("getCursorAddr", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual int set_toggle(LIB3270_TOGGLE ix, bool value)
{
dbus_int32_t i = (dbus_int32_t) ix;
dbus_int32_t v = (dbus_int32_t) value;
return getInteger("setToggle",
DBUS_TYPE_STRING, &this->id,
DBUS_TYPE_INT32, &i,
DBUS_TYPE_INT32, &v,
DBUS_TYPE_INVALID);
}
virtual int enter(void)
{
return getInteger("enter", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual int pfkey(int key)
{
dbus_int32_t val = (dbus_int32_t) key;
return getInteger("pfKey", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
}
virtual int pakey(int key)
{
dbus_int32_t val = (dbus_int32_t) key;
return getInteger("paKey", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
}
virtual int quit(void)
{
return getInteger("quit",
DBUS_TYPE_STRING, &this->id,
DBUS_TYPE_INVALID);
}
virtual int action(const char *name)
{
return getInteger("action",
DBUS_TYPE_STRING, &this->id,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID);
}
virtual int erase(void)
{
return getInteger("erase", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual int erase_eof(void)
{
return getInteger("eraseEof", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual int erase_eol(void)
{
return getInteger("eraseEol", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual int erase_input(void)
{
return getInteger("eraseInput", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual int print(void)
{
return getInteger("print", DBUS_TYPE_STRING, &this->id, DBUS_TYPE_INVALID);
}
virtual int get_field_start(int baddr = -1)
{
return -1;
}
virtual int get_field_len(int baddr = -1)
{
return -1;
}
virtual int get_next_unprotected(int baddr = -1)
{
return -1;
}
virtual int get_is_protected(int baddr = -1)
{
return -1;
}
virtual int get_is_protected_at(int row, int col)
{
return -1;
}
};
session * create_service_client(const char *session)
{
return new client(session);
}
}
#endif // HAVE_DBUS