ckpt-wr.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <errno.h>
  37. #include <unistd.h>
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <sys/select.h>
  41. #include <sys/un.h>
  42. #include <time.h>
  43. #include "ais_types.h"
  44. #include "saCkpt.h"
  45. #include "sa_error.h"
  46. #define SECONDS_TO_EXPIRE 500
  47. int ckptinv;
  48. void printSaNameT (SaNameT *name)
  49. {
  50. int i;
  51. for (i = 0; i < name->length; i++) {
  52. printf ("%c", name->value[i]);
  53. }
  54. }
  55. SaVersionT version = { 'B', 1, 1 };
  56. SaNameT checkpointName = { 16, "checkpoint-sync\0" };
  57. SaCkptCheckpointCreationAttributesT checkpointCreationAttributes = {
  58. SA_CKPT_WR_ALL_REPLICAS,
  59. 100000,
  60. 2000,
  61. 5,
  62. 20000,
  63. 10
  64. };
  65. char readBuffer1[1025];
  66. SaCkptIOVectorElementT ReadVectorElements[] = {
  67. {
  68. SA_CKPT_DEFAULT_SECTION_ID,
  69. readBuffer1,
  70. sizeof (readBuffer1),
  71. 0,
  72. 0
  73. }
  74. };
  75. #define DATASIZE 127000
  76. char data[DATASIZE];
  77. SaCkptIOVectorElementT WriteVectorElements[] = {
  78. {
  79. SA_CKPT_DEFAULT_SECTION_ID,
  80. data, /*"written data #1, this should extend past end of old section data", */
  81. DATASIZE, /*sizeof ("data #1, this should extend past end of old section data") + 1, */
  82. 0, //5,
  83. 0
  84. }
  85. };
  86. SaCkptCallbacksT callbacks = {
  87. 0,
  88. 0
  89. };
  90. #define MAX_DATA_SIZE 100
  91. int main (void) {
  92. SaCkptHandleT ckptHandle;
  93. SaCkptCheckpointHandleT checkpointHandle;
  94. SaAisErrorT error;
  95. char data[MAX_DATA_SIZE];
  96. struct timespec delay;
  97. struct timespec delay2;
  98. SaCkptIOVectorElementT writeElement;
  99. long count = 0;
  100. SaUint32T erroroneousVectorIndex = 0;
  101. delay.tv_sec = 1;
  102. delay.tv_nsec = 0;
  103. error = saCkptInitialize (&ckptHandle, &callbacks, &version);
  104. error = saCkptCheckpointOpen (ckptHandle,
  105. &checkpointName,
  106. &checkpointCreationAttributes,
  107. SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  108. 0,
  109. &checkpointHandle);
  110. printf ("%s: initial open of checkpoint\n",
  111. get_test_output (error, SA_AIS_OK));
  112. do{
  113. error = saCkptCheckpointRead (checkpointHandle,
  114. ReadVectorElements,
  115. 1,
  116. &erroroneousVectorIndex);
  117. if (error != SA_AIS_OK) {
  118. if (error == SA_AIS_ERR_TRY_AGAIN) {
  119. continue;
  120. }
  121. return (0);
  122. }
  123. if (ReadVectorElements->dataBuffer == 0) {
  124. printf ("Default Checkpoint has no data\n");
  125. } else {
  126. count = atol((char *)ReadVectorElements->dataBuffer);
  127. }
  128. count++;
  129. sprintf((char*)&data, "%d",(int)count);
  130. writeElement.sectionId = (SaCkptSectionIdT)SA_CKPT_DEFAULT_SECTION_ID;
  131. writeElement.dataBuffer = data;
  132. writeElement.dataSize = strlen (data) + 1;
  133. writeElement.dataOffset = 0;
  134. writeElement.readSize = 0;
  135. do {
  136. error = saCkptCheckpointWrite (checkpointHandle,
  137. &writeElement,
  138. 1,
  139. &erroroneousVectorIndex);
  140. printf ("%s: checkpoint write with data %s\n",
  141. get_test_output (error, SA_AIS_OK), (char*)data);
  142. }while (error == SA_AIS_ERR_TRY_AGAIN);
  143. nanosleep(&delay,&delay2);
  144. }while (1);
  145. return (0);
  146. }