HIPC  0.5
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
main_posix.c
Go to the documentation of this file.
1 #include <unistd.h>
2 
3 #include <errno.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <pthread.h>
7 
8 #include <hipc/posix/ioutils.h>
9 #include <hipc/posix/usoc.h>
10 
11 #include "CFGrt.h"
12 #include "hipcsvs_rt.h"
13 
14 int hipcUsocReadReady(struct HIPC_usoc *so_p)
15 {
16  return hipcPosixReadReady(so_p->fdr);
17 }
18 
19 static void *runMockServer(void *arg)
20 {
21  struct HIPC_pbank *const pbank_p = &hipcsvsRt_pbank;
22  struct HIPC_queue *const queue_p = &hipcsvsRt_queue;
23  struct STRUprm const *prm_p;
24  struct STRUlog *log_p;
25  uint8_t output;
26  unsigned int tick = 0;
27  unsigned int cnt = 0;
28 
29  prm_p = hipcPbankGetReadOnlyPage(pbank_p);
30 
31  for (;;) {
32  fprintf(stderr, "---\n");
33 
34  if (0 == cnt) {
35  if (1 == hipcPbankSwitchAccept(pbank_p)) {
36  fprintf(stderr, "pbank changed\n");
37  prm_p = hipcPbankGetReadOnlyPage(pbank_p);
38  cnt = prm_p->size;
39  }
40  }
41 
42  if (0 < cnt) {
43  output = prm_p->pattern[prm_p->size - cnt];
44  fprintf(stderr, "tick/cnt/output %02d/%d/%d\n",
45  tick, cnt, output);
46 
47  log_p = hipcQueueFull(queue_p);
48  if (NULL != log_p) {
49  log_p->tick = tick;
50  log_p->output = output;
51  hipcQueuePush(queue_p);
52  }
53  --cnt;
54  }
55  usleep(1000 * 1000);
56  ++tick;
57  }
58 
59  pthread_exit(NULL);
60 }
61 
62 int main(void)
63 {
64  pthread_attr_t attr;
65  pthread_t thread;
66  struct HIPC_usoc usoc;
67 
68  hipcPosixUsocInit(&usoc, STDIN_FILENO, STDOUT_FILENO);
69  hipcsvsRtInit();
70 
71  if (0 != pthread_attr_init(&attr)) {
72  perror("pthread_attr_init faild");
73  exit(EXIT_FAILURE);
74  }
75  if (0 != pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
76  perror("pthread_attr_setdetachstate faild");
77  exit(EXIT_FAILURE);
78  }
79  if (0 != pthread_create(&thread, &attr, runMockServer, NULL)) {
80  perror("pthread_create faild");
81  exit(EXIT_FAILURE);
82  }
83  if (0 != pthread_attr_destroy(&attr)) {
84  perror("pthread_attr_destroy faild");
85  exit(EXIT_FAILURE);
86  }
87 
88  hipcMain(&usoc);
89 
90  if (0 != errno) {
91  perror("Error message from perror()");
92  fprintf(stderr, "errno: %d\n", errno);
93  }
94  fprintf(stderr, "end\n");
95 
96  return 0;
97 }
static void * runMockServer(void *arg)
Definition: main_posix.c:19
void hipcPosixUsocInit(struct HIPC_usoc *so_p, const int fdr, const int fdw)
Initializes a universal socket.
Definition: posix_usoc.c:14
int hipcPosixReadReady(int fd)
Check a file descriptor is ready to read by using select(2) and returns the result of select(2)...
Definition: posix_ioutils.c:17
int hipcUsocReadReady(struct HIPC_usoc *so_p)
Definition: main_posix.c:14
int main(void)
Definition: main_posix.c:18
const void * hipcPbankGetReadOnlyPage(struct HIPC_pbank *const bank_p)
Gets a current read only page.
Definition: pbank.c:44
struct HIPC_queue hipcsvsRt_queue
Definition: hipcsvs_rt.c:16
void hipcQueuePush(struct HIPC_queue *const que_p)
Pushes an item and occupies a slot.
Definition: queue.c:93
Definition: usoc.h:4
int fdr
Definition: usoc.h:5
void hipcsvsRtInit(void)
Definition: hipcsvs_rt.c:15
void hipcMain(struct HIPC_usoc *const so_p)
Definition: server.c:68
void * hipcQueueFull(struct HIPC_queue const *const que_p)
Returns an address of an empty slot if it exists.
Definition: queue.c:76
struct HIPC_pbank hipcsvsRt_pbank
Definition: hipcsvs_rt.c:13
int hipcPbankSwitchAccept(struct HIPC_pbank *const bank_p)
Accepts a switching request and switches pages if switching is requested.
Definition: pbank.c:96