2 libfmail: Socket Based IPC mechanism
4 Copyright (C) 2007 Carlos Daniel Ruvalcaba Valenzuela <clsdaniel@gmail.com>
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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <libfmail/socket.h>
30 #include <libfmail/ipcmsg.h>
31 #include <libfmail/ipc.h>
32 #include <libfmail/socketipc.h>
34 SocketIPC::SocketIPC(Socket
*s
){
38 SocketIPC::SocketIPC(char *uri
){
42 pcrecpp::RE
re("([\\w\\d\\.]+)\\:(\\d*)[/]*(\\w*)");
44 re
.PartialMatch(uri
, &shost
, &port
, &options
);
45 host
= (char*)shost
.c_str();
47 printf("Started socket on: %s\n", uri
);
48 printf("Host: %s\nPort: %i\n", host
, port
);
49 sock
= Socket::CreateSocket(SOCKET_INET
, 0);
51 sock
->setAddress(host
);
55 int SocketIPC::RequestIPC(){
56 return sock
->Connect();
59 int SocketIPC::ListenIPC(){
63 auxsock
= sock
->Accept();
67 int SocketIPC::CloseIPC(){
73 /* TODO: Change to tree/graph based state machine */
74 int SocketIPC::FetchMessage2(){
75 char buffer
[255], xbuff
[255], tbuff
[20], *msgdata
;
77 int i
, t
, r
, mlen
, margc
;
92 memset(buffer
, 0, 255);
93 r
= auxsock
->Read(buffer
, 255);
101 for (i
= 0; i
< r
; i
++){
104 if (buffer
[i
] == 'M')
108 if (buffer
[i
] == 'S')
112 if (buffer
[i
] == 'G')
116 if (buffer
[i
] == '['){
118 memset(tbuff
, 0, 20);
123 if ((buffer
[i
] >= 48) && (buffer
[i
] <= 57)){
124 tbuff
[t
] = buffer
[i
];
134 if (buffer
[i
] == '['){
136 memset(tbuff
, 0, 20);
141 if ((buffer
[i
] >= 48) && (buffer
[i
] <= 57)){
142 tbuff
[t
] = buffer
[i
];
152 msgdata
= (char*)malloc(sizeof(char) * mlen
+ 1);
159 msgdata
[t
] = buffer
[i
];
168 msg
= new IPCMessage(msgdata
);
169 if (msgdata
!= xbuff
)
178 if (buffer
[i
] == 'P')
182 if (buffer
[i
] == 'A')
186 if (buffer
[i
] == 'R')
190 if (buffer
[i
] == 'A')
194 if (buffer
[i
] == 'M')
198 if (buffer
[i
] == '['){
200 memset(tbuff
, 0, 20);
205 if ((buffer
[i
] >= 48) && (buffer
[i
] <= 57)){
206 tbuff
[t
] = buffer
[i
];
219 msgdata
= (char*)malloc(sizeof(char) * mlen
+ 1);
227 msgdata
[t
] = buffer
[i
];
237 msg
->PushParam(msgdata
);
238 if (msgdata
!= xbuff
)
240 if (msg
->ParamCount() == margc
){
266 char buffer[255], *param;
270 memset(buffer, 0, 255);
271 r = auxsock->Read(buffer, 255);
276 printf("buffer: %s\n", buffer);
277 m = odk_regex_match(re, buffer, 0);
281 printf("Regex Matched!!\n");
282 if (buffer[0] != 'M')
285 param = odk_submatch_copy(buffer, m, 1);
289 param = odk_submatch_copy(buffer, m, 2);
293 memset(buffer, 0, 255);
294 r = auxsock->Read(buffer, 255);
299 msg = new IPCMessage(buffer);
300 for (i = 0; i < j; i++){
301 auxsock->Read(buffer, 255);
302 m = odk_regex_match(re, buffer, 0);
304 if ((m == NULL) || (buffer[0] != 'P'))
306 param = odk_submatch_copy(buffer, m, 1);
310 if ((k < 1) || (k > 32000))
313 param = (char*)malloc(sizeof(char) * k+1);
315 memset(param, 0, k+1);
319 r = auxsock->Read(buffer, 255);
320 memcpy((char*)((int)param+s), buffer, r);
323 msg->PushParam(param);
331 int SocketIPC::PeekMessage(){
336 if (msgQueue
.size() > 0)
342 /* TODO: Better memory management for IPCMessage */
343 int SocketIPC::PushMessage(IPCMessage
*msg
){
344 char *tmp
, buffer
[50];
347 tmp
= msg
->GetMessageName();
350 sprintf(buffer
, "MSG[%i][%i]", l
, msg
->ParamCount());
351 blen
= strlen(buffer
);
353 sock
->Write(buffer
, blen
);
356 while(tmp
= msg
->PopParam()){
359 sprintf(buffer
, "PARAM[%i]", l
);
360 blen
= strlen(buffer
);
362 sock
->Write(buffer
, blen
);
371 IPCMessage
*SocketIPC::PopMessage(){
374 msg
= msgQueue
.front();
380 int SocketIPC::RawRead(char *buff
, int size
){
381 return sock
->Read(buff
, size
);
384 int SocketIPC::RawWrite(char *buff
, int size
){
385 return sock
->Write(buff
, size
);