1
|
/*
|
2
|
This source file is part of Scol
|
3
|
For the latest info, see http://www.scolring.org
|
4
|
|
5
|
Copyright (c) 2011 Stephane Bisaro, aka Iri <iri@irizone.net>
|
6
|
|
7
|
This program is free software; you can redistribute it and/or modify it under
|
8
|
the terms of the GNU Lesser General Public License as published by the Free Software
|
9
|
Foundation; either version 2 of the License, or (at your option) any later
|
10
|
version.
|
11
|
|
12
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
13
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
14
|
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
15
|
|
16
|
You should have received a copy of the GNU Lesser General Public License along with
|
17
|
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
18
|
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
19
|
http://www.gnu.org/copyleft/lesser.txt
|
20
|
|
21
|
For others informations, please contact us from http://www.scolring.org/
|
22
|
*/
|
23
|
|
24
|
#ifdef __cplusplus
|
25
|
#error This source file is not C++ but rather C. Please use a C-compiler
|
26
|
#endif
|
27
|
|
28
|
#include "../include/scol_glib_net.h"
|
29
|
|
30
|
#if ((defined _WIN32) || (defined __WIN32__))
|
31
|
cbmachine ww;
|
32
|
#endif
|
33
|
mmachine mm;
|
34
|
|
35
|
|
36
|
|
37
|
int SCOL_gnetConnect (mmachine m)
|
38
|
{
|
39
|
int maddress, mport;
|
40
|
gchar *address, *buffer, *url;
|
41
|
int port, bufferpos = 0;
|
42
|
gssize received = 0;
|
43
|
GString *page = NULL;
|
44
|
GSocketConnectable *addr;
|
45
|
GSocket * socket;
|
46
|
GSocketConnection *connection = NULL;
|
47
|
GSocketClient *client;
|
48
|
GError *err = NULL;
|
49
|
|
50
|
const int sizeloop = 32*1024;
|
51
|
|
52
|
mport = MMpull (m);
|
53
|
maddress = MMpull (m);
|
54
|
/*mchannel = MMget (m, 0);
|
55
|
|
56
|
if (mchannel == NIL)
|
57
|
{
|
58
|
MMechostr (0, "SCOL_gnetConnect : entering\n");
|
59
|
MMpull (m);
|
60
|
MMpush (m, NIL);
|
61
|
return 0;
|
62
|
}*/
|
63
|
|
64
|
if (maddress == NIL)
|
65
|
address = "127.0.0.1";
|
66
|
else
|
67
|
address = MMstartstr (m, MTOP (maddress));
|
68
|
if (mport == NIL)
|
69
|
port = 80;
|
70
|
else
|
71
|
port = MTOI (mport);
|
72
|
|
73
|
url = "index.php";
|
74
|
|
75
|
addr = g_network_address_new (address, port);
|
76
|
client = g_socket_client_new ();
|
77
|
connection = g_socket_client_connect (client, addr, NULL, &err);
|
78
|
if (err != NULL)
|
79
|
{
|
80
|
MMechostr (0, "SCOL_gnetConnect error : %d -> %s", err->code, err->message);
|
81
|
MMpush (m, NIL);
|
82
|
g_error_free (err);
|
83
|
return 0;
|
84
|
}
|
85
|
socket = g_socket_connection_get_socket (connection);
|
86
|
|
87
|
if (socket)
|
88
|
{
|
89
|
buffer = g_malloc (sizeof (gchar) * sizeloop);
|
90
|
page = g_string_new (NULL);
|
91
|
g_socket_send_with_blocking(socket, url, strlen (url), FALSE, NULL, NULL);
|
92
|
while ((received = g_socket_receive_with_blocking (socket, buffer, sizeloop, FALSE, NULL, NULL)))
|
93
|
{
|
94
|
for (bufferpos = 0; bufferpos < received; bufferpos++)
|
95
|
{
|
96
|
g_string_append_c (page, buffer[bufferpos]);
|
97
|
}
|
98
|
}
|
99
|
g_free (buffer);
|
100
|
}
|
101
|
else
|
102
|
{
|
103
|
MMechostr (0, "SCOL_gnetConnect error : no socket available\n");
|
104
|
MMpush (m, NIL);
|
105
|
return 0;
|
106
|
}
|
107
|
Mpushstrbloc (m, page->str);
|
108
|
g_string_free (page, TRUE);
|
109
|
|
110
|
g_socket_close (socket, NULL);
|
111
|
return 0;
|
112
|
}
|
113
|
|
114
|
|
115
|
|
116
|
|
117
|
|
118
|
|
119
|
|
120
|
/* API definitions : */
|
121
|
|
122
|
char* glib_net_name[GLIB_NET_PKG_NB]=
|
123
|
{
|
124
|
"_gnetConnect"
|
125
|
};
|
126
|
|
127
|
int (*glib_net_fun[GLIB_NET_PKG_NB])(mmachine m)=
|
128
|
{
|
129
|
SCOL_gnetConnect
|
130
|
};
|
131
|
|
132
|
int glib_net_narg[GLIB_NET_PKG_NB]=
|
133
|
{
|
134
|
3
|
135
|
};
|
136
|
|
137
|
char* glib_net_type[GLIB_NET_PKG_NB]=
|
138
|
{
|
139
|
"fun [S S] S"
|
140
|
};
|
141
|
|
142
|
/**
|
143
|
* \brief Load the Scol api
|
144
|
*/
|
145
|
int SCOLinitGnetClass (mmachine m)
|
146
|
{
|
147
|
int k;
|
148
|
|
149
|
MMechostr (0, "SCOLinitGnetClass : entering\n");
|
150
|
|
151
|
k = PKhardpak (m, "GNETengine", GLIB_NET_PKG_NB, glib_net_name, glib_net_fun, glib_net_narg, glib_net_type);
|
152
|
return k;
|
153
|
}
|
154
|
|
155
|
|
156
|
/**
|
157
|
* \brief Load and free the regular expression library
|
158
|
* Plateforms supported : MS Windows and GNU / Linux
|
159
|
*/
|
160
|
|
161
|
int GnetRelease ()
|
162
|
{
|
163
|
MMechostr (0, "\nGNET library released !\n");
|
164
|
return 0;
|
165
|
}
|
166
|
|
167
|
#if ((defined _WIN32) || (defined __WIN32__))
|
168
|
|
169
|
__declspec (dllexport) int SCOLloadGNET (mmachine m, cbmachine w)
|
170
|
{
|
171
|
int k = 0;
|
172
|
ww = w;
|
173
|
mm = m;
|
174
|
|
175
|
SCOLinitplugin (w);
|
176
|
if ((k = SCOLinitGnetClass (m))) return k;
|
177
|
MMechostr(MSKDEBUG, "\nGNET library loaded !\n");
|
178
|
return k;
|
179
|
}
|
180
|
|
181
|
__declspec (dllexport) int SCOLfreeGNET ()
|
182
|
{
|
183
|
GnetRelease ();
|
184
|
return 0;
|
185
|
}
|
186
|
|
187
|
|
188
|
|
189
|
|
190
|
|
191
|
/* Version GNU / Linux */
|
192
|
#elif ((defined linux) || (defined __linux))
|
193
|
|
194
|
int SCOLloadGNET (mmachine m)
|
195
|
{
|
196
|
int k = 0;
|
197
|
mm = m;
|
198
|
|
199
|
if ((k = SCOLinitGnetClass (m))) return k;
|
200
|
MMechostr (MSKDEBUG, "\nGNET library loaded !\n");
|
201
|
|
202
|
return k;
|
203
|
}
|
204
|
|
205
|
int SCOLfreeGNET ()
|
206
|
{
|
207
|
MMechostr(MSKDEBUG, "\nGNET library release !\n");
|
208
|
|
209
|
GnetRelease ();
|
210
|
return 0;
|
211
|
}
|
212
|
|
213
|
#else
|
214
|
#error no platform supported
|
215
|
#endif
|