# Path: common/httpd.py
# class ThreadedHTTPServer(SocketServer.ThreadingMixIn,
#                          BaseHTTPServer.HTTPServer):
# class BaseRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
#   def url_to_fs_path(self, url):
#   def send_head(self):
# 
# Path: common/util.py
# ANSI_BLUE = '\033[36m'
# ANSI_GREEN = '\033[32m'
# ANSI_RESET = '\033[0;0m'
# class Logger(object):
#   def __init__(self, color):
#   def __call__(self, msg, *args):
# def GetPackageDir():
# def GetUserDir():
# 
# Path: common/spy.py
# class Error(Exception):
# class UsageReporter(object):
# class NullUsageReporter(object):
#   def __init__(self, host_port):
#   def Send(self, msg):
#   def SendDict(self, d):
#   def SendRecord(self, event, d):
#   def Send(self, msg):
#   def SendDict(self, d):
#   def SendRecord(self, event, d):
# def _GetUsageConfig():
# def _ReadAddressFile(address_file=None):
# def GetClientFromConfig(address_file=None):


import optparse
import re
import os
import sys
import threading
import jsontemplate
import templates
from common import httpd
from common import util
from common import spy
class Error(Exception):
  pass


class LatchApp(object):
  """Get and set latches."""

  def __init__(self, num_slots=3):
    """
    Args:
      num_slots: Maximum number of simultaneous waiters.  We don't want to take
          up all the threads in the server, so this is limited.
    """
    self.slots = threading.Semaphore(num_slots)
    # threading.Condition()?  or Queue.Queue()?
    # Simply dictionary
    # when you get a GET, just do .get().  Block forever.
    # when you get a POST, just do .put().
    self.latches = {'default': threading.Event()}

  def HandleRequest(self, request):
    # Is there a better way to do this?
    route = request['__META_INTERNAL']['route']  # hack to get route
    if route == 'index':
      data = {'latches': self.latches.keys()}
    elif route == 'wait':
      name = request['latch_name']

      ok = self.slots.acquire(False)
      if not ok:

# Path: common/httpd.py
# class ThreadedHTTPServer(SocketServer.ThreadingMixIn,
#                          BaseHTTPServer.HTTPServer):
# class BaseRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
#   def url_to_fs_path(self, url):
#   def send_head(self):
# 
# Path: common/util.py
# ANSI_BLUE = '\033[36m'
# ANSI_GREEN = '\033[32m'
# ANSI_RESET = '\033[0;0m'
# class Logger(object):
#   def __init__(self, color):
#   def __call__(self, msg, *args):
# def GetPackageDir():
# def GetUserDir():
# 
# Path: common/spy.py
# class Error(Exception):
# class UsageReporter(object):
# class NullUsageReporter(object):
#   def __init__(self, host_port):
#   def Send(self, msg):
#   def SendDict(self, d):
#   def SendRecord(self, event, d):
#   def Send(self, msg):
#   def SendDict(self, d):
#   def SendRecord(self, event, d):
# def _GetUsageConfig():
# def _ReadAddressFile(address_file=None):
# def GetClientFromConfig(address_file=None):


# Path: latch/latch.py
import optparse
import re
import os
import sys
import threading
import jsontemplate
import templates
from common import httpd
from common import util
from common import spy

class Error(Exception):
  pass


class LatchApp(object):
  """Get and set latches."""

  def __init__(self, num_slots=3):
    """
    Args:
      num_slots: Maximum number of simultaneous waiters.  We don't want to take
          up all the threads in the server, so this is limited.
    """
    self.slots = threading.Semaphore(num_slots)
    # threading.Condition()?  or Queue.Queue()?
    # Simply dictionary
    # when you get a GET, just do .get().  Block forever.
    # when you get a POST, just do .put().
    self.latches = {'default': threading.Event()}

  def HandleRequest(self, request):
    # Is there a better way to do this?
    route = request['__META_INTERNAL']['route']  # hack to get route
    if route == 'index':
      data = {'latches': self.latches.keys()}
    elif route == 'wait':
      name = request['latch_name']

      ok = self.slots.acquire(False)
      if not ok:

        return util.TextResponse(503, 'All slots taken')