<commit_before>/*
 * Copyrights phwang
 * Written by <NAME>
 * File name: switch_module.js
 */

module.exports = {
    malloc: function (fibre_val) {
        return new SwitchObject(fibre_val);
    },
};

function SwitchObject(fibre_val) {
    "use strict";
    this.theFibreObject = fibre_val;

    this.linkModuleMalloc = function (my_name_val, link_id_val) {
        var link_module = require("./link_module.js");
        return link_module.malloc(this, my_name_val, link_id_val);
    };

    this.objectName = function () {
        return "SwitchObject";
    };

    this.fibreObject = function () {
        return this.theFibreObject;
    };

    this.rootObject = function () {
        return this.fibreObject().rootObject();
    };

    this.utilObject = function () {
        return this.rootObject().utilObject();
    };

    this.linkMgrObject = function () {
        return this.fibreObject().linkMgrObject();
    };

    this.sessionMgrObject = function () {
        return this.fibreObject().sessionMgrObject();
    };

    this.initSwitchTable = function () {
        this.switch_table = {
            "setup_link": this.setupLink,
            "get_link_data": this.getLinkData,
            "put_link_data": this.putLinkData,
            "get_name_list": this.getNameList,
            "setup_session": this.setupSession,
            "get_session_data": this.getSessionData,
            "put_session_data": this.putSessionData,
            "keep_alive": this.keepAlive,
        };
    };

    this.switchRequest = function (go_request) {
        if (!go_request) {
            this.abend("switchRequest", "null go_request");
            return null;
        }

        this.debug(true, "switchRequest", "command=" + go_request.command);

        var func = this.switch_table[go_request.command];
        if (func) {
            return func.bind(this)(go_request);
        }
        else {
            this.abend("switchRequest", "bad command=" + go_request.command);
            return null;
        }
    }

    this.setupLink = function (go_request) {
        var link = this.linkMgrObject().searchAndCreate(go_request.my_name, 0);
        if (!link) {
            this.abend("setupLink", "null link");
            return null;
        }
        link.resetKeepAliveTimer();

        var json_data = JSON.stringify({link_id: link.linkId()});
        this.debug(true, "setupLink", "name=" + go_request.my_name + " link_id=" + link.linkId());
        return json_data;
    };

    this.getLinkObject = function (go_request) {
        var link = this.linkMgrObject().searchLink(go_request.my_name, go_request.link_id);
        if (!link) {
            this.abend("getLinkObject", "null link" + "link_id=" + go_request.link_id + " my_name=" + go_request.my_name);
            return null;
        }
        if (link.linkId() === 0) {
            this.abend("getLinkObject", "link_id = 0");
            return null;
        }
        link.resetKeepAliveTimer();
        return link;
    };

    this.getLinkData = function (go_request) {
        this.debug(false, "getLinkData", "link_id=" + go_request.link_id + " my_name=" + go_request.my_name + " ajax_id=" + go_request.ajax_id);

        var link = this.getLinkObject(go_request);
        if (!link) {
            return null;
        }

        var data = link.receiveQueue().deQueue();
        if (data) {
            this.logit("getLinkData", "link_id=" + go_request.link_id + " my_name="  + go_request.my_name + " data={" + data + "}");
        }
        return JSON.stringify(data);
    };

    this.putLinkData = function (go_request) {
        this.abend("putLinkData", "putLinkData is not implemented");
    };

    this.getNameList = function (go_request) {
        var link = this.getLinkObject(go_request);
        if (!link) {
            return null;
        }

        var json_data = JSON.stringify(this.linkMgrObject().getNameList());
        this.debug(false, "getNameList", "(" + link.linkId() + ",0) " + go_request.my_name + "=>server " + json_data);
        return json_data;
    };

    this.setupSession = function (go_request) {
        var session = this.sessionMgrObject().searchSession(go_request.my_name, go_request.his_name, go_request.link_id);
        if (!session){
            session = this.sessionMgrObject().searchAndCreate(go_request.my_name, go_request.his_name, 0);
            if (!session) {
                res.send(this.jsonStingifyData(go_request.command, go_request.ajax_id, null));
                this.abend("setupSession", "null session");
                return null;
            }
        }

        var his_link = this.linkMgrObject().searchLink(go_request.his_name, 0);
        if (!his_link) {
            res.send(this.jsonStingifyData(go_request.command, go_request.ajax_id, null));
            return null;
        }

        this.debug(true, "setupSession", "(" + go_request.link_id + "," + session.sessionId() + "," + session.hisSession().sessionId() + ") " + go_request.my_name + "=>" + go_request.his_name + " data=" + go_request.data);

        if (go_request.data !== null) {
            session.clusterObject().processSetupTopicData(go_request.data);
        }

        var session_id_str = "" + session.hisSession().sessionId();
        var data = JSON.stringify({
                        order: "setup_session",
                        session_id: session_id_str,
                        his_name: go_request.my_name,
                        my_name: go_request.his_name,
                        extra_data: go_request.data,
                    });
        his_link.receiveQueue().enQueue(data);
        this.sessionMgrObject().preSessionQueue().enQueue(data)
        return this.setupSessionReply(session, go_request);
    }

    this.setupSessionReply = function (session_val, go_request) {
        var json_data = JSON.stringify({
                        session_id: session_val.sessionId(),
                        extra_data: go_request.data,
                    });
        this.logit("setupSessionReply", "(" + go_request.link_id + "," + session_val.sessionId() + "," + session_val.hisSession().sessionId() + ") " + go_request.my_name + "=>" + go_request.his_name);
        return json_data;
    }

    this.getSessionObject = function (go_request) {
        var link = this.getLinkObject(go_request);
        if (!link) {
            return null;
        }

        var session = this.sessionMgrObject().searchSession(go_request.my_name, go_request.his_name, go_request.session_id);
        if (!session) {
            this.abend("getSessionObject", "null session" + " session_id=" + go_request.session_id);
            return null;
        }

        return session;
    };

    this.getSessionData = function (go_request) {
        this.debug(false, "getSessionData", "(" + go_request.link_id + "," + go_request.session_id + ") my_name=" + go_request.my_name + "=>" + go_request.his_name);

        var session = this.getSessionObject(go_request);
        if (!session) {
            return null;
        }

        var res_data = session.dequeueTransmitData();
        if (!res_data) {
            this.debug(false, "getSessionData", "no data");
            //res.send(this.jsonStingifyData(go_request.command, go_request.ajax_id, null));
            return null;
        }
        this.logit("getSessionData", "res_data=" + res_data);

        this.debug(false, "getSessionData", "ajax_id=" + go_request.ajax_id);
        this.logit("getSessionData", "(" + go_request.link_id + "," + go_request.session_id + ") "  + go_request.his_name + "=>" + go_request.my_name + " {" + res_data + "}");
        return res_data;
    };

    this.putSessionData = function (go_request) {
        //console.log(req.headers);
        this.debug(true, "putSessionData ", "ajax_id=" + go_request.ajax_id);
        this.debug(true, "putSessionData ", "(" + go_request.link_id + "," + go_request.session_id + ") "  + go_request.his_name + "=>" + go_request.my_name + " {" + go_request.data + "}");

        var session = this.getSessionObject(go_request);
        if (!session) {
            return null;
        }

        this.debug(true, "putSessionData", "(" + go_request.link_id + "," + go_request.session_id + ") "  + go_request.my_name + "=>" + go_request.his_name + " {" + go_request.data + "} " + go_request.xmt_seq + "=>" + session.up_seq);

        if (go_request.xmt_seq === session.up_seq) {
            session.clusterObject().enqueAndPocessReceiveData(go_request.data);
            session.up_seq += 1;
        } else if (go_request.xmt_seq < session.up_seq) {
            if (go_request.xmt_seq === 0) {
                session.clusterObject().enqueAndPocessReceiveData(go_request.data);
                session.up_seq = 1;
                this.logit("putSessionData", go_request.data + " post " + go_request.xmt_seq + " reset");
            } else {
                this.logit("putSessionData", "(" + link_id + "," + session_id + ") "  + go_request.my_name + "=>" + go_request.his_name + " {" + go_request.data + "} " + go_request.xmt_seq + " dropped");
            }<commit_msg>get session<commit_after>/*
 * Copyrights phwang
 * Written by <NAME>
 * File name: switch_module.js
 */

module.exports = {
    malloc: function (fibre_val) {
        return new SwitchObject(fibre_val);
    },
};

function SwitchObject(fibre_val) {
    "use strict";
    this.theFibreObject = fibre_val;

    this.linkModuleMalloc = function (my_name_val, link_id_val) {
        var link_module = require("./link_module.js");
        return link_module.malloc(this, my_name_val, link_id_val);
    };

    this.objectName = function () {
        return "SwitchObject";
    };

    this.fibreObject = function () {
        return this.theFibreObject;
    };

    this.rootObject = function () {
        return this.fibreObject().rootObject();
    };

    this.utilObject = function () {
        return this.rootObject().utilObject();
    };

    this.linkMgrObject = function () {
        return this.fibreObject().linkMgrObject();
    };

    this.sessionMgrObject = function () {
        return this.fibreObject().sessionMgrObject();
    };

    this.initSwitchTable = function () {
        this.switch_table = {
            "setup_link": this.setupLink,
            "get_link_data": this.getLinkData,
            "put_link_data": this.putLinkData,
            "get_name_list": this.getNameList,
            "setup_session": this.setupSession,
            "get_session_data": this.getSessionData,
            "put_session_data": this.putSessionData,
            "keep_alive": this.keepAlive,
        };
    };

    this.switchRequest = function (go_request) {
        if (!go_request) {
            this.abend("switchRequest", "null go_request");
            return null;
        }

        this.debug(false, "switchRequest", "command=" + go_request.command);

        var func = this.switch_table[go_request.command];
        if (func) {
            return func.bind(this)(go_request);
        }
        else {
            this.abend("switchRequest", "bad command=" + go_request.command);
            return null;
        }
    }

    this.setupLink = function (go_request) {
        var link = this.linkMgrObject().searchAndCreate(go_request.my_name, 0);
        if (!link) {
            this.abend("setupLink", "null link");
            return null;
        }
        link.resetKeepAliveTimer();

        var json_data = JSON.stringify({link_id: link.linkId()});
        this.debug(true, "setupLink", "name=" + go_request.my_name + " link_id=" + link.linkId());
        return json_data;
    };

    this.getLinkObject = function (go_request) {
        var link = this.linkMgrObject().searchLink(go_request.my_name, go_request.link_id);
        if (!link) {
            this.abend("getLinkObject", "null link" + "link_id=" + go_request.link_id + " my_name=" + go_request.my_name);
            return null;
        }
        if (link.linkId() === 0) {
            this.abend("getLinkObject", "link_id = 0");
            return null;
        }
        link.resetKeepAliveTimer();
        return link;
    };

    this.getLinkData = function (go_request) {
        this.debug(false, "getLinkData", "link_id=" + go_request.link_id + " my_name=" + go_request.my_name + " ajax_id=" + go_request.ajax_id);

        var link = this.getLinkObject(go_request);
        if (!link) {
            return null;
        }

        var data = link.receiveQueue().deQueue();
        if (data) {
            this.logit("getLinkData", "link_id=" + go_request.link_id + " my_name="  + go_request.my_name + " data={" + data + "}");
        }
        return JSON.stringify(data);
    };

    this.putLinkData = function (go_request) {
        this.abend("putLinkData", "putLinkData is not implemented");
    };

    this.getNameList = function (go_request) {
        var link = this.getLinkObject(go_request);
        if (!link) {
            return null;
        }

        var json_data = JSON.stringify(this.linkMgrObject().getNameList());
        this.debug(false, "getNameList", "(" + link.linkId() + ",0) " + go_request.my_name + "=>server " + json_data);
        return json_data;
    };

    this.setupSession = function (go_request) {
        var session = this.sessionMgrObject().searchSession(go_request.my_name, go_request.his_name, go_request.link_id);
        if (!session){
            session = this.sessionMgrObject().searchAndCreate(go_request.my_name, go_request.his_name, 0);
            if (!session) {
                res.send(this.jsonStingifyData(go_request.command, go_request.ajax_id, null));
                this.abend("setupSession", "null session");
                return null;
            }
        }

        var his_link = this.linkMgrObject().searchLink(go_request.his_name, 0);
        if (!his_link) {
            res.send(this.jsonStingifyData(go_request.command, go_request.ajax_id, null));
            return null;
        }

        this.debug(true, "setupSession", "(" + go_request.link_id + "," + session.sessionId() + "," + session.hisSession().sessionId() + ") " + go_request.my_name + "=>" + go_request.his_name + " data=" + go_request.data);

        if (go_request.data !== null) {
            session.clusterObject().processSetupTopicData(go_request.data);
        }

        var session_id_str = "" + session.hisSession().sessionId();
        var data = JSON.stringify({
                        order: "setup_session",
                        session_id: session_id_str,
                        his_name: go_request.my_name,
                        my_name: go_request.his_name,
                        extra_data: go_request.data,
                    });
        his_link.receiveQueue().enQueue(data);
        this.sessionMgrObject().preSessionQueue().enQueue(data)
        return this.setupSessionReply(session, go_request);
    }

    this.setupSessionReply = function (session_val, go_request) {
        var json_data = JSON.stringify({
                        session_id: session_val.sessionId(),
                        extra_data: go_request.data,
                    });
        this.logit("setupSessionReply", "(" + go_request.link_id + "," + session_val.sessionId() + "," + session_val.hisSession().sessionId() + ") " + go_request.my_name + "=>" + go_request.his_name);
        return json_data;
    }

    this.getSessionObject = function (go_request) {
        var link = this.getLinkObject(go_request);
        if (!link) {
            return null;
        }

        var session = this.sessionMgrObject().searchSession(go_request.my_name, go_request.his_name, go_request.session_id);
        if (!session) {
            this.abend("getSessionObject", "null session" + " session_id=" + go_request.session_id);
            return null;
        }

        return session;
    };

    this.getSessionData = function (go_request) {
        this.debug(false, "getSessionData", "(" + go_request.link_id + "," + go_request.session_id + ") my_name=" + go_request.my_name + "=>" + go_request.his_name);

        var session = this.getSessionObject(go_request);
        if (!session) {
            return null;
        }

        var res_data = session.dequeueTransmitData();
        if (!res_data) {
            this.debug(false, "getSessionData", "no data");
            //res.send(this.jsonStingifyData(go_request.command, go_request.ajax_id, null));
            return null;
        }
        this.logit("getSessionData", "res_data=" + res_data);

        this.debug(false, "getSessionData", "ajax_id=" + go_request.ajax_id);
        this.logit("getSessionData", "(" + go_request.link_id + "," + go_request.session_id + ") "  + go_request.his_name + "=>" + go_request.my_name + " {" + res_data + "}");
        return JSON.stringify({session_id: session.sessionId(),
                               res_data: res_data,
                           });
    };

    this.putSessionData = function (go_request) {
        //console.log(req.headers);
        this.debug(true, "putSessionData ", "ajax_id=" + go_request.ajax_id);
        this.debug(true, "putSessionData ", "(" + go_request.link_id + "," + go_request.session_id + ") "  + go_request.his_name + "=>" + go_request.my_name + " {" + go_request.data + "}");

        var session = this.getSessionObject(go_request);
        if (!session) {
            return null;
        }

        this.debug(true, "putSessionData", "(" + go_request.link_id + "," + go_request.session_id + ") "  + go_request.my_name + "=>" + go_request.his_name + " {" + go_request.data + "} " + go_request.xmt_seq + "=>" + session.up_seq);

        if (go_request.xmt_seq === session.up_seq) {
            session.clusterObject().enqueAndPocessReceiveData(go_request.data);
            session.up_seq += 1;
        } else if (go_request.xmt_seq < session.up_seq) {
            if (go_request.xmt_seq === 0) {
                session.clusterObject().enqueAndPocessReceiveData(go_request.data);
                session.up_seq = 1;
                this.logit("putSessionData", go_request.data + " post " + go_request.xmt_seq + " reset");
            } else {
                this.logit("putSessionData", "(" + link_id + "," + session_id + ") "  + go_request.my_name + "=>" + go_request.his_name + " {" + go_request.data + "} " + go_request.xmt_seq + " dropped");
            }
{'fraction_non_alphanumeric': 0.13908872901678657, 'fraction_numerical': 0.0009810333551340746, 'mean_word_length': 2.300179856115108, 'pattern_counts': {'":': 16, '<': 7, '<?xml version=': 0, '>': 23, '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': '970709', 'n_tokens_mistral': 5736, 'n_tokens_neox': 5604, 'n_words': 890}