<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gevent import monkey
monkey.patch_all()

from gevent.event import Event
from gevent.wsgi import WSGIServer
from flask import Flask, request, abort
from time import sleep
import ESL
from singleton import singleton

import sys
import logging
import optparse
from daemon import Daemon

import redis
from random import randint

__version__ = 'v1.0'

PORT = 5000

#Event Socket
EVENTSOCKET_HOST = '127.0.0.1'
EVENTSOCKET_PORT = '8021'
EVENTSOCKET_PASSWORD = '<PASSWORD>'

TESTDEBUG = True

# List of interface of Khomp Card
#INTERFACE_LIST = ['b0', 'b1', 'b2', 'b3']
INTERFACE_LIST = ['b0']
# Number of SIM cards on the Khomp Card
N_SIM = 4
#Expire Ressource / 300 seconds
SIM_TTL = 300
#Ressouce name
RESNAME = 'interface-4'

r_server = redis.Redis(host='localhost', port=6379)


def interface_reserve():
    """This function will try to find an interface we can use to send SMS
    which hasn't been busy for the last SIM_TTL
    it will select randomly an interface and then will increment to find
    one that is available.
    """
    randinterf = randint(0, len(INTERFACE_LIST) - 1)
    randsim = randint(1, N_SIM)
    mkey = "%s-%s-%d" % (RESNAME,
                        INTERFACE_LIST[randinterf],
                        randsim)

    for j in range(len(INTERFACE_LIST)):
        nextinterf = (randinterf + j) % len(INTERFACE_LIST)
        #print "Next interface to check %s" % INTERFACE_LIST[nextinterf]
        for i in range(1, N_SIM + 1):
            nextsim = (randsim + i) % N_SIM + 1
            #print "Next Sim to check %d" % nextsim

            mkey = "%s-%s-%d" % (RESNAME,
                        INTERFACE_LIST[nextinterf],
                        nextsim)
            #print "Searching on the interface %s...\n" % mkey
            if not r_server.get(mkey):
                #reserve ressource
                #print "Reserved Ressource (%s)" % mkey
                r_server.set(mkey, 1)
                r_server.expire(mkey, SIM_TTL)
                return mkey
            #else:
                #Ressource busy
                #print "Ressource Busy"
    return False


# for k in range(1,10):
#     res_interface = interface_reserve()
#     print res_interface
# sys.exit()

@singleton
class connectESL(object):
    def __init__(self, host, port, password):
        self.host = host
        self.port = port
        self.password = password
        self.con = ESL.ESLconnection(self.host, self.port, self.password)

    def spam_connection(self):
        return self.con

    def reconnect(self):
        self.con = ESL.ESLconnection(self.host, self.port, self.password)


handler_esl = connectESL(
                    EVENTSOCKET_HOST,
                    EVENTSOCKET_PORT,
                    EVENTSOCKET_PASSWORD)

app = Flask(__name__)

#setup logger
logger = logging.getLogger("sms_khomp_api")
logger.setLevel(logging.DEBUG)

# create file handler which logs even debug messages
fh = logging.FileHandler("/var/log/sms-khomp-api/sms_khomp_api.log")
fh.setLevel(logging.DEBUG)

# create formatter and add it to the handlers
formatter = logging.Formatter("%(asctime)s [%(levelname)s] "\
                                "%(name)s\t%(message)s",
                                datefmt="%Y-%m-%d %H:%M:%S")
fh.setFormatter(formatter)

# add the handlers to the logger
logger.addHandler(fh)


#Add Flask Routes
@app.route("/")
def index():
    return "Welcome to Khomp SMS API " + __version__


@app.route("/documentation/")
def documenation():
    documentation = "<b>DOCUMENTATION</b><br>"\
        "------------------------------<br/>"\
        "<br/>Send SMS - Url <b>/v1.0/sendsms</b><br/><br/>"\
        "Parameters :<br/>"\
        " @ recipient : Phone Number of the person receving the SMS<br>"\
        " @ message : Message content to be send on the SMS<br/>"\
        " @ interface : Set the interface to use to send the SMS, default b0"
    return documentation

#<EMAIL>('/v1.0/sendsms/<recipient>/<sender>/<message>')
#def sendsms(recipient, sender, message):
#    # show the user profile for that user
#    return 'Send SMS %s / %s / %s' % (recipient, sender, message)


@app.route('/v1.0/sendsms', methods=['POST'])
def sendsms():
    if request.method == 'POST':
        if 'recipient' in request.form \
            and 'message' in request.form \
            and 'interface' in request.form:

            if not request.form['interface'] \
                or len(request.form['interface']) > 4:
                interface = 'b0'
            else:
                interface = request.form['interface']

            recipient = request.form['recipient']
            message = request.form['message']

            #Send SMS via ESL
            command_string = "sms %s %s '%s'" % \
                                (str(interface), str(recipient), str(message))
            if (TESTDEBUG):
                #reserve a ressource
                rsd_int = interface_reserve()
                if not rsd_int:
                    #TODO: Check 500 code, replace something for throttle
                    abort(500, 'Ressource unvailable throttle')
                print "Ressource is being used %s" % (rsd_int)
                sleep(10)
                #Send SMS Via Khomp FreeSWITCH API
                #...

                #Free ressource
                r_server.delete(rsd_int)
                return "Sent Fake SMS - Success"

            elif (TESTDEBUG and False):
                if not handler_esl.con.connected():
                    #Try to reconnect
                    handler_esl.reconnect()
                    if not handler_esl.con.connected():
                        abort(500, 'API Server not connected to FreeSWITCH')
                ev = handler_esl.con.api("show channels")
                sleep(1)
                try:
                    result = ev.getBody()
                except AttributeError:
                    abort(500, 'Error Sending SMS')
                return result
            else:<commit_msg>change sleep time<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gevent import monkey
monkey.patch_all()

from gevent.event import Event
from gevent.wsgi import WSGIServer
from flask import Flask, request, abort
from time import sleep
import ESL
from singleton import singleton

import sys
import logging
import optparse
from daemon import Daemon

import redis
from random import randint

__version__ = 'v1.0'

PORT = 5000

#Event Socket
EVENTSOCKET_HOST = '127.0.0.1'
EVENTSOCKET_PORT = '8021'
EVENTSOCKET_PASSWORD = '<PASSWORD>'

TESTDEBUG = True

# List of interface of Khomp Card
#INTERFACE_LIST = ['b0', 'b1', 'b2', 'b3']
INTERFACE_LIST = ['b0']
# Number of SIM cards on the Khomp Card
N_SIM = 4
#Expire Ressource / 300 seconds
SIM_TTL = 300
#Ressouce name
RESNAME = 'interface-4'

r_server = redis.Redis(host='localhost', port=6379)


def interface_reserve():
    """This function will try to find an interface we can use to send SMS
    which hasn't been busy for the last SIM_TTL
    it will select randomly an interface and then will increment to find
    one that is available.
    """
    randinterf = randint(0, len(INTERFACE_LIST) - 1)
    randsim = randint(1, N_SIM)
    mkey = "%s-%s-%d" % (RESNAME,
                        INTERFACE_LIST[randinterf],
                        randsim)

    for j in range(len(INTERFACE_LIST)):
        nextinterf = (randinterf + j) % len(INTERFACE_LIST)
        #print "Next interface to check %s" % INTERFACE_LIST[nextinterf]
        for i in range(1, N_SIM + 1):
            nextsim = (randsim + i) % N_SIM + 1
            #print "Next Sim to check %d" % nextsim

            mkey = "%s-%s-%d" % (RESNAME,
                        INTERFACE_LIST[nextinterf],
                        nextsim)
            #print "Searching on the interface %s...\n" % mkey
            if not r_server.get(mkey):
                #reserve ressource
                #print "Reserved Ressource (%s)" % mkey
                r_server.set(mkey, 1)
                r_server.expire(mkey, SIM_TTL)
                return mkey
            #else:
                #Ressource busy
                #print "Ressource Busy"
    return False


# for k in range(1,10):
#     res_interface = interface_reserve()
#     print res_interface
# sys.exit()

@singleton
class connectESL(object):
    def __init__(self, host, port, password):
        self.host = host
        self.port = port
        self.password = password
        self.con = ESL.ESLconnection(self.host, self.port, self.password)

    def spam_connection(self):
        return self.con

    def reconnect(self):
        self.con = ESL.ESLconnection(self.host, self.port, self.password)


handler_esl = connectESL(
                    EVENTSOCKET_HOST,
                    EVENTSOCKET_PORT,
                    EVENTSOCKET_PASSWORD)

app = Flask(__name__)

#setup logger
logger = logging.getLogger("sms_khomp_api")
logger.setLevel(logging.DEBUG)

# create file handler which logs even debug messages
fh = logging.FileHandler("/var/log/sms-khomp-api/sms_khomp_api.log")
fh.setLevel(logging.DEBUG)

# create formatter and add it to the handlers
formatter = logging.Formatter("%(asctime)s [%(levelname)s] "\
                                "%(name)s\t%(message)s",
                                datefmt="%Y-%m-%d %H:%M:%S")
fh.setFormatter(formatter)

# add the handlers to the logger
logger.addHandler(fh)


#Add Flask Routes
@app.route("/")
def index():
    return "Welcome to Khomp SMS API " + __version__


@app.route("/documentation/")
def documenation():
    documentation = "<b>DOCUMENTATION</b><br>"\
        "------------------------------<br/>"\
        "<br/>Send SMS - Url <b>/v1.0/sendsms</b><br/><br/>"\
        "Parameters :<br/>"\
        " @ recipient : Phone Number of the person receving the SMS<br>"\
        " @ message : Message content to be send on the SMS<br/>"\
        " @ interface : Set the interface to use to send the SMS, default b0"
    return documentation

<EMAIL>('/v1.0/sendsms/<recipient>/<sender>/<message>')
#def sendsms(recipient, sender, message):
#    # show the user profile for that user
#    return 'Send SMS %s / %s / %s' % (recipient, sender, message)


@app.route('/v1.0/sendsms', methods=['POST'])
def sendsms():
    if request.method == 'POST':
        if 'recipient' in request.form \
            and 'message' in request.form \
            and 'interface' in request.form:

            if not request.form['interface'] \
                or len(request.form['interface']) > 4:
                interface = 'b0'
            else:
                interface = request.form['interface']

            recipient = request.form['recipient']
            message = request.form['message']

            #Send SMS via ESL
            command_string = "sms %s %s '%s'" % \
                                (str(interface), str(recipient), str(message))
            if (TESTDEBUG):
                #reserve a ressource
                rsd_int = interface_reserve()
                if not rsd_int:
                    #TODO: Check 500 code, replace something for throttle
                    abort(500, 'Ressource unvailable throttle')
                print "Ressource is being used %s" % (rsd_int)
                sleep(0.001)
                #Send SMS Via Khomp FreeSWITCH API
                #...

                #Free ressource
                r_server.delete(rsd_int)
                return "Sent Fake SMS - Success"

            elif (TESTDEBUG and False):
                if not handler_esl.con.connected():
                    #Try to reconnect
                    handler_esl.reconnect()
                    if not handler_esl.con.connected():
                        abort(500, 'API Server not connected to FreeSWITCH')
                ev = handler_esl.con.api("show channels")
                sleep(10)
                try:
                    result = ev.getBody()
                except AttributeError:
                    abort(500, 'Error Sending SMS')
                return result
            else:
{'fraction_non_alphanumeric': 0.1126480576493001, 'fraction_numerical': 0.0115132941273917, 'mean_word_length': 1.9564152791381, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '753013', 'n_tokens_mistral': 3824, 'n_tokens_neox': 3549, 'n_words': 1015}