Advertisement
Guest User

main

a guest
Apr 19th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. /* Data Store Server.
  2. * This service implements a little publish/subscribe data store that is
  3. * crucial for the system's fault tolerance. Components that require state
  4. * can store it here, for later retrieval, e.g., after a crash and subsequent
  5. * restart by the reincarnation server.
  6. *
  7. * Created:
  8. * Oct 19, 2005 by Jorrit N. Herder
  9. */
  10.  
  11. #include "inc.h" /* include master header file */
  12. #include <minix/endpoint.h>
  13.  
  14. /* Allocate space for the global variables. */
  15. static endpoint_t who_e; /* caller's proc number */
  16. static int callnr; /* system call number */
  17.  
  18. /* Declare some local functions. */
  19. static void get_work(message *m_ptr);
  20. static void reply(endpoint_t whom, message *m_ptr);
  21.  
  22. /* SEF functions and variables. */
  23. static void sef_local_startup(void);
  24.  
  25.  
  26. static int pgmem[PG_MEM_SIZE];
  27.  
  28. /*===========================================================================*
  29. * main *
  30. *===========================================================================*/
  31. int main(int argc, char **argv)
  32. {
  33. /* This is the main routine of this service. The main loop consists of
  34. * three major activities: getting new work, processing the work, and
  35. * sending the reply. The loop never terminates, unless a panic occurs.
  36. */
  37. message m;
  38. int result;
  39.  
  40. /* SEF local startup. */
  41. env_setargs(argc, argv);
  42. sef_local_startup();
  43.  
  44. /* Main loop - get work and do it, forever. */
  45. while (TRUE) {
  46.  
  47. /* Wait for incoming message, sets 'callnr' and 'who'. */
  48. get_work(&m);
  49.  
  50. switch(callnr)
  51. {
  52. case writeTest: pgmem[m.m_m1.m1i1] = m.m_m1.m1i2; m.m_type = 1; break;
  53. case readTest: m.m_m1.m1i2=pgmem[m.m_m1.m1i1]; m.m_type = 2; break;
  54. default: m.m_type = 3; break;
  55. }
  56.  
  57.  
  58. /* Finally send reply message, unless disabled. */
  59.  
  60. reply(who_e, &m); /* send it away */
  61.  
  62. }
  63. return(OK); /* shouldn't come here */
  64. }
  65.  
  66. /*===========================================================================*
  67. * sef_local_startup *
  68. *===========================================================================*/
  69. static void sef_local_startup()
  70. {
  71. /* Register init callbacks. */
  72. sef_setcb_init_fresh(sef_cb_init_fresh);
  73. sef_setcb_init_restart(sef_cb_init_fail);
  74.  
  75. /* No live update support for now. */
  76.  
  77. /* Let SEF perform startup. */
  78. sef_startup();
  79. }
  80.  
  81. /*===========================================================================*
  82. * sef_cb_init_fresh *
  83. *===========================================================================*/
  84. int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *info)
  85. {
  86. /* Initialize the data store server. */
  87. int i, r;
  88.  
  89. for( i=0; i<PG_MEM_SIZE; i++) pgmem[i]=0;
  90.  
  91. pgmem[4]=6; pgmem[5]=3;
  92.  
  93. return(OK);
  94. }
  95.  
  96. /*===========================================================================*
  97. * get_work *
  98. *===========================================================================*/
  99. static void get_work(
  100. message *m_ptr /* message buffer */
  101. )
  102. {
  103. int status = sef_receive(ANY, m_ptr); /* blocks until message arrives */
  104. if (OK != status)
  105. panic("failed to receive message!: %d", status);
  106. who_e = m_ptr->m_source; /* message arrived! set sender */
  107. callnr = m_ptr->m_type; /* set function call number */
  108. }
  109.  
  110. /*===========================================================================*
  111. * reply *
  112. *===========================================================================*/
  113. static void reply(
  114. endpoint_t who_e, /* destination */
  115. message *m_ptr /* message buffer */
  116. )
  117. {
  118. int s = ipc_send(who_e, m_ptr); /* send the message */
  119. if (OK != s)
  120. printf("DS: unable to send reply to %d: %d\n", who_e, s);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement