# Path: iottoolkit/interfaces/HttpObjectService.py
# class HttpObjectService(object):
#     def __init__(self, baseObject=None, port=None):
# 
#         if port == None:
#             self._port=8000
#         else:
#             self._port = port  # default port 8000
# 
#         if baseObject == None:
#             from iottoolkit.core.SmartObject import SmartObject
#             self._baseObject = SmartObject()
#         else:
#             self._baseObject = baseObject
# 
#         self.resources = self._baseObject.resources
# 
#         self.start(self._port)
# 
#     @property
#     def baseObject(self):
#         return self._baseObject
# 
#     def start(self, port=None):
#         if port==None:
#             self._port = 8000 # default port 8000
#         else:
#             self._port=port # override port on start if supplied
# 
#         httpThread = threading.Thread(target = self._startHttpObjectService)
#         httpThread.daemon = True
#         httpThread.start()
#         self._baseObject.Properties.update({'httpService': 'http://' + gethostname() + ':' + repr(self._port)})
#         print 'HTTP server started at', self._baseObject.Properties.get('httpService')
# 
# 
#     def _startHttpObjectService(self):
#         from wsgiref.simple_server import make_server
#         # HttpObjectService constructor method creates a Smart Object service and
#         # returns a constructor for a restlite router instance
#         self.httpObjectHandler = HttpHandler(self._baseObject)
#         self.httpd = make_server('', self._port, restlite.router(self.httpObjectHandler.routes))
#         try: self.httpd.serve_forever()
#         except KeyboardInterrupt: pass
# 
# Path: iottoolkit/interfaces/CoapObjectService.py
# class CoapObjectService(object):
#     def __init__(self, baseObject=None, port=None): # FIXME if no baseObject given, create a default base object
# 
#         if port == None:
#             self._port = 5683 # IETF default port for coap://
#         else:
#             self._port = port
# 
#         if baseObject == None:
#             from iottoolkit.core.SmartObject import SmartObject
#             self._baseObject = SmartObject()
#         else:
#             self._baseObject = baseObject
# 
#         self.resources = self._baseObject.resources
# 
#         self._host = gethostname()
#         self._baseObject.Properties.update({'coapService': 'coap://' + self._host + ':' + repr(self._port)})
# 
#         self._coapHandler = CoapRequestHandler(self._baseObject)
#         self._coapServer = COAPServer(self._host, self._port, self._coapHandler)
#         #print 'CoAP Service started at', self._baseObject.Properties.get('coapService')
#         #starts thread as daemon, has run method loop
# 
#     @property
#     def baseObject(self):
#         return self._baseObject


from iottoolkit.interfaces.HttpObjectService import HttpObjectService
from iottoolkit.interfaces.CoapObjectService import CoapObjectService
from time import sleep
from rdflib.plugin import Serializer, Parser
import rdflib
'''
Created on Dec 7, 2013

Minimal service creation, base object auto-created by the first server,
passed to constructor of second server

@author: mjkoster
'''

#workaround to register rdf JSON plugins
rdflib.plugin.register('json-ld', Serializer, 'rdflib_jsonld.serializer', 'JsonLDSerializer')
rdflib.plugin.register('json-ld', Parser, 'rdflib_jsonld.parser', 'JsonLDParser')
rdflib.plugin.register('rdf-json', Serializer, 'rdflib_rdfjson.rdfjson_serializer', 'RdfJsonSerializer')
rdflib.plugin.register('rdf-json', Parser, 'rdflib_rdfjson.rdfjson_parser', 'RdfJsonParser')


if __name__ == '__main__' :

    # make an empty instance of a SmartObject shared by 2 interfaces,
    # CoAP and HTTP, default object root and default ports 5683 and 8000
    # CoAP service makes the base object and it is passed to the http service constructor


# Path: iottoolkit/interfaces/HttpObjectService.py
# class HttpObjectService(object):
#     def __init__(self, baseObject=None, port=None):
# 
#         if port == None:
#             self._port=8000
#         else:
#             self._port = port  # default port 8000
# 
#         if baseObject == None:
#             from iottoolkit.core.SmartObject import SmartObject
#             self._baseObject = SmartObject()
#         else:
#             self._baseObject = baseObject
# 
#         self.resources = self._baseObject.resources
# 
#         self.start(self._port)
# 
#     @property
#     def baseObject(self):
#         return self._baseObject
# 
#     def start(self, port=None):
#         if port==None:
#             self._port = 8000 # default port 8000
#         else:
#             self._port=port # override port on start if supplied
# 
#         httpThread = threading.Thread(target = self._startHttpObjectService)
#         httpThread.daemon = True
#         httpThread.start()
#         self._baseObject.Properties.update({'httpService': 'http://' + gethostname() + ':' + repr(self._port)})
#         print 'HTTP server started at', self._baseObject.Properties.get('httpService')
# 
# 
#     def _startHttpObjectService(self):
#         from wsgiref.simple_server import make_server
#         # HttpObjectService constructor method creates a Smart Object service and
#         # returns a constructor for a restlite router instance
#         self.httpObjectHandler = HttpHandler(self._baseObject)
#         self.httpd = make_server('', self._port, restlite.router(self.httpObjectHandler.routes))
#         try: self.httpd.serve_forever()
#         except KeyboardInterrupt: pass
# 
# Path: iottoolkit/interfaces/CoapObjectService.py
# class CoapObjectService(object):
#     def __init__(self, baseObject=None, port=None): # FIXME if no baseObject given, create a default base object
# 
#         if port == None:
#             self._port = 5683 # IETF default port for coap://
#         else:
#             self._port = port
# 
#         if baseObject == None:
#             from iottoolkit.core.SmartObject import SmartObject
#             self._baseObject = SmartObject()
#         else:
#             self._baseObject = baseObject
# 
#         self.resources = self._baseObject.resources
# 
#         self._host = gethostname()
#         self._baseObject.Properties.update({'coapService': 'coap://' + self._host + ':' + repr(self._port)})
# 
#         self._coapHandler = CoapRequestHandler(self._baseObject)
#         self._coapServer = COAPServer(self._host, self._port, self._coapHandler)
#         #print 'CoAP Service started at', self._baseObject.Properties.get('coapService')
#         #starts thread as daemon, has run method loop
# 
#     @property
#     def baseObject(self):
#         return self._baseObject


# Path: examples/MinimalServer.py
from iottoolkit.interfaces.HttpObjectService import HttpObjectService
from iottoolkit.interfaces.CoapObjectService import CoapObjectService
from time import sleep
from rdflib.plugin import Serializer, Parser
import rdflib

'''
Created on Dec 7, 2013

Minimal service creation, base object auto-created by the first server,
passed to constructor of second server

@author: mjkoster
'''

#workaround to register rdf JSON plugins
rdflib.plugin.register('json-ld', Serializer, 'rdflib_jsonld.serializer', 'JsonLDSerializer')
rdflib.plugin.register('json-ld', Parser, 'rdflib_jsonld.parser', 'JsonLDParser')
rdflib.plugin.register('rdf-json', Serializer, 'rdflib_rdfjson.rdfjson_serializer', 'RdfJsonSerializer')
rdflib.plugin.register('rdf-json', Parser, 'rdflib_rdfjson.rdfjson_parser', 'RdfJsonParser')


if __name__ == '__main__' :

    # make an empty instance of a SmartObject shared by 2 interfaces,
    # CoAP and HTTP, default object root and default ports 5683 and 8000
    # CoAP service makes the base object and it is passed to the http service constructor


    baseObject = HttpObjectService( CoapObjectService().baseObject ).baseObject