File: msgtest.c

package info (click to toggle)
gmerlin 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,108 kB
  • sloc: ansic: 133,761; javascript: 6,967; makefile: 1,400; sh: 702; sed: 16
file content (163 lines) | stat: -rw-r--r-- 3,739 bytes parent folder | download
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
/*****************************************************************
 * gmerlin - a general purpose multimedia framework and applications
 *
 * Copyright (c) 2001 - 2024 Members of the Gmerlin project
 * http://github.com/bplaum
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/



#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#include <gmerlin/parameter.h>
#include <gmerlin/streaminfo.h>
#include <gmerlin/msgqueue.h>
#include <gavl/gavl.h>

#define BUFFER_SIZE 1024

#define MSG_QUIT   0
#define MSG_VOID   1
#define MSG_INT    2
#define MSG_STRING 3
#define MSG_FLOAT  4

static void * thread1_func(void * data)
  {
  //  int keep_going = 1;
  gavl_msg_t * msg;
  bg_msg_queue_t * queue;
  const char * str;
    
  queue = (bg_msg_queue_t *)data;
    
  while((msg = bg_msg_queue_try_lock_read(queue)))
    {
    switch(gavl_msg_get_id(msg))
      {
      case MSG_QUIT:
        fprintf(stderr, "Got message quit\n");
        //        keep_going = 0;
        break;
      case MSG_VOID:
        fprintf(stderr, "Got message void\n");
        break;
      case MSG_INT:
        fprintf(stderr, "Got message int %d\n", gavl_msg_get_arg_int(msg, 0));
        break;
      case MSG_STRING:
        str = gavl_msg_get_arg_string_c(msg, 0);
        fprintf(stderr, "Got message string %s\n", str);
        break;
      case MSG_FLOAT:
        fprintf(stderr, "Got message float %f\n", gavl_msg_get_arg_float(msg, 0));
        break;
        
      }
    bg_msg_queue_unlock_read(queue);
    }
  return NULL;
  }

static int read_message(bg_msg_queue_t * q)
  {
  int num_messages;
  int id;
  int val_i;
  float val_f;
  char val_str[BUFFER_SIZE];
  gavl_msg_t * msg;
  int i;
  printf("Enter ID [0-4]: ");
  scanf("%d", &id);

  num_messages = 1;
  
  switch(id)
    {
    case MSG_QUIT:
      break;
    case MSG_VOID:
      break;
    case MSG_INT:
      printf("Enter Integer: ");
      scanf("%d", &val_i);
      num_messages = 10;
      break;
    case MSG_STRING:
      printf("Enter String: ");
      fgets(val_str, BUFFER_SIZE, stdin);
      break;
    case MSG_FLOAT:
      printf("Enter float: ");
      scanf("%f", &val_f);
      break;
    }

  for(i = 0; i < num_messages; i++)
    {
    msg = bg_msg_queue_lock_write(q);
    gavl_msg_set_id(msg, id);
    
    switch(id)
      {
      case MSG_QUIT:
      case MSG_VOID:
        break;
      case MSG_INT:
        gavl_msg_set_arg_int(msg, 0, val_i);
        break;
      case MSG_STRING:
        gavl_msg_set_arg_string(msg, 0, val_str);
        break;
      case MSG_FLOAT:
        gavl_msg_set_arg_float(msg, 0, val_f);
        break;
      }
    bg_msg_queue_unlock_write(q);
    }
  if(id == MSG_QUIT)
    return 0;
  return 1;
  }


int main(int argc, char ** argv)
  {
  pthread_t read_thread;
  
  bg_msg_queue_t * queue;

  queue = bg_msg_queue_create();

  pthread_create(&read_thread,
                 NULL,
                 thread1_func, queue);

  while(1)
    {
    if(!read_message( queue))
      break;
    }

  pthread_join(read_thread,NULL);
  
  
  return 0;
  }