/* * Copyright (c) 2003 INRIA - All rights reserved * main authors: Christoph Neumann - christoph.neumann@inrialpes.fr * Vincent Roca - vincent.roca@inrialpes.fr * Julien Laboure - julien.laboure@inrialpes.fr * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ #include "flute.h" /*local variables*/ GdomeDocument *fdt; GdomeDOMImplementation *domimpl; /*** Begin of private functions ***/ unsigned long gdomeString2long(GdomeDOMString* myint) { unsigned long result=0; //result=atol((char *) myint->str); //use the following instead (manages greater numbers) int i=0; for(i=0;istr); /* Update File nodes */ name = gdome_str_mkref ("TOI"); for (i = 0; i < nchilds; i++) { printf("kkk\n"); nodeel = gdome_nl_item (childs, i, &exc); if (nodeel == NULL) { EXIT(("NodeList.item(%d): NULL\n\tException #%d\n", (int)i, exc)) } if(gdome_n_nodeType(nodeel,&exc)==GDOME_ELEMENT_NODE) { attr = gdome_el_getAttributeNode((GdomeElement*) nodeel, name, &exc); if (attr == NULL) { EXIT(("Element.getAttributeNode: NULL\n\tException #%d\n", exc)) } toi = gdome_a_nodeValue(attr, &exc); if(! hasTOIinFDT((unsigned int)gdomeString2long(toi)) ) { /* I get reference to the root element of the fdt document */ root = gdome_doc_documentElement (fdt, &exc); if (root == NULL) { EXIT(("Document.documentElement: NULL\n\tException #%d\n", exc)) } /*we have to add this element */ importnode = gdome_doc_importNode(fdt, (GdomeNode* )nodeel, TRUE, &exc); /* I append the imported Node to the childs list of the root element */ resultnode = gdome_el_appendChild (root, (GdomeNode *)importnode, &exc); if (resultnode != (GdomeNode *)importnode) { EXIT(("Element.appendChild: failed\n\tException #%d\n", exc)) } gdome_n_unref(importnode, &exc); gdome_el_unref(root, &exc); } } gdome_n_unref (nodeel, &exc); } if (name != NULL) gdome_str_unref (name); if (toi != NULL) gdome_str_unref (toi); if (attr != NULL) gdome_a_unref(attr,&exc); gdome_el_unref (rootel, &exc); gdome_di_freeDoc (domimpl, fdtinstance, &exc); } /* Adds elements of the FDTinstance to the FileList */ pFFile addTOItoFFile(pFFile filelist, unsigned int toi) { GdomeXPathEvaluator* xpatheval; GdomeException exc; GdomeXPathResult* result=NULL; GdomeDOMString *expression, *filename; GdomeAttr * attr; GdomeElement *rootel, * el; GdomeXPathNSResolver* xpathresolver; char* XPathexpr=(char*)malloc(20); /* First I get a XPath evaluator */ xpatheval = gdome_xpeval_mkref(); /* I get reference to the root element of the document */ rootel = gdome_doc_documentElement (fdt, &exc); if (rootel == NULL) { EXIT(("Document.documentElement: NULL\n\tException #%d\n", exc)) } /* then I get a XPath reseolver */ xpathresolver = gdome_xpeval_createNSResolver (xpatheval, (GdomeNode*) rootel, & exc); /* Evaluate Xpath expression */ sprintf(XPathexpr,"//File[@TOI=%i]",toi); expression = gdome_str_mkref (XPathexpr); result = gdome_xpeval_evaluate (xpatheval,expression,(GdomeNode*) rootel, xpathresolver, GDOME_UNORDERED_NODE_ITERATOR_TYPE, result, &exc); if ((el = (GdomeElement *) gdome_xpresult_singleNodeValue (result, &exc))==NULL) return filelist; else { FFile NewFile; expression = gdome_str_mkref ("Content-Location"); attr = gdome_el_getAttributeNode(el, expression,&exc); filename = gdome_a_value(attr,&exc); strncpy(NewFile.fullname,"./", 2); strncpy(NewFile.fullname+2, filename->str, MAX_PATH+MAX_FILENAME ); NewFile.writeIt = CheckWriteContext(NewFile.fullname, overwrite); NewFile.received=0; NewFile.toi = toi; if(NewFile.writeIt) { #ifdef WIN32 if ((NewFile.fd = open( NewFile.fullname, O_WRONLY | O_CREAT | O_BINARY | O_TRUNC, _S_IWRITE) ) < 0) #else if ((NewFile.fd = open( NewFile.fullname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU)) < 0) #endif { EXIT(("Error while opening file \"%s\"\n", NewFile.fullname)) } } NewFile.next = NULL; FFileInsert(&filelist, NewFile); gdome_el_unref (el, &exc); gdome_a_unref (attr, &exc); gdome_str_unref (filename); } gdome_str_unref (expression); gdome_xpresult_unref(result, &exc); gdome_el_unref (rootel, &exc); gdome_xpnsresolv_unref (xpathresolver,&exc); gdome_xpeval_unref(xpatheval,&exc); return filelist; } /* Adds elements of the FDT to the FileList */ /* ONLY in !interactive mode*/ pFFile updateFFile(pFFile filelist) { GdomeException exc; GdomeAttr* attr=NULL; GdomeElement *rootel; GdomeNodeList *childs = NULL; long i, nchilds; GdomeNode *nodeel; GdomeDOMString *toi=NULL; GdomeDOMString *attrnametoi=NULL; unsigned int inttoi; /* I get reference to the root element of the document */ rootel = gdome_doc_documentElement (fdt, &exc); if (rootel == NULL) { EXIT(("Document.documentElement: NULL\n\tException #%d\n", exc)) } /* I get the reference to the childrens NodeList of the root element */ childs = gdome_el_childNodes (rootel, &exc); if (childs == NULL) { EXIT(("Element.childNodes: NULL\n\tException #%d\n", exc)) } /* Go through the node of the instance and check if it is already included in the FFile */ nchilds = gdome_nl_length (childs, &exc); attrnametoi = gdome_str_mkref ("TOI"); for (i = 0; i < nchilds; i++) { nodeel = gdome_nl_item (childs, i, &exc); if (nodeel == NULL) { EXIT(("NodeList.item(%d): NULL\n\tException #%d\n", (int)i, exc)) } if(gdome_n_nodeType(nodeel,&exc)==GDOME_ELEMENT_NODE) { attr = gdome_el_getAttributeNode((GdomeElement*) nodeel, attrnametoi, &exc); if (attr == NULL) { EXIT(("Element.getAttributeNode: NULL\n\tException #%d\n", exc)) } toi = gdome_a_nodeValue(attr, &exc); inttoi=(unsigned int) gdomeString2long(toi); if(((FFileFindTOI(inttoi, filelist))==NULL)) { filelist=addTOItoFFile(filelist, inttoi); { int mcl_option = inttoi; if (mcl_ctl(id, MCL_OPT_FLUTE_DELIVER_THIS_ADU, (void*)&mcl_option, sizeof(mcl_option))) EXIT(("mcl_ctl: MCL_OPT_FLUTE_DELIVER_THIS_ADU failed\n")) } } gdome_n_unref (nodeel, &exc); } } if (toi != NULL) gdome_str_unref (toi); if (attrnametoi != NULL) gdome_str_unref (attrnametoi); if (attr != NULL) gdome_a_unref(attr,&exc); gdome_el_unref (rootel, &exc); return filelist; } unsigned int displayFDT(unsigned int highlight, pFFile filelist) { GdomeAttr* attr=NULL; unsigned int i, nchilds,j; GdomeNode *nodeel; GdomeElement *rootel; GdomeDOMString *attrname=NULL; GdomeDOMString *toi=NULL; GdomeDOMString *attrnametoi=NULL; GdomeDOMString *filename=NULL; GdomeNodeList *childs = NULL; GdomeException exc; unsigned int returntoi=0; /*clear screen*/ printf("\e[H\e[J"); /* I get reference to the root element of the document */ rootel = gdome_doc_documentElement (fdt, &exc); if (rootel == NULL) { EXIT(("Document.documentElement: NULL\n\tException #%d\n", exc)) } /* I get the reference to the childrens NodeList of the root element */ childs = gdome_el_childNodes (rootel, &exc); if (childs == NULL) { EXIT(("Element.childNodes: NULL\n\tException #%d\n", exc)) } /* Go through the node of the instance and check if it is already included in the FDT */ if ((nchilds = gdome_nl_length (childs, &exc))==0) { printf("FDT is empty\n"); goto exit; } printf(" TOI \t Content-Location"); if (filelist!=NULL) printf("\t Selected \t Received "); printf("\n\n"); attrname = gdome_str_mkref ("Content-Location"); attrnametoi = gdome_str_mkref ("TOI"); for (i = 0, j = 0; i < nchilds; i++) { nodeel = gdome_nl_item (childs, i, &exc); if (nodeel == NULL) { EXIT(("NodeList.item(%d): NULL\n\tException #%d\n", (int)i, exc)) } if (gdome_n_nodeType(nodeel,&exc)==GDOME_ELEMENT_NODE) { attr = gdome_el_getAttributeNode((GdomeElement*) nodeel, attrname, &exc); if (attr == NULL) { EXIT(("Element.getAttributeNode: NULL\n\tAttribute %s\n\tException #%d\n",(char *)attrname->str, exc)) } filename = gdome_a_nodeValue(attr, &exc); attr = gdome_el_getAttributeNode((GdomeElement*) nodeel, attrnametoi, &exc); if (attr == NULL) { EXIT(("Element.getAttributeNode: NULL\n\tAttribute %s\n\tException #%d\n",(char *)attrnametoi->str, exc)) } toi = gdome_a_nodeValue(attr, &exc); if (j==highlight) printf("\e[7m"); printf(" %s \t %s ", (char *)toi->str,(char *)filename->str); if (filelist!=NULL) { pFFile afile; if((afile=FFileFindTOI((unsigned int)gdomeString2long(toi),filelist))!=NULL) { printf(" X \t"); if(afile->received) printf(" X "); } } printf("\n"); if (j==highlight) { printf("\e[0m"); returntoi=gdomeString2long(toi); } j++; } gdome_n_unref (nodeel, &exc); } /*Get the expires value*/ printf("\nExpires: %lu (max: %lu)\n", getExpires(), ULONG_MAX); exit: if (attrname != NULL) gdome_str_unref (attrname); if (filename != NULL) gdome_str_unref (filename); if (toi != NULL) gdome_str_unref (toi); if (attrnametoi != NULL) gdome_str_unref (attrnametoi); if (attr != NULL) gdome_a_unref(attr,&exc); gdome_el_unref (rootel, &exc); return returntoi; } GdomeElement* createNewFile() { GdomeElement *el; GdomeDOMString *name; GdomeException exc; /* I create a new element called RELEASE */ name = gdome_str_mkref ("File"); el = gdome_doc_createElement (fdt, name, &exc); if (el == NULL) { EXIT(("Document.createElement: NULL\n\tException #%d\n", exc)) } gdome_str_unref (name); return el; } void appendFile(GdomeElement *el) { GdomeElement *root; GdomeException exc; GdomeNode *result; /* I get reference to the root element of the document */ root = gdome_doc_documentElement (fdt, &exc); if (root == NULL) { EXIT(("Document.documentElement: NULL\n\tException #%d\n", exc)) } /* I append the FILE element to the childs list of the root element */ result = gdome_el_appendChild (root, (GdomeNode *)el, &exc); if (result != (GdomeNode *)el) { EXIT(("Element.appendChild: failed\n\tException #%d\n", exc)) } gdome_el_unref(el, &exc); gdome_el_unref(root, &exc); } GdomeElement* setFileAttritbute(GdomeElement *el, char* iname, char* ivalue) { GdomeDOMString *name, *value; GdomeException exc; /* I add the attribute to the element */ name = gdome_str_mkref (iname); value = gdome_str_mkref (ivalue); gdome_el_setAttribute (el, name, value, &exc); if (exc) { EXIT(("Element.setAttribute: failed\n\tException #%d\n", exc)) } gdome_str_unref (name); gdome_str_unref (value); return el; } GdomeDocument* createNewFDTinstance() { GdomeElement *rootel; GdomeDOMString *name,*value; GdomeDocument* fdtinstance; GdomeException exc; char* tempstr=(char*) malloc(10); /* First I get a DOMImplementation reference */ domimpl = gdome_di_mkref (); /* I create a new document with FDT-Payload as root element */ name = gdome_str_mkref ("FDT-Payload"); fdtinstance = gdome_di_createDocument(domimpl, NULL, name, NULL, &exc); if (fdt == NULL) { EXIT(("DOMImplementation.createDocument: failed\n\tException #%d\n", exc)) } /* I get reference to the root element of the document */ rootel = gdome_doc_documentElement (fdtinstance, &exc); if (rootel == NULL) { EXIT(("Document.documentElement: NULL\n\tException #%d\n", exc)) } /* I add the Expires attribute to the element */ name = gdome_str_mkref ("Expires"); sprintf(tempstr,"%lu",getExpires()); value = gdome_str_mkref (tempstr); gdome_el_setAttribute (rootel, name, value, &exc); if (exc) { EXIT(("Element.setAttribute: failed\n\tException #%d\n", exc)) } gdome_str_unref (name); gdome_str_unref (value); gdome_el_unref (rootel, &exc); return fdtinstance; } GdomeDocument* AddFileToFDTinstance(GdomeDocument* fdtinstance ,unsigned int itoi) { GdomeException exc; GdomeAttr* attr=NULL; GdomeElement *rootel, *root; GdomeNodeList *childs = NULL; long i, nchilds; GdomeNode *importnode,*resultnode, *nodeel; GdomeDOMString *name=NULL; GdomeDOMString *toi=NULL; /* I get reference to the root element of the fdt document */ rootel = gdome_doc_documentElement (fdt, &exc); if (rootel == NULL) { EXIT(("Document.documentElement: NULL\n\tException #%d\n", exc)) } /* I get the reference to the childrens NodeList of the root element */ childs = gdome_el_childNodes (rootel, &exc); if (childs == NULL) { EXIT(("Element.childNodes: NULL\n\tException #%d\n", exc)) } /* Go through the node of the instance and search TOI in the FDT */ nchilds = gdome_nl_length (childs, &exc); /* Update File nodes */ name = gdome_str_mkref ("TOI"); for (i = 0; i < nchilds; i++) { nodeel = gdome_nl_item (childs, i, &exc); if (nodeel == NULL) { EXIT(("NodeList.item(%d): NULL\n\tException #%d\n", (int)i, exc)) } if(gdome_n_nodeType(nodeel,&exc)==GDOME_ELEMENT_NODE) { attr = gdome_el_getAttributeNode((GdomeElement*) nodeel, name, &exc); if (attr == NULL) { EXIT(("Element.getAttributeNode: NULL\n\tException #%d\n", exc)) } toi = gdome_a_nodeValue(attr, &exc); if(itoi==(unsigned int) gdomeString2long(toi)) { /* I get reference to the root element of the fdtinstance document */ root = gdome_doc_documentElement (fdtinstance, &exc); if (root == NULL) { EXIT(("Document.documentElement: NULL\n\tException #%d\n", exc)) } /*we have to add this element */ importnode = gdome_doc_importNode(fdtinstance, (GdomeNode* )nodeel, TRUE, &exc); /* I append the imported Node to the childs list of the root element */ resultnode = gdome_el_appendChild (root, (GdomeNode *)importnode, &exc); if (resultnode != (GdomeNode *)importnode) { EXIT(("Element.appendChild: failed\n\tException #%d\n", exc)) } gdome_n_unref(importnode, &exc); gdome_el_unref(root, &exc); } } gdome_n_unref (nodeel, &exc); } if (name != NULL) gdome_str_unref (name); if (toi != NULL) gdome_str_unref (toi); if (attr != NULL) gdome_a_unref(attr,&exc); gdome_el_unref (rootel, &exc); return fdtinstance; } int getFinalFDTInstance(GdomeDocument* fdtinstance, char ** buffer) { GdomeException exc; int max_fragment_size; int i=0; max_fragment_size = RSE_MAX_FRAGMENT_SIZE; #ifdef ALC max_fragment_size = max(max_fragment_size, LDPC_MAX_FRAGMENT_SIZE); #endif /* RM_PROTOCOL */ max_fragment_size = max(max_fragment_size, NO_FEC_MAX_FRAGMENT_SIZE); if (!gdome_di_saveDocToMemory(domimpl, fdtinstance, buffer, GDOME_SAVE_STANDARD, &exc)) { EXIT(("DOMImplementation.saveDocToFile: failed\n\tException #%d\n", exc)) } /* I free the document structure and the DOMImplementation */ //gdome_di_freeDoc (domimpl, fdtinstance, &exc); while(strncmp((*buffer)+i, "\0", 1)!=0) { i++; } return i; }