text:
AgeRanger web site is implemented and good to go! Sales manager should be proud of his IT team! :-)

Most important technologies and frameworks:
	Server-side:
		ASP.NET 4
		APS.NET Web API 2
		Ninject
		Dapper
		NLog
		Swashbuckle

	Front-end:
		Angular 1.5
		Bootstrap 3
		angular-bootstrap

	Unit testing:
		NUnit
		Moq
		Jasmine
		PhantomJS

	Visual Studio extensions:
		ReSharper - used as Jasmine and NUnit tests runtime.

Demo web site - http://ageranger.azurewebsites.net/#/

Demo API page - http://ageranger.azurewebsites.net/swagger/ui/index

Further improvements:
1. Add server-side paging and search
2. Move setting of AgeGroup from Linq query to SQL query
3. Implement caching
4. Implement security

Cheers,
Evgeni0us


text:
package com.amazonaws.services.iot1clickprojects.model;

import java.io.Serializable;
import javax.annotation.Generated;

import com.amazonaws.AmazonWebServiceRequest;

/**
 *
 * @see AWS
 *      API Documentation
 */
@Generated("com.amazonaws:aws-java-sdk-code-generator")
public class DescribeProjectRequest extends com.amazonaws.AmazonWebServiceRequest implements Serializable, Cloneable {

    /**
     *

     * The name of the project to be described.
     *

     */
    private String projectName;

    /**
     *

     * The name of the project to be described.
     *

     *
     * @param projectName
     *        The name of the project to be described.
     */

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    /**
     *

     * The name of the project to be described.
     *

     *
     * @return The name of the project to be described.
     */

    public String getProjectName() {
        return this.projectName;
    }

    /**
     *

     * The name of the project to be described.
     *

     *
     * @param projectName
     *        The name of the project to be described.
     * @return Returns a reference to this object so that method calls can be chained together.
     */

    public DescribeProjectRequest withProjectName(String projectName) {
        setProjectName(projectName);
        return this;
    }

    /**
     * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be
     * redacted from this string using a placeholder value.
     *
     * @return A string representation of this object.
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        if (getProjectName() != null)
            sb.append("ProjectName: ").append(getProjectName());
        sb.append("}");
        return sb.toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;

        if (obj instanceof DescribeProjectRequest == false)
            return false;
        DescribeProjectRequest other = (DescribeProjectRequest) obj;
        if (other.getProjectName() == null ^ this.getProjectName() == null)
            return false;
        if (other.getProjectName() != null && other.getProjectName().equals(this.getProjectName()) == false)
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int hashCode = 1;

        hashCode = prime * hashCode + ((getProjectName() == null) ? 0 : getProjectName().hashCode());
        return hashCode;
    }

    @Override
    public DescribeProjectRequest clone() {
        return (DescribeProjectRequest) super.clone();
    }

}

redpajama_set_name: RedPajamaGithub


text:
namespace AddingANewOrder
{
    using System;
    using System.Transactions;
    using Northwind.Data;
    using System.Collections.Generic;

    class AddingANewOrderUI
    {
        static void Main()
        {
            string decorationLine = new string('-', Console.WindowWidth);
            Console.Write(decorationLine);
            Console.WriteLine("***Adding a new order in the 'Northwind' database***");
            Console.Write(decorationLine);

            int orderInfosCount = 5;
            IEnumerable<object[]> orderInfos = GetOrderInfos(orderInfosCount);

            try
            {
                AddNewOrders(orderInfos);
                Console.WriteLine("Successfully added!");
            }
            catch (TransactionAbortedException)
            {
                Console.WriteLine("Adding orders failed. All changes are reversed!");
            }
        }

        static void AddNewOrders(IEnumerable<object[]> orderInfos)
        {
            using (NorthwindEntities northwindEntities = new NorthwindEntities())
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    foreach (object[] orderInfo in orderInfos)
                    {
                        Order order = new Order()
                        {
                            CustomerID = (orderInfo[0] as string),
                            EmployeeID = (orderInfo[1] as int?),
                            OrderDate = (orderInfo[2] as DateTime?),
                            RequiredDate = (orderInfo[3] as DateTime?),
                            ShippedDate = (orderInfo[4] as DateTime?),
                            ShipVia = (orderInfo[5] as int?),
                            Freight = (orderInfo[6] as decimal?),
                            ShipName = (orderInfo[7] as string),
                            ShipAddress = (orderInfo[8] as string),
                            ShipCity = (orderInfo[9] as string),
                            ShipRegion = (orderInfo[10] as string),
                            ShipPostalCode = (orderInfo[11] as string),
                            ShipCountry = (orderInfo[12] as string),
                        };

                        northwindEntities.Orders.Add(order);
                    }

                    northwindEntities.SaveChanges();
                    scope.Complete();
                }
            }
        }

        static IEnumerable<object[]> GetOrderInfos(int orderInfosCount)
        {
            IList<object[]> orderInfos = new List<object[]>();
            for (int i = 0; i < orderInfosCount; i++)
            {
                object[] orderInfo = new object[13];
                orderInfo[0] = null;
                orderInfo[1] = i + 1;
                orderInfo[2] = DateTime.Now.Subtract(new TimeSpan(i + 1, 0, 0));
                orderInfo[3] = DateTime.Now.Subtract(new TimeSpan(i + 3, 0, 0));
                orderInfo[4] = null;
                orderInfo[5] = null;
                orderInfo[6] = (decimal)(12.2 + i);
                orderInfo[7] = null;
                orderInfo[8] = null;
                orderInfo[9] = null;
                orderInfo[10] = null;
                orderInfo[11] = null;
                orderInfo[12] = null;

                orderInfos.Add(orderInfo);
            }

            return orderInfos;
        }
    }
}


text:
/*
@note:
	读取配置文件, #后面的内容为注释,	支持[section]章节
@eg:
	#define CONFIG_NOTIFY_TEST (*CSingleton<CBitMapConfigFile>::instance())("notify", "test").c_str())
@config file:
	[notify] #notify section
	test=123
*/

#ifndef _FILE_CONFIG_H_
#define _FILE_CONFIG_H_

#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <stdlib.h>
#include <stdio.h>

#define INI_GLOBAL_SECTION      "__global__"

using namespace std;

class CFileConfig
{
public:
	CFileConfig(bool parse_raw_data = false);
	CFileConfig(const char* szFileName, bool parse_raw_data = false);
	virtual ~CFileConfig();

public:
	string& operator[](const char* szName);
	string& operator()(const char* szSection, const char* szName);

	int ParseFile(const char* szConfigFile);
    map<string, map<string, string> > &GetRawData();
    map<string, string> GetMap();

private:
	static int StrimString(char* szLine);
	int ParseFile();

private:
	ifstream m_ConfigFile;
	map<string, string> m_ConfigMap;
    map<string, map<string, string> > m_raw_data;
    bool m_parse_raw_data;
};

#endif


text:
#ifdef JUCE_OSC_H_INCLUDED
 /* When you add this cpp file to your project, you mustn't include it in a file where you've
    already included any other headers - just put it inside a file on its own, possibly with your config
    flags preceding it, but don't include anything else. That also includes avoiding any automatic prefix
    header files that the compiler may be using.
 */
 #error "Incorrect use of JUCE cpp file"
#endif

#include "juce_osc.h"

#include "osc/juce_OSCTypes.cpp"
#include "osc/juce_OSCTimeTag.cpp"
#include "osc/juce_OSCArgument.cpp"
#include "osc/juce_OSCAddress.cpp"
#include "osc/juce_OSCMessage.cpp"
#include "osc/juce_OSCBundle.cpp"
#include "osc/juce_OSCReceiver.cpp"
#include "osc/juce_OSCSender.cpp"


text:
default: all

top_builddir = ..
include $(top_builddir)/src/Makefile.global

.PHONY: all install clean distclean gpcloud pxf mapreduce

#
# targets built via top_builddir/GNUmakefile
#
recurse_targets = ""

ifeq "$(enable_debug_extensions)" "yes"
	recurse_targets = gp_sparse_vector \
               gp_distribution_policy \
               gp_internal_tools \
               gp_debug_numsegments \
               gp_inject_fault \
               gp_replica_check \
               gp_exttable_fdw
else
	recurse_targets = gp_sparse_vector \
               gp_distribution_policy \
               gp_internal_tools \
               gp_exttable_fdw
endif

ifeq "$(with_zstd)" "yes"
	recurse_targets += zstd
endif
ifeq "$(with_quicklz)" "yes"
	recurse_targets += quicklz
endif
$(call recurse,all install clean distclean, $(recurse_targets))

all: gpcloud pxf mapreduce orafce

gpcloud:
	@if [ "$(enable_gpcloud)" = "yes" ]; then \
		$(MAKE) -C gpcloud && \
		$(MAKE) -C gpcloud/bin/gpcheckcloud; \
	fi

pxf:
	@if [ "$(enable_pxf)" = "yes" ]; then \
		$(MAKE) -C pxf_fdw; \
	fi

mapreduce:
	@if [ "$(enable_mapreduce)" = "yes" ]; then \
		$(MAKE) -C gpmapreduce; \
	fi

orafce:
	@if [ "$(enable_orafce)" = "yes" ]; then \
		$(MAKE) -C orafce NO_PGXS=true; \
	fi

install:
	@if [ "$(enable_gpcloud)" = "yes" ]; then \
		$(MAKE) -C gpcloud install && \
		$(MAKE) -C gpcloud/bin/gpcheckcloud install; \
	fi
	@if [ "$(enable_mapreduce)" = "yes" ]; then \
		$(MAKE) -C gpmapreduce install; \
	fi
	@if [ "$(enable_pxf)" = "yes" ]; then \
		$(MAKE) -C pxf_fdw install; \
	fi
	@if [ "$(enable_orafce)" = "yes" ]; then \
		$(MAKE) -C orafce NO_PGXS=true install; \
	fi

clean:
	if [ "$(enable_mapreduce)" = "yes" ]; then $(MAKE) -C gpmapreduce clean; fi
	if [ "$(enable_gpcloud)" = "yes" ]; then $(MAKE) -C gpcloud cleanall; fi
	if [ "$(enable_pxf)" = "yes" ]; then $(MAKE) -C pxf_fdw clean; fi
	if [ "${enable_orafce}" = "yes" ]; then $(MAKE) -C orafce NO_PGXS=true clean; fi

distclean:
	if [ "$(enable_mapreduce)" = "yes" ]; then $(MAKE) -C gpmapreduce distclean; fi
	if [ "$(enable_gpcloud)" = "yes" ]; then $(MAKE) -C gpcloud distclean; fi
	if [ "$(enable_pxf)" = "yes" ]; then $(MAKE) -C pxf_fdw distclean; fi
	if [ "${enable_orafce}" = "yes" ]; then $(MAKE) -C orafce NO_PGXS=true distclean; fi

installcheck:
	$(MAKE) -C gp_internal_tools installcheck
	if [ "$(enable_mapreduce)" = "yes" ]; then \
		$(MAKE) -C gpmapreduce installcheck; \
	fi
	if [ "$(enable_orafce)" = "yes" ]; then $(MAKE) -C orafce installcheck; fi
	if [ "$(enable_pxf)" = "yes" ]; then $(MAKE) -C pxf_fdw installcheck; fi

ifeq "$(with_zstd)" "yes"
	$(MAKE) -C zstd installcheck
endif

ifeq "$(with_quicklz)" "yes"
	$(MAKE) -C quicklz installcheck
endif


text:
                Radiology Exam List

             <?php
                $attributes = array('class'=>'form-horizontal', 'role'=>'form', 'id'=>'insert_radiology');
                echo form_open(base_url().'Radiology/insert_request/', $attributes);
            ?>

                        <?php
                        foreach($radiology_exams as $radiology_exam){
                            echo "
";
                            echo "
";
                            echo "
";
                            echo "
";
                            echo "";
                            echo "
";
                            echo "
<input type='checkbox' name='exams[]' value='".$radiology_exam['exam_id']."'> ".$radiology_exam['exam_name']."
";
                            echo '<address>Price: '.$radiology_exam['exam_price'].'</address>';
                            echo '<address>Description: '.$radiology_exam['exam_description'].'</address>';
                            echo "
";
                            echo "
";
                            echo "
";
                            echo "
";
                            echo "
";
                        }
                     ?>

                                Request Note (Optional)

                                    Note

                                        <textarea class="wysihtml5 form-control" name="note" rows="10"></textarea>

                        <input type="hidden" name="user_id" id="user_id">
                        <input type="hidden" name="patient_id" id="patient_id" value="<?=$this->uri->segment(3)?>">
                        Submit

        <?=form_close()?>


text:
{{collectionVm.records[$index].album}}
{{collectionVm.records[$index].artist}}
{{collectionVm.records[$index].year}}

  <div class="col-md-4" ng-if="$index + 1 < collectionVm.records.length">

{{collectionVm.records[$index + 1].album}}
{{collectionVm.records[$index + 1].artist}}
{{collectionVm.records[$index + 1].year}}

  <div class="col-md-4" ng-if="$index + 2 < collectionVm.records.length">

{{collectionVm.records[$index + 2].album}}
{{collectionVm.records[$index + 2].artist}}
{{collectionVm.records[$index + 2].year}}


text:
package CircuitEvaluator.Components;

/**
 * Created by kareem on 7/29/17.
 */
public class Wire<T> {

    private Gate<T> inputGate;
    private Gate<T> outputGate;
    //object so whatever the inputGate we can chnage it later
    private T value;

    public Gate<T> getInputGate() {
        return inputGate;
    }

    public void setInputGate(Gate<T> inputGate) {
        this.inputGate = inputGate;
    }

    public Gate<T> getOutputGate() {
        return outputGate;
    }

    public void setOutputGate(Gate<T> outputGate) {
        this.outputGate = outputGate;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

}


text:
function fbPrintReport(href)
{
    $.fancybox({
        "width"			: "100%",
        "height"		: "100%",
        "autoScale"    	: false,
        "href"			: href,
        "type"			: "iframe",
        "transitionIn"	: "elastic",
        "transitionOut"	: "elastic",
        "speedIn"       : 600,
        "speedOut"      : 200,
        "overlayShow"   : true,
        "overlayColor"  : "#000",
        "titlePosition" : "over"
    });
}

function deleteSelected(tableObj, descriptionColumnIdx, actionUrl, buttonCancelFunction)
{
    actionSelected(
        tableObj,
        descriptionColumnIdx,
        actionUrl,
        buttonCancelFunction,
        "Tem certeza que deseja excluir estes itens?",
        "Excluir selecionados",
        "Cancelar"
    );
}

function deleteRow(row, tableObj, descriptionColumnIdx, actionUrl)
{
    //Desmarca tudo primeiro
    $(tableObj).find('tr').find(':checkbox:first').attr('checked', false);
    // Marca a checkbox
    $(row).closest("tr").find(":checkbox:first").attr("checked", true);
    deleteSelected(
        tableObj,
        descriptionColumnIdx,
        actionUrl,
        function() {
            $(row).closest("tr").find(":checkbox:first").attr("checked", false);
            retornoConfirm = false;
        }
    );
}

function actionRow(row, tableObj, descriptionColumnIdx, actionUrl, title, buttonOk, buttonCancel)
{
    //Desmarca tudo primeiro
    $(tableObj).find('tr').find(':checkbox:first').attr('checked', false);
    // Marca a checkbox
    $(row).closest("tr").find(":checkbox:first").attr("checked", true);
    actionSelected(
        tableObj,
        descriptionColumnIdx,
        actionUrl,
        function() {
            $(row).closest("tr").find(":checkbox:first").attr("checked", false);
            retornoConfirm = false;
        },
        title,
        buttonOk,
        buttonCancel
    );
}

function actionSelected(tableObj, descriptionColumnIdx, actionUrl, buttonCancelFunction, title, buttonOk, buttonCancel)
{
    var selecteds = $(tableObj).find(":checkbox:checked").not(".checkall").closest("tr");

    if(selecteds.exists())
    {
        var description = "";
        $.each(selecteds, function(idx, item) {
            if(description.length > 0) description += "
";
            description += tableObj.fnGetData(item)[descriptionColumnIdx];
        });

        confirmDialog(
            title,
            description,
            buttonOk,
            buttonCancel,
            function() {
                $(tableObj).wrap("<form method='post' id='confirm_form' action='"+actionUrl+"'>"); $("#confirm_form").submit();
            },
            buttonCancelFunction
        );
    }
}

function confirmDialog(title, html, buttonOk, buttonCancel, buttonOkFunction, buttonCancelFunction, width, height, iconType)
{
    var retornoConfirm = false;

    title = typeof title !== 'undefined' ? title : "Você tem certeza?";
    html = typeof html !== 'undefined' ? html : "Você tem certeza?";
    buttonOk = typeof buttonOk !== 'undefined' ? buttonOk : "Sim";
    buttonCancel = typeof buttonCancel !== 'undefined' ? buttonCancel : "Não";
    buttonOkFunction = typeof buttonOkFunction !== 'undefined' ? buttonOkFunction : function() { retornoConfirm = true; };
    buttonCancelFunction = typeof buttonCancelFunction !== 'undefined' ? buttonCancelFunction : function() { retornoConfirm = false; };
    width = typeof width !== 'undefined' ? width : 400;
    height = typeof height !== 'undefined' ? height : "auto";
    iconType = typeof iconType !== 'undefined' ? iconType : "ui-icon-alert";

    var buttons = { };
    buttons[buttonCancel] = function() {
        buttonCancelFunction();
        $( this ).dialog( "close" );
    };

    buttons[buttonOk] = function() {
        buttonOkFunction();
        $( this ).dialog( "close" );
    };

    //$("#dialog-confirm").attr("title", title);
    $("#dialog-confirm-text").html(html);
    $("#dialog-confirm-icon").addClass(iconType);

    $("#dialog-confirm").dialog({
        title: title,
        autoOpen: true,
        closeText: '',
        resizable: false,
        minWidth: 400,
        minHeight: 150,
        width: width,
        height: height,
        modal: true,
        buttons: buttons
    });

    return retornoConfirm;
}

function dumpElement( jqueryElement ) {

    var element = jqueryElement.get(0);
    var elementDump;

    // dump element attributes
    var attrDump = '';

    var attribute;
    var dumpedAttribute;
    for( var i = 0; i < element.attributes.length; i++) {

        attribute = element.attributes[i];

        // skip every not specified attribute (useful for example in IE)
        if ( attribute.specified == false ) continue;

        // value correction
        if( (attribute.name == "value") && (attribute.value == "") && (element.value != "") )
            attribute.value = element.value;

        // current attribute dump
        dumpedAttribute = attribute.name + '="' + attribute.value + '"';

        // add current attribute to dump, separating attributes with whitespace
        attrDump += ((attrDump != '')?' ':'') + dumpedAttribute;
    }

    if(element.checked) {
        dumpedAttribute = 'checked="checked"';
        attrDump += ((attrDump != '')?' ':'') + dumpedAttribute;
    }

    var tagName = element.tagName.toLowerCase();

    // note: innerHTML does not preserve code formatting
    // note: innerHTML on IE sets the tags names to uppercase (e.g. not W3C Valid)
    if( element.innerHTML == '' ) {

        // self closing tag syntax
        elementDump = '<' + tagName + ((attrDump != '')? ' '+attrDump : '') + '/>';

    } else {

        elementDump = '<' + tagName + ((attrDump != '')? ' '+attrDump : '') + '>' +
            element.innerHTML +
            '</' + tagName + '>';
    }

    return elementDump;
}

// Funcao .exists() para jQuery
jQuery.fn.exists = function(){return this.length>0;}

// Funcao para centralizar uma DIV na tela
jQuery.fn.center = function () {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.height())/ 2 + $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    return this;
}

/*
    Pega o parametro passado via GET de nome NAME
*/
function get_parameter( name )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

/*
Abrir tela de EXCLUIR via FancyBox, utilizando parametro via GET
*/
function fbConfirmDelete(href)
{
	$.fancybox({
        "width"         : 480,
        "height"        : 200,
	    "href"			: href,
        "autoScale"    	: true,
		"type"			: "iframe",
		"transitionIn"	: "elastic",
		"transitionOut"	: "elastic",
        "scrolling"     : "no",
        "modal"         : true,
        "speedIn"       : 600,
        "speedOut"      : 200,
        "overlayShow"   : true,
        "overlayColor"  : "#000"
	});
}

/*
Abrir tela de EXCLUIR via FancyBox, utilizando parametro via POST
*/
function fbConfirmDeletePost(href)
{
    var arr = new Array();
    $("input[name=check]:checked").each(function() {
        arr[arr.length] = $(this).val();
    }) // adiciona tudo no array

    if(arr.length > 0)
    {
        $.ajax({
            type     : "POST",
            cache    : false,
            url      : href,
            data     : {ids : arr},
            success  : function(data2) {
                $.fancybox({
                    "scrolling" : "no",
                    "modal"     : true,
                    "content"   : data2
                });
            }
        });
    } else {
        alert("Selecione ao menos 1 registro!");
    }
}

$(document).ready(function() {
    $("#messages:not(:has(.red))").hide().fadeIn("slow").delay(5000).fadeOut("slow");
});

/*
 Devido ao LAYOUT comprado,
 se for necessário mudar o valor selecionado numa COMBO será necessário utilizar esta função
 */
function atualizaSelect(id, valor)
{
    $("#" + id).selectmenu('value', $("#" + id + " > option[value='"+valor+"']").index());
}

/*
    Devido ao LAYOUT comprado,
    se for necessário CHECKAR uma checkbox via javascript é necessário utilizar esta função.
 */
function atualizaCheckbox(id, checked)
{
    $("#" + id).attr("checked", checked);
    $("#" + id).trigger('updateState');
}

/*
    Devido ao LAYOUT comprado,
    esta função é mais garantida de retornar um valor correto
 */
function isChecked(id)
{
    return $("#" + id).is(":checked");
}

function getValueFromInput(obj, checkedValue, uncheckedValue)
{
    checkedValue = typeof checkedValue !== 'undefined' ? checkedValue : true;
    uncheckedValue = typeof uncheckedValue !== 'undefined' ? uncheckedValue : false;

    var value;

    switch(obj.get(0).type)
    {
        case 'password':
        case 'text':
        case 'textarea':
            value = obj.val();
            break;
        case 'select-multiple':
        case 'select-one':
            value = obj.val()
            break;
        case 'checkbox':
        case 'radio':
            if(obj.get(0).checked)
                value = checkedValue;
            else
                value = uncheckedValue;
    }

    return value;
}

/*
    Limpa os itens de FORMULARIO filhos do ID passado no parâmetro
 */
function limparForm(id) {

    $("#"+id).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'select-multiple':
            case 'select-one':
                $(this).val('');
                $(this).selectmenu('value',0);
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
                $(this).trigger('updateState');
        }
    });

}

function adicionarErroAoElementoDoForm(id, msgErro)
{
    if($("#" + id).parent().find(".errors").exists()) // valida se já existe a UL errors
    {
        $("#" + id).parent().find(".errors").append('
' + msgErro + '
');
    }
    else
    {
        $("#" + id).parent().append('

' + msgErro + '

');
    }
}

function limparErrosDosForms()
{
    $("ul.errors").remove();
}

function isEmptyValue(obj, optionalCheckbox)
{
    optionalCheckbox = typeof optionalCheckbox !== 'undefined' ? optionalCheckbox : false;

    switch(obj.get(0).type) {
        case undefined:
        case 'password':
        case 'text':
        case 'textarea':
        case 'select-multiple':
        case 'select-one':
            if(obj.val() == "")
                return true;
            break;
        case 'checkbox':
        case 'radio':
            if(!obj.get(0).checked && !optionalCheckbox)
                return true;
    }

    return false;
}

function validarObrigatorios(arrayDeIds)
{
    $("ul.errors").remove();

    var houveErro = false;
    for (var i = 0; i < arrayDeIds.length; i++)
    {
        var id = arrayDeIds[i];
        var obj = $("#" + id );

        var exibeErro = false;
        exibeErro = isEmptyValue(obj);

        if(exibeErro)
        {
            adicionarErroAoElementoDoForm(id, "Campo obrigatório.");
            if(!houveErro)
            {
                $.scrollTo('#' + id, 0,  {offset:-50});
                houveErro = true;
            }
        }
    }

    return !houveErro;
}
/*
    Valida se o valor do form é inteiro;
 */
function form_input_is_int(input){
    return !isNaN(input)&&parseInt(input)==input;
}

ValidateAjax = {
    initialize: function(formid, url) {
        this.error = true;
        this.parentId = "";
        this.end_url = url;
        this.form_id = '#'+formid;
        $(this.form_id).submit(function(event) {
            if(ValidateAjax.error)
            {
                var valid = ValidateAjax.doValidate();
                if(!valid) {
                    event.preventDefault();
                }
                return valid;
            } else {
                return true;
            }
        });
    },
    doValidate: function() {
        var url = this.end_url;
        var data = $(this.form_id).serializeArray();
        $.ajax({
            type: "POST",
            url: url,
            async: false,
            data: data,
            success: function(response) {
                ValidateAjax.error = false;

                $(ValidateAjax.form_id).find('.errors').remove();

                $.each(response, function(id, value) {
                    this.parentId = "";
                    ValidateAjax.eachFunction(id, value, "");
                });

                if(ValidateAjax.error)
                    $.scrollTo($(".list.errors:first").parent(), 100, {offset:-50});
            }
        });

        return !ValidateAjax.error;
    },
    getHTML: function(errArray) {
        var o = '
';
        $.each(errArray,function(key,value){
            o += '
'+ value+'
';
        });
        o += '
';

        return o;
    },
    eachFunction: function(id, value, parentId) {
        var finalId = id;
        if(parentId.length > 0) {
            finalId = parentId + '_' + id;
        }

        if($('#' + finalId).exists()) {
            $('#' + finalId).parent().append(ValidateAjax.getHTML(value));
            ValidateAjax.error = true;
        } else {
            if(typeof(value) == "object") {

                if(parentId.length > 0) {
                    parentId = parentId + '_' + id;
                } else {
                    parentId = id;
                }

                //this.parentId += '_' + id;
                $.each(value, function(id, value) {
                    if(typeof(value) == "object") {
                        ValidateAjax.eachFunction(id, value, parentId);
                    }
                });
            }
        }
    }
}

function decoraSelects(selector)
{
    if($(selector + ' select').exists()) {

        $(selector + ' select').not('table select').each(function(idx, select) {
            if ($(select).multiselect("widget").get(0).type !== undefined)
            {
                var multiple = ($(select).attr("multiple") != undefined);
                var required = ($(select).attr("required") != undefined);
                var filter = !$(select).hasClass("select_nofilter");
                var openUp = $(select).hasClass("select_openup");

                var open = {};
                if (openUp) {
                    open = {
                        my: 'left bottom',
                        at: 'left top'
                    };
                }
                var classes = "";
                if (required) {
                    classes = "required";
                }

                if (multiple && filter) {
                    $(select).multiselect({ minWidth: 140, classes: classes, position: open }).multiselectfilter({ classes: classes });
                } else if(multiple) {
                    $(select).multiselect({ minWidth: 140, classes: classes, position: open });
                } else if(filter) {
                    $(select).multiselect({
                        multiple: false,
                        selectedList: 1,
                        minWidth: 140,
                        classes: classes,
                        position: open
                    }).multiselectfilter({ classes: classes });
                } else {
                    $(select).multiselect({
                        multiple: false,
                        selectedList: 1,
                        minWidth: 140,
                        classes: classes,
                        position: open
                    });
                }
            } else {
                $(select).multiselect("refresh");
            }
        });

        /*
        $(selector + ' select[multiple][required]').not('table select').not('.select_nofilter').multiselect({ minWidth: 140, classes:"required" }).multiselectfilter({ classes:"required" });

        $(selector + ' select[multiple]').not('table select').not('.select_nofilter').multiselect({ minWidth: 140 }).multiselectfilter({ });

        $(selector + ' select[required]').not('select[multiple]').not('table select').not('.select_nofilter').multiselect({
            multiple: false,
            selectedList: 1,
            minWidth: 140,
            classes:"required",
            position: open
        }).multiselectfilter({ classes:"required" });

        $(selector + ' select').not('select[multiple]').not('table select').not('.select_nofilter').multiselect({
            multiple: false,
            selectedList: 1,
            minWidth: 140,
            position: open
        }).multiselectfilter({  });

        $(selector + ' select.select_nofilter').not('select[multiple]').not('table select').multiselect({
            multiple: false,
            selectedList: 1,
            minWidth: 140,
            position: open
        });
        */

        /*
        $(selector + ' select').multiselect({
            multiple: false,
            selectedList: 1
        });
        */

    } else if($(selector).exists()) {
        if( ($(selector).get(0).type == "select-one") || ($(selector).get(0).type == "select-multiple")) {
            $(selector).multiselect("refresh");
        }
    }

    /*
    $(function() {

        var selector = '#' + id + ' select';

        if($('#' + id).exists())
            if( ($('#' + id).get(0).type === "select-one")
                || ($('#' + id).get(0).type === "select-multiple") )
                selector = '#' + id;

        if($(selector).exists())
        {
            $(selector).not("select.multi").selectmenu({
                style: 'dropdown',
                transferClasses: true,
                width: null
            });
        }
    });*/
}

function boxHide(obj)
{
    $(obj).parent().next('.content').fadeToggle(600);
    if($(obj).hasClass("hide")) {
        $(obj).removeClass("hide");
        $(obj).addClass("unhide");
    } else {
        $(obj).removeClass("unhide");
        $(obj).addClass("hide");
    }
}

function decoraAButtons(id)
{
    $("#" + id + " a.button").wrapInner("");
    $("#" + id + " a.button, #" + id + " button, #" + id + "  .pager img").hover(

        function() {
            $(this).stop().fadeTo(200, 0.7);
        }, function() {
            $(this).stop().fadeTo(200, 1.0);
        });
}

function decoraChecks(selector)
{
    if($(selector + " > input[type=radio], " + selector + " input[type=checkbox]").exists()) {
        $(selector + " > input[type=radio], " + selector + " input[type=checkbox]").each(function() {
            if ($(this).parents("table").length === 0) {
                $(this).customInput();
            }
        });
    }
}

function getdigits (s) {
    return s.replace (/[^\d]/g, '');
}

function gettingAnHtmlByJson(formId, actionPath, idOfFieldToBeFilled) {

    var form = $('#' + formId).serialize();

    $.ajax
        ({
            type: "POST",
            url: actionPath,
            data: form,
            cache: false,
            success: function(html)
            {
                $("#" + idOfFieldToBeFilled).html(html);
                $("#" + idOfFieldToBeFilled).show();
            }
        });
}

function BuscaTudo(obj, id, url, idCombo, selected, tipo, firsttext, combosToClear, collectionsToClear)
{
    if((collectionsToClear != undefined) && (collectionsToClear != null) && (collectionsToClear.length > 0))
    {
        var newVal = $(obj).val();
        var oldVal = $.data(obj, 'val');

        if(oldVal == undefined) {
            var defaultValue;
            $(obj).find('option').each(function (i, o) {
                if (o.defaultSelected) {
                    defaultValue = o.value;
                    return;
                }
            });
            oldVal = defaultValue;
        }

        if((oldVal != undefined) && (oldVal != null) && (oldVal != "") && (oldVal != newVal)) {
            if (!confirm("Ao trocar este valor você irá perder informações previamente selecionadas de outros campos?")) {
                $(obj).val(oldVal);
                $(obj).multiselect("refresh");
                return;
            }
        }
        $.data(obj, 'val', newVal);        //store new value for next time

        $.each(collectionsToClear, function(index, idCollectionToClear) {
            var fn = window[idCollectionToClear + "_collectionDelAll"]; // pega o clear
            if(typeof fn === 'function') {
                fn();
            }
        });
    }

    if( (combosToClear != undefined) && (combosToClear != null) && (combosToClear.length > 0) )
    {
        $.each(combosToClear, function(index, idComboToClear) {
            var firstTextComboToClear = $('#'+idComboToClear+' option:first').text();

            $('#'+idComboToClear).empty();

            if($('#' + idComboToClear).attr("multiple") == null) //quando não for multiple
                $('#'+idComboToClear).append('<option value="">'+firstTextComboToClear+'</option>');

            $('#'+idComboToClear).multiselect("refresh");
        });
    }

    if((idCombo != undefined) && (idCombo != null))
    {
        $('#'+idCombo).empty();
        if(firsttext != null) {
            $('#'+idCombo).append('<option value="">'+firsttext+'</option>');
        }
        $('#'+idCombo).multiselect("refresh");

        $.getJSON(url + '?id='+id+'&selected='+selected+'&tipo='+tipo, function(data) {

            var item = data[0];

            $.each(data, function(i, item) {
                var selected = "";
                if(item.selected == item.chave) {
                    selected = 'selected="selected"';
                }

                $('#'+idCombo).append('<option value="'+item.chave+'" '+selected+'>'+item.descricao+'</option>');
            });

            $('#'+idCombo).multiselect("refresh");
        }).error(function() { decoraSelects('#'+idCombo); });

    }
}

function isScrolledIntoView(elem)
{
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
        && (elemBottom <= docViewBottom) &&  (elemTop >= docViewTop) );
}

//------------------------------CONFIGURA O AJAX PADRAO---------------------------//
$.ajaxPool = [];
$.ajaxPool.abortAll = function() {
    $(this).each(function(idx, jqXHR) {
        jqXHR.abort();
    });
    $.ajaxPool.length = 0
};
////////////////////////////////////////////////////////////////////////////////////
// Configura o AJAX do jQuery para aparecer a DIV Carregando enquanto está
// processando
////////////////////////////////////////////////////////////////////////////////////
$.ajaxSetup({
    beforeSend : function(jqXHR) {
        $.ajaxPool.push(jqXHR);
        $('#divCarregando').center(); $("#divCarregando").show();
    },
    complete : function(jqXHR) {
        var index = $.inArray(jqXHR, $.ajaxPool);
        if (index > -1) {
            $.ajaxPool.splice(index, 1);
        }

        if($.ajaxPool.length == 0) {
            $("#divCarregando").hide();
        }
    },
    error : function(jqXHR) {
        var index = $.inArray(jqXHR, $.ajaxPool);
        if (index > -1) {
            $.ajaxPool.splice(index, 1);
        }

        if($.ajaxPool.length == 0) {
            $("#divCarregando").hide();
        }
    }
});


text:
   Copyright (C) 2016 Netronome Systems, Inc. All rights reserved.

   Description : Generates a bursty traffic pattern using the rules specified
                 below.

   -- All packets generated will share the same source IP, source MAC and size
      profile (fixed or IMIX).
   -- The destination IP and MAC are selected sequentially in the following
      pattern:

   a) The stream number is set to 0.
   b) The stream base is calculated by multiplying the stream number with the
      MAC and IP stride.
   c) The burst number is set to 0.
   d) The flow number is set to 0.
   e) A packet is generated with the destination IP and MAC set to "stream
      base" + "flow number".
   f) The flow number is increased, if it is less than the number of flows per
      stream, loop back to (e), else continue to (g).
   g) The burst number is increased, if it is less than the number of bursts
      per stream, loop back to (d), else continue to (h).
   h) The stream base is incremented, if it is less than the number of streams,
      loop back to (a).

   -- This results in a set of streams that are separated by the MAC and IP
      stride and repeat in bursts. The destination IP and MAC increase in
      lock-step.
   -- Packet size can be constant, or selected from a 7:4:1 IMIX profile with
      packet sizes 1514, 570, and 64, respectively.
--]]

local libmoon  = require "libmoon"
local mg       = require "moongen"
local moongen  = require "dpdk"
local dpdkc    = require "dpdkc"
local memory   = require "memory"
local device   = require "device"
local ffi      = require "ffi"
local log      = require "log"
local bit      = require "bit"

local lshift    , rshift    , band    , bswap     , bor    , ror    =
      bit.lshift, bit.rshift, bit.band, bit.bswap , bit.bor, bit.ror

-------------------------------------------------------------------------------
-- Table for RX stats:
--
--  bytes         : Total number of bytes received.
--  prevBytes     : Number of bytes received at the previous iteration.
--  packets       : Total number of packets received.
--  prevPackets   : Number of packets received at the previous iteration.
--  dropped       : Number of received packets which have been dropped.
--  tstampPackets : Number of received packets with a timestamp.
--  meanLatency   : Mean latency of received packets with timestamps.
--  elapsedTime   : Time elapsed since the start of the run.
--  deltaTime     : Time since the last iteration.
-------------------------------------------------------------------------------
local statsRx = {
  bytes         = 0,
  prevBytes     = 0,
  packets       = 0,
  prevPackets   = 0,
  dropped       = 0,
  tstampPackets = 0,
  meanLatency   = 0,
  elapsedTime   = 0,
  deltaTime     = 0
}

-------------------------------------------------------------------------------
-- Table for TX stats:
--
--  bytes         : Total number of bytes sent.
--  prevBytes     : Number of bytes sent at the previous iteration.
--  packets       : Total number of packets sent.
--  prevPackets   : Number of packets sent at the previous iteration.
--  bursts        : Number of bursts sent.
--  short         : Number of generated by not sent packets.
--  elapsedTime   : Time elapsed since the start of the run.
--  deltaTime     : Time since the last iteration.
--------------------------------------------------------------------------------
local statsTx = {
  bytes         = 0,
  prevBytes     = 0,
  packets       = 0,
  prevPackets   = 0,
  bursts        = 0,
  short         = 0,
  elapsedTime   = 0,
  deltaTime     = 0
}

-------------------------------------------------------------------------------
-- Table for command line configuration parameters, the following are options:
--
--  rxSlavePorts    : Ports to use for the RX slaves.
--  txSlavePorts    : Ports to use for the TX slaves.
--  rxDescs         : Number of RX descriptors.
--  txDescs         : Number of TX descriptors.
--  numberOfStreams : Number of streams to create.
--  flowsPerStream  : The number of flows in each of the streams.
--  burstsPerStream : The number of bursts to send for each stream.
--  totalFlows      : The totla number of flows to generate.
--  paramDisplay    : The number of milliseconds to display the parameters for.
--  statsDisplay    : The amount of time between each device stats display.
--  txDelta         : Amount of time to pass before sending a burst.
--  packetSize      : The size of each packet, in bytes.
--  mustImix        : If IMIXing must be used to determine packet sizes.
--  iterations      : Number of iterations to run for.
--  timeout         : Number of seconds to run for.
--  fileprefix      : The prefix of the file to write to.
--  writemode       : Write mode: 0-off, 1-print, 1-globalfile, 2-percorefile
--  rest            : Parameters used to generate the traffic pattern.
-------------------------------------------------------------------------------
local clParams = {
    rxSlavePorts    = {}                      ,
    txSlavePorts    = {}                      ,
    rxDescs         = 1024                    ,
    txDescs         = 1024                    ,
    numberOfStreams = 1                       ,
    flowsPerStream  = 2047                    ,
    burstsPerStream = 1                       ,
    totalFlows      = 0                       ,
    paramDisplay    = 3000                    ,
    statsDisplay    = 1000                    ,
    txDelta         = 0                       ,
    packetSize      = 64                      ,
    mustImix        = false                   ,
    iterations      = 0                       ,
    timeout         = 0                       ,
    fileprefix      = ""                      ,
    writemode       = 1                       ,
    srcMacBase      = 0x021100000000          ,
    dstMacBase      = 0x022200000000          ,
    srcIpBase       = 3232238337              ,
    dstIpBase       = 3232241153              ,
    srcPortBase     = 50110                   ,
    dstPortBase     = 50220                   ,
    srcPortVary     = 1                       ,
    dstPortVary     = 0                       ,
    srcIpVary       = 0                       ,
    dstIpVary       = 1                       ,
    srcMacVary      = 0                       ,
    dstMacVary      = 1                       ,
    srcPortStride   = 0                       ,
    dstPortStride   = 0                       ,
    srcIpStride     = 0                       ,
    dstIpStride     = 0                       ,
    srcMacStride    = 0                       ,
    dstMacStride    = 0
}

-------------------------------------------------------------------------------
-- Defines parameters for timing functionality.
--
--  prevtsc  : The previous clock count.
--  currtsc  : The current clock count.
--  difftsc  : The difference between the clock counts.
-------------------------------------------------------------------------------
local timeParams = {
    prevtsc  = 0 ,
    currtsc  = 0 ,
    difftsc  = 0
}

-------------------------------------------------------------------------------
-- Table for constants:
--
--  maxSlavePort      : Maximum port number a slave may use.
--  maxSlaves         : Maximum number of total slaves (limited by cores).
--  maxFlowsPerStream : Maximum number of flows per stream.
--  maxPacketSize     : Maximum size of a packet.
--  rxBurstSize       : The size of each of the RX burst.
--  txBurstSize       : The size of each of the TX bursts.
--  receiveTimeout    : Time to try receive for (ms)
--  ethHeaderLength   : Number of bytes in ethernet header.
--  ipHeaderLength    : Number of bytes in ip header.
--  udpHeaderLength   : Number of bytes in udp header
--  ethTypeIpv4       : Type code for ethernet ipv4.
--  printTimePassPcnt : % of print time which must pass since last print.
--  defaultPayload    : Default payload to use for packets.
-------------------------------------------------------------------------------
constants = {
    maxSlavePort      = 32    ,
    maxSlaves         = 32    ,
    maxFlowsPerStream = 2047  ,
    maxPacketSize     = 64    ,
    maxPacketSize     = 1522  ,
    rxBurstSize       = 128   ,
    txBurstSize       = 32    ,
    receiveTimeout    = 100   ,
    ethHeaderLength   = 14    ,
    ipHeaderLength    = 20    ,
    udpHeaderLength   = 8     ,
    ethTypeIpv4       = 0x0800,
    printTimePassPcnt = 0.8   ,
    defaultPayload    = "\x01\x02 MoonGen Payload"
}

-- Start time of the appliction. This is used to sync
-- the printing and logging accross the slaves.
local globalStartTime = mg.getTime()

-------------------------------------------------------------------------------
-- Master function to do the slave setup and invoke the slave instances.
-------------------------------------------------------------------------------
function master(...)
    local continue = parseArgs(clParams, ...)
    if continue == false then
        os.exit()
    end

    local totalSlaves = #clParams.rxSlavePorts + #clParams.txSlavePorts
    if totalSlaves == 0 then
        print("No slaves requested. Packetgen will now exit.")
        os.exit()
    end
    if totalSlaves > constants.maxSlaves then
        print(string.format("Too many slaves requested:\n %u requested, " ..
            "%u maximum.\n Packetgen will now exit.",
            totalSlaves, constants.maxSlaves)
        )
    end

    -- Create a list of ports, default to the tx ports:
    local portList = deepCopy(clParams.txSlavePorts)

    -- Check which of the rx ports were already added as tx ports,
    -- and add any of the rx ports which are not already in the list:
    for i, rxPortId in ipairs(clParams.rxSlavePorts) do
        local canAdd = true
        for _, portId in ipairs(portList) do
            if rxPortId == portId then
                canAdd = false
                break
            end
        end
        if canAdd then
            portList[#portList + 1] = rxPortId
        end
    end

    -- Table of MoonGen devices:
    local devices = {}
    -- Configure the devices:
    for _, port in ipairs(portList) do
        local deviceIdx = #devices + 1
        devices[deviceIdx] = device.config{
            port     = port            ,
            rxQueues = 1               , -- Only 1 supported for now.
            txQueues = 1               , -- Only 1 supported for now.
            rxDescs  = clParams.rxDescs,
            txDescs  = clParams.txDescs
        }
    end

    -- If writing globally, write the header for the global file:
    if clParams.writemode == 2 then
        writeStatsHeader(clParams.fileprefix .. ".txt")
    end

    -- Display the parameters:
    clParams:print()
    mg.sleepMillis(clParams.paramDisplay)

    local slaveId = 0
    -- Launch the slaves:
    for i, portId in ipairs(portList) do
        -- Check if the port must be used as a TX slave:
        for _, txPortId in ipairs(clParams.txSlavePorts) do
            if txPortId == portId then
                libmoon.startTask("txSlave", devices[i], portId, slaveId, clParams)
                slaveId = slaveId + 1
                break
            end
        end
        -- Check if the port must be used as a RX slave:
        for _, rxPortId in ipairs(clParams.rxSlavePorts) do
            if rxPortId == portId then
                libmoon.startTask("rxSlave", devices[i], portId, slaveId, clParams)
                slaveId = slaveId + 1
                break
            end
        end
    end

    mg.waitForTasks()
end

---- RX Functionality ---------------------------------------------------------

-------------------------------------------------------------------------------
-- Slave function to perform RX.
-- device   : The MoonGen device to sent the packets from.
-- portId   : The port the device is configured to use.
-- slaveId  : The identifier (index) of the slave.
-- clParams : The command line parameters.
-------------------------------------------------------------------------------
function rxSlave(device, portId, slaveId, clParams)
  print(string.format("Launching RX slave: %u, Core: %u, Port: %u", slaveId, mg:getCore(), portId))

  -- Variables for receiving:
  local rxPackets = 0
  local rxQueue   = device:getRxQueue(0)
  local rxBurst   = memory.bufArray(constants.rxBurstSize)
  local latency   = 0
  local latDelta  = 0

  -- Variables for logging, printing, stopping:
  local state         = "canrun"
  local stats         = deepCopy(statsRx)
  local startTime     = mg.getTime()
  local lastPrintTime = 0
  local iteration     = 0

  -- Create the filename of the file to write to.
  if clParams.writemode == 2 then
      outFile = clParams.fileprefix .. ".txt"
  elseif clParams.writemode == 3 then
      -- Prer core mode - create file and write header:
      outFile = clParams.fileprefix                 ..
                "-c" .. tostring(mg:getCore()) ..
                "-p" .. tostring(portId) .. ".txt";
      writeStatsHeader(outFile)
  end

  while state == "canrun" and mg.running() do
      rxPackets     = rxQueue:tryRecv(rxBurst, constants.receiveTimeout)
      stats.packets = stats.packets + rxPackets
      stats.dropped = stats.dropped + rxPackets

      for i = 1, rxPackets do
          stats.bytes = stats.bytes + rxBurst[i].pkt_len
          latency     = calculateLatency(rxBurst[i])

          if latency ~= 0 then
              stats.tstampPackets = stats.tstampPackets + 1
              latDelta            = latency - stats.meanLatency
              stats.meanLatency   = stats.meanLatency +
                                    (latDelta / stats.tstampPackets)
          end
      end
      simpleDrop(rxBurst)

      -- Update termination params:
      iteration         = iteration + 1
      stats.deltaTime   = (mg.getTime() - startTime) - stats.elapsedTime
      stats.elapsedTime = mg.getTime() - startTime

      -- If printing or logging must be done:
      if clParams.writemode > 0 then
          -- To make sure that enough time has passed since logging
          -- and printing was done for the core.
          sufficientTimeSincePrint =
            (stats.elapsedTime - lastPrintTime) >=
            (constants.printTimePassPcnt * clParams.statsDisplay / 1000)

          -- Printing and logging:
          if canPrint(slaveId, clParams) and sufficientTimeSincePrint then
              lastPrintTime = mg.getTime() - startTime
              if clParams.writemode == 1 then
                  os.execute("clear")
                  stats:print(portId)
              else
                  stats:write(outFile, portId)
              end
          end
      end

      -- Update stats:
      stats.prevPackets = stats.packets
      stats.prevBytes   = stats.bytes

       -- Check for exit conditions:
      if clParams.iterations ~= 0 and iteration >= clParams.iterations then
          state = "stop"
      elseif clParams.timeout ~= 0                 and
             stats.elapsedTime >= clParams.timeout then
          state = "stop"
      end
  end

  if clParams.writemode == 0 then
      stats:print(portId)
  end
end

-------------------------------------------------------------------------------
-- Drops the received packets
-- rxPackets  : The received packets to drop.
-------------------------------------------------------------------------------
function simpleDrop(rxPackets)
    rxPackets:freeAll()
end

-------------------------------------------------------------------------------
-- Gets the timestamp from a packet and then calculates the latency
-- buf  : The buffer to get the timestamp from and then calculate the latency.
-------------------------------------------------------------------------------
function calculateLatency(buf)
    local pkt       = buf:getUdpPacket()
    local cycles    = tonumber(mg:getCycles())
    local timestamp = tonumber(pkt.payload.uint64[0])
    local latency   = 0

    if timestamp ~= 0 then
        latency = tonumber(cycles - timestamp)
                / tonumber(mg:getCyclesFrequency())
    end
    return latency
end

-------------------------------------------------------------------------------
-- Prints RX stats
-- portId : The id of the port to print the stats for
-------------------------------------------------------------------------------
function statsRx:print(portId)
  local statsString = string.format(
    "\n+------ RX Statistics for core %3u, port %3u -------------------+"   ..
    "\n| Packets received           : %32u |"                               ..
    "\n| Packet receive rate        : %32.2f |"                             ..
    "\n| Avg packet receive rate    : %32.2f |"                             ..
    "\n| Bytes received             : %32u |"                               ..
    "\n| Byte receive rate          : %32.2f |"                             ..
    "\n| Avg byte receive rate      : %32.2f |"                             ..
    "\n| Packets dropped on receive : %32u |"                               ..
    "\n| RX mean latency            : %32.10f |"                            ..
    "\n+---------------------------------------------------------------+"   ,
    mg:getCore()                                   ,
    portId                                              ,
    self.packets                                        ,
    (self.packets - self.prevPackets) / self.deltaTime  ,
    self.packets / self.elapsedTime                     ,
    self.bytes                                          ,
    (self.bytes - self.prevBytes) / self.deltaTime      ,
    self.bytes / self.elapsedTime                       ,
    self.dropped                                        ,
    self.meanLatency
  )
  print(statsString)
end

-------------------------------------------------------------------------------
-- Writes RX a stats entry to a file. The row entry contains the following
-- fields:

--  Time, tx packets, tx packet rate, avg tx packet rate, rx packets,
--  rx packet rate, avg rx packet rate, core, port
--
-- filename : Name of the file to write to.
-- portId   : The port the stats are being written for.
-------------------------------------------------------------------------------
function statsRx:write(filename, portId)
    file = io.open(filename, "a")

    local time = mg.getTime() - globalStartTime

    local statsString = string.format(
        "%6.3f,%13u,%18.2f,%18.2f,%13u,%18.2f,%18.2f,%5u,%5u\n",
        time                                                ,
        0                                                   ,
        0                                                   ,
        0                                                   ,
        self.packets                                        ,
        (self.packets - self.prevPackets) / self.deltaTime  ,
        self.packets / self.elapsedTime                     ,
        mg:getCore()                                   ,
        portId
    )

    file:write(statsString)
    file:close()
end

---- TX Functionality ---------------------------------------------------------

-------------------------------------------------------------------------------
-- Slave function to perform TX.
-- device   : The MoonGen device to sent the packets from.
-- portId   : The port the device is configures to use.
-- slaveId  : The identifier (index) of the slave.
-- clParams : The command line parameters
-------------------------------------------------------------------------------
function txSlave(device, portId, slaveId, clParams)
  print(string.format("Launching TX slave: %u, Core: %u, Port: %u",
      slaveId, mg:getCore(), portId))

  local state       = "canrun"
  local stats       = deepCopy(statsTx)
  local txQueue     = device:getTxQueue(0)
  local timerParams = deepCopy(timeParams)
  local outFile     = ""

  -- Create the filename of the file to write to.
  if clParams.writemode == 2 then
      outFile = clParams.fileprefix .. ".txt"
  elseif clParams.writemode == 3 then
      -- Per core mode: write header
      outFile = clParams.fileprefix                 ..
                "-c" .. tostring(mg:getCore()) ..
                "-p" .. tostring(portId) .. ".txt";
      writeStatsHeader(outFile)
  end

  -- Allocate memory for all the streams. Each of the streams can have
  -- a maximum of 2047 flows due to mbuf allocation limits.
  local streams = {}
  for i = 1, clParams.numberOfStreams do
    streams[i] = {
        mempool  = {},
        bufArray = nil
    }

    -- Create a mempool for the stream:
    streams[i].mempool = memory:createMemPool(
        function(buf)
            buf:getUdpPacket():fill{
                pktLength = clParams.packetSize - 4,
                ethLength = constants.ethHeaderLength
            }
        end
    )
    -- Allocate buffers from the mempool:
    streams[i].bufArray = streams[i].mempool:bufArray(clParams.flowsPerStream)
  end

  local counter = 0
  -- Modify all the packets so that their data follows the traffic pattern:
  for streamid, stream in ipairs(streams) do
      -- Subtraction of 4 is for the FCS
      local maxPacketSize = clParams.packetSize - 4
      if clParams.mustImix then
          maxPacketSize = constants.maxPacketSize - 4
      end

      -- Allocate buffers using the max possible packet size. If using Imix
      -- then each of the sent packets will vary in size, despite the constant
      -- size allocation done here.
      stream.bufArray:alloc(maxPacketSize)

      -- Modify the packets:
      for flowid, buf in ipairs(stream.bufArray) do
          if clParams.mustImix then
              local pktSize = imixSize() - 4
              buf.pkt_len   = pktSize
              buf.data_len  = pktSize
          end

          buildTxFrame(device.id, buf, streamid - 1, flowid - 1, clParams)
      end

      -- Offload the checksum to the NIC:
      stream.bufArray:offloadUdpChecksums()
  end

  -- Do sending:
  local iteration     = 0
  local lastPrintTime = 0
  local startTime     = mg.getTime()

  while state == "canrun" and mg.running() do
      for _, stream in ipairs(streams) do
          timerParams.currtsc = tonumber(mg:getCycles())
          timerParams.difftsc = timerParams.currtsc - timerParams.prevtsc

          sendBursts(txQueue, stats, timerParams, clParams, stream)
      end

      -- Update the iteration and timeout params:
      iteration         = iteration + 1
      stats.deltaTime   = (mg.getTime() - startTime) - stats.elapsedTime
      stats.elapsedTime = mg.getTime() - startTime

      -- Printing and writing:
      if clParams.writemode > 0 then
          -- To make sure that enough time has passed since logging
          -- and printing was done for the core.
          sufficientTimeSincePrint =
            ((stats.elapsedTime - lastPrintTime) >=
             (constants.printTimePassPcnt * clParams.statsDisplay / 1000))

          -- Printing and logging:
          if canPrint(slaveId, clParams) and sufficientTimeSincePrint then
              lastPrintTime = mg.getTime() - startTime
              if clParams.writemode == 1 then
                  os.execute("clear")
                  stats:print(portId)
              else
                  stats:write(outFile, portId)
              end
          end
      end

      -- Update stats:
      stats.prevPackets = stats.packets
      stats.prevBytes   = stats.bytes

      -- Check for exit conditions:
      if clParams.iterations ~= 0 and iteration >= clParams.iterations then
          state = "stop"
      elseif clParams.timeout ~= 0                 and
             stats.elapsedTime >= clParams.timeout then
          state = "stop"
      end
  end

  if clParams.writemode == 0 then
      stats:print(portId)
  end
end

-------------------------------------------------------------------------------
-- Builds a frame for TX.
-- portId     : The port of the device to build the frame for.
-- buf        : The buffer for the packet holding the frame.
-- streamid   : The id of the stream for the frame.
-- flowid     : The id of the flow for the frame.
-- clParams   : The command line parameters.
-------------------------------------------------------------------------------
function buildTxFrame(portId, buf, streamid, flowid, clParams)
    local srcMac  = clParams.srcMacBase + (streamid * clParams.srcMacStride)
                  + (flowid * clParams.srcMacVary)
    local dstMac  = clParams.dstMacBase + (streamid * clParams.dstMacStride)
                  + (flowid * clParams.dstMacVary)
    local srcIp   = clParams.srcIpBase + (streamid * clParams.srcIpStride)
                  + (flowid * clParams.srcIpVary)
    local dstIp   = clParams.dstIpBase + (streamid * clParams.dstIpStride)
                  + (flowid * clParams.dstIpVary)
    local srcPort = band(clParams.srcPortBase
                            + (streamid * clParams.srcPortStride)
                            + (flowid * clParams.srcPortVary),
                         0xffff)
    local dstPort = band(clParams.dstPortBase
                            + (streamid * clParams.dstPortStride)
                            + (flowid * clParams.dstPortVary),
                         0xffff)

    local srcMacFlipped = rshift(bswap(srcMac + 0ULL), 16)
    local dstmacFlipped = rshift(bswap(dstMac + 0ULL), 16)

    local frameSize = buf.pkt_len
    local ethLength = constants.ethHeaderLength
    local ipLength  = frameSize - ethLength
    local udpLength = ipLength  - constants.ipHeaderLength

    local pkt = buf:getUdpPacket()

    -- ETH mod:
    pkt.eth:setType(constants.ethTypeIpv4)
    pkt.eth:setSrc(srcMacFlipped)
    pkt.eth:setDst(dstmacFlipped)

    -- IP mod:
    pkt.ip4:setLength(ipLength)
    pkt.ip4:setHeaderLength(5)  -- Number of 32 bit words : use min value.
    pkt.ip4:setProtocol(17)
    pkt.ip4:setTTL(64)
    pkt.ip4:setSrc(srcIp)
    pkt.ip4:setDst(dstIp)
    pkt.ip4:setVersion(4)

    -- UDP mod:
    pkt.udp:setLength(udpLength)
    pkt.udp:setSrcPort(srcPort)
    pkt.udp:setDstPort(dstPort)

    -- Start of payload looks as follows:
    --
    -- |  Byte 0  |  Byte 4  |  Byte 8  |  Byte 12  |
    -- ----------------------------------------------
    -- |      timestamp      |  flowid  |  streamid |
    -- ----------------------------------------------
    --
    -- The timestamp is alredy set, so set flow and stream id.
    pkt.payload.uint32[2] = flowid
    pkt.payload.uint32[3] = streamid

    -- Fill the rest of the payload
    local payLength   = udpLength - constants.udpHeaderLength
    local i           = 16             -- tstamp, flowid, streamid = 16 bytes
    local offset      = i              -- Start offset into payload bytes

    -- Copy the default payload into the packet.
    while i < payLength do
        -- NOTE: (i - offset) is to remove the 16 byte initial offset.
        pkt.payload.uint8[i] =
            string.byte(constants.defaultPayload, i - offset + 1) or 0
        i = i + 1
    end
end

-------------------------------------------------------------------------------
-- Generates and sends a burst if enough time has passed to achieve the desired
-- packet rate, otherwise nothing happens.
-- txQueue     : The queue to send the burst on.
-- stats       : The stats to update.
-- timerParams : The parameters for the burst to send.
-- clParams    : The command line parameters.
-- stream      : The stream to send.
-------------------------------------------------------------------------------
function sendBursts(txQueue, stats , timerParams, clParams, stream)
    -- If enough time has passed to achieve the requested rate.
    if timerParams.difftsc > clParams.txDelta then
        local nbtx = 0
        for i = 1, clParams.burstsPerStream do
            stats.bursts = stats.bursts + 1
            for _, buf in ipairs(stream.bufArray) do
                updateTimestamp(buf)
            end
            nbtx = nbtx + txQueue:send(stream.bufArray)
        end

        stats.packets = stats.packets + nbtx

        for _, buf in ipairs(stream.bufArray) do
            -- Add four for the FCS.
            stats.bytes = 4 + stats.bytes +
                          (clParams.burstsPerStream * buf.pkt_len)
        end

        if nbtx < constants.txBurstSize then
            stats.short = stats.short + 1
        end

        timerParams.prevtsc = timerParams.currtsc
    end
end

-------------------------------------------------------------------------------
-- Updates the timestamp for a packet. The first 8 bytes of the packet payload
-- are used for the timestamp.
-- buf  : The buffer to update the timestamp of.
-------------------------------------------------------------------------------
function updateTimestamp(buf)
    local pkt             = buf:getUdpPacket()
    local cycles          = mg:getCycles()
    pkt.payload.uint64[0] = cycles
end

-------------------------------------------------------------------------------
-- Prints TX stats
-- portId : The id of the port to print the stats for
-------------------------------------------------------------------------------
function statsTx:print(portId)
  local statsString = string.format(
    "\n+------ TX Statistics for core %3u, port %3u -------------------+"   ..
    "\n| Packets sent               : %32u |"                               ..
    "\n| Packet send rate           : %32.2f |"                             ..
    "\n| Avg packet send rate       : %32.2f |"                             ..
    "\n| Bytes sent                 : %32u |"                               ..
    "\n| Byte send rate             : %32.2f |"                             ..
    "\n| Avg byte send rate         : %32.2f |"                             ..
    "\n| Packets short              : %32u |"                               ..
    "\n| Bursts                     : %32u |"                               ..
    "\n+---------------------------------------------------------------+"   ,
    mg:getCore()                                   ,
    portId                                              ,
    self.packets                                        ,
    (self.packets - self.prevPackets) / self.deltaTime  ,
    self.packets / self.elapsedTime                     ,
    self.bytes                                          ,
    (self.bytes - self.prevBytes) / self.deltaTime      ,
    self.bytes / self.elapsedTime                       ,
    self.short                                          ,
    self.bursts
  )
  print(statsString)
end

-------------------------------------------------------------------------------
-- Writes TX a stats entry to a file. The row entry contains the following
-- fields:

--  Time, tx packets, tx packet rate, avg tx packet rate, rx packets,
--  rx packet rate, avg rx packet rate, core, port
--
-- filename : Name of the file to write to.
-- portId   : The port the stats are being written for.
-------------------------------------------------------------------------------
function statsTx:write(filename, portId)
    file = io.open(filename, "a")

    local time = mg.getTime() - globalStartTime

    local statsString = string.format(
        "%6.3f,%13u,%18.2f,%18.2f,%13u,%18.2f,%18.2f,%5u,%5u\n",
        time,
        self.packets                                        ,
        (self.packets - self.prevPackets) / self.deltaTime  ,
        self.packets / self.elapsedTime                     ,
        0                                                   ,
        0                                                   ,
        0                                                   ,
        mg:getCore()                                   ,
        portId
    )

    file:write(statsString)
    file:close()
end

---- Utility Functions --------------------------------------------------------

-------------------------------------------------------------------------------
-- Write the header for a stats file.
-- filename : The name of the file to write the headerto.
-------------------------------------------------------------------------------
function writeStatsHeader(filename)
    file = io.open(filename, "a")

    local headerString = string.format(
        "%6s %13s %18s %18s %13s %18s %18s %5s %5s\n",
        "Time",
        "TX Packets",
        "TX Packet Rate"    ,
        "Avg TX Packet Rate",
        "RX Packets"        ,
        "RX Packet Rate"    ,
        "Avg RX Packet Rate",
        "Core"              ,
        "Port"
    )

    file:write(headerString)
    file:close()
end

-------------------------------------------------------------------------------
-- Determines if a core is allowed to print it's stats.
-- slaveId  : Identifier for the slave requesting to print.
-- clParams : The command line parameters
-------------------------------------------------------------------------------
function canPrint(slaveId, clParams)
    local elapsedTime = mg.getTime() - globalStartTime
    local timePerCore = clParams.statsDisplay /
                        (#clParams.rxSlavePorts + #clParams.txSlavePorts)

    local timeMod = math.floor(
        math.fmod(elapsedTime * 1000, clParams.statsDisplay))

    return timeMod == math.floor(timePerCore * slaveId)
end

-------------------------------------------------------------------------------
-- Prints the command line arguments.
-------------------------------------------------------------------------------
function clParams:print()
    local paramString = string.format(
      "\n+-------- Parameters ---------------------------------+"       ..
      "\n| RX Slaves          : %30u |"                                 ..
      "\n| TX Slaves          : %30u |"                                 ..
      "\n| RX Descs           : %30u |"                                 ..
      "\n| TX Descs           : %30u |"                                 ..
      "\n| Param display (s)  : %30.3f |"                               ..
      "\n| Stat display (s)   : %30.3f |"                               ..
      "\n| Number of streams  : %30u |"                                 ..
      "\n| Bursts per stream  : %30u |"                                 ..
      "\n| Flows per stream   : %30u |"                                 ..
      "\n| Total flows        : %30u |"                                 ..
      "\n| Iterations         : %30u |"                                 ..
      "\n| Timeout            : %30u |"                                 ..
      "\n| Packet size        : %30u |"                                 ..
      "\n| Using Imix size    : %30s |"                                 ..
      "\n| File prefix        : %30s |"                                 ..
      "\n| Write mode         : %30u |"                                 ..
      "\n| ETH src base       : %30s |"                                 ..
      "\n| ETH dst base       : %30s |"                                 ..
      "\n| ETH src vary       : %30s |"                                 ..
      "\n| ETH dst vary       : %30s |"                                 ..
      "\n| ETH src stride     : %30s |"                                 ..
      "\n| ETH dst stride     : %30s |"                                 ..
      "\n| IP src base        : %30s |"                                 ..
      "\n| IP dst base        : %30s |"                                 ..
      "\n| IP src vary        : %30s |"                                 ..
      "\n| IP dst vary        : %30s |"                                 ..
      "\n| IP src stride      : %30s |"                                 ..
      "\n| IP dst stride      : %30s |"                                 ..
      "\n| PORT src base      : %30u |"                                 ..
      "\n| PORT dst base      : %30u |"                                 ..
      "\n| PORT src vary      : %30u |"                                 ..
      "\n| PORT dst vary      : %30u |"                                 ..
      "\n| PORT src stride    : %30u |"                                 ..
      "\n| PORT dst stride    : %30u |"                                 ..
      "\n+-----------------------------------------------------+\n"     ,
      #self.rxSlavePorts                       ,
      #self.txSlavePorts                       ,
      self.rxDescs                             ,
      self.txDescs                             ,
      self.paramDisplay / 1000                 ,
      self.statsDisplay / 1000                 ,
      self.numberOfStreams                     ,
      self.burstsPerStream                     ,
      self.flowsPerStream                      ,
      self.totalFlows                          ,
      self.iterations                          ,
      self.timeout                             ,
      self.packetSize                          ,
      tostring(self.mustImix)                  ,
      self.fileprefix                          ,
      self.writemode                           ,
      string.format("%012x", self.srcMacBase)  ,
      string.format("%012x", self.dstMacBase)  ,
      string.format("%012x", self.srcMacVary)  ,
      string.format("%012x", self.dstMacVary  ),
      string.format("%012x", self.srcMacStride),
      string.format("%012x", self.dstMacStride),
      formatAsIp(self.srcIpBase)               ,
      formatAsIp(self.dstIpBase)               ,
      formatAsIp(self.srcIpVary)               ,
      formatAsIp(self.dstIpVary)               ,
      formatAsIp(self.srcIpStride)             ,
      formatAsIp(self.dstIpStride)             ,
      self.srcPortBase                         ,
      self.dstPortBase                         ,
      self.srcPortVary                         ,
      self.dstPortVary                         ,
      self.srcPortStride                       ,
      self.dstPortStride
    )

    print(paramString)
end

---- Argument Parsing ---------------------------------------------------------

-------------------------------------------------------------------------------
-- Converts a MAC address from its string representation to a numeric one, in
-- network byte order.
-- address  : The address to convert.
-------------------------------------------------------------------------------
function convertMacAddress(address)
	  local bytes = {string.match(address,
                    '(%x+)[-:](%x+)[-:](%x+)[-:](%x+)[-:](%x+)[-:](%x+)')}

    local convertedAddress = 0
    for i = 1, 6 do
        convertedAddress = convertedAddress +
                           tonumber(bytes[#bytes + 1 - i], 16) * 256 ^ (i - 1)
    end
    return convertedAddress
end

-------------------------------------------------------------------------------
-- Formats a 32 bit value as an IP address.
-- ip   : The numeric representation of the IP address to format.
-------------------------------------------------------------------------------
function formatAsIp(ip)
    return string.format("%d.%d.%d.%d",
      band(rshift(ip, 24), 0xff), band(rshift(ip, 16), 0xff),
      band(rshift(ip, 8 ), 0xff), band(ip, 0xff))
end

-------------------------------------------------------------------------------
-- Parses the command line arguments.
-- params : The a table of parameters to modify based on the command line
--          parameters
-- ...    : A table of command line parameters to parse.
-------------------------------------------------------------------------------
function parseArgs(params, ...)
    local command  = true
    local args     = {...}

    for i,v in ipairs(args) do
        if type(v) == "string" then
            if v == "--tx-slave" or v == "-tx" then
                local slavePort = tonumber(args[i + 1])
                if slavePort < 0 or slavePort > constants.maxSlavePort then
                    print("Invalid TX slave port: ", slavePort)
                    printUsage()
                    return false
                end
                local canAdd = true
                for _, v in ipairs(params.txSlavePorts) do
                    if v == slavePort then
                        canAdd = false
                        print("Ignoring second specification of TX slave " ..
                              "port: ", slavePort)
                    end
                end
                if canAdd then
                    params.txSlavePorts[#params.txSlavePorts + 1] =  slavePort
                end
            elseif v == "--rx-slave" or v == "-rx" then
                local slavePort = tonumber(args[i + 1])
                if slavePort < 0 or slavePort > constants.maxSlavePort then
                    print("Invalid RX slave port: ", slavePort)
                    printUsage()
                    return false
                end
                local canAdd = true
                for _, v in ipairs(params.rxSlavePorts) do
                    if v == slavePort then
                        canAdd = false
                        print("Ignoring second specification of RX slave " ..
                              "port: ", slavePort)
                    end
                end
                if canAdd then
                    params.rxSlavePorts[#params.rxSlavePorts + 1] =  slavePort
                end
            elseif v == "--tx-descs" or v == "-txd" then
                local txDescs = tonumber(args[i + 1])
                if txDescs < 0 then
                    print("Invalid TX descriptors: ", txDescs)
                    printUsage()
                    return false
                end
                params.txDescs = txDescs
            elseif v == "--rx-descs" or v == "-rxd" then
                local rxDescs = tonumber(args[i + 1])
                if rxDescs < 0 then
                    print("Invalid RX descriptors: ", rxDescs)
                    printUsage()
                    return false
                end
                params.rxDescs = rxDescs
            elseif v == "--streams" or v == "-s" then
                local streams = tonumber(args[i + 1])
                if (streams < 0) then
                    print("Invalid number of streams:", streams)
                    printUsage()
                    return false
                end
                params.numberOfStreams = streams
            elseif v == "--bursts-per-stream" or v == "-bps" then
                local bps = tonumber(args[i + 1])
                if (bps < 0) then
                    print("Invalid number of bursts per stream:", bps)
                    printUsage()
                    return false
                end
                params.burstsPerStream = bps
            elseif v == "--flows-per-stream" or v == "-fps" then
                local fps = tonumber(args[i + 1])
                if fps < 0 or fps > constants.maxFlowsPerStream then
                    print("Invalid number of flows per stream:", fps)
                    printUsage()
                    return false
                end
                params.flowsPerStream = fps
            elseif v == "--param-display" or v == "-pd" then
                local paramDisplay = tonumber(args[i + 1])
                if paramDisplay < 0 then
                    print(string.format("Invalid param display time: %u, " ..
                          "using default: %u",
                           paramDisplay, (params.paramDisplay / 1000)))
                else
                    params.paramDisplay = paramDisplay * 1000
                end
            elseif v == "--stats-display" or v == "-sd" then
                local period = tonumber(args[i + 1])
                if period < 0 or period > 86400 then
                    print(string.format("Invalid port stats display time: " ..
                          "%u, using default: %u", period,
                          (params.statsDisplay / 1000))
                    )
                    return false
                end
                params.statsDisplay = period * 1000
            elseif v == "--pkt-size" or v == "-ps" then
                local pktSize = tonumber(args[i + 1])
                if pktSize == 0 then
                    params.mustImix   = true
                    params.packetSize = pktSize
                elseif pktSize < 64 or pktSize > 1514 then
                    print("Invalid packet size")
                    printUsage()
                    return false
                else
                    params.packetSize = pktSize
                end
            elseif v == "--timeout" or v == "-to" then
                local timeout = tonumber(args[i + 1])
                if timeout < 0 then
                    print("Invalid timeout period, can't be negative.")
                    printUsage()
                    return false
                else
                    params.timeout = timeout
                end
                params.writemode = 0
            elseif v == "--iterations" or v == "-it" then
                local iterations = tonumber(args[i + 1])
                if iterations < 0 then
                    print("Invalid number of iterations, cannot be negative.")
                    printUsage()
                    return false
                else
                    params.iterations = iterations
                end
                params.writemode = 0
            elseif v == "--pkts-per-sec" or v == "-pps" then
                print("NOTE: flows per stream and burst per stream " ..
                      "should be specified first")

                local txPps = tonumber(args[i + 1])
                if txPps < 0 then
                    print("Invalid packets per second, can't be negaive.")
                    printUsage()
                    return false
                end

                params.txDelta =
                    math.floor(mg:getCyclesFrequency() / txPps) *
                      params.flowsPerStream * params.burstsPerStream
            elseif v == "--file-prefix" or v == "-fp" then
                params.fileprefix = args[i + 1]
                if params.writemode < 2 then
                    params.writemode = 2 -- write to global file
                end
            elseif v == "--write-mode" or v == "-wm" then
                local writemode = tonumber(args[i +1])
                if writemode < 0 or writemode > 3 then
                    print("Invalid write mode, must be 0 - 3.")
                    printUsage()
                    return false
                end
                params.writemode = writemode
            elseif v == "--src-port" or v == "-spt" then
                params.srcPortBase = tonumber(args[i + 1])
            elseif v == "--dst-port" or v == "-dpt" then
                params.dstPortBase = tonumber(args[i + 1])
            elseif v == "--src-ip" or v == "-sip" then
                params.srcIpBase = parseIPAddress(
                    string.match(args[i + 1],
                        "%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?"),
                    true)
            elseif v == "--dst-ip" or v == "-dip" then
                params.dstIpBase = parseIPAddress(
                    string.match(args[i + 1],
                        "%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?"),
                    true)
            elseif v == "--src-mac" or v == "-sm" then
                params.srcMacBase = convertMacAddress(
                    string.match(args[i + 1],
                        "%x%x:%x%x:%x%x:%x%x:%x%x:%x%x"))
            elseif v == "--dst-mac" or v == "-dm" then
                params.dstMacBase = convertMacAddress(
                    string.match(args[i + 1],
                        "%x%x:%x%x:%x%x:%x%x:%x%x:%x%x"))
            elseif v == "--src-port-vary" or v == "-sptv" then
                params.srcPortVary = tonumber(string.match(args[i + 1], "%d+"))
            elseif v == "--dst-port-vary" or v == "-dptv" then
                params.dstPortVary = tonumber(string.match(args[i + 1], "%d+"))
            elseif v == "--src-ip-vary" or v == "-sipv" then
                params.srcIpVary = parseIPAddress(
                    string.match(args[i +1 ],
                        "%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?"),
                    true)
            elseif v == "--dst-ip-vary" or v == "-dipv" then
                params.dstIpVary = parseIPAddress(
                    string.match(args[i + 1],
                        "%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?"),
                    true)
            elseif v == "--src-mac-vary" or v == "-smv" then
                params.srcMacVary = convertMacAddress(
                    string.match(args[i + 1],
                        "%x%x:%x%x:%x%x:%x%x:%x%x:%x%x"))
            elseif v == "--dst-mac-vary" or v == "-dmv" then
                params.dstMacVary = convertMacAddress(
                    string.match(args[i + 1],
                        "%x%x:%x%x:%x%x:%x%x:%x%x:%x%x"))
            elseif v == "--src-port-stride" or v == "-spts" then
                params.srcPortStride = tonumber(string.match(args[i + 1], "%d+"))
            elseif v == "--dst-port-stride" or v == "-dpts" then
                params.dstPortStride = tonumber(string.match(args[i + 1], "%d+"))
            elseif v == "--src-ip-stride" or v == "-sips" then
                params.srcIpStride = parseIPAddress(
                    string.match(args[i +1 ],
                        "%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?"),
                    true)
            elseif v == "--dst-ip-stride" or v == "-dips" then
                params.dstIpStride = parseIPAddress(
                    string.match(args[i + 1],
                        "%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?"),
                    true)
            elseif v == "--src-mac-stride" or v == "-sms" then
                params.srcMacStride = convertMacAddress(
                    string.match(args[i + 1],
                        "%x%x:%x%x:%x%x:%x%x:%x%x:%x%x"))
            elseif v == "--dst-mac-stride" or v == "-dms" then
                params.dstMacStride = convertMacAddress(
                    string.match(args[i + 1],
                        "%x%x:%x%x:%x%x:%x%x:%x%x:%x%x"))

            elseif v == "--help" or v == "-h" then
                printUsage()
                return false
            end
        end
    end
    params.totalFlows = params.numberOfStreams * params.flowsPerStream

    -- Check that the strides are valid:
    checkStrideValidity(params)

    return true
end

-------------------------------------------------------------------------------
-- Checks that the strides for the parameters are valid
-- params   : The parameters whose strides validity must be checked
-------------------------------------------------------------------------------
function checkStrideValidity(params)
    if params.srcMacStride == 0 then
        params.srsMacStride = band(params.flowsPerStream, 0xffffffffffff)
    end
    if params.dstMacStride == 0 then
        params.dstMacStride = band(params.flowsPerStream, 0xffffffffffff)
    end
    if params.srcIpStride == 0 then
        params.srcIpStride = band(params.flowsPerStream, 0xffffffff)
    end
    if params.dstIpStride == 0 then
        params.dstIpStride = band(params.flowsPerStream, 0xffffffff)
    end
    if params.srcPortStride == 0 then
        params.srcPortStride = band(params.flowsPerStream, 0xffff)
    end
    if params.dstPortStride == 0 then
        params.dstPortStride = band(params.flowsPerStream, 0xffff)
    end
end

-------------------------------------------------------------------------------
-- Prints the example usage for the packet generation.
-------------------------------------------------------------------------------
function printUsage()
    local usage = string.format(
        "Usage is:\n Moongen packegen.lua <--dpdk-config=/path/to/config>" ..
        " <Required Args> <Optional Args> where:\n"                        ..
        " Required Args:\n\n"                                              ..
        "   --tx-slave|-tx PORT where\n"                                   ..
        "     PORT : Id of a port to use to TX\n"                          ..
        "     NOTE : This can be specified multiple times\n\n"             ..
        "   --rx-slave|-rx PORT where\n"                                   ..
        "     PORT : Id of a port to use to RX\n"                          ..
        "     NOTE : This can be specified multiple times\n\n"             ..
        " Optional Args:\n\n"                                              ..
        "   --tx-descs|-txd DESCRIPTORS where\n"                           ..
        "     DESCRIPTORS : Number of TX descriptors (default 1024)\n\n"   ..
        "   --rx-descs|-rxd DESCRIPTORS where\n"                           ..
        "     DESCRIPTORS : Number of RX descriptors (default 1024)\n\n"   ..
        "   --streams|-s NUM_STREAMS where\n"                              ..
        "     NUM_STREAMS : Number of streams (default 1)\n\n"             ..
        "   --bursts-per-stream|-bps NUM_BURSTS where\n"                   ..
        "     NUM_BURSTS : Number of bursts per stream (default 1)\n\n"    ..
        "   --flows-per-stream|-fps NUM_FLOWS where\n"                     ..
        "     NUM_FLOWS : Number of flows per stream (default 2047)\n"     ..
        "     NOTE      : 2047 is the max.\n\n"                            ..
        "   --param-display|-pd TIME where\n"                              ..
        "     TIME : Seconds to display params before running\n"           ..
        "            (default 3 seconds)\n\n"                              ..
        "   --stats-display|-sd TIME where\n"                              ..
        "     TIME Stats refresh period on each core, in seconds\n"        ..
        "     (default = 1, 0 = disable)\n"                                ..
        "     NOTE: The stats for each core are printed iteratively\n"     ..
        "           A core's stats will be printed every TIME seconds\n"   ..
        "           TIME = 3 with 3 cores will result in:\n"               ..
        "             core 1 : 0s, 3s, 6s ...\n"                           ..
        "             core 2 : 1s, 4s, 7s ...\n"                           ..
        "             core 3 : 2s, 5s, 8s ...\n\n"                         ..
        "   --pkt-size|-ps PKT_SIZE where\n"                               ..
        "     PKT_SIZE : Packet size (0 for IMIX, default 64)\n\n"         ..
        "   --timeout|-to TIME where\n"                                    ..
        "     TIME : The time (in seconds) to run for.\n"                  ..
        "            (default is to run indefinitely)\n\n"                 ..
        "   --iterations|-it ITERS where\n"                                ..
        "     ITERS : The number of TX/RX iterations to run.\n"            ..
        "             (defualt is to run indefinitely)\n\n"                ..
        "   --pps|-r RATE where\n"                                         ..
        "     RATE : Packets per second rate to attempt.\n\n"              ..
        "   --file-prefix|-fp PREFIX where\n"                              ..
        "     PREFIX : Prefix of the file to write results to\n"           ..
        "     NOTE   : .txt is added to the PREFIX so\n"                   ..
        "              -fp eg will write to eg.txt\n"                      ..
        "              By default a single file is written, (-wm 2)\n"     ..
        "              SEE --write-mode for more options\n\n"              ..
        "   --write-mode|-wm MODE where\n"                                 ..
        "     MODE : Writing mode for stats, options:\n"                   ..
        "       0  : Don't write or show stats\n"                          ..
        "       1  : Print stats to console (default)\n"                   ..
        "       2  : Write all core stats to a global file\n"              ..
        "       3  : Write separate stats for each core\n\n"               ..
        "   --src-mac|-sm SRC_MAC where\n"                                 ..
        "     SRC_MAC : Base SRC mac address (aa:bb:cc:dd:ee:ff)\n\n"      ..
        "   --dst-mac|-dm DST_MAC where\n"                                 ..
        "     DST_MAC : Base DST mac address (aa:bb:cc:dd:ee:ff)\n\n"      ..
        "   --src-ip|-sip SRC_IP where\n"                                  ..
        "     SRC_IP : Base SRC ip address (A.B.C.D)\n\n"                  ..
        "   --dst-ip|-dip DST_IP where\n"                                 ..
        "     DST_IP : Base DST ip address (A.B.C.D)\n\n"                  ..
        "   --src-port|-spt SRC_PORT where\n"                              ..
        "     SRC_PORT : Base source UDP port\n\n"                         ..
        "   --dst-port|-dpt DST_PORT where\n"                              ..
        "     DST_PORT : Base destination UDP port\n\n"                    ..
        "   --src-mac-vary|-smv SRC_MAC_VARY where\n"                      ..
        "     SRC_MAC_VARY : Variation in SRC mac address between flows\n" ..
        "                    (aa:bb:cc:dd:ee:ff)\n\n"                      ..
        "   --dst-mac-vary|-dmv DST_MAC_VARY where\n"                      ..
        "     DST_MAC_VARY : Variation in DST mac address between flows\n" ..
        "                    (aa:bb:cc:dd:ee:ff)\n\n"                      ..
        "   --src-ip-vary|-sipv SRC_IP_VARY where\n"                       ..
        "     SRC_IP_VARY : Variation in SRC ip address between flows\n"   ..
        "                   (A.B.C.D)\n\n"                                 ..
        "   --dst-ip-vary|-dipv DST_IP_VARY where\n"                       ..
        "     DST_IP_VARY : Variation in DST ip address between flows\n"   ..
        "                   (A.B.C.D)\n\n"                                 ..
        "   --src-port-vary|-sptv SRC_PORT_VARY where\n"                   ..
        "     SRC_PORT_VARY : Variation in SRC port between flows\n\n"     ..
        "   --dst-port-vary|-dptv DST_PORT_VARY where\n"                   ..
        "     DST_PORT_VARY : Variation in DST port between flows\n\n"     ..
        "   --src-mac-stride|-sms SRC_MAC_STRIDE where\n"                  ..
        "     SRC_MAC_STRIDE : Variation in SRC mac address between\n"     ..
        "                      streams (aa:bb:cc:dd:ee:ff)\n\n"            ..
        "   --dst-mac-stride|-dms DST_MAC_STRIDE where\n"                  ..
        "     DST_MAC_STRIDE : Variation in DST mac address between\n"     ..
        "                      streams (aa:bb:cc:dd:ee:ff)\n\n"            ..
        "   --src-ip-stride|-sips SRC_IP_STRIDE where\n"                   ..
        "     SRC_IP_STRIDE : Variation in SRC ip address between\n"       ..
        "                     streams (A.B.C.D)\n\n"                       ..
        "   --dst-ip-stride|-dips DST_IP_STRIDE where\n"                   ..
        "     DST_IP_STRIDE: Variation in DST ip address between\n"        ..
        "                     streams (A.B.C.D)\n\n"                       ..
        "   --src-port-stride|-spts SRC_PORT_STRIDE where\n"               ..
        "     SRC_PORT_STRIDE : Variation in SRC port between streams\n\n" ..
        "   --dst-port-stride|-dpts DST_PORT_STRIDE where\n"               ..
        "     DST_PORT_STRIDE : Variation in DST port between streams\n\n" ..
        "   --help|-h Print usage\n\n"                                     ..
        "   NOTE: Later arguments will overwrite earlier ones.\n"
    )
    print(usage)
end


text:
from pwn import *
import misc.coloredstatus as cs
from time import sleep
import os

def execute(session, configs, params):

    try:
        shell = session.shell("/bin/bash")
        shell.sendline("echo c > /proc/sysrq-trigger")
        output = str(shell.recvrepeat(0.2), "UTF-8")
        shell.close()
        return 0
    except:
        return 2


text:
/*********************************************************************
	Rhapsody	: 8.0.3
	Login		: janiec
	Component	: exe
	Configuration 	: DefaultConfig
	Model Element	: Move13
//!	Generated Date	: Fri, 22, Nov 2013
	File Path	: exe/DefaultConfig/Default/Move13.java
*********************************************************************/

package Default;

//## auto_generated
import com.ibm.rational.rhapsody.oxf.RiJEvent;

//----------------------------------------------------------------------------
// Default/Move13.java
//----------------------------------------------------------------------------

//## package Default

//## event Move13()
public class Move13 extends RiJEvent {

    public static final int Move13_Default_id = 18651;		//## ignore

    // Constructors

    public  Move13() {
        lId = Move13_Default_id;
    }

    public boolean isTypeOf(long id) {
        return (Move13_Default_id==id);
    }

}
/*********************************************************************
	File Path	: exe/DefaultConfig/Default/Move13.java
*********************************************************************/


text:
package demos.util;

/** Interface abstracting concept of time from applications. */

public interface Time {
  /** Updates this Time object. Call update() each frame before
      calling the accessor routines. */
  public void  update();
  /** Time in seconds since beginning of application. */
  public double time();
  /** Time in seconds since last update. */
  public double deltaT();
}


text:
<template name="notFound">
  404 Not Found
</template>