libjapi  0.3.2
Universal JSON API Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
Examples

To view the full example take a look here.

Server example

Include required headers:
#include <stdio.h>
#include "japi.h"
#include "japi_pushsrv.h"
#include "japi_utils.h"
Register services and start server:
int main(int argc, char *argv[])
{
int ret;
japi_pushsrv_context *psc_counter, *psc_temperature;
/* Declare & initialise resources*/
resources temperature_sensor = {17.0};
/* Read port */
if (argc != 2) {
fprintf(stderr, "ERROR: Missing argument or wrong amount of arguments.\n" \
"Usage:\n\t%s <port>\n", argv[0]);
return -1;
}
/* Create JSON API context */
ctx = japi_init(&temperature_sensor);
if (ctx == NULL) {
fprintf(stderr, "ERROR: Failed to create japi context\n");
return -1;
}
/* Include request args in response */
/* Register JSON API requests */
japi_register_request(ctx, "request_not_found_handler", &rnf_handler);
japi_register_request(ctx, "get_temperature", &get_temperature);
/* Register push services */
psc_counter = japi_pushsrv_register(ctx, "push_counter");
psc_temperature = japi_pushsrv_register(ctx, "push_temperature");
/* Start push threads */
japi_pushsrv_start(psc_counter, &push_counter);
japi_pushsrv_start(psc_temperature, &push_temperature);
/* Set maximal number of allowed clients. 0 for unlimited */
/* Provide JSON API interface via TCP */
ret = japi_start_server(ctx, argv[1]);
/* Destroy JAPI context */
return ret;
}

Client request example

1 #!/usr/bin/env python3
2 
3 # Copyright (c) 2023 Fraunhofer IIS
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the “Software”), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 # THE SOFTWARE.
22 
23 
24 import errno
25 import json
26 import socket
27 import select
28 
29 def process_request(cmd_request,sock):
30  json_cmd = json.dumps(cmd_request) + "\n"
31  sock.sendall(json_cmd.encode())
32 
33  resp = sock.recv(4096)
34 
35  # Just dump if not empty
36  if resp:
37  print(json.dumps(json.loads(resp.decode("utf-8")), indent=4))
38 
39 def main():
40  IP = 'localhost'
41  TCP_PORT = 1234
42  counter = 0 # response counter
43 
44  try:
45  sock = socket.create_connection((IP, TCP_PORT))
46  except socket.error as e:
47  if e.errno != errno.ECONNREFUSED:
48  print("Exception was thrown. Message is %s" % (e))
49  return 1
50  print("A connection to '%s:%d' could not be established." %(IP, TCP_PORT))
51  return 1
52 
53  cmd_request = {
54  "japi_request": "get_temperature",
55  "args": {"unit": "kelvin"},
56  }
57  push_service_request = {
58  "japi_request": "japi_pushsrv_list",
59  }
60  push_temperature_request_subscr = {
61  "japi_request": "japi_pushsrv_subscribe",
62  "args": {"service": "push_temperature"},
63  }
64  push_temperature_request_unsubscr = {
65  "japi_request": "japi_pushsrv_unsubscribe",
66  "args": {"service": "push_temperature"},
67  }
68 
69  process_request(cmd_request,sock)
70  process_request(push_service_request,sock)
71  process_request(push_temperature_request_subscr,sock)
72 
73  # get push service messages
74  sock.settimeout(10)
75  try:
76  resp = sock.recv(4096)
77  # Iterate line by line
78  for n, line in enumerate(sock.makefile(), start=1):
79  # Just dump if received message is not empty
80  if line:
81  jdata = json.loads(line)
82  print(json.dumps(json.loads(line), indent=4))
83  except:
84  pass
85  finally:
86  pass
87 
88  process_request(push_temperature_request_unsubscr,sock)
89 
90  sock.close()
91 
92 if __name__ == "__main__":
93  main()