<commit_before>require('./db_util.js');
var http       = require('http');
var dispatcher = require('httpdispatcher');
var PORT       = 80;
var db_util    = new dbUtil();

var handleRegisterRequest = function(data, onSuccess, onFailure) {
    var clientInfo = {
        service:data.service,
        token:data.token,
        kpTrigger:data.kpTrigger,
        latitude:data.latitude,
        longitude:data.longitude,
        notify_start_time:data.notify_start_time,
        notify_stop_time:data.notify_stop_time
    };

    db_util.insertClient(clientInfo, onSuccess, onFailure);
};

var handleUpdateRequest = function(data, onSuccess, onFailure) {
    var clientInfo = {
        service:data.service,
        token:data.token,
        kpTrigger:data.kpTrigger,
        latitude:data.latitude,
        longitude:data.longitude,
        notify_start_time:data.notify_start_time,
        notify_stop_time:data.notify_stop_time
    };

    db_util.updateClient(clientInfo, onSuccess, onFailure);
};

var handleRemoveRequest = function(data, onSuccess, onFailure) {
    var clientInfo = {
        service:data.service,
        token:data.token,
        kpTrigger:data.kpTrigger,
        latitude:data.latitude,
        longitude:data.longitude,
        notify_start_time:data.notify_start_time,
        notify_stop_time:data.notify_stop_time
    };

    db_util.removeClient(clientInfo, onSuccess, onFailure);
};

var handlePostData = function(postData, onSuccess, onFailure) {
    var mode = postData.mode;
    console.log('Handling post with mode: ' + mode);

    switch(mode) {
        case 'register':
            handleRegisterRequest(postData, onSuccess, onFailure);
            break;
        case 'update':
            handleUpdateRequest(postData, onSuccess, onFailure);
            break;
        case 'remove':
            handleRemoveRequest(postData, onSuccess, onFailure);
            break;
        default:
            onFailure('No mode set.');
    }
};

dispatcher.onError(function(req, res) {
    console.log('Received request for nonexistent page.\nSending 404.');
    res.writeHead(404);
    res.end('404 - NOT FOUND');
});  

dispatcher.onPost('/notification_service', function(req, res) {
    var params;
    try{
        params = Object.keys(req.params)[0];
        params = params.replace(/[\\]+/g, '');
        params = JSON.parse(params);
    }
    catch(err) {
        params = req.params;
    }

    console.log('Received post: ' + JSON.stringify(params));
    var onSuccess = function(successMessage) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end(JSON.stringify({message:successMessage}));
    };

    var onFailure = function(errorMessage) {
        res.writeHead(400, {'Content-Type': 'text/html'});
        res.end(JSON.stringify({err:errorMessage}));
    };

    handlePostData(params, onSuccess, onFailure);
});

var handleRequest = function(request, response){
    try {
        dispatcher.dispatch(request, response);
    } catch(err) {
        console.log(err);
    }
};


var server = http.createServer(handleRequest);

server.listen(PORT, function(){
    console.log('Listening on: http://localhost:%s', PORT);
});<commit_msg>Integrated notification sender into server and set to run in 15 minute intervals.<commit_after>require('./db_util.js');
require('./notification_sender.js');
var http       = require('http');
var dispatcher = require('httpdispatcher');
var PORT       = 80;
var db_util    = new dbUtil();
var notifier   = new Notifier();

var handleRegisterRequest = function(data, onSuccess, onFailure) {
    var clientInfo = {
        service:data.service,
        token:data.token,
        kpTrigger:data.kpTrigger,
        latitude:data.latitude,
        longitude:data.longitude,
        notify_start_time:data.notify_start_time,
        notify_stop_time:data.notify_stop_time
    };

    db_util.insertClient(clientInfo, onSuccess, onFailure);
};

var handleUpdateRequest = function(data, onSuccess, onFailure) {
    var clientInfo = {
        service:data.service,
        token:data.token,
        kpTrigger:data.kpTrigger,
        latitude:data.latitude,
        longitude:data.longitude,
        notify_start_time:data.notify_start_time,
        notify_stop_time:data.notify_stop_time
    };

    db_util.updateClient(clientInfo, onSuccess, onFailure);
};

var handleRemoveRequest = function(data, onSuccess, onFailure) {
    var clientInfo = {
        service:data.service,
        token:data.token,
        kpTrigger:data.kpTrigger,
        latitude:data.latitude,
        longitude:data.longitude,
        notify_start_time:data.notify_start_time,
        notify_stop_time:data.notify_stop_time
    };

    db_util.removeClient(clientInfo, onSuccess, onFailure);
};

var handlePostData = function(postData, onSuccess, onFailure) {
    var mode = postData.mode;
    console.log('Handling post with mode: ' + mode);

    switch(mode) {
        case 'register':
            handleRegisterRequest(postData, onSuccess, onFailure);
            break;
        case 'update':
            handleUpdateRequest(postData, onSuccess, onFailure);
            break;
        case 'remove':
            handleRemoveRequest(postData, onSuccess, onFailure);
            break;
        default:
            onFailure('No mode set.');
    }
};

dispatcher.onError(function(req, res) {
    console.log('Received request for nonexistent page.\nSending 404.');
    res.writeHead(404);
    res.end('404 - NOT FOUND');
});  

dispatcher.onPost('/notification_service', function(req, res) {
    var params;
    try{
        params = Object.keys(req.params)[0];
        params = params.replace(/[\\]+/g, '');
        params = JSON.parse(params);
    }
    catch(err) {
        params = req.params;
    }

    console.log('Received post: ' + JSON.stringify(params));
    var onSuccess = function(successMessage) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end(JSON.stringify({message:successMessage}));
    };

    var onFailure = function(errorMessage) {
        res.writeHead(400, {'Content-Type': 'text/html'});
        res.end(JSON.stringify({err:errorMessage}));
    };

    handlePostData(params, onSuccess, onFailure);
});

var handleRequest = function(request, response){
    try {
        dispatcher.dispatch(request, response);
    } catch(err) {
        console.log(err);
    }
};

var notificationInterval = setInterval(function() {
    notifier.notifyClients();
}, 1000*60*15);

var server = http.createServer(handleRequest);

server.listen(PORT, function(){
    console.log('Listening on: http://localhost:%s', PORT);
});
{'fraction_non_alphanumeric': 0.1264714760036221, 'fraction_numerical': 0.006942348324781165, 'mean_word_length': 2.9777911164465785, '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': '3601674', 'n_tokens_mistral': 2017, 'n_tokens_neox': 1955, 'n_words': 354}