"""Module for creating xml"""

from xml.dom.minidom import Document

class CreateXml:
	"""Functions for creating xml using dictionaries received from the network module and for sending it to the respective client"""
	
	def __init__(self):
		"""Creates a minidom Document doc"""
		self.doc = Document()
	
	def createXml_cacherequest(self, argsIn_dict):
		"""Creates xml according to the schema
		<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
		<xsd:include schemaLocation="client.xsd"/>
		<xsd:element name="cacherequest" type="cache"/>
		<xsd:complexType name="cache">
  		<xsd:sequence>
    		<xsd:element name="destination" type="client" minOccurs="1" 
      		maxOccurs="1"/>
    		<xsd:element name="source" type="client" minOccurs="1" maxOccurs="1"/>
		    		<xsd:element name="count" type="xsd:nonNegativeInteger" minOccurs="1" 
    		  maxOccurs="1"/>
    		<xsd:element name="requestid" type="xsd:nonNegativeInteger" minOccurs="1" 
      		maxOccurs="1"/>
  		</xsd:sequence>
		</xsd:complexType>
		</xsd:schema>"""
		
		"""element cacherequest"""
		creq = self.doc.createElement("cacherequest")
		
		"""element destination"""
		dest = self.doc.createElement("destination")
		
		dUid = self.doc.createElement("uid")
		dUidText = self.doc.createTextNode(argsIn_dict['destination']['uid'])
		dUid.appendChild(dUidText)
		dest.appendChild(dUid)
		
		dIp = self.doc.createElement("ip")
		dIpText = self.doc.createTextNode(argsIn_dict['destination']['ip'])
		dIp.appendChild(dIpText)
		dest.appendChild(dIp)
		
		dPort = self.doc.createElement("port")
		dPortText = self.doc.createTextNode(argsIn_dict['destination']['port'])
		dPort.appendChild(dPortText)
		dest.appendChild(dPort)
		
		dIpv = self.doc.createElement("ipversion")
		dIpvText = self.doc.createTextNode(argsIn_dict['destination']['ipversion'])
		dIpv.appendChild(dIpvText)
		dest.appendChild(dIpv)
			
		creq.appendChild(dest)
		
		"""element source"""
		source = self.doc.createElement("source")
		
		sUid = self.doc.createElement("uid")
		sUidText = self.doc.createTextNode(argsIn_dict['source']['uid'])
		sUid.appendChild(sUidText)
		source.appendChild(sUid)
		
		sIp = self.doc.createElement("ip")
		sIpText = self.doc.createTextNode(argsIn_dict['source']['ip'])
		sIp.appendChild(sIpText)
		source.appendChild(sIp)
		
		sPort = self.doc.createElement("port")
		sPortText = self.doc.createTextNode(argsIn_dict['source']['port'])
		sPort.appendChild(sPortText)
		source.appendChild(sPort)
		
		sIpv = self.doc.createElement("ipversion")
		sIpvText = self.doc.createTextNode(argsIn_dict['source']['ipversion'])
		sIpv.appendChild(sIpvText)
		source.appendChild(sIpv)
			
		creq.appendChild(source)
			
		"""element count"""
		count = self.doc.createElement("count")
		countText = self.doc.createTextNode(argsIn_dict['count'])
		count.appendChild(countText)
		creq.appendChild(count)
		
		"""element requestid"""
		reqid = self.doc.createElement("requestid")
		reqidText = self.doc.createTextNode(argsIn_dict['requestid'])
		reqid.appendChild(reqidText)
		creq.appendChild(reqid)
		
		"""return xml"""
		str_xml = "<?xml version='1.0'?>" + creq.toxml()
		return str_xml
		
	def createXml_cacheresponse(self, argsIn_dict):
		"""Creates xml according to the schema
		<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
		<xsd:include schemaLocation="client.xsd"/>
		<xsd:element name="cacheresponse" type="cacheres"/>
		<xsd:complexType name="cacheres">
  		<xsd:sequence>
    		<xsd:element name="destination" type="client" minOccurs="1" 
      		maxOccurs="1"/>
    		<xsd:element name="source" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="list" minOccurs="1" maxOccurs="1">
      		<xsd:complexType>
        		<xsd:sequence>
          		<xsd:element name="node" type="client" minOccurs="0" 
            		maxOccurs="unbounded"/>
        		</xsd:sequence>
      		</xsd:complexType>
    		</xsd:element>
    		<xsd:element name="requestid" type="xsd:nonNegativeInteger" 
      		minOccurs="1" maxOccurs="1"/>
  		</xsd:sequence>
		</xsd:complexType>
		</xsd:schema>"""
		
		"""element cacherequest"""
		cres = self.doc.createElement("cacheresponse")
		
		"""element destination"""
		dest = self.doc.createElement("destination")
		
		dUid = self.doc.createElement("uid")
		dUidText = self.doc.createTextNode(argsIn_dict['destination']['uid'])
		dUid.appendChild(dUidText)
		dest.appendChild(dUid)
		
		dIp = self.doc.createElement("ip")
		dIpText = self.doc.createTextNode(argsIn_dict['destination']['ip'])
		dIp.appendChild(dIpText)
		dest.appendChild(dIp)
		
		dPort = self.doc.createElement("port")
		dPortText = self.doc.createTextNode(argsIn_dict['destination']['port'])
		dPort.appendChild(dPortText)
		dest.appendChild(dPort)
		
		dIpv = self.doc.createElement("ipversion")
		dIpvText = self.doc.createTextNode(argsIn_dict['destination']['ipversion'])
		dIpv.appendChild(dIpvText)
		dest.appendChild(dIpv)
			
		cres.appendChild(dest)
		
		"""element source"""
		source = self.doc.createElement("source")
		
		sUid = self.doc.createElement("uid")
		sUidText = self.doc.createTextNode(argsIn_dict['source']['uid'])
		sUid.appendChild(sUidText)
		source.appendChild(sUid)
		
		sIp = self.doc.createElement("ip")
		sIpText = self.doc.createTextNode(argsIn_dict['source']['ip'])
		sIp.appendChild(sIpText)
		source.appendChild(sIp)
		
		sPort = self.doc.createElement("port")
		sPortText = self.doc.createTextNode(argsIn_dict['source']['port'])
		sPort.appendChild(sPortText)
		source.appendChild(sPort)
		
		sIpv = self.doc.createElement("ipversion")
		sIpvText = self.doc.createTextNode(argsIn_dict['source']['ipversion'])
		sIpv.appendChild(sIpvText)
		source.appendChild(sIpv)
			
		cres.appendChild(source)
		
		"""element list"""
		nodelist = self.doc.createElement("list")
		
		nlist = argsIn_dict['list']
		for item in nlist.iteritems():
			node = self.doc.createElement("node")
			
			nUid = self.doc.createElement("uid")
			nUidText = self.doc.createTextNode(item[0])
			nUid.appendChild(nUidText)
			node.appendChild(nUid)
			
			nIp = self.doc.createElement("ip")
			nIpText = self.doc.createTextNode(item[1]['ip'])
			nIp.appendChild(nIpText)
			node.appendChild(nIp)
			
			nPort = self.doc.createElement("port")
			nPortText = self.doc.createTextNode(item[1]['port'])
			nPort.appendChild(nPortText)
			node.appendChild(nPort)
			
			nIpv = self.doc.createElement("ipversion")
			nIpvText = self.doc.createTextNode(item[1]['ipversion'])
			nIpv.appendChild(nIpvText)
			node.appendChild(nIpv)
			
			nodelist.appendChild(node)
			
		cres.appendChild(nodelist)
		
		"""element requestid"""
		reqid = self.doc.createElement("requestid")
		reqidText = self.doc.createTextNode(argsIn_dict['requestid'])
		reqid.appendChild(reqidText)
		cres.appendChild(reqid)
		
		"""return xml"""
		str_xml = "<?xml version='1.0'?>" + cres.toxml()
		return str_xml
		
	def createXml_echorequest(self, argsIn_dict):
		"""Creates xml according to the schema	
		<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
		<xsd:include schemaLocation="client.xsd"/>
		<xsd:element name="echorequest" type="echoreq"/>
		<xsd:complexType name="echoreq">
  		<xsd:sequence>
    		<xsd:element name="destination" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="source" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="requestid" type="xsd:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
  		</xsd:sequence>
		</xsd:complexType>
		</xsd:schema>"""
		
		"""element echorequest"""
		ereq = self.doc.createElement("echorequest")
		
		"""element destination"""
		dest = self.doc.createElement("destination")
		
		dUid = self.doc.createElement("uid")
		dUidText = self.doc.createTextNode(argsIn_dict['destination']['uid'])
		dUid.appendChild(dUidText)
		dest.appendChild(dUid)
		
		dIp = self.doc.createElement("ip")
		dIpText = self.doc.createTextNode(argsIn_dict['destination']['ip'])
		dIp.appendChild(dIpText)
		dest.appendChild(dIp)
		
		dPort = self.doc.createElement("port")
		dPortText = self.doc.createTextNode(argsIn_dict['destination']['port'])
		dPort.appendChild(dPortText)
		dest.appendChild(dPort)
		
		dIpv = self.doc.createElement("ipversion")
		dIpvText = self.doc.createTextNode(argsIn_dict['destination']['ipversion'])
		dIpv.appendChild(dIpvText)
		dest.appendChild(dIpv)
			
		ereq.appendChild(dest)
		
		"""element source"""
		source = self.doc.createElement("source")
		
		sUid = self.doc.createElement("uid")
		sUidText = self.doc.createTextNode(argsIn_dict['source']['uid'])
		sUid.appendChild(sUidText)
		source.appendChild(sUid)
		
		sIp = self.doc.createElement("ip")
		sIpText = self.doc.createTextNode(argsIn_dict['source']['ip'])
		sIp.appendChild(sIpText)
		source.appendChild(sIp)
		
		sPort = self.doc.createElement("port")
		sPortText = self.doc.createTextNode(argsIn_dict['source']['port'])
		sPort.appendChild(sPortText)
		source.appendChild(sPort)
		
		sIpv = self.doc.createElement("ipversion")
		sIpvText = self.doc.createTextNode(argsIn_dict['source']['ipversion'])
		sIpv.appendChild(sIpvText)
		source.appendChild(sIpv)
			
		ereq.appendChild(source)
		
		"""element requestid"""
		reqid = self.doc.createElement("requestid")
		reqidText = self.doc.createTextNode(argsIn_dict['requestid'])
		reqid.appendChild(reqidText)
		ereq.appendChild(reqid)
		
		"""return xml"""
		str_xml = "<?xml version='1.0'?>" + ereq.toxml()
		return str_xml
		
	def createXml_echoresponse(self, argsIn_dict):
		"""Creates xml according to the schema	
		<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
		<xsd:include schemaLocation="client.xsd"/>
		<xsd:element name="echoresponse" type="echores"/>
		<xsd:complexType name="echores">
  		<xsd:sequence>
    		<xsd:element name="destination" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="source" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="requestid" type="xsd:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
  		</xsd:sequence>
		</xsd:complexType>
		</xsd:schema>"""
		
		"""element echoresponse"""
		eres = self.doc.createElement("echoresponse")
		
		"""element destination"""
		dest = self.doc.createElement("destination")
		
		dUid = self.doc.createElement("uid")
		dUidText = self.doc.createTextNode(argsIn_dict['destination']['uid'])
		dUid.appendChild(dUidText)
		dest.appendChild(dUid)
		
		dIp = self.doc.createElement("ip")
		dIpText = self.doc.createTextNode(argsIn_dict['destination']['ip'])
		dIp.appendChild(dIpText)
		dest.appendChild(dIp)
		
		dPort = self.doc.createElement("port")
		dPortText = self.doc.createTextNode(argsIn_dict['destination']['port'])
		dPort.appendChild(dPortText)
		dest.appendChild(dPort)
		
		dIpv = self.doc.createElement("ipversion")
		dIpvText = self.doc.createTextNode(argsIn_dict['destination']['ipversion'])
		dIpv.appendChild(dIpvText)
		dest.appendChild(dIpv)
			
		eres.appendChild(dest)
		
		"""element source"""
		source = self.doc.createElement("source")
		
		sUid = self.doc.createElement("uid")
		sUidText = self.doc.createTextNode(argsIn_dict['source']['uid'])
		sUid.appendChild(sUidText)
		source.appendChild(sUid)
		
		sIp = self.doc.createElement("ip")
		sIpText = self.doc.createTextNode(argsIn_dict['source']['ip'])
		sIp.appendChild(sIpText)
		source.appendChild(sIp)
		
		sPort = self.doc.createElement("port")
		sPortText = self.doc.createTextNode(argsIn_dict['source']['port'])
		sPort.appendChild(sPortText)
		source.appendChild(sPort)
		
		sIpv = self.doc.createElement("ipversion")
		sIpvText = self.doc.createTextNode(argsIn_dict['source']['ipversion'])
		sIpv.appendChild(sIpvText)
		source.appendChild(sIpv)
		
		eres.appendChild(source)
		
		"""element requestid"""
		reqid = self.doc.createElement("requestid")
		reqidText = self.doc.createTextNode(argsIn_dict['requestid'])
		reqid.appendChild(reqidText)
		eres.appendChild(reqid)
		
		"""return xml"""
		str_xml = "<?xml version='1.0'?>" + eres.toxml()
		return str_xml

	def createXml_hello(self, argsIn_dict):
		"""Creates xml according to the schema	
		<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
		<xsd:include schemaLocation="client.xsd"/>
		<xsd:element name="hello" type="hi"/>
		<xsd:complexType name="hi">
  		<xsd:sequence>
    		<xsd:element name="destination" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="source" minOccurs="1" maxOccurs="1">
      		<xsd:complexType>
        		<xsd:sequence>
          		<xsd:element name="uid" type="xsd:string" minOccurs="1" maxOccurs="1"/>
          		<xsd:element name="ip" type="xsd:string" minOccurs="0" maxOccurs="1"/>
          		<xsd:element name="port" type="xsd:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
          		<xsd:element name="ipversion" minOccurs="0" maxOccurs="1">
            		<xsd:simpleType>
              		<xsd:restriction base="xsd:nonNegativeInteger">
                		<xsd:enumeration value="4"/>
                		<xsd:enumeration value="6"/>
              		</xsd:restriction>
            		</xsd:simpleType>
          		</xsd:element>
        		</xsd:sequence>
      		</xsd:complexType>
    		</xsd:element>
    		<xsd:element name="count" type="xsd:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
  		</xsd:sequence>
		</xsd:complexType>
		</xsd:schema>"""
		
		"""element hello"""
		hello = self.doc.createElement("hello")
		
		"""element destination"""
		dest = self.doc.createElement("destination")
		
		dUid = self.doc.createElement("uid")
		dUidText = self.doc.createTextNode(argsIn_dict['destination']['uid'])
		dUid.appendChild(dUidText)
		dest.appendChild(dUid)
		
		dIp = self.doc.createElement("ip")
		dIpText = self.doc.createTextNode(argsIn_dict['destination']['ip'])
		dIp.appendChild(dIpText)
		dest.appendChild(dIp)
		
		dPort = self.doc.createElement("port")
		dPortText = self.doc.createTextNode(argsIn_dict['destination']['port'])
		dPort.appendChild(dPortText)
		dest.appendChild(dPort)
		
		dIpv = self.doc.createElement("ipversion")
		dIpvText = self.doc.createTextNode(argsIn_dict['destination']['ipversion'])
		dIpv.appendChild(dIpvText)
		dest.appendChild(dIpv)
			
		hello.appendChild(dest)
		
		"""element source"""
		source = self.doc.createElement("source")
		
		sUid = self.doc.createElement("uid")
		sUidText = self.doc.createTextNode(argsIn_dict['source']['uid'])
		sUid.appendChild(sUidText)
		source.appendChild(sUid)
		
		if argsIn_dict['source']['ip'] != '':
			sIp = self.doc.createElement("ip")
			sIpText = self.doc.createTextNode(argsIn_dict['source']['ip'])
			sIp.appendChild(sIpText)
			source.appendChild(sIp)
		
		sPort = self.doc.createElement("port")
		sPortText = self.doc.createTextNode(argsIn_dict['source']['port'])
		sPort.appendChild(sPortText)
		source.appendChild(sPort)
		
		if argsIn_dict['source']['ipversion'] != '':
			sIpv = self.doc.createElement("ipversion")
			sIpvText = self.doc.createTextNode(argsIn_dict['source']['ipversion'])
			sIpv.appendChild(sIpvText)
			source.appendChild(sIpv)
			
		hello.appendChild(source)
			
		"""element count"""
		count = self.doc.createElement("count")
		countText = self.doc.createTextNode(argsIn_dict['count'])
		count.appendChild(countText)
		hello.appendChild(count)
		
		"""return xml"""
		str_xml = "<?xml version='1.0'?>" + hello.toxml()
		return str_xml
		
	def createXml_pkrequest(self, argsIn_dict):
		"""Creates xml according to the schema
		<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
		<xsd:include schemaLocation="client.xsd"/>
		<xsd:element name="pkrequest" type="pkreq"/>
		<xsd:complexType name="pkreq">
  		<xsd:sequence>
    		<xsd:element name="destination" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="source" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="requestid" type="xsd:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
  		</xsd:sequence>
		</xsd:complexType>
		</xsd:schema>"""
		
		"""element pkrequest"""
		pkreq = self.doc.createElement("pkrequest")
		
		"""element destination"""
		dest = self.doc.createElement("destination")
		
		dUid = self.doc.createElement("uid")
		dUidText = self.doc.createTextNode(argsIn_dict['destination']['uid'])
		dUid.appendChild(dUidText)
		dest.appendChild(dUid)
		
		dIp = self.doc.createElement("ip")
		dIpText = self.doc.createTextNode(argsIn_dict['destination']['ip'])
		dIp.appendChild(dIpText)
		dest.appendChild(dIp)
		
		dPort = self.doc.createElement("port")
		dPortText = self.doc.createTextNode(argsIn_dict['destination']['port'])
		dPort.appendChild(dPortText)
		dest.appendChild(dPort)
		
		dIpv = self.doc.createElement("ipversion")
		dIpvText = self.doc.createTextNode(argsIn_dict['destination']['ipversion'])
		dIpv.appendChild(dIpvText)
		dest.appendChild(dIpv)
			
		pkreq.appendChild(dest)
		
		"""element source"""
		source = self.doc.createElement("source")
		
		sUid = self.doc.createElement("uid")
		sUidText = self.doc.createTextNode(argsIn_dict['source']['uid'])
		sUid.appendChild(sUidText)
		source.appendChild(sUid)
		
		sIp = self.doc.createElement("ip")
		sIpText = self.doc.createTextNode(argsIn_dict['source']['ip'])
		sIp.appendChild(sIpText)
		source.appendChild(sIp)
		
		sPort = self.doc.createElement("port")
		sPortText = self.doc.createTextNode(argsIn_dict['source']['port'])
		sPort.appendChild(sPortText)
		source.appendChild(sPort)
		
		sIpv = self.doc.createElement("ipversion")
		sIpvText = self.doc.createTextNode(argsIn_dict['source']['ipversion'])
		sIpv.appendChild(sIpvText)
		source.appendChild(sIpv)
			
		pkreq.appendChild(source)
		
		"""element requestid"""
		reqid = self.doc.createElement("requestid")
		reqidText = self.doc.createTextNode(argsIn_dict['requestid'])
		reqid.appendChild(reqidText)
		pkreq.appendChild(reqid)
		
		"""return xml"""
		str_xml = "<?xml version='1.0'?>" + pkreq.toxml()
		return str_xml

	def createXml_pkresponse(self, argsIn_dict):
		"""Creates xml according to the schema
		<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
		<xsd:include schemaLocation="client.xsd"/>
		<xsd:element name="pkresponse" type="pkres"/>
		<xsd:complexType name="pkres">
  		<xsd:sequence>
    		<xsd:element name="destination" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="source" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="publickey" type="xsd:string" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="requestid" type="xsd:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
  		</xsd:sequence>
		</xsd:complexType>
		</xsd:schema>"""
		
		"""element pkresponse"""
		pkres = self.doc.createElement("pkresponse")
		
		"""element destination"""
		dest = self.doc.createElement("destination")
		
		dUid = self.doc.createElement("uid")
		dUidText = self.doc.createTextNode(argsIn_dict['destination']['uid'])
		dUid.appendChild(dUidText)
		dest.appendChild(dUid)
		
		dIp = self.doc.createElement("ip")
		dIpText = self.doc.createTextNode(argsIn_dict['destination']['ip'])
		dIp.appendChild(dIpText)
		dest.appendChild(dIp)
		
		dPort = self.doc.createElement("port")
		dPortText = self.doc.createTextNode(argsIn_dict['destination']['port'])
		dPort.appendChild(dPortText)
		dest.appendChild(dPort)
		
		dIpv = self.doc.createElement("ipversion")
		dIpvText = self.doc.createTextNode(argsIn_dict['destination']['ipversion'])
		dIpv.appendChild(dIpvText)
		dest.appendChild(dIpv)
			
		pkres.appendChild(dest)
		
		"""element source"""
		source = self.doc.createElement("source")
		
		sUid = self.doc.createElement("uid")
		sUidText = self.doc.createTextNode(argsIn_dict['source']['uid'])
		sUid.appendChild(sUidText)
		source.appendChild(sUid)
		
		sIp = self.doc.createElement("ip")
		sIpText = self.doc.createTextNode(argsIn_dict['source']['ip'])
		sIp.appendChild(sIpText)
		source.appendChild(sIp)
		
		sPort = self.doc.createElement("port")
		sPortText = self.doc.createTextNode(argsIn_dict['source']['port'])
		sPort.appendChild(sPortText)
		source.appendChild(sPort)
		
		sIpv = self.doc.createElement("ipversion")
		sIpvText = self.doc.createTextNode(argsIn_dict['source']['ipversion'])
		sIpv.appendChild(sIpvText)
		source.appendChild(sIpv)
			
		pkres.appendChild(source)
		
		"""element publickey"""
		pkey = self.doc.createElement("publickey")
		pkeyText = self.doc.createCDATASection(argsIn_dict['publickey'])
		pkey.appendChild(pkeyText)
		pkres.appendChild(pkey)
		
		"""element requestid"""
		reqid = self.doc.createElement("requestid")
		reqidText = self.doc.createTextNode(argsIn_dict['requestid'])
		reqid.appendChild(reqidText)
		pkres.appendChild(reqid)
		
		"""return xml"""
		str_xml = "<?xml version='1.0'?>" + pkres.toxml()
		return str_xml
		
	def createXml_searchrequest(self, argsIn_dict):
		"""Creates xml according to the schema
		<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
		<xsd:include schemaLocation="client.xsd"/>
		<xsd:element name="searchrequest" type="search"/>
		<xsd:complexType name="search">
  		<xsd:sequence>
    		<xsd:element name="destination" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="source" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="ttl" default="4" minOccurs="1" maxOccurs="1">
      		<xsd:simpleType>
        		<xsd:restriction base="xsd:nonNegativeInteger">
          		<xsd:max-inclusive value="255"/>
        		</xsd:restriction>
      		</xsd:simpleType>
    		</xsd:element>
    		<xsd:element name="searchfields" minOccurs="1" maxOccurs="1">
      		<xsd:complexType>
        		<xsd:sequence>
          		<xsd:element name="value" type="xsd:string" minOccurs="1" maxOccurs="unbounded">
            		<xsd:attribute name="field" type="xsd:string" use="required"/>
          		</xsd:element>
        		</xsd:sequence>
        		<xsd:attribute name="operation" use="optional" default="and">
          		<xsd:simpleType>
            		<xsd:restriction base="xsd:string">
              		<xsd:enumeration value="and"/>
              		<xsd:enumeration value="or"/>
            		</xsd:restriction>
          		</xsd:simpleType>
        		</xsd:attribute>
      		</xsd:complexType>
    		</xsd:element>
    		<xsd:element name="requestid" type="xsd:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="relay" type="client" minOccurs="0" maxOccurs="8"/>
  		</xsd:sequence>
		</xsd:complexType>
		</xsd:schema>"""
		
		"""element searchrequest"""
		sreq = self.doc.createElement("searchrequest")
		
		"""element destination"""
		dest = self.doc.createElement("destination")
		
		dUid = self.doc.createElement("uid")
		dUidText = self.doc.createTextNode(argsIn_dict['destination']['uid'])
		dUid.appendChild(dUidText)
		dest.appendChild(dUid)
		
		dIp = self.doc.createElement("ip")
		dIpText = self.doc.createTextNode(argsIn_dict['destination']['ip'])
		dIp.appendChild(dIpText)
		dest.appendChild(dIp)
		
		dPort = self.doc.createElement("port")
		dPortText = self.doc.createTextNode(argsIn_dict['destination']['port'])
		dPort.appendChild(dPortText)
		dest.appendChild(dPort)
		
		dIpv = self.doc.createElement("ipversion")
		dIpvText = self.doc.createTextNode(argsIn_dict['destination']['ipversion'])
		dIpv.appendChild(dIpvText)
		dest.appendChild(dIpv)
			
		sreq.appendChild(dest)
		
		"""element source"""
		source = self.doc.createElement("source")
		
		sUid = self.doc.createElement("uid")
		sUidText = self.doc.createTextNode(argsIn_dict['source']['uid'])
		sUid.appendChild(sUidText)
		source.appendChild(sUid)
		
		sIp = self.doc.createElement("ip")
		sIpText = self.doc.createTextNode(argsIn_dict['source']['ip'])
		sIp.appendChild(sIpText)
		source.appendChild(sIp)
		
		sPort = self.doc.createElement("port")
		sPortText = self.doc.createTextNode(argsIn_dict['source']['port'])
		sPort.appendChild(sPortText)
		source.appendChild(sPort)
		
		sIpv = self.doc.createElement("ipversion")
		sIpvText = self.doc.createTextNode(argsIn_dict['source']['ipversion'])
		sIpv.appendChild(sIpvText)
		source.appendChild(sIpv)
			
		sreq.appendChild(source)
		
		"""element ttl"""
		ttl = self.doc.createElement("ttl")
		ttlText = self.doc.createTextNode(argsIn_dict['ttl'])
		ttl.appendChild(ttlText)
		sreq.appendChild(ttl)
		
		"""element searchfields"""
		searchfields = self.doc.createElement("searchfields")
		searchfields.setAttribute("operation", argsIn_dict['operation'])
		
		for key in argsIn_dict['searchfields'].keys():
			value = self.doc.createElement("value")
			valueText = self.doc.createTextNode(argsIn_dict['searchfields'][key])
			value.appendChild(valueText)
			value.setAttribute("field", key)
			searchfields.appendChild(value)
		
		sreq.appendChild(searchfields)
		
		"""element requestid"""
		reqid = self.doc.createElement("requestid")
		reqidText = self.doc.createTextNode(argsIn_dict['requestid'])
		reqid.appendChild(reqidText)
		sreq.appendChild(reqid)
		
		"""element relay"""
		relay = self.doc.createElement("relay")
		relayText = self.doc.createTextNode(argsIn_dict['relay'])
		relay.appendChild(relayText)
		sreq.appendChild(relay)
		
		"""return xml"""
		str_xml = "<?xml version='1.0'?>" + sreq.toxml()
		return str_xml
		
	def createXml_searchresponse(self, argsIn_dict):
		"""Creates xml according to the schema
		<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
		<xsd:include schemaLocation="client.xsd"/>
		<xsd:element name="searchresponse" type="searchres"/>
		<xsd:complexType name="searchres">
  		<xsd:sequence>
    		<xsd:element name="destination" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="source" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="requestid" type="xsd:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="searchfields" minOccurs="0" maxOccurs="1">
      		<xsd:complexType>
        		<xsd:sequence>
          		<xsd:element name="value" type="xsd:string" minOccurs="1" maxOccurs="unbounded">
            		<xsd:attribute name="field" type="xsd:string" use="required"/>
          		</xsd:element>
        		</xsd:sequence>
        		<xsd:attribute name="operation" use="optional" default="and">	
          		<xsd:simpleType>
            		<xsd:restriction base="xsd:string">
              		<xsd:enmueration value="and"/>
              		<xsd:enmueration value="or"/>
            		</xsd:restriction>
          		</xsd:simpleType>
        		</xsd:attribute>
      		</xsd:complexType>
    		</xsd:element>
    		<xsd:element name="foundnode" type="client" minOccurs="0" maxOccurs="unbounded"/>
  		</xsd:sequence>
		</xsd:complexType>
		</xsd:schema>"""
		
		"""element searchresponse"""
		sres = self.doc.createElement("searchresponse")
		
		"""element destination"""
		dest = self.doc.createElement("destination")
		
		dUid = self.doc.createElement("uid")
		dUidText = self.doc.createTextNode(argsIn_dict['destination']['uid'])
		dUid.appendChild(dUidText)
		dest.appendChild(dUid)
		
		dIp = self.doc.createElement("ip")
		dIpText = self.doc.createTextNode(argsIn_dict['destination']['ip'])
		dIp.appendChild(dIpText)
		dest.appendChild(dIp)
		
		dPort = self.doc.createElement("port")
		dPortText = self.doc.createTextNode(argsIn_dict['destination']['port'])
		dPort.appendChild(dPortText)
		dest.appendChild(dPort)
		
		dIpv = self.doc.createElement("ipversion")
		dIpvText = self.doc.createTextNode(argsIn_dict['destination']['ipversion'])
		dIpv.appendChild(dIpvText)
		dest.appendChild(dIpv)
			
		sres.appendChild(dest)
		
		"""element source"""
		source = self.doc.createElement("source")
		
		sUid = self.doc.createElement("uid")
		sUidText = self.doc.createTextNode(argsIn_dict['source']['uid'])
		sUid.appendChild(sUidText)
		source.appendChild(sUid)
		
		sIp = self.doc.createElement("ip")
		sIpText = self.doc.createTextNode(argsIn_dict['source']['ip'])
		sIp.appendChild(sIpText)
		source.appendChild(sIp)
		
		sPort = self.doc.createElement("port")
		sPortText = self.doc.createTextNode(argsIn_dict['source']['port'])
		sPort.appendChild(sPortText)
		source.appendChild(sPort)
		
		sIpv = self.doc.createElement("ipversion")
		sIpvText = self.doc.createTextNode(argsIn_dict['source']['ipversion'])
		sIpv.appendChild(sIpvText)
		source.appendChild(sIpv)
			
		sres.appendChild(source)
		
		"""element requestid"""
		reqid = self.doc.createElement("requestid")
		reqidText = self.doc.createTextNode(argsIn_dict['requestid'])
		reqid.appendChild(reqidText)
		sres.appendChild(reqid)
		
		"""element searchfields"""
		searchfields = self.doc.createElement("searchfields")
		searchfields.setAttribute("operation", argsIn_dict['operation'])
		
		for key in argsIn_dict['searchfields'].keys():
			value = self.doc.createElement("value")
			valueText = self.doc.createTextNode(argsIn_dict['searchfields'][key])
			value.appendChild(valueText)
			value.setAttribute("field", key)
			searchfields.appendChild(value)
		
		sres.appendChild(searchfields)
		
		"""elements foundnode"""
		nlist = argsIn_dict['foundnode']
		for node in nlist.iteritems():
			foundnode = self.doc.createElement("foundnode")
			
			nUid = self.doc.createElement("uid")
			nUidText = self.doc.createTextNode(node[0])
			nUid.appendChild(nUidText)
			foundnode.appendChild(nUid)
			
			nIp = self.doc.createElement("ip")
			nIpText = self.doc.createTextNode(node[1]['ip'])
			nIp.appendChild(nIpText)
			foundnode.appendChild(nIp)
			
			nPort = self.doc.createElement("port")
			nPortText = self.doc.createTextNode(node[1]['port'])
			nPort.appendChild(nPortText)
			foundnode.appendChild(nPort)
			
			nIpv = self.doc.createElement("ipversion")
			nIpvText = self.doc.createTextNode(node[1]['ipversion'])
			nIpv.appendChild(nIpvText)
			foundnode.appendChild(nIpv)
			
			sres.appendChild(foundnode)
		
		"""return xml"""
		str_xml = "<?xml version='1.0'?>" + sres.toxml()
		return str_xml
		
	def createXml_userdata(self, argsIn_dict):
		"""Creates xml according to the schema
		<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
		<xsd:include schemaLocation="client.xsd"/>
		<xsd:element name="userdata" type="udata"/>
		<xsd:complexType name="udata">
  		<xsd:sequence>
    		<xsd:element name="destination" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="source" type="client" minOccurs="1" maxOccurs="1"/>
    		<xsd:element name="data" type="xsd:string" minOccurs="1" maxOccurs="1"/>
  		</xsd:sequence>
		</xsd:complexType>
		</xsd:schema>"""
		
		"""element userdata"""
		udata = self.doc.createElement("userdata")
		
		"""element destination"""
		dest = self.doc.createElement("destination")
		
		dUid = self.doc.createElement("uid")
		dUidText = self.doc.createTextNode(argsIn_dict['destination']['uid'])
		dUid.appendChild(dUidText)
		dest.appendChild(dUid)
		
		dIp = self.doc.createElement("ip")
		dIpText = self.doc.createTextNode(argsIn_dict['destination']['ip'])
		dIp.appendChild(dIpText)
		dest.appendChild(dIp)
		
		dPort = self.doc.createElement("port")
		dPortText = self.doc.createTextNode(argsIn_dict['destination']['port'])
		dPort.appendChild(dPortText)
		dest.appendChild(dPort)
		
		dIpv = self.doc.createElement("ipversion")
		dIpvText = self.doc.createTextNode(argsIn_dict['destination']['ipversion'])
		dIpv.appendChild(dIpvText)
		dest.appendChild(dIpv)
			
		udata.appendChild(dest)
		
		"""element source"""
		source = self.doc.createElement("source")
		
		sUid = self.doc.createElement("uid")
		sUidText = self.doc.createTextNode(argsIn_dict['source']['uid'])
		sUid.appendChild(sUidText)
		source.appendChild(sUid)
		
		sIp = self.doc.createElement("ip")
		sIpText = self.doc.createTextNode(argsIn_dict['source']['ip'])
		sIp.appendChild(sIpText)
		source.appendChild(sIp)
		
		sPort = self.doc.createElement("port")
		sPortText = self.doc.createTextNode(argsIn_dict['source']['port'])
		sPort.appendChild(sPortText)
		source.appendChild(sPort)
		
		sIpv = self.doc.createElement("ipversion")
		sIpvText = self.doc.createTextNode(argsIn_dict['source']['ipversion'])
		sIpv.appendChild(sIpvText)
		source.appendChild(sIpv)
			
		udata.appendChild(source)
			
		"""element data"""
		data = self.doc.createElement("data")
		dataText = self.doc.createCDATASection(argsIn_dict['data'])
		data.appendChild(dataText)
		udata.appendChild(data)
		
		"""return xml"""
		str_xml = "<?xml version='1.0'?>" + udata.toxml()
		return str_xml
		
	def incoming(self, type_str, args_dict):
		"""Dispatches incoming dictionary to the according functions"""
		try:
			function = getattr(self, 'createXml_' + type_str)
			return function(args_dict)
		except AttributeError:
			raise NotImplementedError
