#!/usr/bin/env python

import sys
import os
import time

from   multiprocessing.managers import BaseManager

from optparse import OptionParser
import datetime

import numpy as np

import interp.bootstrap
from   interp.grid.gmsh import ggrid
from   interp.tools import baker_exact_3D as exact

from interp.cluster import QueueManager, get_qs

if __name__ == '__main__':
  parser = OptionParser(usage = "usage: %s [options] <server> <gmsh file>")

  parser.add_option("-v", "--verbose",
    action="store_true", dest="verbose", default=False,
    help="verbose flag (default: %default)")

  parser.add_option('-p', '--port',
    type="int", dest="port", default=6666,
    help="specify the port to use on the server (default: %default)")

  (options, args) = parser.parse_args()

  if len(args) != 2:
    parser.print_usage()
    sys.exit(1)

  server, input_file = args

  myname = "%s-%d" % (os.uname()[1], os.getpid())
  if options.verbose:
    print "%s: started" % myname


  m = QueueManager(address=(server, options.port), authkey='asdf')
  m.connect()

  tasksq, resultsq, masterq, minionsq = get_qs(m)

  if options.verbose:
    print "%s: starting parse input file" % myname
  g = ggrid(input_file)
  g.q = np.array([exact(x) for x in g.verts])
  if options.verbose:
    print "%s: done parsing input file" % myname


  while True:
    if options.verbose:
      print "%s: letting master know that I am ready" % myname
    masterq.put(myname)

    if options.verbose:
      print "%s: waiting for master to tell me to start" % myname
    action = minionsq.get()
    if options.verbose:
      print "%s: master said go!!" % myname

    if action in ('teardown', 'slay'):
      break

    while not tasksq.empty():
      i, o, e, X = tasksq.get()
      try:
        a = g.run_baker(X, order = o, extra_points = e)
        resultsq.put((i, myname, a['qlin'], a['error'], a['final'], exact(X)))
      except Exception as e:
        print X, e
        resultsq.put((i, myname, 0.0, 0.0, 0.0, 0.0))

  if options.verbose:
    print "%s: exiting" % myname
