<commit_before>#!/usr/bin/env python
__author__ = 'anthony'
from tornado import websocket, web, ioloop
import os, sys
import json
import logging
from daemon import Daemon
from urllib import urlencode, quote
from collections import OrderedDict

APP_PORT = 8004
SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIR = os.path.join(SCRIPT_DIR, 'templates')
STATIC_DIR = os.path.join(SCRIPT_DIR, 'static')
LOG_FILE = os.path.join(SCRIPT_DIR, 'webrtc.log')
PID_FILE = os.path.join(SCRIPT_DIR, 'webrtc.pid')

CALL_INIT = 'INI'
CALL_ONGOING = 'IN_PROG'
CALL_DROPPED = 'DROPPED'
LOGIN_TYPE = 'LOGIN'
CALL_TYPE = 'CALL'
PEER_UNAVAILABLE = 'Peer Unavailable'
PEER_BUSY = 'Peer Busy'
PEER_NOT_FOUND = 'Recepient not found'

sys.path.append(SCRIPT_DIR)
handler = logging.FileHandler(LOG_FILE)
formatter = logging.Formatter('[%(asctime)s] %(levelname)s %(message)s')
handler.setFormatter(formatter)
logger = logging.getLogger("WebRTCServer")
logger.addHandler(handler)
logger.setLevel(logging.INFO)

#in the future these values would be put in cache maybe
peers = {}
clients = {}

def get_notice_msg(caller_id, msg_type, status):
    data = {
     'caller' : caller_id,
     'msg_type' : msg_type,
     'status': status
     }
    return json.dumps(data)

def make_url(host, path, protocol='ws', **kwargs):
    base_url = '%s://%s%s' % (protocol, host, path)
    query_string =  urlencode(OrderedDict(kwargs))
    return '%s?%s' % (base_url, query_string)

class CallTemplateHandler(web.RequestHandler):

    def process_request(self, *args, **kwargs):
        peer_id = self.get_query_argument('peer_id', None) or self.get_argument('peer_id', None)
        id = self.get_query_argument('id', None) or self.get_argument('id', None)
        if clients.has_key(peer_id) is False:
            logger.warn('peer unavailable: %s' % peer_id)
            self.set_status(404, reason=PEER_UNAVAILABLE)
            return self.write(PEER_UNAVAILABLE)
        logger.debug('client call status %s: %s' % (peer_id, clients[peer_id].call_status))
        if clients[peer_id].call_status == LoginHandler.BUSY:
            self.set_status(423, reason=PEER_BUSY)
            return self.write(PEER_BUSY)
        caller_ws_uri = make_url(self.request.host, app.reverse_url('caller_ws'), id=id, peer_id=peer_id)
        self.render(os.path.join(TEMPLATE_DIR, "base.html"),
                    caller_ws_uri=caller_ws_uri, my_id=id, peer_id=peer_id, title='Caller')

    def get(self, *args, **kwargs):<commit_msg>log error when peer unavailable changed to debug<commit_after>#!/usr/bin/env python
__author__ = 'anthony'
from tornado import websocket, web, ioloop
import os, sys
import json
import logging
from daemon import Daemon
from urllib import urlencode, quote
from collections import OrderedDict

APP_PORT = 8004
SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIR = os.path.join(SCRIPT_DIR, 'templates')
STATIC_DIR = os.path.join(SCRIPT_DIR, 'static')
LOG_FILE = os.path.join(SCRIPT_DIR, 'webrtc.log')
PID_FILE = os.path.join(SCRIPT_DIR, 'webrtc.pid')

CALL_INIT = 'INI'
CALL_ONGOING = 'IN_PROG'
CALL_DROPPED = 'DROPPED'
LOGIN_TYPE = 'LOGIN'
CALL_TYPE = 'CALL'
PEER_UNAVAILABLE = 'Peer Unavailable'
PEER_BUSY = 'Peer Busy'
PEER_NOT_FOUND = 'Recepient not found'

sys.path.append(SCRIPT_DIR)
handler = logging.FileHandler(LOG_FILE)
formatter = logging.Formatter('[%(asctime)s] %(levelname)s %(message)s')
handler.setFormatter(formatter)
logger = logging.getLogger("WebRTCServer")
logger.addHandler(handler)
logger.setLevel(logging.INFO)

#in the future these values would be put in cache maybe
peers = {}
clients = {}

def get_notice_msg(caller_id, msg_type, status):
    data = {
     'caller' : caller_id,
     'msg_type' : msg_type,
     'status': status
     }
    return json.dumps(data)

def make_url(host, path, protocol='ws', **kwargs):
    base_url = '%s://%s%s' % (protocol, host, path)
    query_string =  urlencode(OrderedDict(kwargs))
    return '%s?%s' % (base_url, query_string)

class CallTemplateHandler(web.RequestHandler):

    def process_request(self, *args, **kwargs):
        peer_id = self.get_query_argument('peer_id', None) or self.get_argument('peer_id', None)
        id = self.get_query_argument('id', None) or self.get_argument('id', None)
        if clients.has_key(peer_id) is False:
            logger.debug('peer unavailable: %s' % peer_id)
            self.set_status(404, reason=PEER_UNAVAILABLE)
            return self.write(PEER_UNAVAILABLE)
        logger.debug('client call status %s: %s' % (peer_id, clients[peer_id].call_status))
        if clients[peer_id].call_status == LoginHandler.BUSY:
            self.set_status(423, reason=PEER_BUSY)
            return self.write(PEER_BUSY)
        caller_ws_uri = make_url(self.request.host, app.reverse_url('caller_ws'), id=id, peer_id=peer_id)
        self.render(os.path.join(TEMPLATE_DIR, "base.html"),
                    caller_ws_uri=caller_ws_uri, my_id=id, peer_id=peer_id, title='Caller')

    def get(self, *args, **kwargs):
{'fraction_non_alphanumeric': 0.12882703777335985, 'fraction_numerical': 0.003976143141153081, 'mean_word_length': 4.421336206896552, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1152995', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1795, 'n_words': 364}