VCL: refactor async & spinlocks
[vpp.git] / src / vcl / test_vcl_listener_server.c
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16
17 #include <string.h>
18 #include <errno.h>
19 #include <stdio.h>
20 #include <signal.h>
21 #include <stdlib.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <sys/socket.h>
25
26
27 #include <vcl/vppcom.h>
28 #include <vcl/vcl_event.h>
29 #include <unistd.h>
30
31 char MESSAGE[] = "Hello, World!\n";
32
33 static const int PORT = 9995;
34
35 void
36 listener_cb (uint32_t new_session_index, vppcom_endpt_t *ep, void *stuff)
37 {
38
39   vppcom_session_write (new_session_index, &MESSAGE, sizeof (MESSAGE));
40   printf ("\n Heard from port: %d\n", ep->port);
41 }
42
43
44 typedef struct vppcomm_listener_main_
45 {
46   int new_fd;
47
48   struct event *event;
49
50 } vppcomm_listener_main_t;
51
52 vppcomm_listener_main_t _vlm_main;
53 vppcomm_listener_main_t *vlm = &_vlm_main;
54
55
56 int
57 main (int argc, char **argv)
58 {
59
60   int rv;
61   struct sockaddr_in sin;
62   uint32_t listen_fd;
63   vppcom_endpt_t endpt;
64
65   //Address stuff
66   memset (&sin, 0, sizeof (sin));
67   sin.sin_family = AF_INET;
68   sin.sin_port = htons (PORT);
69   //sin.sin_addr.s_addr = inet_addr("127.0.0.1");
70
71   endpt.is_ip4 = (sin.sin_family == AF_INET);
72   endpt.ip = (uint8_t *) & sin.sin_addr;
73   endpt.port = (uint16_t) sin.sin_port;
74
75   //VCL stuff
76   rv = vppcom_app_create ("test_vcl_listener_server");
77   if (rv) return rv;
78
79   listen_fd = vppcom_session_create (VPPCOM_PROTO_TCP,
80                                           0 /* is_nonblocking */ );
81
82   rv = vppcom_session_bind (listen_fd, &endpt);
83
84   //Make a listener and dispatch
85   rv = vppcom_session_register_listener (listen_fd, listener_cb, 0,
86                                             0, 0, &MESSAGE);
87
88   if (rv)
89     {
90       fprintf (stderr, "Could not create a listener!\n");
91       return 1;
92     }
93
94   while (1)
95     {
96       sleep (3);
97     }
98
99   printf ("done\n");
100   return 0;
101 }
102
103