09.service.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * PROGRAM: Object oriented API samples.
  3. * MODULE: 09.service.cpp
  4. * DESCRIPTION: A sample of using service manager.
  5. * Prints server version and employee's encryption statistics.
  6. *
  7. * Example for the following interfaces:
  8. * IService - interface to work with service manager.
  9. *
  10. * The contents of this file are subject to the Initial
  11. * Developer's Public License Version 1.0 (the "License");
  12. * you may not use this file except in compliance with the
  13. * License. You may obtain a copy of the License at
  14. * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
  15. *
  16. * Software distributed under the License is distributed AS IS,
  17. * WITHOUT WARRANTY OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing rights
  19. * and limitations under the License.
  20. *
  21. * The Original Code was created by Alexander Peshkoff
  22. * for the Firebird Open Source RDBMS project.
  23. *
  24. * Copyright (c) 2016 Alexander Peshkoff <[email protected]>
  25. * and all contributors signed below.
  26. *
  27. * All Rights Reserved.
  28. * Contributor(s): ______________________________________.
  29. *
  30. */
  31. #include "ifaceExamples.h"
  32. static IMaster* master = fb_get_master_interface();
  33. // print information, returned by isc_svc_query() call
  34. bool printLine(const unsigned char*& p)
  35. {
  36. const ISC_USHORT length = (ISC_USHORT) isc_vax_integer((char*)p, sizeof(ISC_USHORT));
  37. p += sizeof(ISC_USHORT);
  38. if (length > 0)
  39. printf("%*.*s\n", length, length, p);
  40. p += length;
  41. return length > 0;
  42. }
  43. bool printInfo(const unsigned char* p, size_t pSize)
  44. {
  45. bool ret = false;
  46. bool ignoreTruncation = false;
  47. const unsigned char* const end = p + pSize;
  48. while (p < end && *p != isc_info_end)
  49. {
  50. switch (*p++)
  51. {
  52. case isc_info_svc_server_version:
  53. printf ("Server version: ");
  54. if (!printLine(p))
  55. printf ("<no data>\n");
  56. break;
  57. case isc_info_svc_line:
  58. ret = printLine(p);
  59. break;
  60. case isc_info_truncated:
  61. if (!ignoreTruncation)
  62. {
  63. printf("\n<<< truncated >>>\n");
  64. }
  65. fflush(stdout);
  66. ret = true;
  67. break;
  68. case isc_info_svc_timeout:
  69. case isc_info_data_not_ready:
  70. ret = true;
  71. break;
  72. default:
  73. printf("Unknown item 0x%x in received buffer\n", p[-1]);
  74. }
  75. }
  76. fflush(stdout);
  77. return ret;
  78. }
  79. int main()
  80. {
  81. int rc = 0;
  82. // Status wrapper
  83. ThrowStatusWrapper status(master->getStatus());
  84. // Declare pointers to required interfaces
  85. IProvider* prov = master->getDispatcher();
  86. IUtil* utl = master->getUtilInterface();
  87. IService* svc = NULL;
  88. IXpbBuilder* spb1 = NULL;
  89. IXpbBuilder* spb2 = NULL;
  90. try {
  91. printf("** Attaching to service manager...\n");
  92. // Prepare SPB to attach to service manager
  93. spb1 = utl->getXpbBuilder(&status, IXpbBuilder::SPB_ATTACH, NULL, 0);
  94. spb1->insertString(&status, isc_spb_user_name, "sysdba");
  95. spb1->insertString(&status, isc_spb_password, "masterkey");
  96. // In case when your program is expected to be used on a server
  97. // with multiple security database it's very good idea to specify
  98. // DB expected to be used by services (you anyway need separate
  99. // services connectons for databases with different user list location).
  100. spb1->insertString(&status, isc_spb_expected_db, "employee");
  101. // Attach to service manager
  102. svc = prov->attachServiceManager(&status, "service_mgr", spb1->getBufferLength(&status),
  103. spb1->getBuffer(&status));
  104. printf("** Demo of querying information about server version...\n");
  105. // In the simplest case sendItems parameter may be NULL
  106. // Building receiveItems is mostly trivial
  107. const unsigned char receiveItems[] = {isc_info_svc_server_version};
  108. // Output buffer
  109. unsigned char results[1024];
  110. // Query server version
  111. svc->query(&status, 0, NULL, sizeof(receiveItems), receiveItems, sizeof(results), results);
  112. printInfo(results, sizeof(results));
  113. printf("** Demo of running utility using service manager...\n");
  114. // Build service start SPB
  115. spb2 = utl->getXpbBuilder(&status, IXpbBuilder::SPB_START, NULL, 0);
  116. spb2->insertTag(&status, isc_action_svc_db_stats);
  117. spb2->insertString(&status, isc_spb_dbname, "employee");
  118. spb2->insertInt(&status, isc_spb_options, isc_spb_sts_encryption);
  119. // Start service
  120. svc->start(&status, spb2->getBufferLength(&status), spb2->getBuffer(&status));
  121. // Prepare receiveItems block
  122. const unsigned char receiveItems2[] = {isc_info_svc_line};
  123. // Query service output
  124. do
  125. {
  126. svc->query(&status, 0, NULL, sizeof(receiveItems2), receiveItems2, sizeof(results), results);
  127. } while (printInfo(results, sizeof(results)));
  128. printf("** Detaching from service manager...\n");
  129. // Detach from service manager
  130. svc->detach(&status);
  131. svc = NULL;
  132. printf("** Done.\n");
  133. }
  134. catch (const FbException& error)
  135. {
  136. // handle error
  137. rc = 1;
  138. char buf[256];
  139. master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
  140. fprintf(stderr, "%s\n", buf);
  141. }
  142. // release interfaces after error caught
  143. if (svc)
  144. svc->release();
  145. // generic cleanup
  146. prov->release();
  147. status.dispose();
  148. if (spb1)
  149. spb1->dispose();
  150. if (spb2)
  151. spb2->dispose();
  152. return rc;
  153. }