<commit_before>#
# CORE
# Copyright (c)2010-2012 the Boeing Company.
# See the LICENSE file included in this distribution.
#
''' aDTNPlus user-defined service.
'''

from core.service import CoreService, addservice
from core.misc.utils import *
from core.constants import *

class AdtnPlus(CoreService):
    '''This is the aDTNPlus user-defined service.
    '''
    # a unique name is required, without spaces
    _name = "aDTNPlus"
    # you can create your own group here
    _group = "Utility"
    # list of other services this service depends on
    _depends = ()
    # per-node directories
    _dirs = ()
    # generated files (without a full path this file goes in the node's dir,
    #  e.g. /tmp/pycore.12345/n1.conf/)
    _configs = ('adtnPlus/adtn.ini', 'adtnPlus/NodeState.json', 'plugins.sh',
                'adtnPlus-startup.sh', 'adtnPlus-shutdown.sh',)
    # this controls the starting order vs other enabled services
    _startindex = 50
    # list of startup commands, also may be generated during startup
    _startup = ('sh plugins.sh', 'sh adtnPlus-startup.sh',)
    # list of shutdown commands
    _shutdown = ('sh adtnPlus-shutdown',)

    @classmethod
    def generateconfig(cls, node, filename, services):
        '''This method calls the functions for every defined config file.
        '''
        if filename == "adtnPlus/adtn.ini":
            return cls.generateconfigadtn(node, filename, services)
        elif filename == "adtnPlus/NodeState.json":
            return cls.generatenodeState(node, filename, services)
        elif filename == "plugins.sh":
            return cls.generatestartupplugin(node, filename, services)
        elif filename == "adtnPlus-startup.sh":
            return cls.generatestartupadtnstartup(node, filename, services)
        elif filename == "adtnPlus-shutdown.sh":
            return cls.generateshutdownadtnshutdown(node, filename, services)
        else:
            return ""

    @classmethod
    def generateconfigadtn(cls, node, filename, services):
        for ifc in node.netifs():
                nodeip = ifc.addrlist[0].split("/")[0]
                break
        return """\
# This file contains the configuration of the aDTNPlus.

[Node]
# Node identification name.
nodeId : %s
# IP of this node to receive bundles.
nodeAddress : %s
# Port of this node to receive bundles.
nodePort : 40000
# Clean the previous bundles
clean : false

[NeighbourDiscovery]
# IP address to send the beacons.
discoveryAddress : 192.168.3.11
# Port to send the beacons.
discoveryPort : 40001
# Interval time to send the beacons in seconds.
discoveryPeriod : 2
# Neighbour Expiration time in seconds.
neighbourExpirationTime : 4
# Neighbour Cleaner waiting time in seconds.
neighbourCleanerTime : 2
# Test mode, in this mode the neighbour discovery takes our beacons as a neighbour
# allowing to send and receiver beacons and bundles to ourselves.
testMode : false

[Logger]
# File to save the logs.
filename : %s/adtnPlus/adtn.log
# Level of log, 1 to 100, 1 only errors 100 all log.
# Level 1 is errors only
# Level 6 is errors and warnings
# Level 21 is errors, warnings and information.
# Up to this level are debug logs.
level : 21

[Constants]
# Socket timeout in seconds.
timeout : 10

[BundleProcess]
# Path to save the bundles, it has to exist and the application has to have
# permissions in the folder.
dataPath : %s/adtnPlus/Bundles/
# Bundle process shared library name to use in this node.
bundleProcessName : %s/adtnPlus/Plugins/libaDTNPlus_FirstFwkBundleProcessor.so
# Path to save the generated codes, it has to exist and the application has to
# have permissions in the folder.
codePath : %s/adtnPlus/Codes/
# Path to save the not correctly delivered bundles.
deliveryPath : %s/adtnPlus/Delivered/
# Path to save the trashed bundles when aggregating in the node.
trashAggregationReception : %s/adtnPlus/Trash/aggregation/reception/
# Path to save the trashed bundles when aggregating in the delivery.
trashAggregationDelivery : %s/adtnPlus/Trash/aggregation/delivery/

[AppListener]
# IP address to listen
listenerAddress : %s
# Port to register apps
listenerPort : 50000

[NodeState]
# Default node state path
path : %s/adtnPlus/NodeState.json
""" % (node.name, nodeip, node.nodedir, node.nodedir,
            node.nodedir, node.nodedir, node.nodedir, node.nodedir,node.nodedir,
            nodeip, node.nodedir)

    @classmethod
    def generatenodeState(cls, node, filename, services):
        return r"""
{
  "configuration" : {
    "defaultCodes" : {
      "forwarding" : "if (bps[\"delivered\"]) {bps[\"discard\"] = true; return std::vector<std::string>();} else {auto neighbours = ns(\"eid.connected.all\"); std::vector<std::string> toSend = std::vector<std::string>(); if (neighbours.size() > 0) {int pos = rand() %% neighbours.size(); toSend.push_back(neighbours[pos]);}return toSend;}",
      "lifetime" : "uint64_t creationTimestamp = bs(\"timestamp.value\"); if(bs(\"lifetime\") < (time(NULL) - g_timeFrom2000 - creationTimestamp)) return true; else return false;",
      "destination" : "auto destination = bs(\"destination\"); auto endpoints = ns(\"eid.registered\"); if(std::find(endpoints.begin(), endpoints.end(), destination) != endpoints.end()) return std::vector<std::string>({destination}); else return std::vector<std::string>();",
      "creation" : "bps[\"delivered\"] = false; bps[\"discard\"] = false;",
      "deletion" : ""
    },
    "logLevel" : 21
  },
  "state" : {
    "stop" : false,
    "changed" : false
  },
  "id" : "%s"
}
""" % (node.name)

    @classmethod
    def generatestartupplugin(cls, node, filename, services):
        return """\
#! /bin/bash
NODEDIR=%s
mkdir -p $NODEDIR/adtnPlus/Plugins

localPath=/var/lib/adtnPlus/Plugins/
if [ -d $localPath ]; then
    cp /var/lib/adtnPlus/Plugins/* $NODEDIR/adtnPlus/Plugins/
fi
""" % (node.nodedir)

    @classmethod
    def generatestartupadtnstartup(cls, node, filename, services):
        return """\
#! /bin/bash
# auto-generated by adtnPlus user-defined service.

# create data folder for aDTNPlus platform
mkdir -p adtnPlus/Bundles
mkdir -p adtnPlus/Delivered
mkdir -p adtnPlus/Codes
mkdir -p adtnPlus/Trash/aggregation/delivery
mkdir -p adtnPlus/Trash/aggregation/reception
CFGPATH=%s
sleep 2
# launch aDTNPlus platform service
BundleAgent $CFGPATH &
""" % (node.nodedir + "/adtnPlus/adtn.ini")

    @classmethod
    def generateshutdownadtnshutdown(cls, node, filename, services):
        return """\
#! /bin/bash
# auto-generated by adtnPlus user-defined service.

# stop aDTNPlus platform
pkill BundleAgent > /dev/null 2>&1 &

sleep 3
# delete data folder for aDTN platform
rm -fr adtnPlus
"""

# this line is required to add the above class to the list of available services
addservice(AdtnPlus)
<commit_msg>Adds missing configuration parameter in CORE service file.<commit_after>#
# CORE
# Copyright (c)2010-2012 the Boeing Company.
# See the LICENSE file included in this distribution.
#
''' aDTNPlus user-defined service.
'''

from core.service import CoreService, addservice
from core.misc.utils import *
from core.constants import *

class AdtnPlus(CoreService):
    '''This is the aDTNPlus user-defined service.
    '''
    # a unique name is required, without spaces
    _name = "aDTNPlus"
    # you can create your own group here
    _group = "Utility"
    # list of other services this service depends on
    _depends = ()
    # per-node directories
    _dirs = ()
    # generated files (without a full path this file goes in the node's dir,
    #  e.g. /tmp/pycore.12345/n1.conf/)
    _configs = ('adtnPlus/adtn.ini', 'adtnPlus/NodeState.json', 'plugins.sh',
                'adtnPlus-startup.sh', 'adtnPlus-shutdown.sh',)
    # this controls the starting order vs other enabled services
    _startindex = 50
    # list of startup commands, also may be generated during startup
    _startup = ('sh plugins.sh', 'sh adtnPlus-startup.sh',)
    # list of shutdown commands
    _shutdown = ('sh adtnPlus-shutdown',)

    @classmethod
    def generateconfig(cls, node, filename, services):
        '''This method calls the functions for every defined config file.
        '''
        if filename == "adtnPlus/adtn.ini":
            return cls.generateconfigadtn(node, filename, services)
        elif filename == "adtnPlus/NodeState.json":
            return cls.generatenodeState(node, filename, services)
        elif filename == "plugins.sh":
            return cls.generatestartupplugin(node, filename, services)
        elif filename == "adtnPlus-startup.sh":
            return cls.generatestartupadtnstartup(node, filename, services)
        elif filename == "adtnPlus-shutdown.sh":
            return cls.generateshutdownadtnshutdown(node, filename, services)
        else:
            return ""

    @classmethod
    def generateconfigadtn(cls, node, filename, services):
        for ifc in node.netifs():
                nodeip = ifc.addrlist[0].split("/")[0]
                break
        return """\
# This file contains the configuration of the aDTNPlus.

[Node]
# Node identification name.
nodeId : %s
# IP of this node to receive bundles.
nodeAddress : %s
# Port of this node to receive bundles.
nodePort : 40000
# Clean the previous bundles
clean : false

[NeighbourDiscovery]
# IP address to send the beacons.
discoveryAddress : 192.168.3.11
# Port to send the beacons.
discoveryPort : 40001
# Interval time to send the beacons in seconds.
discoveryPeriod : 2
# Neighbour Expiration time in seconds.
neighbourExpirationTime : 4
# Neighbour Cleaner waiting time in seconds.
neighbourCleanerTime : 2
# Test mode, in this mode the neighbour discovery takes our beacons as a neighbour
# allowing to send and receiver beacons and bundles to ourselves.
testMode : false

[Logger]
# File to save the logs.
filename : %s/adtnPlus/adtn.log
# Level of log, 1 to 100, 1 only errors 100 all log.
# Level 1 is errors only
# Level 6 is errors and warnings
# Level 21 is errors, warnings and information.
# Up to this level are debug logs.
level : 21

[Constants]
# Socket timeout in seconds.
timeout : 10
# Queue max size it bytes, K M and G can be used to express KB, MB and GB
queueByteSize : 1M

[BundleProcess]
# Path to save the bundles, it has to exist and the application has to have
# permissions in the folder.
dataPath : %s/adtnPlus/Bundles/
# Bundle process shared library name to use in this node.
bundleProcessName : %s/adtnPlus/Plugins/libaDTNPlus_FirstFwkBundleProcessor.so
# Path to save the generated codes, it has to exist and the application has to
# have permissions in the folder.
codePath : %s/adtnPlus/Codes/
# Path to save the not correctly delivered bundles.
deliveryPath : %s/adtnPlus/Delivered/
# Path to save the trashed bundles when aggregating in the node.
trashAggregationReception : %s/adtnPlus/Trash/aggregation/reception/
# Path to save the trashed bundles when aggregating in the delivery.
trashAggregationDelivery : %s/adtnPlus/Trash/aggregation/delivery/

[AppListener]
# IP address to listen
listenerAddress : %s
# Port to register apps
listenerPort : 50000

[NodeState]
# Default node state path
path : %s/adtnPlus/NodeState.json
""" % (node.name, nodeip, node.nodedir, node.nodedir,
            node.nodedir, node.nodedir, node.nodedir, node.nodedir,node.nodedir,
            nodeip, node.nodedir)

    @classmethod
    def generatenodeState(cls, node, filename, services):
        return r"""
{
  "configuration" : {
    "defaultCodes" : {
      "forwarding" : "if (bps[\"delivered\"]) {bps[\"discard\"] = true; return std::vector<std::string>();} else {auto neighbours = ns(\"eid.connected.all\"); std::vector<std::string> toSend = std::vector<std::string>(); if (neighbours.size() > 0) {int pos = rand() %% neighbours.size(); toSend.push_back(neighbours[pos]);}return toSend;}",
      "lifetime" : "uint64_t creationTimestamp = bs(\"timestamp.value\"); if(bs(\"lifetime\") < (time(NULL) - g_timeFrom2000 - creationTimestamp)) return true; else return false;",
      "destination" : "auto destination = bs(\"destination\"); auto endpoints = ns(\"eid.registered\"); if(std::find(endpoints.begin(), endpoints.end(), destination) != endpoints.end()) return std::vector<std::string>({destination}); else return std::vector<std::string>();",
      "creation" : "bps[\"delivered\"] = false; bps[\"discard\"] = false;",
      "deletion" : ""
    },
    "logLevel" : 21
  },
  "state" : {
    "stop" : false,
    "changed" : false
  },
  "id" : "%s"
}
""" % (node.name)

    @classmethod
    def generatestartupplugin(cls, node, filename, services):
        return """\
#! /bin/bash
NODEDIR=%s
mkdir -p $NODEDIR/adtnPlus/Plugins

localPath=/var/lib/adtnPlus/Plugins/
if [ -d $localPath ]; then
    cp /var/lib/adtnPlus/Plugins/* $NODEDIR/adtnPlus/Plugins/
fi
""" % (node.nodedir)

    @classmethod
    def generatestartupadtnstartup(cls, node, filename, services):
        return """\
#! /bin/bash
# auto-generated by adtnPlus user-defined service.

# create data folder for aDTNPlus platform
mkdir -p adtnPlus/Bundles
mkdir -p adtnPlus/Delivered
mkdir -p adtnPlus/Codes
mkdir -p adtnPlus/Trash/aggregation/delivery
mkdir -p adtnPlus/Trash/aggregation/reception
CFGPATH=%s
sleep 2
# launch aDTNPlus platform service
BundleAgent $CFGPATH &
""" % (node.nodedir + "/adtnPlus/adtn.ini")

    @classmethod
    def generateshutdownadtnshutdown(cls, node, filename, services):
        return """\
#! /bin/bash
# auto-generated by adtnPlus user-defined service.

# stop aDTNPlus platform
pkill BundleAgent > /dev/null 2>&1 &

sleep 3
# delete data folder for aDTN platform
rm -fr adtnPlus
"""

# this line is required to add the above class to the list of available services
addservice(AdtnPlus)

{'fraction_non_alphanumeric': 0.11418356923974901, 'fraction_numerical': 0.010871151320589523, 'mean_word_length': 3.5858146537303446, 'pattern_counts': {'":': 10, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6765863', 'n_tokens_mistral': 4444, 'n_tokens_neox': 4253, 'n_words': 1349}