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
.FullMatch(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
);
53 int SocketIPC::RequestIPC(){
54 return sock
->Connect(host
, port
);
57 int SocketIPC::ListenIPC(){
61 auxsock
= sock
->Accept();
65 int SocketIPC::CloseIPC(){
71 int SocketIPC::FetchMessage2(){
72 char buffer
[255], xbuff
[255], tbuff
[20], *msgdata
;
74 int i
, t
, r
, mlen
, margc
;
89 memset(buffer
, 0, 255);
90 r
= auxsock
->Read(buffer
, 255);
98 for (i
= 0; i
< r
; i
++){
101 if (buffer
[i
] == 'M')
105 if (buffer
[i
] == 'S')
109 if (buffer
[i
] == 'G')
113 if (buffer
[i
] == '['){
115 memset(tbuff
, 0, 20);
120 if ((buffer
[i
] >= 48) && (buffer
[i
] <= 57)){
121 tbuff
[t
] = buffer
[i
];
131 if (buffer
[i
] == '['){
133 memset(tbuff
, 0, 20);
138 if ((buffer
[i
] >= 48) && (buffer
[i
] <= 57)){
139 tbuff
[t
] = buffer
[i
];
149 msgdata
= (char*)malloc(sizeof(char) * mlen
+ 1);
156 msgdata
[t
] = buffer
[i
];
165 msg
= new IPCMessage(msgdata
);
166 if (msgdata
!= xbuff
)
175 if (buffer
[i
] == 'P')
179 if (buffer
[i
] == 'A')
183 if (buffer
[i
] == 'R')
187 if (buffer
[i
] == 'A')
191 if (buffer
[i
] == 'M')
195 if (buffer
[i
] == '['){
197 memset(tbuff
, 0, 20);
202 if ((buffer
[i
] >= 48) && (buffer
[i
] <= 57)){
203 tbuff
[t
] = buffer
[i
];
216 msgdata
= (char*)malloc(sizeof(char) * mlen
+ 1);
224 msgdata
[t
] = buffer
[i
];
234 msg
->PushParam(msgdata
);
235 if (msgdata
!= xbuff
)
237 if (msg
->ParamCount() == margc
){
263 char buffer[255], *param;
267 memset(buffer, 0, 255);
268 r = auxsock->Read(buffer, 255);
273 printf("buffer: %s\n", buffer);
274 m = odk_regex_match(re, buffer, 0);
278 printf("Regex Matched!!\n");
279 if (buffer[0] != 'M')
282 param = odk_submatch_copy(buffer, m, 1);
286 param = odk_submatch_copy(buffer, m, 2);
290 memset(buffer, 0, 255);
291 r = auxsock->Read(buffer, 255);
296 msg = new IPCMessage(buffer);
297 for (i = 0; i < j; i++){
298 auxsock->Read(buffer, 255);
299 m = odk_regex_match(re, buffer, 0);
301 if ((m == NULL) || (buffer[0] != 'P'))
303 param = odk_submatch_copy(buffer, m, 1);
307 if ((k < 1) || (k > 32000))
310 param = (char*)malloc(sizeof(char) * k+1);
312 memset(param, 0, k+1);
316 r = auxsock->Read(buffer, 255);
317 memcpy((char*)((int)param+s), buffer, r);
320 msg->PushParam(param);
328 int SocketIPC::PeekMessage(){
333 if (msgQueue
.size() > 0)
339 int SocketIPC::PushMessage(IPCMessage
*msg
){
340 char *tmp
, buffer
[50];
343 tmp
= msg
->GetMessageName();
346 sprintf(buffer
, "MSG[%i][%i]", l
, msg
->ParamCount());
347 blen
= strlen(buffer
);
349 sock
->Write(buffer
, blen
);
352 while(tmp
= msg
->PopParam()){
355 sprintf(buffer
, "PARAM[%i]", l
);
356 blen
= strlen(buffer
);
358 sock
->Write(buffer
, blen
);
367 IPCMessage
*SocketIPC::PopMessage(){
370 msg
= msgQueue
.front();
376 int SocketIPC::RawRead(char *buff
, int size
){
377 return sock
->Read(buff
, size
);
380 int SocketIPC::RawWrite(char *buff
, int size
){
381 return sock
->Write(buff
, size
);