00001
00002
00003
00004
00005 #include "rtlmem.hh"
00006 #include "nmlmsg.hh"
00007 #include "timer.hh"
00008 #include "rcs_prnt.hh"
00009
00010 static int rtlnml_count = 0;
00011
00012 RTLMEM::RTLMEM (char *bufline, char *procline, int set_to_server,
00013 int set_to_master):
00014 CMS (bufline, procline, set_to_server)
00015 {
00016 if (rtlnml_count == 0)
00017 {
00018 rtlnml_init ();
00019 }
00020 rtlnml_count++;
00021 #ifdef HAVE_RTAI
00022 if (sscanf (bufline, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %d", &key) != 1)
00023 {
00024 rcs_print_error ("SHMEM: Invalid configuration file format.\n");
00025 return;
00026 }
00027 r =
00028 rtainml_open (BufferName, ProcessName, NULL, size, is_local_master, key);
00029 #else
00030 r = rtlnml_open (BufferName, ProcessName, NULL, size, is_local_master);
00031 #endif
00032 if (NULL == r)
00033 {
00034 status = CMS_MISC_ERROR;
00035 return;
00036 }
00037 rtlnml_set_local_pointer (r, ((char *) data) + 8);
00038 }
00039
00040 RTLMEM::~RTLMEM ()
00041 {
00042 if (NULL != r)
00043 {
00044 rtlnml_close (r, BufferName);
00045 }
00046 rtlnml_count--;
00047 if (rtlnml_count == 0)
00048 {
00049 rtlnml_exit ();
00050 }
00051 }
00052
00053
00054 CMS_STATUS
00055 RTLMEM::read ()
00056 {
00057 int split_read_count = 0;
00058 long type = rtlnml_read (r);
00059 if (type > 0)
00060 {
00061 ((NMLmsg *) data)->type = type;
00062 return (status = CMS_READ_OK);
00063 }
00064 if (type == RTLNML_READ_OLD)
00065 {
00066 return (status = CMS_READ_OLD);
00067 }
00068 if (type == RTLNML_SPLIT_READ_ERROR)
00069 {
00070 double start_time = etime ();
00071 double now = start_time;
00072 while ((now - start_time) < timeout || timeout < 0.0
00073 || split_read_count < 1)
00074 {
00075 long type = rtlnml_read (r);
00076 if (type > 0)
00077 {
00078 ((NMLmsg *) data)->type = type;
00079 return (status = CMS_READ_OK);
00080 }
00081 if (type == RTLNML_READ_OLD)
00082 {
00083 return (status = CMS_READ_OLD);
00084 }
00085 if (type == RTLNML_SPLIT_READ_ERROR)
00086 {
00087 split_read_count++;
00088 }
00089 else
00090 {
00091 return (status = CMS_MISC_ERROR);
00092 }
00093 now = etime ();
00094 }
00095 rcs_print_error
00096 ("Timedout after %d RTLNML_SPLIT_READ_ERRORs and %f seconds.\n",
00097 split_read_count, (etime () - start_time));
00098 return (status = CMS_TIMED_OUT);
00099 }
00100 return (status = CMS_MISC_ERROR);
00101 }
00102
00103 CMS_STATUS
00104 RTLMEM::peek ()
00105 {
00106 return read ();
00107 }
00108
00109 CMS_STATUS
00110 RTLMEM::write (void *data)
00111 {
00112 if (rtlnml_write
00113 (r, ((char *) data) + 8, ((NMLmsg *) data)->type,
00114 header.in_buffer_size) < 0)
00115 {
00116 return (status = CMS_MISC_ERROR);
00117 }
00118 return (status = CMS_WRITE_OK);
00119 }