2 # ============LICENSE_START===============================================
3 # Copyright (C) 2020 Nordix Foundation. All rights reserved.
4 # ========================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # ============LICENSE_END=================================================
19 from flask import Flask, request
20 from time import sleep
24 from flask import Flask
25 from flask import Response
30 # list of callback messages
41 # Request and response constants
42 CALLBACK_URL="/callbacks/<string:id>"
43 APP_READ_URL="/get-event/<string:id>"
45 MIME_TEXT="text/plain"
46 MIME_JSON="application/json"
47 CAUGHT_EXCEPTION="Caught exception: "
48 SERVER_ERROR="Server error :"
56 ### Callback interface, for control
58 # Fetch the oldest callback message for an id
59 # URI and parameter, (GET): /get-event/<id>
60 # response: message + 200 or 204
61 @app.route(APP_READ_URL,
63 def receiveresponse(id):
65 global cntr_msg_fetched
68 if ((id in msg_callbacks.keys()) and (len(msg_callbacks[id]) > 0)):
70 msg=str(msg_callbacks[id][0])
71 print("Fetching msg for id: "+id+", msg="+msg)
72 del msg_callbacks[id][0]
74 print("No messages for id: "+id)
75 except Exception as e:
76 print(CAUGHT_EXCEPTION+str(e))
81 # Receive a callback message
82 # URI and payload, (PUT or POST): /callbacks/<id> <json array of response messages>
83 # response: OK 200 or 500 for other errors
84 @app.route(CALLBACK_URL,
85 methods=['PUT','POST'])
88 global cntr_msg_callbacks
91 print("Received callback for id: "+id +", content-type="+request.content_type)
93 if (request.content_type == MIME_JSON):
95 print("Payload(json): "+str(msg))
96 elif (request.content_type == MIME_TEXT):
98 print("Payload(text): "+str(msg))
101 print("Payload(content-type="+request.content_type+"). Setting data to empty, quoted, string")
104 print("(Exception) Payload does not contain any json or text data, setting empty string as payload")
106 cntr_msg_callbacks += 1
107 if (id in msg_callbacks.keys()):
108 msg_callbacks[id].append(msg)
111 msg_callbacks[id].append(msg)
112 except Exception as e:
113 print(CAUGHT_EXCEPTION+str(e))
119 ### Functions for metrics read out ###
121 @app.route('/counter/received_callbacks',
123 def requests_submitted():
124 return Response(str(cntr_msg_callbacks), status=200, mimetype=MIME_TEXT)
126 @app.route('/counter/fetched_callbacks',
128 def requests_fetched():
129 return Response(str(cntr_msg_fetched), status=200, mimetype=MIME_TEXT)
131 @app.route('/counter/current_messages',
133 def current_messages():
134 return Response(str(cntr_msg_callbacks-cntr_msg_fetched), status=200, mimetype=MIME_TEXT)
140 # Reset all messsages and counters
142 methods=['GET', 'POST', 'PUT'])
145 global cntr_msg_fetched
146 global cntr_msg_callbacks
152 return Response('OK', status=200, mimetype=MIME_TEXT)
154 ### Main function ###
156 if __name__ == "__main__":
157 app.run(port=HOST_PORT, host=HOST_IP)