Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
tsn-measures
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
tsn-measures
Commits
d738089c
Commit
d738089c
authored
Apr 23, 2020
by
Joanne Hugé
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a periodic real-time thread
parent
126b0141
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
193 additions
and
81 deletions
+193
-81
simple-client/build/Makefile
simple-client/build/Makefile
+3
-1
simple-client/src/main.c
simple-client/src/main.c
+115
-6
simple-server/src/main.c
simple-server/src/main.c
+75
-74
No files found.
simple-client/build/Makefile
View file @
d738089c
...
...
@@ -9,11 +9,13 @@ SRCS += send_packet.c
OBJS
=
$(SRCS:%.c=%.o)
CFLAGS
=
-O
1
-g
-Wall
-Werror
-Wextra
CFLAGS
=
-O
g
-g
-Wall
-Werror
-Wextra
CFLAGS
+=
-MD
-MP
CFLAGS
+=
-I
$(SRCDIR)
CFLAGS
+=
-std
=
gnu99
LDFLAGS
=
-pthread
vpath
%.c
$(SRCDIR)
...
...
simple-client/src/main.c
View file @
d738089c
#include <errno.h>
#include <error.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "getip.h"
#include "send_packet.h"
int
main
(
int
argc
,
char
*
argv
[])
{
#define CLOCK_ID CLOCK_MONOTONIC
if
(
argc
!=
2
)
{
typedef
struct
thread_stat
{
int
nb_cycles
;
}
thread_stat_t
;
typedef
struct
thread_param
{
int
interval
;
int
priority
;
int
max_cycles
;
const
char
*
ip_address
;
thread_stat_t
stats
;
}
thread_param_t
;
// Real-time thread
// Sends packets at a regular intervall
static
void
*
packet_sending_thread
(
void
*
p
)
{
struct
timespec
next
;
struct
sched_param
priority
;
thread_param_t
*
param
=
(
thread_param_t
*
)
p
;
priority
.
sched_priority
=
param
->
priority
;
int
err
=
sched_setscheduler
(
0
,
SCHED_FIFO
,
&
priority
);
if
(
err
)
error
(
EXIT_FAILURE
,
errno
,
"Couldn't set priority"
);
for
(
param
->
stats
.
nb_cycles
=
0
;;
param
->
stats
.
nb_cycles
++
)
{
if
(
param
->
max_cycles
)
if
(
param
->
stats
.
nb_cycles
>=
param
->
max_cycles
)
break
;
clock_gettime
(
CLOCK_ID
,
&
next
);
next
.
tv_sec
+=
param
->
interval
;
send_udp_packet
(
param
->
ip_address
);
clock_nanosleep
(
CLOCK_ID
,
TIMER_ABSTIME
,
&
next
,
NULL
);
}
return
NULL
;
}
static
void
process_options
(
int
argc
,
char
*
argv
[],
thread_param_t
*
param
)
{
for
(;;)
{
int
c
=
getopt
(
argc
,
argv
,
"l:p:i:"
);
if
(
c
==
-
1
)
break
;
switch
(
c
)
{
case
'p'
:
param
->
priority
=
atoi
(
optarg
);
break
;
case
'l'
:
param
->
max_cycles
=
atoi
(
optarg
);
break
;
case
'i'
:
param
->
interval
=
atoi
(
optarg
);
break
;
default:
exit
(
EXIT_FAILURE
);
break
;
}
}
if
(
argc
!=
optind
+
1
)
{
printf
(
"Usage: %s server_ip
\n
"
,
argv
[
0
]);
return
-
1
;
exit
(
EXIT_FAILURE
)
;
}
param
->
ip_address
=
argv
[
optind
];
}
// Main thread, has non-real time priority
// Handles the IO and creates real time threads
int
main
(
int
argc
,
char
*
argv
[])
{
for
(
int
i
=
0
;
i
<
100
;
i
++
)
send_udp_packet
(
argv
[
1
]);
pthread_t
thread
;
thread_param_t
param
;
// Default values
param
.
interval
=
1
;
param
.
max_cycles
=
-
1
;
param
.
priority
=
80
;
process_options
(
argc
,
argv
,
&
param
);
int
err
=
pthread_create
(
&
thread
,
NULL
,
packet_sending_thread
,
(
void
*
)
&
param
);
if
(
err
)
error
(
EXIT_FAILURE
,
errno
,
"Couldn't create thread"
);
for
(;;)
{
usleep
(
100000
);
printf
(
"Nb cycles: %d
\n
"
,
param
.
stats
.
nb_cycles
);
if
(
param
.
max_cycles
==
param
.
stats
.
nb_cycles
)
break
;
}
return
0
;
exit
(
EXIT_SUCCESS
)
;
}
simple-server/src/main.c
View file @
d738089c
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <unistd.h>
#define SERVER_PORT "50000"
#define BUFFER_SIZE 1024
static
void
*
get_in_addr
(
struct
sockaddr
*
sa
)
{
if
(
sa
->
sa_family
==
AF_INET
)
return
&
(((
struct
sockaddr_in
*
)
sa
)
->
sin_addr
);
return
&
(((
struct
sockaddr_in6
*
)
sa
)
->
sin6_addr
);
if
(
sa
->
sa_family
==
AF_INET
)
return
&
(((
struct
sockaddr_in
*
)
sa
)
->
sin_addr
);
return
&
(((
struct
sockaddr_in6
*
)
sa
)
->
sin6_addr
);
}
int
main
()
{
int
status
;
int
sockfd
=
0
;
struct
addrinfo
hints
,
*
servinfo
,
*
servinfo_it
;
...
...
@@ -38,20 +36,22 @@ int main() {
hints
.
ai_flags
=
AI_PASSIVE
;
status
=
getaddrinfo
(
NULL
,
SERVER_PORT
,
&
hints
,
&
servinfo
);
if
(
status
!=
0
)
{
if
(
status
!=
0
)
{
fprintf
(
stderr
,
"getaddrinfo: %s
\n
"
,
gai_strerror
(
status
));
printf
(
"getaddrinfo: %s
\n
"
,
gai_strerror
(
status
));
return
1
;
}
for
(
servinfo_it
=
servinfo
;
servinfo_it
;
servinfo_it
=
servinfo_it
->
ai_next
)
{
sockfd
=
socket
(
servinfo
->
ai_family
,
servinfo
->
ai_socktype
,
servinfo
->
ai_protocol
);
if
(
sockfd
==
-
1
)
{
for
(
servinfo_it
=
servinfo
;
servinfo_it
;
servinfo_it
=
servinfo_it
->
ai_next
)
{
sockfd
=
socket
(
servinfo
->
ai_family
,
servinfo
->
ai_socktype
,
servinfo
->
ai_protocol
);
if
(
sockfd
==
-
1
)
{
printf
(
"Socket error, continuing...
\n
"
);
continue
;
}
if
(
bind
(
sockfd
,
servinfo_it
->
ai_addr
,
servinfo_it
->
ai_addrlen
)
==
-
1
)
{
if
(
bind
(
sockfd
,
servinfo_it
->
ai_addr
,
servinfo_it
->
ai_addrlen
)
==
-
1
)
{
close
(
sockfd
);
printf
(
"Bind error, continuing...
\n
"
);
continue
;
...
...
@@ -65,20 +65,21 @@ int main() {
printf
(
"waiting to recvfrom...
\n
"
);
while
(
1
)
{
if
(
(
bytes_received
=
recvfrom
(
sockfd
,
buf
,
BUFFER_SIZE
-
1
,
0
,
while
(
1
)
{
if
((
bytes_received
=
recvfrom
(
sockfd
,
buf
,
BUFFER_SIZE
-
1
,
0
,
(
struct
sockaddr
*
)
&
client_addr
,
&
addr_len
))
==
-
1
)
{
printf
(
"recvfrom error
\n
"
);
return
2
;
}
inet_ntop
(
client_addr
.
ss_family
,
get_in_addr
((
struct
sockaddr
*
)
&
client_addr
)
,
client_addr_str
,
sizeof
(
client_addr_str
));
get_in_addr
((
struct
sockaddr
*
)
&
client_addr
),
client_addr_str
,
sizeof
(
client_addr_str
));
buf
[
bytes_received
]
=
'\0'
;
printf
(
"got packet from %s: %s (%d long)
\n
"
,
client_addr_str
,
buf
,
bytes_received
);
printf
(
"got packet from %s: %s (%d long)
\n
"
,
client_addr_str
,
buf
,
bytes_received
);
}
close
(
sockfd
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment