32 #include <system_threads.h>
33 #include <system_net.h>
37 #include <libFreeWRL.h>
39 #include "../vrml_parser/Structs.h"
40 #include "../main/headers.h"
41 #include "../x3d_parser/Bindable.h"
43 #include "pluginUtils.h"
44 #include "PluginSocket.h"
48 OLDCODE #ifdef F_SETSIG
49 OLDCODE #define FSIGOK
55 #define LOCK_PLUGIN_COMMUNICATION pthread_mutex_lock(&p->mylocker);
56 #define UNLOCK_PLUGIN_COMMUNICATION pthread_mutex_unlock(&p->mylocker);
63 pthread_mutex_t mylocker;
66 char return_url[FILENAME_MAX];
69 void *PluginSocket_constructor(){
74 void PluginSocket_init(
struct tPluginSocket *t){
77 t->prv = PluginSocket_constructor();
80 pthread_mutex_init(&(p->mylocker), NULL);
91 #ifdef PLUGINSOCKETVERBOSE
93 static void pluginprint (
const char *m,
const char *p)
96 if (gglobal()->internalc.global_plugin_print) {
99 printf (
"%f: freewrl: ",myt);
107 int waitForData(
int sock) {
114 #ifdef PLUGINSOCKETVERBOSE
115 pluginprint (
"waitForData, socket %d\n",sock);
132 FD_SET(sock, &p->rfds);
135 retval = select(sock+1, &p->rfds, NULL, NULL, &p->tv);
139 #ifdef PLUGINSOCKETVERBOSE
140 pluginprint (
"waitForData returns TRUE\n",
"");
146 if (count > totalcount) {
147 #ifdef PLUGINSOCKETVERBOSE
148 pluginprint (
"waitForData, timing out\n",
"");
158 void requestPluginPrint(
int to_plugin,
const char *msg) {
159 size_t len = 0, ulen = 0, bytes = 0;
162 request.notifyCode = 2;
164 len = FILENAME_MAX *
sizeof(char);
165 memset(request.url, 0, len);
167 ulen = strlen(msg) + 1;
168 memmove(request.url, msg, ulen);
172 if (write(to_plugin, (
urlRequest *) &request, bytes) < 0) {
173 printf (
"COULD NOT WRITE TO THE PLUGIN SOCKET!\n");
177 char * requestUrlfromPlugin(
int to_plugin, uintptr_t plugin_instance,
const char *url) {
178 size_t len = 0, ulen = 0, bytes = 0;
184 char encodedUrl[2000];
187 LOCK_PLUGIN_COMMUNICATION
191 URLencod(encodedUrl,url,2000);
193 #ifdef PLUGINSOCKETVERBOSE
194 pluginprint (
"NEW REQUEST\n",url);
195 pluginprint (
"requestURL fromPlugin, getting %s\n",url);
196 pluginprint (
" ... encoded is %s\n",encodedUrl);
199 request.instance = (
void *) plugin_instance;
200 request.notifyCode = 0;
202 len = FILENAME_MAX *
sizeof(char);
203 memset(request.url, 0, len);
204 memset(p->return_url, 0, len);
206 ulen = strlen(encodedUrl) + 1;
207 memmove(request.url, encodedUrl, ulen);
211 #ifdef PLUGINSOCKETVERBOSE
212 pluginprint (
"requestURL fromPlugin, step 1\n",
"");
213 pluginprint (
"sending url request to socket %d\n",to_plugin);
216 if (write(to_plugin, (
urlRequest *) &request, bytes) < 0) {
217 #ifdef PLUGINSOCKETVERBOSE
218 pluginprint (
"write failed in requestUrlfromPlugin",
"");
223 #ifdef PLUGINSOCKETVERBOSE
224 pluginprint (
"requestURL fromPlugin, step 2\n",
"");
230 if (!waitForData(to_plugin)) {
231 request.notifyCode = -99;
232 if (write(to_plugin, (
urlRequest *) &request, bytes) < 0) {
233 #ifdef PLUGINSOCKETVERBOSE
234 pluginprint (
"write failed in requestUrlfromPlugin",
"");
236 UNLOCK_PLUGIN_COMMUNICATION
240 ConsoleMessage (
"failed to find URL %s\n",url);
241 UNLOCK_PLUGIN_COMMUNICATION
246 if (read(to_plugin, (
char *) p->return_url, len) < 0) {
247 #ifdef PLUGINSOCKETVERBOSE
248 pluginprint(
"read failed in requestUrlfromPlugin",
"");
249 pluginprint(
"Testing: error from read -- returned url is %s.\n", return_url);
251 UNLOCK_PLUGIN_COMMUNICATION
255 #ifdef PLUGINSOCKETVERBOSE
256 pluginprint (
"requestURL fromPlugin, returning %s\n",return_url);
257 pluginprint (
"REQUEST FINISHED\n",return_url);
261 #define returnErrorString "this file is not to be found on the internet"
262 if (strncmp(p->return_url,returnErrorString,strlen(returnErrorString)) == 0)
return NULL;
265 infile = fopen (p->return_url,
"r");
266 if (infile == NULL) {
267 #ifdef PLUGINSOCKETVERBOSE
268 pluginprint (
"requestUrlFromPlugin, file %s could not be opened",return_url);
271 UNLOCK_PLUGIN_COMMUNICATION
276 linelen = (int) fread (buf,1,2000,infile);
278 while ((linelen > 0) && (linecount < 5)){
284 if (strstr(buf,
"<TITLE>404 Not Found</TITLE>") != NULL) {
285 #ifdef PLUGINSOCKETVERBOSE
286 pluginprint (
"found a 404 in :%s:\n",buf);
289 UNLOCK_PLUGIN_COMMUNICATION
293 linelen = (int) fread (buf,1,2000,infile);
298 UNLOCK_PLUGIN_COMMUNICATION
301 return p->return_url;
308 void requestNewWindowfromPlugin(
int sockDesc,
309 uintptr_t plugin_instance,
312 size_t len = 0, ulen = 0, bytes = 0;
316 #ifdef PLUGINSOCKETVERBOSE
317 pluginprint (
"requestNewWindow fromPlugin, getting %s\n",url);
320 request.instance = (
void *) plugin_instance;
321 request.notifyCode = 1;
323 len = FILENAME_MAX *
sizeof(char);
324 memset(request.url, 0, len);
325 memset(p->return_url, 0, len);
327 ulen = strlen(url) + 1;
328 memmove(request.url, url, ulen);
331 #ifdef PLUGINSOCKETVERBOSE
332 pluginprint (
"requestNewWindow fromPlugin, step 1\n",
"");
335 if (write(sockDesc, (
urlRequest *) &request, bytes) < 0) {
336 #ifdef PLUGINSOCKETVERBOSE
337 pluginprint (
"write failed in requestUrlfromPlugin",
"");