Skip to content
Snippets Groups Projects
Commit bff30add authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Merge branch 'feat/pathcomp-component-frontend' of...

Merge branch 'feat/pathcomp-component-frontend' of https://gitlab.com/teraflow-h2020/controller into feat/ecoc22-dc-interconnect-disjoint
parents ce0208b1 94ae57b1
No related branches found
No related tags found
2 merge requests!54Release 2.0.0,!4Compute component:
Showing
with 6432 additions and 25 deletions
......@@ -130,6 +130,9 @@ venv.bak/
# VSCode project settings
.vscode/
# Visual Studio project settings
/.vs
# Rope project settings
.ropeproject
......
......@@ -41,3 +41,4 @@ include:
#- local: '/src/l3_attackmitigator/.gitlab-ci.yml'
#- local: '/src/slice/.gitlab-ci.yml'
#- local: '/src/interdomain/.gitlab-ci.yml'
- local: '/src/pathcomp/.gitlab-ci.yml'
......@@ -25,4 +25,4 @@ RCFILE=$PROJECTDIR/coverage/.coveragerc
#-o log_cli=true -o log_file=service.log -o log_file_level=DEBUG
coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
pathcomp/tests/test_unitary.py
pathcomp/frontend/tests/test_unitary.py
......@@ -18,7 +18,7 @@ from typing import Dict, List
def get_link_uuid(a_endpoint_id : Dict, z_endpoint_id : Dict) -> str:
return '{:s}/{:s}=={:s}/{:s}'.format(
a_endpoint_id['device_id']['device_uuid']['uuid'], a_endpoint_id['endpoint_uuid']['uuid'],
a_endpoint_id['device_id']['device_uuid']['uuid'], z_endpoint_id['endpoint_uuid']['uuid'])
z_endpoint_id['device_id']['device_uuid']['uuid'], z_endpoint_id['endpoint_uuid']['uuid'])
def json_link_id(link_uuid : str):
return {'link_uuid': {'uuid': link_uuid}}
......
......@@ -21,7 +21,7 @@ from common.tools.object_factory.Context import json_context_id
def get_service_uuid(a_endpoint_id : Dict, z_endpoint_id : Dict) -> str:
return 'svc:{:s}/{:s}=={:s}/{:s}'.format(
a_endpoint_id['device_id']['device_uuid']['uuid'], a_endpoint_id['endpoint_uuid']['uuid'],
a_endpoint_id['device_id']['device_uuid']['uuid'], z_endpoint_id['endpoint_uuid']['uuid'])
z_endpoint_id['device_id']['device_uuid']['uuid'], z_endpoint_id['endpoint_uuid']['uuid'])
def json_service_id(service_uuid : str, context_id : Optional[Dict] = None):
result = {'service_uuid': {'uuid': service_uuid}}
......
......@@ -13,7 +13,7 @@
# limitations under the License.
# Build, tag, and push the Docker image to the GitLab Docker registry
build pathcomp:
build pathcomp-backend:
variables:
IMAGE_NAME: 'pathcomp' # name of the microservice
IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)
......@@ -21,9 +21,11 @@ build pathcomp:
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
script:
- docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/Dockerfile .
- docker tag "$IMAGE_NAME:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
- docker push "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
# This first build tags the builder resulting image to prevent being removed by dangling image removal command
- docker build -t "${IMAGE_NAME}-backend:${IMAGE_TAG}-builder" --target builder -f ./src/$IMAGE_NAME/backend/Dockerfile .
- docker build -t "${IMAGE_NAME}-backend:$IMAGE_TAG" -f ./src/$IMAGE_NAME/backend/Dockerfile .
- docker tag "${IMAGE_NAME}-backend:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/${IMAGE_NAME}-backend:$IMAGE_TAG"
- docker push "$CI_REGISTRY_IMAGE/${IMAGE_NAME}-backend:$IMAGE_TAG"
after_script:
- docker images --filter="dangling=true" --quiet | xargs -r docker rmi
rules:
......@@ -32,35 +34,126 @@ build pathcomp:
- changes:
- src/common/**/*.py
- proto/*.proto
- src/$IMAGE_NAME/**/*.{py,in,yml}
- src/$IMAGE_NAME/Dockerfile
- src/$IMAGE_NAME/tests/*.py
- src/$IMAGE_NAME/.gitlab-ci.yml
- src/$IMAGE_NAME/backend/**/*.{c,h,conf}
- src/$IMAGE_NAME/backend/Makefile
- src/$IMAGE_NAME/backend/Dockerfile
- manifests/${IMAGE_NAME}service.yaml
- .gitlab-ci.yml
# Build, tag, and push the Docker image to the GitLab Docker registry
build pathcomp-frontend:
variables:
IMAGE_NAME: 'pathcomp' # name of the microservice
IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)
stage: build
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
script:
- docker build -t "${IMAGE_NAME}-frontend:$IMAGE_TAG" -f ./src/$IMAGE_NAME/frontend/Dockerfile .
- docker tag "${IMAGE_NAME}-frontend:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/${IMAGE_NAME}-frontend:$IMAGE_TAG"
- docker push "$CI_REGISTRY_IMAGE/${IMAGE_NAME}-frontend:$IMAGE_TAG"
after_script:
- docker images --filter="dangling=true" --quiet | xargs -r docker rmi
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "develop"'
- changes:
- src/common/**/*.py
- proto/*.proto
- src/$IMAGE_NAME/.gitlab-ci.yml
- src/$IMAGE_NAME/frontend/**/*.{py,in,yml}
- src/$IMAGE_NAME/frontend/Dockerfile
- src/$IMAGE_NAME/frontend/tests/*.py
- manifests/${IMAGE_NAME}service.yaml
- .gitlab-ci.yml
# Apply unit test to the component
unit test pathcomp-backend:
variables:
IMAGE_NAME: 'pathcomp' # name of the microservice
IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)
stage: unit_test
needs:
- build pathcomp-backend
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
- if docker network list | grep teraflowbridge; then echo "teraflowbridge is already created"; else docker network create -d bridge teraflowbridge; fi
- if docker container ls | grep ${IMAGE_NAME}-frontend; then docker rm -f ${IMAGE_NAME}-frontend; else echo "${IMAGE_NAME}-frontend image is not in the system"; fi
- if docker container ls | grep ${IMAGE_NAME}-backend; then docker rm -f ${IMAGE_NAME}-backend; else echo "${IMAGE_NAME}-backend image is not in the system"; fi
script:
- docker pull "$CI_REGISTRY_IMAGE/${IMAGE_NAME}-backend:$IMAGE_TAG"
#- docker run --name ${IMAGE_NAME}-backend -d -p 8081:8081 -v "$PWD/src/${IMAGE_NAME}/backend/tests:/opt/results" --network=teraflowbridge ${IMAGE_NAME}-backend:${IMAGE_TAG}-builder
- docker run --name ${IMAGE_NAME}-backend -d -p 8081:8081 --network=teraflowbridge ${IMAGE_NAME}-backend:${IMAGE_TAG}
- sleep 5
- docker ps -a
- docker logs ${IMAGE_NAME}-backend
- sleep 5
- docker exec -i ${IMAGE_NAME}-backend bash -c "curl -0 -v -X POST -H 'Expect:' -H 'Content-Type:\ application/json' http://127.0.0.1:8081/pathComp/api/v1/compRoute -d @/var/teraflow/tests/pc-req.json"
- sleep 5
- docker exec -i ${IMAGE_NAME}-backend bash -c "killall -USR1 pathComp-cvr"
- sleep 5
- docker exec -i ${IMAGE_NAME}-backend bash -c "gcovr"
coverage: '/TOTAL\s+\d+\s+\d+\s+(\d+%)/'
after_script:
- docker logs ${IMAGE_NAME}-backend
- docker rm -f ${IMAGE_NAME}-backend
- docker network rm teraflowbridge
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "develop"'
- changes:
- src/common/**/*.py
- proto/*.proto
- src/$IMAGE_NAME/.gitlab-ci.yml
- src/$IMAGE_NAME/backend/**/*.{c,h,conf}
- src/$IMAGE_NAME/backend/Makefile
- src/$IMAGE_NAME/backend/Dockerfile
- manifests/${IMAGE_NAME}service.yaml
- .gitlab-ci.yml
#artifacts:
# when: always
# reports:
# junit: src/$IMAGE_NAME/backend/tests/${IMAGE_NAME}-backend_report.xml
# Apply unit test to the component
unit test pathcomp:
unit test pathcomp-frontend:
variables:
IMAGE_NAME: 'pathcomp' # name of the microservice
IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)
stage: unit_test
needs:
- build pathcomp
- build pathcomp-frontend
- build pathcomp-backend
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
- if docker network list | grep teraflowbridge; then echo "teraflowbridge is already created"; else docker network create -d bridge teraflowbridge; fi
- if docker container ls | grep $IMAGE_NAME; then docker rm -f $IMAGE_NAME; else echo "$IMAGE_NAME image is not in the system"; fi
- if docker container ls | grep ${IMAGE_NAME}-frontend; then docker rm -f ${IMAGE_NAME}-frontend; else echo "${IMAGE_NAME}-frontend image is not in the system"; fi
- if docker container ls | grep ${IMAGE_NAME}-backend; then docker rm -f ${IMAGE_NAME}-backend; else echo "${IMAGE_NAME}-backend image is not in the system"; fi
script:
- docker pull "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
- docker run --name $IMAGE_NAME -d -p 10020:10020 -v "$PWD/src/$IMAGE_NAME/tests:/opt/results" --network=teraflowbridge $CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG
- docker pull "$CI_REGISTRY_IMAGE/${IMAGE_NAME}-frontend:$IMAGE_TAG"
- docker pull "$CI_REGISTRY_IMAGE/${IMAGE_NAME}-backend:$IMAGE_TAG"
- docker run --name ${IMAGE_NAME}-backend -d -p 8081:8081 -v "$PWD/src/${IMAGE_NAME}/backend/tests:/opt/results" --network=teraflowbridge $CI_REGISTRY_IMAGE/${IMAGE_NAME}-backend:$IMAGE_TAG
- sleep 1
- docker run --name ${IMAGE_NAME}-frontend -d -p 10020:10020 --env "PATHCOMP_BACKEND_HOST=${IMAGE_NAME}-backend" --env "PATHCOMP_BACKEND_PORT=8081" -v "$PWD/src/${IMAGE_NAME}/frontend/tests:/opt/results" --network=teraflowbridge $CI_REGISTRY_IMAGE/${IMAGE_NAME}-frontend:$IMAGE_TAG
- docker exec -i ${IMAGE_NAME}-frontend bash -c "env"
- docker exec -i ${IMAGE_NAME}-backend bash -c "env"
- sleep 5
- docker ps -a
- docker logs $IMAGE_NAME
- docker exec -i $IMAGE_NAME bash -c "coverage run -m pytest --log-level=INFO --verbose $IMAGE_NAME/tests/test_unitary.py --junitxml=/opt/results/${IMAGE_NAME}_report.xml"
- docker exec -i $IMAGE_NAME bash -c "coverage report --include='${IMAGE_NAME}/*' --show-missing"
- docker logs ${IMAGE_NAME}-frontend
- docker logs ${IMAGE_NAME}-backend
- sleep 5
- docker exec -i ${IMAGE_NAME}-frontend bash -c "coverage run -m pytest --log-level=INFO --verbose $IMAGE_NAME/frontend/tests/test_unitary.py --junitxml=/opt/results/${IMAGE_NAME}-frontend_report.xml"
- sleep 5
- docker exec -i ${IMAGE_NAME}-frontend bash -c "coverage report --include='${IMAGE_NAME}/*' --show-missing"
- sleep 5
coverage: '/TOTAL\s+\d+\s+\d+\s+(\d+%)/'
after_script:
- docker rm -f $IMAGE_NAME
- docker ps -a
- docker logs ${IMAGE_NAME}-frontend
- docker logs ${IMAGE_NAME}-backend
- docker rm -f ${IMAGE_NAME}-frontend
- docker rm -f ${IMAGE_NAME}-backend
- docker network rm teraflowbridge
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
......@@ -68,16 +161,16 @@ unit test pathcomp:
- changes:
- src/common/**/*.py
- proto/*.proto
- src/$IMAGE_NAME/**/*.{py,in,yml}
- src/$IMAGE_NAME/Dockerfile
- src/$IMAGE_NAME/tests/*.py
- src/$IMAGE_NAME/tests/Dockerfile
- src/$IMAGE_NAME/.gitlab-ci.yml
- src/$IMAGE_NAME/frontend/**/*.{py,in,yml}
- src/$IMAGE_NAME/frontend/Dockerfile
- src/$IMAGE_NAME/frontend/tests/*.py
- manifests/${IMAGE_NAME}service.yaml
- .gitlab-ci.yml
artifacts:
when: always
reports:
junit: src/$IMAGE_NAME/tests/${IMAGE_NAME}_report.xml
junit: src/$IMAGE_NAME/frontend/tests/${IMAGE_NAME}-frontend_report.xml
# Deployment of the service in Kubernetes Cluster
deploy pathcomp:
......@@ -86,7 +179,8 @@ deploy pathcomp:
IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)
stage: deploy
needs:
- unit test pathcomp
- unit test pathcomp-backend
- unit test pathcomp-frontend
# - integ_test execute
script:
- 'sed -i "s/$IMAGE_NAME:.*/$IMAGE_NAME:$IMAGE_TAG/" manifests/${IMAGE_NAME}service.yaml'
......
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Multi-stage Docker image build
# Stage 1
FROM ubuntu:20.04 AS builder
ARG DEBIAN_FRONTEND=noninteractive
# Install build software
RUN apt-get update -y && apt-get install build-essential curl gcovr libglib2.0-dev -y
# mkdir
RUN mkdir -p /var/teraflow
# Define working directory
WORKDIR /var/teraflow
# Copy every file in working directory
COPY src/pathcomp/backend/. ./
# Build release version and move it to bin folder
RUN make release
RUN mkdir /var/teraflow/bin
RUN mv pathComp /var/teraflow/bin
# Build code coverage version
RUN make clean
RUN make coverage
EXPOSE 8081
# builder defines coverage version of pathcomp by default
ENTRYPOINT [ "./pathComp-cvr", "config/pathcomp.conf", "pathcomp.log" ]
# Stage 2
FROM ubuntu:20.04 AS release
ARG DEBIAN_FRONTEND=noninteractive
# Install build software
RUN apt-get update -y && apt-get install curl libglib2.0-bin -y
# mkdir
RUN mkdir -p /var/teraflow/config
# Define working directory
WORKDIR /var/teraflow
# We make four distinct layers so if there are application changes the library layers can be re-used
COPY --from=builder /var/teraflow/bin/pathComp .
COPY --from=builder /var/teraflow/config/pathcomp.conf ./config
ENTRYPOINT [ "./pathComp", "config/pathcomp.conf", "pathcomp.log" ]
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Multi-stage Docker image build
# Stage 1
FROM ubuntu:20.04 AS builder
ARG DEBIAN_FRONTEND=noninteractive
# Install build software
RUN apt-get update -y && apt-get install build-essential libglib2.0-dev -y
RUN apt-get install gdb gdbserver -y
# mkdir
RUN mkdir -p /var/teraflow
# Define working directory
WORKDIR /var/teraflow
# Copy every file in working directory
COPY src/pathcomp/backend/. ./
RUN make
EXPOSE 8081
ENTRYPOINT [ "gdb", "--args", "./pathComp", "config/pathcomp.conf", "pathcomp.log" ]
#
# # Copyright 2022 Centre Tecnològic de Telecomunicacions de Catalunya (CTTC/CERCA) www.cttc.es
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: CTTC/CERCA PONS RU Ricardo Martínez (ricardo.martinez@cttc.es)
CC = gcc
CFLAGS = -I. -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/
LDLIBS = -lm
LDFLAGS =
all: pathComp
release: CFLAGS += -O6 -Wall -DPOSIX_SOURCE
release: pathComp
debug: CFLAGS += -O0 -ggdb -g -DDEBUG
debug: LDFLAGS += -g
debug: pathComp-dbg
coverage: CFLAGS += -O0 -ggdb -g -DDEBUG -fprofile-arcs -ftest-coverage -DGCOV
coverage: LDFLAGS += -g -lgcov --coverage -fprofile-arcs -ftest-coverage -DGCOV
coverage: pathComp-cvr
pathComp: pathComp.o pathComp_log.o pathComp_cjson.o pathComp_tools.o pathComp_ksp.o pathComp_RESTapi.o
gcc -o pathComp pathComp.o pathComp_log.o pathComp_cjson.o pathComp_tools.o pathComp_ksp.o pathComp_RESTapi.o \
-L/usr/lib/x86_64-linux-gnu/ -lglib-2.0 -luuid $(LDFLAGS) $(LDLIBS)
pathComp-dbg: pathComp.o pathComp_log.o pathComp_cjson.o pathComp_tools.o pathComp_ksp.o pathComp_RESTapi.o
gcc -o pathComp-dbg pathComp.o pathComp_log.o pathComp_cjson.o pathComp_tools.o pathComp_ksp.o pathComp_RESTapi.o \
-L/usr/lib/x86_64-linux-gnu/ -lglib-2.0 -luuid $(LDFLAGS) $(LDLIBS)
pathComp-cvr: pathComp.o pathComp_log.o pathComp_cjson.o pathComp_tools.o pathComp_ksp.o pathComp_RESTapi.o
gcc -o pathComp-cvr pathComp.o pathComp_log.o pathComp_cjson.o pathComp_tools.o pathComp_ksp.o pathComp_RESTapi.o \
-L/usr/lib/x86_64-linux-gnu/ -lglib-2.0 -luuid $(LDFLAGS) $(LDLIBS)
pathComp_log.o: pathComp_log.h pathComp_log.c
$(CC) $(CFLAGS) -c pathComp_log.c -o pathComp_log.o
pathComp_cjson.o: pathComp_log.h pathComp_cjson.h pathComp_cjson.c
$(CC) $(CFLAGS) -c pathComp_cjson.c -o pathComp_cjson.o
pathComp_tools.o: pathComp_log.h pathComp_tools.h pathComp_tools.c
$(CC) $(CFLAGS) -c pathComp_tools.c -o pathComp_tools.o
pathComp_ksp.o: pathComp_log.h pathComp_tools.h pathComp_ksp.h pathComp_ksp.c
$(CC) $(CFLAGS) -c pathComp_ksp.c -o pathComp_ksp.o
pathComp_RESTapi.o: pathComp_tools.h pathComp_log.h pathComp_cjson.h pathComp_ksp.h pathComp_RESTapi.h pathComp_RESTapi.c
$(CC) $(CFLAGS) -c pathComp_RESTapi.c -o pathComp_RESTapi.o
pathComp.o: pathComp_log.h pathComp_RESTapi.h pathComp.c pathComp.h
$(CC) $(CFLAGS) -c pathComp.c -o pathComp.o
clean:
rm -f *.o *.gcno *.gcda *.gcov *.log pathComp pathComp-dbg pathComp-cvr
PATHCOMP_IP_ADDR 0.0.0.0
RESTAPI 1
\ No newline at end of file
////////////////////////////////////////////////////////////////////////////////////////
/**
* # Copyright 2022 Centre Tecnològic de Telecomunicacions de Catalunya (CTTC/CERCA) www.cttc.es
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Author: CTTC/CERCA PONS RU Ricardo Martínez (ricardo.martinez@cttc.es)
*/
/////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <glib.h>
#include <sys/time.h>
#ifdef GCOV
// Instrumentation to report code coverage live
// Ref: https://www.osadl.org/fileadmin/dam/interface/docbook/howtos/coverage.pdf
#include <signal.h>
// Code coverage flush method; used to update code coverage reports while the server is running
void __gcov_flush(void); /* check in gcc sources gcc/gcov-io.h for the prototype */
void my_gcov_handler(int signum)
{
printf("signal received: running __gcov_flush()\n");
__gcov_flush(); /* dump coverage data on receiving SIGUSR1 */
}
#endif
#include "pathComp_log.h"
#include "pathComp_RESTapi.h"
#include "pathComp.h"
// External Variables
FILE *logfile = NULL;
// PATH COMP IP address API Client
struct in_addr pathComp_IpAddr;
// REST API ENABLED
int RESTAPI_ENABLED = 0;
GMainLoop * loop = NULL;
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp.c
* @brief Read the pathComp.conf file @ /etc/pathComp/
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
void read_pathComp_config_file(FILE *fp)
{
DEBUG_PC ("Processing pathComp.conf");
char buff[128], ip[128];
// READ PATH COMP SERVER IP
memset (&pathComp_IpAddr, (int)0, sizeof (struct in_addr));
fscanf(fp, "%s %s ", buff, ip);
pathComp_IpAddr.s_addr = inet_addr(ip);
DEBUG_PC("pathComp_IpAddr: %s", inet_ntoa (pathComp_IpAddr));
memset (buff, 0, sizeof (buff));
// Read REST API
fscanf (fp, "%s %d ", buff, &RESTAPI_ENABLED);
if (RESTAPI_ENABLED) DEBUG_PC ("REST API: ON");
if (RESTAPI_ENABLED == 0) DEBUG_PC ("REST API: OFF");
memset (buff, 0, sizeof (buff));
DEBUG_PC ("CommandLine: %s", buff);
return;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp.c
* @brief Main function for pathComp server
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
#ifdef GCOV
struct sigaction new_action, old_action;
/* setup signal hander */
new_action.sa_handler = my_gcov_handler;
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = 0;
sigaction(SIGUSR1, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN)
sigaction (SIGUSR1, &new_action, NULL);
#endif
DEBUG_PC ("********************************************************************");
DEBUG_PC ("********************************************************************");
DEBUG_PC (" ---------------------- Path Computation Server---------------------");
DEBUG_PC ("********************************************************************");
DEBUG_PC ("********************************************************************");
// processing input parameters
if (argc == 1)
{
DEBUG_PC ("Arguments are missing ...");
exit (-1);
}
// argv[1] specifies the folder and the configuration file
gchar configFile[50];
strcpy (configFile, argv[1]);
DEBUG_PC ("Path Computation Server Config File is: %s", configFile);
// argv[2] specifies the folder and the log file
gchar log[50];
strcpy (log, argv[2]);
DEBUG_PC ("PATH COMP log File: %s", log);
// open the log file
logfile = fopen (log, "w");
DEBUG_PC ("log file is opened");
// Read the pathComp.conf file
FILE *pathComp_config = NULL;
pathComp_config = fopen (configFile, "r");
if (pathComp_config == NULL)
{
DEBUG_PC ("File error\n");
exit (-1);
}
DEBUG_PC ("pathComp_config.conf is opened");
// Check if flag -d for daemonize
if (argc > 3)
{
gchar options[10];
strcpy (options, argv[3]);
gint ret = strcmp (options, "-d");
if (ret == 0) daemon(0,0);
}
// Process the config file
read_pathComp_config_file (pathComp_config);
DEBUG_PC ("\n ---- PATH COMP MAIN LOOP ------");
/** Creates a new GMainLoop structure */
loop = g_main_loop_new (NULL, FALSE);
// Iff RESTAPI_ENABLED is ON
if (RESTAPI_ENABLED)
{
RESTapi_init(PATH_COMP_PORT);
}
/** execute loop */
g_main_loop_run (loop);
/** decrease the one reference of loop when it is finished */
g_main_loop_unref(loop);
loop = NULL;
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* # Copyright 2022 Centre Tecnolgic de Telecomunicacions de Catalunya (CTTC/CERCA) www.cttc.es
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Author: CTTC/CERCA PONS RU Ricardo Martnez (ricardo.martinez@cttc.es)
*/
#ifndef _PATHCOMP_H
#define _PATHCOMP_H
#include <glib.h>
#include <glib/gstdio.h>
#include <glib-2.0/glib/gtypes.h>
///////////////////////////////////////////////////
// IP addressing of peer functional entities
///////////////////////////////////////////////////
extern struct in_addr pathComp_IpAddr;
// TCP Port for the REST API communication with the PATH COMP process
#define PATH_COMP_PORT 8081
// External Variables
extern GMainLoop * loop;
#endif
\ No newline at end of file
This diff is collapsed.
////////////////////////////////////////////////////////////////////////////////////////
/**
* # Copyright 2022 Centre Tecnolgic de Telecomunicacions de Catalunya (CTTC/CERCA) www.cttc.es
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Author: CTTC/CERCA PONS RU Ricardo Martnez (ricardo.martinez@cttc.es)
*/
#ifndef _PATH_COMP_REST_API_H
#define _PATH_COMP_REST_API_H
#include <glib.h>
#include <glib/gstdio.h>
#include <glib-2.0/glib/gtypes.h>
#define MAX_GIO_CHANNEL_BUFFER_SIZE 131072
// HTTP RETURN CODES
#define HTTP_RETURN_CODE_OK 200
#define HTTP_RETURN_CODE_CREATED 201
#define HTTP_RETURN_CODE_BAD_REQUEST 400
#define HTTP_RETURN_CODE_UNAUTHORIZED 401
#define HTTP_RETURN_CODE_FORBIDDEN 403
#define HTTP_RETURN_CODE_NOT_FOUND 404
#define HTTP_RETURN_CODE_NOT_ACCEPTABLE 406
// REST API METHODS (SIMPLY INT ENCODING)
#define REST_API_METHOD_GET 1
#define REST_API_METHOD_POST 2
#define REST_API_METHOD_HTTP 3
#define REST_API_METHOD_PUT 4
#define MAXLENGTH 131072
////////////////////////////////////////////////////
// Client Struct for connecting to PATH COMP SERVER
////////////////////////////////////////////////////
// List of tcp clients connected to PATH COMP
#define PATH_COMP_CLIENT_TYPE 1000
struct pathComp_client
{
/** IO Channel from client. */
GIOChannel *channel;
/** Input/output buffer to the client. */
struct stream *obuf;
struct stream *ibuf;
gint fd; // file descriptor
guint type;
};
void RESTapi_init (gint);
#endif
This diff is collapsed.
////////////////////////////////////////////////////////////////////////////////////////
/**
* # Copyright 2022 Centre Tecnològic de Telecomunicacions de Catalunya (CTTC/CERCA) www.cttc.es
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Author: CTTC/CERCA PONS RU Ricardo Martínez (ricardo.martinez@cttc.es)
*/
////////////////////////////////////////////////////////////////////////////////////////
#ifndef _PATHCOMP_CJSON_H
#define _PATHCOMP_CJSON_H
#include <glib.h>
#include <glib/gstdio.h>
#include <glib-2.0/glib/gtypes.h>
#include <stddef.h>
/* project version */
#define CJSON_VERSION_MAJOR 1
#define CJSON_VERSION_MINOR 6
#define CJSON_VERSION_PATCH 0
/* cJSON Types: */
#define cJSON_Invalid (0)
#define cJSON_False (1 << 0)
#define cJSON_True (1 << 1)
#define cJSON_NULL (1 << 2)
#define cJSON_Number (1 << 3)
#define cJSON_String (1 << 4)
#define cJSON_Array (1 << 5)
#define cJSON_Object (1 << 6)
#define cJSON_Raw (1 << 7) /* raw json */
#define cJSON_IsReference 256
#define cJSON_StringIsConst 512
#define CJSON_PUBLIC(type) type
#define CJSON_NESTING_LIMIT 1000
/* The cJSON structure: */
typedef struct cJSON
{
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *next;
struct cJSON *prev;
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
struct cJSON *child;
/* The type of the item, as above. */
int type;
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
char *valuestring;
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
int valueint;
/* The item's number, if type==cJSON_Number */
double valuedouble;
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
char *string;
} cJSON;
typedef struct cJSON_Hooks
{
void *(*malloc_fn)(size_t sz);
void (*free_fn)(void *ptr);
} cJSON_Hooks;
typedef int cJSON_bool;
/* returns the version of cJSON as a string */
CJSON_PUBLIC(const char*) cJSON_Version(void);
/* Supply malloc, realloc and free functions to cJSON */
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
/* Render a cJSON entity to text for transfer/storage. */
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
/* Render a cJSON entity to text for transfer/storage without any formatting. */
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
/* Delete a cJSON entity and all subentities. */
CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);
/* Returns the number of items in an array (or object). */
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
/* Get item "string" from object. Case insensitive. */
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
/* These functions check the type of an item */
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
/* These calls create a cJSON item of the appropriate type. */
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
/* raw json */
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
/* These utilities create an Array of count items. */
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count);
/* Append item to the specified array/object. */
CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
* WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
* writing to `item->string` */
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
/* Remove/Detatch items from Arrays/Objects. */
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
/* Update array items. */
CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
/* Duplicate a cJSON item */
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
need to be released. With recurse!=0, it will duplicate any children connected to the item.
The item->next and ->prev pointers are always zero on return from Duplicate. */
/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
* case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
CJSON_PUBLIC(void) cJSON_Minify(char *json);
/* Macros for creating things quickly. */
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
#define cJSON_AddRawToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateRaw(s))
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
/* helper for the cJSON_SetNumberValue macro */
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
/* Macro for iterating over an array or object */
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */
CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
CJSON_PUBLIC(void) cJSON_free(void *object);
#endif
This diff is collapsed.
////////////////////////////////////////////////////////////////////////////////////////
/**
* # Copyright 2022 Centre Tecnolgic de Telecomunicacions de Catalunya (CTTC/CERCA) www.cttc.es
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Author: CTTC/CERCA PONS RU Ricardo Martnez (ricardo.martinez@cttc.es)
*/
#ifndef _PATHCOMP_KSP_H
#define _PATHCOMP_KSP_H
#include <glib.h>
#include <glib/gstdio.h>
#include <glib-2.0/glib/gtypes.h>
#define MAX_KSP_VALUE 3
// HTTP RETURN CODES
#define HTTP_CODE_OK 200
#define HTTP_CODE_CREATED 201
#define HTTP_CODE_BAD_REQUEST 400
#define HTTP_CODE_UNAUTHORIZED 401
#define HTTP_CODE_FORBIDDEN 403
#define HTTP_CODE_NOT_FOUND 404
#define HTTP_CODE_NOT_ACCEPTABLE 406
// Prototype of external declaration of functions
gint pathComp_ksp_alg (struct compRouteOutputList_t *);
#endif
////////////////////////////////////////////////////////////////////////////////////////
/**
* # Copyright 2022 Centre Tecnolgic de Telecomunicacions de Catalunya (CTTC/CERCA) www.cttc.es
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Author: CTTC/CERCA PONS RU Ricardo Martnez (ricardo.martinez@cttc.es)
*/
/////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <glib.h>
#include <sys/time.h>
#include <fcntl.h>
#include "pathComp_log.h"
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_log.c
* @brief Create a new variable
*
* @param size
*
* @author Ricardo Martnez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
struct stream* stream_new(size_t size)
{
/** check values */
g_assert(size > 0);
struct stream* stream = g_malloc0(sizeof(struct stream));
if (stream == NULL)
{
DEBUG_PC("%s memory failed\n", __FUNCTION__);
exit(-1);
}
stream->data = g_malloc0(size);
if (stream->data == NULL)
{
DEBUG_PC("%s memory failed\n", __FUNCTION__);
exit(-1);
}
stream->size = size;
/** check values */
g_assert(stream != NULL);
return stream;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_log.c
* @brief removal of a stream variable
*
* @param stream
*
* @author Ricardo Martnez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
void stream_free(struct stream* s)
{
/** check values */
g_assert(s != NULL);
//DEBUG_PC("s: %p, s->data: %p", s, s->data);
/** free data */
g_free(s->data);
g_free(s);
return;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_log.c
* @brief reset the contents of the stream
*
* @param stream
*
* @author Ricardo Martnez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
void stream_reset(struct stream* s)
{
/** check values */
g_assert(s != NULL);
g_assert(s->putp >= 0);
g_assert(s->endp >= 0);
g_assert(s->endp >= 0);
/** reset */
s->putp = 0;
s->endp = 0;
s->getp = 0;
return;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_log.c
* @brief Read over a TCP channel the contents
*
* @param channel
* @param ptr
* @nbytes
*
* @author Ricardo Martnez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
gint read_channel(GIOChannel* channel, guchar* ptr, gint nbytes)
{
/** check values */
g_assert(channel != NULL);
g_assert(ptr != NULL);
g_assert(nbytes >= 0);
/** get the file descriptor */
gint fd;
fd = g_io_channel_unix_get_fd(channel);
gsize nread;
gint nleft;
GError* error = NULL;
GIOStatus status;
nleft = nbytes;
// Set blocking
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags &= ~O_NONBLOCK);
while (nleft > 0)
{
status = g_io_channel_read_chars(channel, (void*)ptr, nleft, &nread, &error);
if (status != G_IO_STATUS_NORMAL)
{
//DEBUG_PC ("gio-test: ...from %d: G_IO_STATUS_%s\n", fd,
// (status == G_IO_STATUS_AGAIN ? "AGAIN" :
// (status == G_IO_STATUS_EOF ? "EOF" :
// (status == G_IO_STATUS_ERROR ? "ERROR" : "???"))));
return -1;
}
if (nread < 0)
{
return (nread);
}
else
{
if (nread == 0)
break;
}
nleft -= nread;
ptr += nread;
}
/** check values */
g_assert(channel != NULL);
g_assert(ptr != NULL);
g_assert(nleft >= 0);
g_assert(nbytes >= 0);
return nbytes - nleft;
}
\ No newline at end of file
////////////////////////////////////////////////////////////////////////////////////////
/**
* # Copyright 2022 Centre Tecnolgic de Telecomunicacions de Catalunya (CTTC/CERCA) www.cttc.es
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Author: CTTC/CERCA PONS RU Ricardo Martnez (ricardo.martinez@cttc.es)
*/
////////////////////////////////////////////////////////////////////////////////////////
#ifndef _PATHCOMP_LOG_H
#define _PATHCOMP_LOG_H
#include <glib.h>
#include <glib/gstdio.h>
#include <glib-2.0/glib/gtypes.h>
#define MAXLENGTH 131072
/** Stream buffer. */
struct stream
{
struct stream *next;
guchar* data;
/** Put pointer. */
gulong putp;
/** Get pointer. */
gulong getp;
/** End of pointer. */
gulong endp;
/** Data size. */
gulong size;
};
extern FILE* logfile;
//////////////////////////////////////////////////////
// For debugging
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
// For debugging
//////////////////////////////////////////////////////
#define __SHORT_FILENAME__ \
(strrchr(__FILE__,'/') \
? strrchr(__FILE__,'/')+1 \
: __FILE__ \
)
#define DEBUG_PC(format,...) \
{ \
if (logfile != NULL) \
{ \
g_fprintf(logfile,"%s:%1.5d %30s "format"\n",\
__SHORT_FILENAME__, \
__LINE__, __FUNCTION__, ##__VA_ARGS__); \
fflush(logfile); \
} \
else \
{ \
g_fprintf(stdout,"%s:%1.5d %30s "format"\n", \
__SHORT_FILENAME__, \
__LINE__, __FUNCTION__, ##__VA_ARGS__); \
fflush(stdout); \
} \
}
//// Prototypes ////////
struct stream* stream_new(size_t);
void stream_free(struct stream*);
void stream_reset(struct stream*);
gint read_channel(GIOChannel*, guchar*, gint);
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment