4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 Messages::Messages(int input_flag, int output_flag, int id)
35 msgid = msgget(IPC_PRIVATE, IPC_CREAT | 0777);
44 this->input_flag = input_flag;
45 this->output_flag = output_flag;
46 bc_enter_msg_id(msgid);
53 msgctl(msgid, IPC_RMID, NULL);
54 bc_remove_msg_id(msgid);
58 char* Messages::get_message_buffer()
63 int Messages::read_message(char *text)
65 buffer.mtype = input_flag;
67 if((msgrcv(msgid, (struct msgbuf*)&buffer, MESSAGESIZE, input_flag, 0)) < 0)
69 printf(_("receive message failed\n"));
70 sleep(1); // don't flood the screen during the loop
74 //printf("%d %d\n", buffer.text[0], buffer.mtype);
75 strcpy(text, buffer.text);
79 long Messages::read_message()
81 buffer.mtype = input_flag;
83 if((msgrcv(msgid, (struct msgbuf*)&buffer, MESSAGESIZE, input_flag, 0)) < 0)
85 printf(_("receive message failed\n"));
89 return atol(buffer.text);
92 float Messages::read_message_f()
95 char *data = read_message_raw();
96 sscanf(data, "%f", &value);
100 char* Messages::read_message_raw()
102 buffer.mtype = input_flag;
104 if((msgrcv(msgid, (struct msgbuf*)&buffer, MESSAGESIZE, input_flag, 0)) < 0)
106 printf(_("receive message failed\n"));
108 return "RECIEVE MESSAGE FAILED";
114 int Messages::read_message(long *value1, long *value2)
116 char *data = read_message_raw();
117 sscanf(data, "%ld %ld", value1, value2);
121 int Messages::read_message_f(float *value1, float *value2)
123 char *data = read_message_raw();
124 sscanf(data, "%f %f", value1, value2);
128 int Messages::read_message(long *command, long *value1, long *value2)
130 char *data = read_message_raw();
131 sscanf(data, "%ld %ld %ld", command, value1, value2);
135 int Messages::read_message(long *command, long *value1, long *value2, long *value3)
137 char *data = read_message_raw();
138 sscanf(data, "%ld %ld %ld %ld", command, value1, value2, value3);
142 int Messages::read_message_f(float *value1, float *value2, float *value3)
144 char *data = read_message_raw();
145 sscanf(data, "%f %f %f", value1, value2, value3);
149 int Messages::read_message_f(float *value1, float *value2, float *value3, float *value4)
151 char *data = read_message_raw();
152 sscanf(data, "%f %f %f %f", value1, value2, value3, value4);
156 int Messages::read_message(int *command, char *text)
160 char *data = read_message_raw();
161 sscanf(data, "%d", command);
162 // find the start of the text
163 for(i = 0; i < MESSAGESIZE && data[i] != ' '; i++) { ; }
166 // copy the text part
167 for(j = 0; (text[j] = data[i]) != 0; i++, j++) { ; }
172 int Messages::write_message(char *text)
174 buffer.mtype = output_flag;
175 strcpy(buffer.text, text);
177 if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(text) + 1, 0)) < 0) printf(_("send message failed\n"));
181 int Messages::write_message_raw()
183 buffer.mtype = output_flag;
185 if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(buffer.text) + 1, 0)) < 0) printf(_("send message failed\n"));
189 int Messages::write_message_flagged(int output_flag, int command)
191 buffer.mtype = output_flag;
192 sprintf(buffer.text, "%d", command);
194 if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(buffer.text) + 1, 0)) < 0) printf(_("send message failed\n"));
198 int Messages::write_message(int number)
200 sprintf(buffer.text, "%d", number);
201 buffer.mtype = output_flag;
202 if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(buffer.text) + 1, 0)) < 0) perror(_("Messages::write_message"));
206 int Messages::write_message_f(float number)
208 sprintf(buffer.text, "%f", number);
209 buffer.mtype = output_flag;
210 if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(buffer.text) + 1, 0)) < 0) perror(_("Messages::write_message"));
214 int Messages::write_message(int command, char *text)
216 sprintf(buffer.text, "%d %s", command, text);
217 return write_message_raw();
220 int Messages::write_message(long command, long value)
222 sprintf(buffer.text, "%ld %ld", command, value);
223 return write_message_raw();
226 int Messages::write_message_f(float value1, float value2)
228 sprintf(buffer.text, "%f %f", value1, value2);
229 return write_message_raw();
232 int Messages::write_message(long command, long value1, long value2)
234 sprintf(buffer.text, "%ld %ld %ld", command, value1, value2);
235 return write_message_raw();
238 int Messages::write_message(long command, long value1, long value2, long value3)
240 sprintf(buffer.text, "%ld %ld %ld %ld", command, value1, value2, value3);
241 return write_message_raw();
244 int Messages::write_message_f(float value1, float value2, float value3, float value4)
246 sprintf(buffer.text, "%f %f %f %f", value1, value2, value3, value4);
247 return write_message_raw();
250 int Messages::write_message_f(float value1, float value2, float value3)
252 sprintf(buffer.text, "%f %f %f", value1, value2, value3);
253 return write_message_raw();
256 int Messages::get_id()