Add clear buffer method
This commit is contained in:
parent
5e1407ec4b
commit
915f679105
1 changed files with 13 additions and 16 deletions
|
|
@ -17,6 +17,7 @@
|
||||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
# SOFTWARE.
|
# SOFTWARE.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
|
|
@ -36,35 +37,37 @@ qng_wrapper = QngWrapperWindows() if (os.name == 'nt') else QngWrapperLinux()
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/randint32')
|
@app.route('/api/randint32')
|
||||||
def api_randint32():
|
def randint32():
|
||||||
qng_wrapper.clear()
|
|
||||||
return Response(str(qng_wrapper.randint32()), content_type='text/plain')
|
return Response(str(qng_wrapper.randint32()), content_type='text/plain')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/randuniform')
|
@app.route('/api/randuniform')
|
||||||
def api_randuniform():
|
def randuniform():
|
||||||
qng_wrapper.clear()
|
|
||||||
return Response(str(qng_wrapper.randuniform()), content_type='text/plain')
|
return Response(str(qng_wrapper.randuniform()), content_type='text/plain')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/randnormal')
|
@app.route('/api/randnormal')
|
||||||
def api_randnormal():
|
def randnormal():
|
||||||
qng_wrapper.clear()
|
|
||||||
return Response(str(qng_wrapper.randnormal()), content_type='text/plain')
|
return Response(str(qng_wrapper.randnormal()), content_type='text/plain')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/randbytes')
|
@app.route('/api/randbytes')
|
||||||
def api_randbytes():
|
def randbytes():
|
||||||
try:
|
try:
|
||||||
length = int(request.args.get('length'))
|
length = int(request.args.get('length'))
|
||||||
if length < 1:
|
if length < 1:
|
||||||
return Response('length must be greater than 0', status=400, content_type='text/plain')
|
return Response('length must be greater than 0', status=400, content_type='text/plain')
|
||||||
qng_wrapper.clear()
|
|
||||||
return Response(qng_wrapper.randbytes(length), content_type='application/octet-stream')
|
return Response(qng_wrapper.randbytes(length), content_type='application/octet-stream')
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
return Response(str(e), status=400, content_type='text/plain')
|
return Response(str(e), status=400, content_type='text/plain')
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/api/clear')
|
||||||
|
def clear():
|
||||||
|
qng_wrapper.clear()
|
||||||
|
return Response(status=204)
|
||||||
|
|
||||||
|
|
||||||
@sockets.route('/ws')
|
@sockets.route('/ws')
|
||||||
def ws(websocket):
|
def ws(websocket):
|
||||||
subscribed = [False]
|
subscribed = [False]
|
||||||
|
|
@ -76,43 +79,35 @@ def handle_ws_message(message, websocket, subscribed):
|
||||||
try:
|
try:
|
||||||
split_message = message.strip().upper().split()
|
split_message = message.strip().upper().split()
|
||||||
if split_message[0] == 'RANDINT32':
|
if split_message[0] == 'RANDINT32':
|
||||||
qng_wrapper.clear()
|
|
||||||
websocket.send(str(qng_wrapper.randint32()))
|
websocket.send(str(qng_wrapper.randint32()))
|
||||||
elif split_message[0] == 'RANDUNIFORM':
|
elif split_message[0] == 'RANDUNIFORM':
|
||||||
qng_wrapper.clear()
|
|
||||||
websocket.send(str(qng_wrapper.randuniform()))
|
websocket.send(str(qng_wrapper.randuniform()))
|
||||||
elif split_message[0] == 'RANDNORMAL':
|
elif split_message[0] == 'RANDNORMAL':
|
||||||
qng_wrapper.clear()
|
|
||||||
websocket.send(str(qng_wrapper.randnormal()))
|
websocket.send(str(qng_wrapper.randnormal()))
|
||||||
elif split_message[0] == 'RANDBYTES':
|
elif split_message[0] == 'RANDBYTES':
|
||||||
length = int(split_message[1])
|
length = int(split_message[1])
|
||||||
if length < 1:
|
if length < 1:
|
||||||
raise ValueError()
|
raise ValueError()
|
||||||
qng_wrapper.clear()
|
|
||||||
websocket.send(qng_wrapper.randbytes(length))
|
websocket.send(qng_wrapper.randbytes(length))
|
||||||
elif split_message[0] == 'SUBSCRIBEINT32':
|
elif split_message[0] == 'SUBSCRIBEINT32':
|
||||||
if not subscribed[0]:
|
if not subscribed[0]:
|
||||||
subscribed[0] = True
|
subscribed[0] = True
|
||||||
qng_wrapper.clear()
|
|
||||||
while subscribed[0] and not websocket.closed:
|
while subscribed[0] and not websocket.closed:
|
||||||
websocket.send(str(qng_wrapper.randint32()))
|
websocket.send(str(qng_wrapper.randint32()))
|
||||||
elif split_message[0] == 'SUBSCRIBEUNIFORM':
|
elif split_message[0] == 'SUBSCRIBEUNIFORM':
|
||||||
if not subscribed[0]:
|
if not subscribed[0]:
|
||||||
subscribed[0] = True
|
subscribed[0] = True
|
||||||
qng_wrapper.clear()
|
|
||||||
while subscribed[0] and not websocket.closed:
|
while subscribed[0] and not websocket.closed:
|
||||||
websocket.send(str(qng_wrapper.randuniform()))
|
websocket.send(str(qng_wrapper.randuniform()))
|
||||||
elif split_message[0] == 'SUBSCRIBENORMAL':
|
elif split_message[0] == 'SUBSCRIBENORMAL':
|
||||||
if not subscribed[0]:
|
if not subscribed[0]:
|
||||||
subscribed[0] = True
|
subscribed[0] = True
|
||||||
qng_wrapper.clear()
|
|
||||||
while subscribed[0] and not websocket.closed:
|
while subscribed[0] and not websocket.closed:
|
||||||
websocket.send(str(qng_wrapper.randnormal()))
|
websocket.send(str(qng_wrapper.randnormal()))
|
||||||
elif split_message[0] == 'SUBSCRIBEBYTES':
|
elif split_message[0] == 'SUBSCRIBEBYTES':
|
||||||
chunk = int(split_message[1])
|
chunk = int(split_message[1])
|
||||||
if chunk < 1:
|
if chunk < 1:
|
||||||
raise ValueError()
|
raise ValueError()
|
||||||
qng_wrapper.clear()
|
|
||||||
if not subscribed[0]:
|
if not subscribed[0]:
|
||||||
subscribed[0] = True
|
subscribed[0] = True
|
||||||
while subscribed[0] and not websocket.closed:
|
while subscribed[0] and not websocket.closed:
|
||||||
|
|
@ -120,6 +115,8 @@ def handle_ws_message(message, websocket, subscribed):
|
||||||
elif split_message[0] == 'UNSUBSCRIBE':
|
elif split_message[0] == 'UNSUBSCRIBE':
|
||||||
subscribed[0] = False
|
subscribed[0] = False
|
||||||
websocket.send('UNSUBSCRIBED')
|
websocket.send('UNSUBSCRIBED')
|
||||||
|
elif split_message[0] == 'CLEAR':
|
||||||
|
qng_wrapper.clear()
|
||||||
except (ValueError, BlockingIOError):
|
except (ValueError, BlockingIOError):
|
||||||
pass
|
pass
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue