from System import Action
import clr
clr.AddReference('Vsync') # The profile of the dll file.
import Vsync
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
import json
import os.path

#from threading import Thread

Vsync.VsyncSystem.Start()


# define RequestHandler
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# define the server for later use
server = SimpleXMLRPCServer(("localhost", 8000),requestHandler=RequestHandler)
server.register_introspection_functions()

group = Vsync.Group("Master")

print "Created Master group"
users = {}
schedules = {}
messages = {}
cache = {}
counter = 0

mod = 0

'''
global
'''
def backup():
    '''
        backup data.
    '''
    global counter
    return

    counter = counter + 1
    if counter % 15 == 1:
	counter = 1
    	print "###backing#####"
    	print "user length"+str(len(users.keys()))
    	print "schedule length"+str(len(schedules.keys()))
    	print "message length"+str(len(messages.keys()))
    	print "cache length"+str(len(cache.keys()))

    	with open('data.json', 'w') as outfile:
    	    outfile.write(json.dumps({"user":users,"schedule":schedules,"message":messages,"cache":cache}))

def loadBK():
    '''
        load backup.
    '''
    if not os.path.isfile('data.json'):
	return
    global users, schedules, cache, messages

    data = ''
    with open('data.json') as data_file:
	data = data_file.read()

    dataset = json.loads(data)
    users = dataset["user"]
    schedules = dataset["schedule"]