00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 extern "C"
00011 {
00012 #include <string.h>
00013 #include <stddef.h>
00014 #include <stdlib.h>
00015
00016 #ifdef VXWORKS
00017 #include "vxWorks.h"
00018 #endif
00019 }
00020
00021 #include "cms.hh"
00022 #include "bbdmem.hh"
00023 #include "rcs_prnt.hh"
00024 #include "timer.hh"
00025
00026 extern "C"
00027 {
00028 #include "bbd.h"
00029 }
00030
00031
00032
00033
00034 BBDMEM::BBDMEM (char *_bufline, char *_procline, int set_to_server,
00035 int set_to_master):
00036 CMS (_bufline, _procline, set_to_server)
00037 {
00038 char bbd_name[20];
00039 int i;
00040 bbd_data = 0;
00041 id = 0;
00042 reader_bbd_id = NULL;
00043 writer_bbd_id = NULL;
00044
00045 if (neutral)
00046 {
00047 rcs_print_error ("Neutral encoded buffers not supported by BBD.\n");
00048 status = CMS_CONFIG_ERROR;
00049 return;
00050 }
00051
00052 if (!read_permission_flag && !write_permission_flag)
00053 {
00054 status = CMS_CONFIG_ERROR;
00055 rcs_print_error
00056 ("BBD requires that the process have either read pr write permission.\n");
00057 return;
00058 }
00059
00060 char *bbd_name_eq = strstr (buflineupper, "BBD_NAME=");
00061
00062 if (NULL != bbd_name_eq)
00063 {
00064 bbd_name_eq += 9;
00065 memset (bbd_name, 0, 20);
00066 for (i = 0; i < 20; i++)
00067 {
00068 if (bbd_name_eq[i] == 0 ||
00069 bbd_name_eq[i] == ' ' ||
00070 bbd_name_eq[i] == '\t' ||
00071 bbd_name_eq[i] == '\b' ||
00072 bbd_name_eq[i] == '\r' || bbd_name_eq[i] == '\n')
00073 {
00074 break;
00075 }
00076 bbd_name[i] = bbd_name_eq[i];
00077 }
00078 }
00079 else
00080 {
00081 strncpy (bbd_name, BufferName, 20);
00082 }
00083
00084 bbd_size = size;
00085 char *bbd_size_eq = strstr (buflineupper, "BBD_SIZE=");
00086 if (bbd_size_eq != 0);
00087 {
00088 bbd_size_eq += 9;
00089 bbd_size = strtol (bbd_size_eq, NULL, 0);
00090 if (bbd_size <= 0)
00091 {
00092 rcs_print_error ("bbd_size of %d is invalid.\n", bbd_size);
00093 bbd_size = size;
00094 }
00095 }
00096
00097
00098 if (read_permission_flag)
00099 {
00100 reader_bbd_id = bbdConnect (bbd_name, BBD_MULT_READER, bbd_size);
00101 bbd_data = NULL;
00102 }
00103 if (write_permission_flag)
00104 {
00105 char *proc_sole_writer = strstr (proclineupper, "SOLE_WRITER");
00106 if (NULL != proc_sole_writer)
00107 {
00108 writer_bbd_id = bbdConnect (bbd_name, BBD_SOLE_WRITER, bbd_size);
00109 }
00110 else
00111 {
00112 writer_bbd_id = bbdConnect (bbd_name, BBD_MULT_WRITER, bbd_size);
00113 }
00114 }
00115 if (NULL == reader_bbd_id && NULL == writer_bbd_id)
00116 {
00117 status = CMS_CREATE_ERROR;
00118 }
00119 bbd_timeout = NO_WAIT;
00120 if (timeout > 0)
00121 {
00122 bbd_timeout = (int) ((double) timeout / clk_tck ());
00123 }
00124 else if (timeout == 0)
00125 {
00126 bbd_timeout = NO_WAIT;
00127 }
00128 else
00129 {
00130 bbd_timeout = WAIT_FOREVER;
00131 }
00132 subdiv_data = data = realloc (data, size + 256);
00133 }
00134
00135 BBDMEM::~BBDMEM ()
00136 {
00137
00138
00139
00140 if (NULL != reader_bbd_id)
00141 {
00142 bbdDelete (reader_bbd_id);
00143 reader_bbd_id = NULL;
00144 }
00145 if (NULL != writer_bbd_id)
00146 {
00147 bbdDelete (writer_bbd_id);
00148 writer_bbd_id = NULL;
00149 }
00150 bbd_data = NULL;
00151 }
00152
00153 CMS_STATUS BBDMEM::read ()
00154 {
00155 if (NULL == bbd_data)
00156 {
00157 bbd_data = (void *) (((char *) data) + sizeof_message_header);
00158 }
00159
00160 if (!read_permission_flag)
00161 {
00162 rcs_print_error ("Read permission denied.\n");
00163 return (status = CMS_PERMISSIONS_ERROR);
00164 }
00165 if (bbdRead (reader_bbd_id, bbd_data, BBD_FRESH_READ, NO_WAIT) == OK)
00166 {
00167 id++;
00168 }
00169 else
00170 {
00171
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 check_id (id);
00198 return status;
00199 }
00200
00201 CMS_STATUS BBDMEM::peek ()
00202 {
00203 if (NULL == bbd_data)
00204 {
00205 bbd_data = (void *) (((char *) data) + sizeof_message_header);
00206 }
00207
00208 if (!read_permission_flag)
00209 {
00210 rcs_print_error ("Read permission denied.\n");
00211 return (status = CMS_PERMISSIONS_ERROR);
00212 }
00213 if (bbdRead (reader_bbd_id, bbd_data, BBD_FRESH_READ, NO_WAIT) == OK)
00214 {
00215 id++;
00216 }
00217 else
00218 {
00219
00220 }
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 check_id (id);
00245 return status;
00246 }
00247
00248 CMS_STATUS BBDMEM::write (void *user_data)
00249 {
00250 bbd_data = (void *) (((char *) user_data) + sizeof_message_header);
00251
00252 if (!write_permission_flag)
00253 {
00254 rcs_print_error ("Write permission denied.\n");
00255 return (status = CMS_PERMISSIONS_ERROR);
00256 }
00257 if (bbdWrite (writer_bbd_id, bbd_data, BBD_PLAIN_WRITE, bbd_timeout) != OK)
00258 {
00259 rcs_print_error ("BBD ERROR:\n");
00260 bbdShow (writer_bbd_id, BufferName);
00261 return (status = CMS_MISC_ERROR);
00262 }
00263
00264 return (status = CMS_WRITE_OK);
00265 }
00266
00267 CMS_STATUS BBDMEM::write_if_read (void *user_data)
00268 {
00269 bbd_data = (void *) (((char *) user_data) + sizeof_message_header);
00270
00271 if (!write_permission_flag)
00272 {
00273 rcs_print_error ("Write permission denied.\n");
00274 return (status = CMS_PERMISSIONS_ERROR);
00275 }
00276 if (bbdWrite (writer_bbd_id, bbd_data, BBD_WAIT_WRITE, NO_WAIT) != OK)
00277 {
00278 rcs_print_error ("BBD ERROR:\n");
00279 bbdShow (writer_bbd_id, BufferName);
00280 return (status = CMS_MISC_ERROR);
00281 }
00282
00283 return (status = CMS_WRITE_OK);
00284 }