# *********************************************************************
# The MIT License (MIT)
#
# Copyright (c) 2016 Christopher Asakawa, Mathew O'Brien, Nicholas McHale, Corey Aing
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# permit persons to whom the Software is furnished to do so, subject 
# to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
# FROM, OUT OF OR I CONNECTION WITH THE SOFTWARE OR THE USE OR 
# OTHER DEALINGS IN THE SOFTWARE.
# *******************************************************************
# bitstream page: https://github.com/boisgera/bitstream
# synch psudo code from: https://en.wikipedia.org/wiki/Readers%E2%80%93writers_problem
#

from flask import Flask, request, abort
from flask_cors import CORS
from bitstream import BitStream
from numpy import *
import binascii
import os
import math
from fish_pool import FishPool
from threading import Semaphore
import urlparse
import psycopg2

MAX_REQUEST_BITS = 16384    # max bits that can be requested
MAX_STREAM_SIZE = 1048576   # 2^20 bits
MIN_STREAM_SIZE = 32768     # 2^15 bits

app = Flask(__name__)  # init the flask application
CORS(app)  # enable cross-origin resource sharing on all routes

# init class objects
fish_stream = BitStream()  # class that holds processed bits in a stream
fish_pool = FishPool()  # class that processes raw bits from fish tank

# synch objects
streamResource = Semaphore()

# database url
url = urlparse.urlparse(os.environ["DATABASE_URL"])


# ************************************************************************
#   The following is all of the flask routes, that can be
#   accessed via http requests (https://fish-bit-hub.herokuapp.com/).
#   Here is a list of the following GET request routes:
#
#       ("/")               home route for stream analysis
#       ("/get-bytes")      returns a series of byte values
#       ("/get-binary")     returns a binary string
#       ("/get-ints")       returns a string of integers
#       ("/get-hex")        returns a string of hex values
#       ("/get-lottery")    returns a string of integers
#
#   The only POST route for this application is secured with
#   a secret key, there is only one client that is allowed to
#   make POST requests (raspberry pi). This route for this is
#
#       ("/set-bits")       sets the bits that are passing in
#                           through the header field.
#
# ************************************************************************

# home route, runs analysis on stream
@app.route("/")
def main_page():
    acquireReadLock()
    result = stream_analysis()
    releaseReadLock()
    return result


# returns a series of byte values
@app.route("/get-bytes")
def get_bytes():