Compare commits
No commits in common. "f-webrtc" and "master" have entirely different histories.
@ -1,10 +1,3 @@
|
||||
OPEN LOOPS - 2025-11-12
|
||||
- websockets
|
||||
- separate websocket handling from websocket parsing/sending
|
||||
- do renaming
|
||||
- make wfc not terrible
|
||||
|
||||
|
||||
VIDEO 1 - 2025-09-16
|
||||
TODONE
|
||||
- add qhl as dep
|
||||
|
||||
@ -3,12 +3,7 @@
|
||||
{registered,[]},
|
||||
{included_applications,[]},
|
||||
{applications,[stdlib,kernel]},
|
||||
{vsn,"0.2.0"},
|
||||
{modules,[fd_httpd,fd_httpd_client,fd_httpd_client_man,
|
||||
fd_httpd_client_sup,fd_httpd_clients,fd_httpd_sfc,
|
||||
fd_httpd_sfc_cache,fd_httpd_sfc_entry,fd_httpd_utils,
|
||||
fd_sup,fd_wfcd,fd_wfcd_cache,fewd,qhl,qhl_ws,wfc,
|
||||
wfc_bm,wfc_eval,wfc_eval_context,wfc_ltr,wfc_pp,
|
||||
wfc_read,wfc_sentence,wfc_sftt,wfc_ttfuns,wfc_utils,
|
||||
wfc_word,zj]},
|
||||
{vsn,"0.1.0"},
|
||||
{modules,[fd_client,fd_client_man,fd_client_sup,fd_clients,
|
||||
fd_sup,fewd]},
|
||||
{mod,{fewd,[]}}]}.
|
||||
|
||||
106
priv/static/chat.html
Normal file
106
priv/static/chat.html
Normal file
@ -0,0 +1,106 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Chat with Websockets</title>
|
||||
<link rel="stylesheet" href="/css/default.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="titlebar">
|
||||
<div class="content">
|
||||
<a href="/" class="tb-home">Home</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<h1 class="content-title">Chat with websockets</h1>
|
||||
|
||||
<div class="content-body">
|
||||
<input autofocus label="Nick" id="nick"></input>
|
||||
<textarea hidden disabled id="wfc-output"></textarea>
|
||||
<input hidden id="wfc-input"></input>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let nelt = document.getElementById('nick');
|
||||
let ielt = document.getElementById('wfc-input');
|
||||
let oelt = document.getElementById('wfc-output');
|
||||
let ws = new WebSocket("/ws/chat");
|
||||
let nick = '';
|
||||
|
||||
// when user hits any key while typing in nick
|
||||
function on_nick(evt) {
|
||||
if (evt.key === 'Enter') {
|
||||
// don't do default thing
|
||||
evt.preventDefault();
|
||||
// grab contents
|
||||
let contents = nelt.value;
|
||||
let trimmed = contents.trim();
|
||||
// if contents are nonempty
|
||||
let nonempty_contents = trimmed.length > 0;
|
||||
if (nonempty_contents) {
|
||||
nick = trimmed;
|
||||
let msg_obj = ['nick', nick];
|
||||
let msg_str = JSON.stringify(msg_obj);
|
||||
console.log('message to server:', contents.trim());
|
||||
// query backend for result
|
||||
ws.send(msg_str);
|
||||
|
||||
// delete element from dom
|
||||
nelt.remove();
|
||||
oelt.hidden = false;
|
||||
ielt.hidden = false;
|
||||
ielt.autofocus = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// when user hits any key while typing in ielt
|
||||
function on_input_key(evt) {
|
||||
if (evt.key === 'Enter') {
|
||||
// don't do default thing
|
||||
evt.preventDefault();
|
||||
// grab contents
|
||||
let contents = ielt.value;
|
||||
let trimmed = contents.trim();
|
||||
// if contents are nonempty
|
||||
let nonempty_contents = trimmed.length > 0;
|
||||
if (nonempty_contents) {
|
||||
let msg_obj = ['chat', trimmed];
|
||||
let msg_str = JSON.stringify(msg_obj);
|
||||
console.log('message to server:', contents.trim());
|
||||
// query backend for result
|
||||
ws.send(msg_str);
|
||||
|
||||
// clear input
|
||||
ielt.value = '';
|
||||
|
||||
// add to output
|
||||
oelt.value += '> ';
|
||||
oelt.value += trimmed;
|
||||
oelt.value += '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
nelt.addEventListener('keydown', on_nick);
|
||||
ielt.addEventListener('keydown', on_input_key);
|
||||
ws.onmessage =
|
||||
function (msg_evt) {
|
||||
console.log('message from server:', msg_evt);
|
||||
let msg_str = msg_evt.data;
|
||||
let msg_obj = JSON.parse(msg_str);
|
||||
|
||||
oelt.value += msg_obj.nick;
|
||||
oelt.value += '> ';
|
||||
oelt.value += msg_obj.msg;
|
||||
oelt.value += '\n';
|
||||
};
|
||||
}
|
||||
|
||||
main();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@ -16,8 +16,9 @@
|
||||
<h1 class="content-title">FEWD: index</h1>
|
||||
|
||||
<ul>
|
||||
<li><a href="/chat.html">Chatroom</a></li>
|
||||
<li><a href="/echo.html">Echo</a></li>
|
||||
<li><a href="/webrtc.html">WebRTC</a></li>
|
||||
<li><a href="/tetris.html">Tetris</a></li>
|
||||
<li><a href="/wfc.html">WFC</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
2
priv/static/js/dist/libfewd.js.map
vendored
2
priv/static/js/dist/libfewd.js.map
vendored
@ -1 +1 @@
|
||||
{"version":3,"file":"libfewd.js","sourceRoot":"","sources":["../ts/libfewd.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACH,WAAW,EACX,qBAAqB,EACxB,CAAC;AAGF,SACA,WAAW,CACN,gBAAmC,EACnC,cAAsC,EACtC,UAAyB;IAG1B,+DAA+D;IAC/D,IAAI,gBAAgB,CAAC,OAAO,EAAE;QAC1B,IAAI,aAAa,GAAW,cAAc,CAAC,YAAY,CAAC;QACxD,sCAAsC;QACtC,IAAI,aAAa,GAAG,UAAU;YAC1B,cAAc,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,aAAa,CAAC,GAAG,IAAI,CAAC;;YAE3D,cAAc,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC;KAC/D;AACL,CAAC;AAGD,SACA,qBAAqB,CAChB,gBAAmC,EACnC,cAAsC;IAGvC,IAAI,gBAAgB,CAAC,OAAO,EAAE;QAC1B,mBAAmB;QACnB,cAAc,CAAC,SAAS,GAAG,cAAc,CAAC,YAAY,CAAC;KAC1D;AACL,CAAC"}
|
||||
{"version":3,"file":"libfewd.js","sourceRoot":"","sources":["../ts/libfewd.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACH,WAAW,EACX,qBAAqB,EACxB,CAAC;AAGF,SACA,WAAW,CACN,gBAAmC,EACnC,cAAsC,EACtC,UAAyB;IAG1B,+DAA+D;IAC/D,IAAI,gBAAgB,CAAC,OAAO,EAAE,CAAC;QAC3B,IAAI,aAAa,GAAW,cAAc,CAAC,YAAY,CAAC;QACxD,sCAAsC;QACtC,IAAI,aAAa,GAAG,UAAU;YAC1B,cAAc,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,aAAa,CAAC,GAAG,IAAI,CAAC;;YAE3D,cAAc,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC;IAChE,CAAC;AACL,CAAC;AAGD,SACA,qBAAqB,CAChB,gBAAmC,EACnC,cAAsC;IAGvC,IAAI,gBAAgB,CAAC,OAAO,EAAE,CAAC;QAC3B,mBAAmB;QACnB,cAAc,CAAC,SAAS,GAAG,cAAc,CAAC,YAAY,CAAC;IAC3D,CAAC;AACL,CAAC"}
|
||||
22
priv/static/js/dist/webrtc.d.ts
vendored
22
priv/static/js/dist/webrtc.d.ts
vendored
@ -1,22 +0,0 @@
|
||||
/**
|
||||
* webrtc page script
|
||||
*
|
||||
* Author: Peter Harpending <peterharpending@qpq.swiss>
|
||||
* Date: 2026-02-04
|
||||
* Copyright: Copyright (c) 2026 QPQ AG
|
||||
*
|
||||
* Reference: https://git.qpq.swiss/QPQ-AG/research-megadoc/src/commit/c7c4592d4b21ad120145ef63334471a1a7ec1e60/paste/2026-02/grok-webrtc.html
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
declare function main(): Promise<void>;
|
||||
/**
|
||||
* Try to get the user's camera
|
||||
*
|
||||
* tries to get screenshare if user camera not available for whatever reason
|
||||
*
|
||||
* chimps out if
|
||||
* - not on https
|
||||
* - user says no
|
||||
*/
|
||||
declare function getUserCamera(): Promise<MediaStream>;
|
||||
41
priv/static/js/dist/webrtc.js
vendored
41
priv/static/js/dist/webrtc.js
vendored
@ -1,41 +0,0 @@
|
||||
"use strict";
|
||||
/**
|
||||
* webrtc page script
|
||||
*
|
||||
* Author: Peter Harpending <peterharpending@qpq.swiss>
|
||||
* Date: 2026-02-04
|
||||
* Copyright: Copyright (c) 2026 QPQ AG
|
||||
*
|
||||
* Reference: https://git.qpq.swiss/QPQ-AG/research-megadoc/src/commit/c7c4592d4b21ad120145ef63334471a1a7ec1e60/paste/2026-02/grok-webrtc.html
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
main();
|
||||
async function main() {
|
||||
console.log('peepy');
|
||||
let video = await getUserCamera();
|
||||
console.log('poopy');
|
||||
console.log('poopu');
|
||||
let moi = document.getElementById('me');
|
||||
moi.srcObject = video;
|
||||
console.log('poopa');
|
||||
}
|
||||
/**
|
||||
* Try to get the user's camera
|
||||
*
|
||||
* tries to get screenshare if user camera not available for whatever reason
|
||||
*
|
||||
* chimps out if
|
||||
* - not on https
|
||||
* - user says no
|
||||
*/
|
||||
async function getUserCamera() {
|
||||
if (!(navigator.mediaDevices)) {
|
||||
console.error('navigator.mediaDevices is null; user not on https or something');
|
||||
}
|
||||
let availableDevices = await navigator.mediaDevices.enumerateDevices();
|
||||
console.log(availableDevices);
|
||||
return await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
|
||||
// return await navigator.mediaDevices.getDisplayMedia();
|
||||
}
|
||||
//# sourceMappingURL=webrtc.js.map
|
||||
1
priv/static/js/dist/webrtc.js.map
vendored
1
priv/static/js/dist/webrtc.js.map
vendored
@ -1 +0,0 @@
|
||||
{"version":3,"file":"webrtc.js","sourceRoot":"","sources":["../ts/webrtc.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;AAEH,IAAI,EAAE,CAAC;AAEP,KAAK,UACL,IAAI;IAIA,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACrB,IAAI,KAAK,GAAsB,MAAM,aAAa,EAAE,CAAC;IACrD,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACrB,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACrB,IAAI,GAAG,GAAG,QAAQ,CAAC,cAAc,CAAC,IAAI,CAAqB,CAAC;IAC5D,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC;IACtB,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACzB,CAAC;AAGD;;;;;;;;GAQG;AACH,KAAK,UACL,aAAa;IAIT,IAAI,CAAC,CAAC,SAAS,CAAC,YAAY,CAAC,EAAE;QAC3B,OAAO,CAAC,KAAK,CAAC,gEAAgE,CAAC,CAAC;KACnF;IAED,IAAI,gBAAgB,GAChB,MAAM,SAAS,CAAC,YAAY,CAAC,gBAAgB,EAAE,CAAC;IACpD,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;IAE9B,OAAO,MAAM,SAAS,CAAC,YAAY,CAAC,YAAY,CAAC,EAAC,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAC,CAAC,CAAC;IAC9E,yDAAyD;AAC7D,CAAC"}
|
||||
2
priv/static/js/dist/wfc.js.map
vendored
2
priv/static/js/dist/wfc.js.map
vendored
@ -1 +1 @@
|
||||
{"version":3,"file":"wfc.js","sourceRoot":"","sources":["../ts/wfc.ts"],"names":[],"mappings":"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"}
|
||||
{"version":3,"file":"wfc.js","sourceRoot":"","sources":["../ts/wfc.ts"],"names":[],"mappings":"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"}
|
||||
@ -1,54 +0,0 @@
|
||||
/**
|
||||
* webrtc page script
|
||||
*
|
||||
* Author: Peter Harpending <peterharpending@qpq.swiss>
|
||||
* Date: 2026-02-04
|
||||
* Copyright: Copyright (c) 2026 QPQ AG
|
||||
*
|
||||
* Reference: https://git.qpq.swiss/QPQ-AG/research-megadoc/src/commit/c7c4592d4b21ad120145ef63334471a1a7ec1e60/paste/2026-02/grok-webrtc.html
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
|
||||
main();
|
||||
|
||||
async function
|
||||
main
|
||||
()
|
||||
: Promise<void>
|
||||
{
|
||||
console.log('peepy');
|
||||
let video : MediaStream = await getUserCamera();
|
||||
console.log('poopy');
|
||||
console.log('poopu');
|
||||
let moi = document.getElementById('me') as HTMLVideoElement;
|
||||
moi.srcObject = video;
|
||||
console.log('poopa');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Try to get the user's camera
|
||||
*
|
||||
* tries to get screenshare if user camera not available for whatever reason
|
||||
*
|
||||
* chimps out if
|
||||
* - not on https
|
||||
* - user says no
|
||||
*/
|
||||
async function
|
||||
getUserCamera
|
||||
()
|
||||
: Promise<MediaStream>
|
||||
{
|
||||
if (!(navigator.mediaDevices)) {
|
||||
console.error('navigator.mediaDevices is null; user not on https or something');
|
||||
}
|
||||
|
||||
let availableDevices: Array<MediaDeviceInfo> =
|
||||
await navigator.mediaDevices.enumerateDevices();
|
||||
console.log(availableDevices);
|
||||
|
||||
return await navigator.mediaDevices.getUserMedia({video: true, audio: false});
|
||||
// return await navigator.mediaDevices.getDisplayMedia();
|
||||
}
|
||||
@ -2,8 +2,9 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>FEWD: voice chat demo</title>
|
||||
<title>Tetris with Websockets</title>
|
||||
<link rel="stylesheet" href="/css/default.css">
|
||||
<link rel="stylesheet" href="/css/tetris.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="titlebar">
|
||||
@ -13,15 +14,11 @@
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<h1 class="content-title">FEWD: webrtc demo</h1>
|
||||
<h1 class="content-title">Tetris</h1>
|
||||
|
||||
<div class="content-body">
|
||||
<textarea disabled id="wfc-output"></textarea>
|
||||
|
||||
<video id="me" autoplay muted></video>
|
||||
</div>
|
||||
<textarea id="tetris-state"></textarea>
|
||||
</div>
|
||||
|
||||
<script src="/js/dist/webrtc.js"></script>
|
||||
<script type="module" src="./js/dist/tetris.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,166 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>Super Simple WebRTC – Manual Signaling</title>
|
||||
<style>
|
||||
body { font-family: sans-serif; margin: 20px; }
|
||||
textarea { width: 100%; height: 90px; font-family: monospace; }
|
||||
button { margin: 6px 0; padding: 8px 16px; }
|
||||
video { max-width: 320px; border: 1px solid #444; margin: 8px; background: #000; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h2>WebRTC 1:1 test (copy-paste signaling)</h2>
|
||||
|
||||
<div>
|
||||
<button id="btnCreateOffer">1. Create Offer (Peer A)</button>
|
||||
<button id="btnSetRemoteOffer">2. Paste offer → Set remote description</button><br>
|
||||
<textarea id="offerSdp" placeholder="Offer SDP will appear here (Peer A) or paste offer here (Peer B)"></textarea>
|
||||
</div>
|
||||
|
||||
<div style="margin-top: 24px;">
|
||||
<button id="btnCreateAnswer">3. Create Answer (Peer B)</button>
|
||||
<button id="btnSetRemoteAnswer">4. Paste answer → Set remote description</button><br>
|
||||
<textarea id="answerSdp" placeholder="Answer SDP will appear here (Peer B) or paste answer here (Peer A)"></textarea>
|
||||
</div>
|
||||
|
||||
<div style="margin-top: 20px;">
|
||||
<button id="btnAddIce">Add ICE candidate manually (if needed)</button><br>
|
||||
<textarea id="iceInput" placeholder="Paste remote ICE candidate here"></textarea>
|
||||
<div id="iceLog"></div>
|
||||
</div>
|
||||
|
||||
<div style="margin-top: 20px;">
|
||||
<video id="localVideo" autoplay playsinline muted></video>
|
||||
<video id="remoteVideo" autoplay playsinline></video>
|
||||
</div>
|
||||
|
||||
<pre id="status">Status: ready</pre>
|
||||
|
||||
<script>
|
||||
// ────────────────────────────────────────────────
|
||||
const $ = s => document.querySelector(s);
|
||||
const status = $('pre#status');
|
||||
const offerArea = $('#offerSdp');
|
||||
const answerArea = $('#answerSdp');
|
||||
const iceInput = $('#iceInput');
|
||||
const iceLog = $('#iceLog');
|
||||
|
||||
let pc = null;
|
||||
let localStream = null;
|
||||
|
||||
// ────────────────────────────────────────────────
|
||||
async function initPeerConnection() {
|
||||
if (pc) pc.close();
|
||||
|
||||
pc = new RTCPeerConnection({
|
||||
iceServers: [
|
||||
{ urls: "stun:stun.l.google.com:19302" },
|
||||
{ urls: "stun:stun.stunprotocol.org" }
|
||||
]
|
||||
});
|
||||
|
||||
pc.onicecandidate = e => {
|
||||
if (e.candidate) {
|
||||
console.log("New ICE candidate:", e.candidate);
|
||||
iceLog.innerHTML += "<div style='color:#c33'>→ " + e.candidate.candidate + "</div>";
|
||||
}
|
||||
};
|
||||
|
||||
pc.oniceconnectionstatechange = () => {
|
||||
status.textContent = "ICE state: " + pc.iceConnectionState;
|
||||
};
|
||||
|
||||
pc.ontrack = e => {
|
||||
console.log("ontrack", e);
|
||||
const remoteVideo = $('#remoteVideo');
|
||||
remoteVideo.srcObject = e.streams[0];
|
||||
status.textContent = "ICE state: " + pc.iceConnectionState + " (receiving media!)";
|
||||
};
|
||||
|
||||
// Add local stream if we have camera/mic
|
||||
if (localStream) {
|
||||
localStream.getTracks().forEach(track => {
|
||||
pc.addTrack(track, localStream);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────
|
||||
async function startLocalCamera() {
|
||||
try {
|
||||
localStream = await navigator.mediaDevices.getUserMedia({
|
||||
video: true,
|
||||
audio: false // change to true if you want audio too
|
||||
});
|
||||
$('#localVideo').srcObject = localStream;
|
||||
} catch (err) {
|
||||
console.error("Camera access failed", err);
|
||||
status.textContent = "Camera access failed: " + err.message;
|
||||
}
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────
|
||||
$('#btnCreateOffer').onclick = async () => {
|
||||
await initPeerConnection();
|
||||
await startLocalCamera();
|
||||
|
||||
const offer = await pc.createOffer();
|
||||
await pc.setLocalDescription(offer);
|
||||
|
||||
offerArea.value = JSON.stringify(pc.localDescription, null, 2);
|
||||
status.textContent = "Offer created – copy and send to peer B";
|
||||
};
|
||||
|
||||
// ────────────────────────────────────────────────
|
||||
$('#btnCreateAnswer').onclick = async () => {
|
||||
await initPeerConnection();
|
||||
await startLocalCamera();
|
||||
|
||||
const offer = JSON.parse(offerArea.value);
|
||||
await pc.setRemoteDescription(offer);
|
||||
|
||||
const answer = await pc.createAnswer();
|
||||
await pc.setLocalDescription(answer);
|
||||
|
||||
answerArea.value = JSON.stringify(pc.localDescription, null, 2);
|
||||
status.textContent = "Answer created – copy and send to peer A";
|
||||
};
|
||||
|
||||
// ────────────────────────────────────────────────
|
||||
$('#btnSetRemoteOffer').onclick = async () => {
|
||||
if (!pc) await initPeerConnection();
|
||||
|
||||
const remote = JSON.parse(offerArea.value);
|
||||
await pc.setRemoteDescription(remote);
|
||||
status.textContent = "Remote offer set";
|
||||
};
|
||||
|
||||
// ────────────────────────────────────────────────
|
||||
$('#btnSetRemoteAnswer').onclick = async () => {
|
||||
const remote = JSON.parse(answerArea.value);
|
||||
await pc.setRemoteDescription(remote);
|
||||
status.textContent = "Remote answer set – waiting for ICE/media";
|
||||
};
|
||||
|
||||
// ────────────────────────────────────────────────
|
||||
$('#btnAddIce').onclick = async () => {
|
||||
if (!iceInput.value.trim()) return;
|
||||
try {
|
||||
const candidate = new RTCIceCandidate(JSON.parse(iceInput.value));
|
||||
await pc.addIceCandidate(candidate);
|
||||
iceLog.innerHTML += "<div style='color:#3a3'>← added ICE candidate</div>";
|
||||
iceInput.value = "";
|
||||
} catch (err) {
|
||||
console.error("Bad ICE candidate", err);
|
||||
}
|
||||
};
|
||||
|
||||
// Optional: auto-start camera when page loads
|
||||
// startLocalCamera();
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,127 +0,0 @@
|
||||
/**
|
||||
* webrtc page script
|
||||
*
|
||||
* Author: Peter Harpending <peterharpending@qpq.swiss>
|
||||
* Date: 2026-02-04
|
||||
* Copyright: Copyright (c) 2026 QPQ AG
|
||||
*
|
||||
* Reference: https://git.qpq.swiss/QPQ-AG/research-megadoc/src/commit/c7c4592d4b21ad120145ef63334471a1a7ec1e60/paste/2026-02/grok-webrtc.html
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
|
||||
type state =
|
||||
{whoami: string,
|
||||
roster: Array<string>};
|
||||
|
||||
let st: state =
|
||||
{whoami: "",
|
||||
roster: []};
|
||||
|
||||
main();
|
||||
|
||||
async function
|
||||
main
|
||||
()
|
||||
: Promise<void>
|
||||
{
|
||||
// start websocket immediately
|
||||
let ws: WebSocket;
|
||||
let init = document.getElementById('init') as HTMLDivElement;
|
||||
let init_join = document.getElementById('init-join') as HTMLButtonElement;
|
||||
|
||||
// handle button click
|
||||
init_join.addEventListener('click',
|
||||
function() {
|
||||
init.hidden = true;
|
||||
document.getElementById('run')!.hidden = false;
|
||||
ws = new WebSocket('/ws/webrtc');
|
||||
ws.onopen = function(e) { console.log('ws open'); };
|
||||
ws.onclose = function(e) { console.warn('ws closed'); };
|
||||
ws.onerror = function(e) { console.error('ws error:', e); };
|
||||
ws.onmessage =
|
||||
function(e) {
|
||||
// console.log('ws message:', e.data);
|
||||
let message = JSON.parse(e.data) as ws_msg;
|
||||
handle_ws_msg(message);
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
type ws_msg = ["whoami", string]
|
||||
| ["roster", Array<string>];
|
||||
|
||||
function
|
||||
handle_ws_msg
|
||||
(message: ws_msg)
|
||||
: void
|
||||
{
|
||||
console.log('message from server:', message);
|
||||
switch(message[0]) {
|
||||
case "whoami":
|
||||
st.whoami = message[1];
|
||||
break;
|
||||
case "roster":
|
||||
st.roster = (message[1] as Array<string>);
|
||||
break;
|
||||
default:
|
||||
console.warn("unknown message", message);
|
||||
}
|
||||
|
||||
render_state();
|
||||
}
|
||||
|
||||
|
||||
|
||||
function
|
||||
ws_send_json
|
||||
(ws : WebSocket,
|
||||
x : any)
|
||||
: void
|
||||
{
|
||||
let s: string = JSON.stringify(x, undefined, 4);
|
||||
console.log('sending:\n', s);
|
||||
|
||||
ws.send(s);
|
||||
}
|
||||
|
||||
|
||||
|
||||
function
|
||||
render_state
|
||||
()
|
||||
: void
|
||||
{
|
||||
console.log('st', st);
|
||||
// whoami
|
||||
document.getElementById('whoami')!.innerText = st.whoami;
|
||||
//
|
||||
let roster_ul = document.getElementById('roster-ul') as HTMLUListElement;
|
||||
let newChildren : Array<HTMLElement> = [];
|
||||
for (let nick of st.roster) {
|
||||
if (!(nick === st.whoami)) {
|
||||
let li = nickkk(nick);
|
||||
newChildren.push(li);
|
||||
}
|
||||
}
|
||||
roster_ul.replaceChildren(...newChildren);
|
||||
}
|
||||
|
||||
function
|
||||
nickkk
|
||||
(nick: string)
|
||||
: HTMLElement
|
||||
{
|
||||
let li = document.createElement('li');
|
||||
|
||||
li.innerText += nick;
|
||||
li.innerText += ' ';
|
||||
|
||||
let call_a = document.createElement('button');
|
||||
call_a.innerText = 'call';
|
||||
|
||||
li.appendChild(call_a);
|
||||
|
||||
return li;
|
||||
}
|
||||
105
src/et_piece.erl
Normal file
105
src/et_piece.erl
Normal file
@ -0,0 +1,105 @@
|
||||
-module(et_piece).
|
||||
-vsn("1.0.4").
|
||||
-author("Craig Everett <zxq9@zxq9.com>").
|
||||
-copyright("Craig Everett <zxq9@zxq9.com>").
|
||||
-license("MIT").
|
||||
|
||||
-export([rand/0, new/1, flip/2, points/2, points/1, type/1, sides/1]).
|
||||
|
||||
-export_type([data/0]).
|
||||
|
||||
-record(p,
|
||||
{flip = 1 :: 1..4,
|
||||
type = i :: erltris:type()}).
|
||||
|
||||
-opaque data() :: #p{}.
|
||||
|
||||
rand() ->
|
||||
case rand:uniform(7) of
|
||||
1 -> new(i);
|
||||
2 -> new(o);
|
||||
3 -> new(t);
|
||||
4 -> new(s);
|
||||
5 -> new(z);
|
||||
6 -> new(j);
|
||||
7 -> new(l)
|
||||
end.
|
||||
|
||||
|
||||
new(T) -> #p{type = T}.
|
||||
|
||||
|
||||
flip(r, Piece = #p{flip = 4}) -> Piece#p{flip = 1};
|
||||
flip(r, Piece = #p{flip = F}) -> Piece#p{flip = F + 1};
|
||||
flip(l, Piece = #p{flip = 1}) -> Piece#p{flip = 4};
|
||||
flip(l, Piece = #p{flip = F}) -> Piece#p{flip = F - 1}.
|
||||
|
||||
|
||||
points(Piece, {LX, LY}) ->
|
||||
Offsets = points(Piece),
|
||||
Translate = fun({OX, OY}) -> {LX + OX, LY + OY} end,
|
||||
lists:map(Translate, Offsets).
|
||||
|
||||
|
||||
points(#p{flip = F, type = T}) ->
|
||||
offset(T, F).
|
||||
|
||||
offset(i, 1) -> [{0, 2}, {1, 2}, {2, 2}, {3, 2}];
|
||||
offset(i, 2) -> [{2, 3}, {2, 2}, {2, 1}, {2, 0}];
|
||||
offset(i, 3) -> [{0, 1}, {1, 1}, {2, 1}, {3, 1}];
|
||||
offset(i, 4) -> [{1, 3}, {1, 2}, {1, 1}, {1, 0}];
|
||||
offset(o, _) -> [{1, 1}, {1, 2}, {2, 1}, {2, 2}];
|
||||
offset(t, 1) -> [{0, 1}, {1, 1}, {2, 1}, {1, 2}];
|
||||
offset(t, 2) -> [{1, 2}, {1, 1}, {1, 0}, {2, 1}];
|
||||
offset(t, 3) -> [{0, 1}, {1, 1}, {2, 1}, {1, 0}];
|
||||
offset(t, 4) -> [{1, 2}, {1, 1}, {1, 0}, {0, 1}];
|
||||
offset(s, 1) -> [{0, 1}, {1, 1}, {1, 2}, {2, 2}];
|
||||
offset(s, 2) -> [{1, 2}, {1, 1}, {2, 1}, {2, 0}];
|
||||
offset(s, 3) -> [{0, 0}, {1, 0}, {1, 1}, {2, 1}];
|
||||
offset(s, 4) -> [{0, 2}, {0, 1}, {1, 1}, {1, 0}];
|
||||
offset(z, 1) -> [{0, 2}, {1, 2}, {1, 1}, {2, 1}];
|
||||
offset(z, 2) -> [{1, 0}, {1, 1}, {2, 1}, {2, 2}];
|
||||
offset(z, 3) -> [{0, 1}, {1, 1}, {1, 0}, {2, 0}];
|
||||
offset(z, 4) -> [{0, 0}, {0, 1}, {1, 1}, {1, 2}];
|
||||
offset(j, 1) -> [{0, 2}, {0, 1}, {1, 1}, {2, 1}];
|
||||
offset(j, 2) -> [{1, 0}, {1, 1}, {1, 2}, {2, 2}];
|
||||
offset(j, 3) -> [{0, 1}, {1, 1}, {2, 1}, {2, 0}];
|
||||
offset(j, 4) -> [{0, 0}, {1, 0}, {1, 1}, {1, 2}];
|
||||
offset(l, 1) -> [{0, 1}, {1, 1}, {2, 1}, {2, 2}];
|
||||
offset(l, 2) -> [{1, 2}, {1, 1}, {1, 0}, {2, 0}];
|
||||
offset(l, 3) -> [{0, 0}, {0, 1}, {1, 1}, {2, 1}];
|
||||
offset(l, 4) -> [{0, 2}, {1, 2}, {1, 1}, {1, 0}].
|
||||
|
||||
|
||||
type(#p{type = T}) -> T.
|
||||
|
||||
|
||||
sides(#p{type = T, flip = F}) ->
|
||||
sides(T, F).
|
||||
|
||||
|
||||
sides(i, 1) -> {0, 3, 2};
|
||||
sides(i, 2) -> {2, 2, 3};
|
||||
sides(i, 3) -> {0, 3, 1};
|
||||
sides(i, 4) -> {1, 1, 3};
|
||||
sides(o, _) -> {1, 2, 2};
|
||||
sides(t, 1) -> {0, 2, 2};
|
||||
sides(t, 2) -> {1, 2, 2};
|
||||
sides(t, 3) -> {0, 2, 1};
|
||||
sides(t, 4) -> {0, 1, 2};
|
||||
sides(s, 1) -> {0, 2, 2};
|
||||
sides(s, 2) -> {1, 2, 2};
|
||||
sides(s, 3) -> {0, 2, 1};
|
||||
sides(s, 4) -> {0, 1, 2};
|
||||
sides(z, 1) -> {0 ,2, 2};
|
||||
sides(z, 2) -> {1, 2, 2};
|
||||
sides(z, 3) -> {0 ,2, 1};
|
||||
sides(z, 4) -> {0, 1, 2};
|
||||
sides(j, 1) -> {0, 2, 2};
|
||||
sides(j, 2) -> {1, 2, 2};
|
||||
sides(j, 3) -> {0, 2, 1};
|
||||
sides(j, 4) -> {0, 1, 2};
|
||||
sides(l, 1) -> {0, 2, 2};
|
||||
sides(l, 2) -> {1, 2, 2};
|
||||
sides(l, 3) -> {0, 2, 1};
|
||||
sides(l, 4) -> {0, 1, 2}.
|
||||
79
src/et_well.erl
Normal file
79
src/et_well.erl
Normal file
@ -0,0 +1,79 @@
|
||||
-module(et_well).
|
||||
-vsn("1.0.4").
|
||||
-author("Craig Everett <zxq9@zxq9.com>").
|
||||
-copyright("Craig Everett <zxq9@zxq9.com>").
|
||||
-license("MIT").
|
||||
|
||||
-export([new/0, new/2,
|
||||
dimensions/1, height/1, width/1,
|
||||
fetch/3, store/4, complete/1, collapse/2]).
|
||||
|
||||
-export_type([playfield/0]).
|
||||
|
||||
|
||||
-opaque playfield() :: tuple().
|
||||
|
||||
|
||||
new() ->
|
||||
new(10, 20).
|
||||
|
||||
|
||||
new(W, H) ->
|
||||
erlang:make_tuple(H, row(W)).
|
||||
|
||||
|
||||
row(W) ->
|
||||
erlang:make_tuple(W, x).
|
||||
|
||||
|
||||
dimensions(Well) ->
|
||||
H = size(Well),
|
||||
W = size(element(1, Well)),
|
||||
{W, H}.
|
||||
|
||||
|
||||
height(Well) ->
|
||||
size(Well).
|
||||
|
||||
|
||||
width(Well) ->
|
||||
size(element(1, Well)).
|
||||
|
||||
|
||||
fetch(Well, X, Y) ->
|
||||
element(X, element(Y, Well)).
|
||||
|
||||
|
||||
store(Well, Value, X, Y) ->
|
||||
setelement(Y, Well, setelement(X, element(Y, Well), Value)).
|
||||
|
||||
|
||||
complete(Well) ->
|
||||
{W, H} = dimensions(Well),
|
||||
complete(H, W, Well, []).
|
||||
|
||||
complete(Y, W, Well, Lines) when Y >= 1 ->
|
||||
case line_complete(W, element(Y, Well)) of
|
||||
true -> complete(Y - 1, W, Well, [Y | Lines]);
|
||||
false -> complete(Y - 1, W, Well, Lines)
|
||||
end;
|
||||
complete(_, _, _, Lines) ->
|
||||
Lines.
|
||||
|
||||
line_complete(X, Line) when X >= 1 ->
|
||||
case element(X, Line) of
|
||||
x -> false;
|
||||
_ -> line_complete(X - 1, Line)
|
||||
end;
|
||||
line_complete(_, _) ->
|
||||
true.
|
||||
|
||||
|
||||
collapse(Well, Lines) ->
|
||||
Blank = row(width(Well)),
|
||||
Crunch =
|
||||
fun(L, {W, Count}) ->
|
||||
Crunched = erlang:insert_element(1, erlang:delete_element(L, W), Blank),
|
||||
{Crunched, Count + 1}
|
||||
end,
|
||||
lists:foldl(Crunch, {Well, 0}, Lines).
|
||||
@ -1,6 +1,5 @@
|
||||
% @doc storing map #{cookie := Context}
|
||||
-module(fd_wfcd_cache).
|
||||
-vsn("0.2.0").
|
||||
-module(fd_cache).
|
||||
|
||||
-behavior(gen_server).
|
||||
|
||||
262
src/fd_chat.erl
Normal file
262
src/fd_chat.erl
Normal file
@ -0,0 +1,262 @@
|
||||
% @doc
|
||||
% controller for chat
|
||||
-module(fd_chat).
|
||||
-vsn("0.1.0").
|
||||
-behavior(gen_server).
|
||||
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
-copyright("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
-license("BSD-2-Clause-FreeBSD").
|
||||
|
||||
-export([
|
||||
join/1,
|
||||
relay/1,
|
||||
nick_available/1
|
||||
]).
|
||||
-export([start_link/0]).
|
||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
||||
code_change/3, terminate/2]).
|
||||
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
|
||||
|
||||
-record(o, {pid :: pid(),
|
||||
nick :: string()}).
|
||||
-type orator() :: #o{}.
|
||||
|
||||
-record(s, {orators = [] :: [orator()]}).
|
||||
|
||||
-type state() :: #s{}.
|
||||
|
||||
|
||||
|
||||
%%% Service Interface
|
||||
|
||||
-spec join(Nick) -> Result
|
||||
when Nick :: string(),
|
||||
Result :: ok
|
||||
| {error, Reason :: any()}.
|
||||
|
||||
join(Nick) ->
|
||||
gen_server:call(?MODULE, {join, Nick}).
|
||||
|
||||
|
||||
|
||||
-spec nick_available(Nick) -> Result
|
||||
when Nick :: string(),
|
||||
Result :: boolean().
|
||||
|
||||
nick_available(Nick) ->
|
||||
gen_server:call(?MODULE, {nick_available, Nick}).
|
||||
|
||||
|
||||
|
||||
-spec relay(Message) -> ok
|
||||
when Message :: string().
|
||||
|
||||
relay(Message) ->
|
||||
gen_server:cast(?MODULE, {relay, self(), Message}).
|
||||
|
||||
|
||||
%%% Startup Functions
|
||||
|
||||
|
||||
-spec start_link() -> Result
|
||||
when Result :: {ok, pid()}
|
||||
| {error, Reason :: term()}.
|
||||
%% @private
|
||||
%% This should only ever be called by fd_chat_orators (the service-level supervisor).
|
||||
|
||||
start_link() ->
|
||||
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
|
||||
|
||||
|
||||
-spec init(none) -> {ok, state()}.
|
||||
%% @private
|
||||
%% Called by the supervisor process to give the process a chance to perform any
|
||||
%% preparatory work necessary for proper function.
|
||||
|
||||
init(none) ->
|
||||
ok = tell("~p Starting.", [?MODULE]),
|
||||
State = #s{},
|
||||
{ok, State}.
|
||||
|
||||
|
||||
|
||||
%%% gen_server Message Handling Callbacks
|
||||
|
||||
|
||||
-spec handle_call(Message, From, State) -> Result
|
||||
when Message :: term(),
|
||||
From :: {pid(), reference()},
|
||||
State :: state(),
|
||||
Result :: {reply, Response, NewState}
|
||||
| {noreply, State},
|
||||
Response :: term(),
|
||||
NewState :: state().
|
||||
%% @private
|
||||
%% The gen_server:handle_call/3 callback.
|
||||
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_call-3
|
||||
|
||||
handle_call({join, Nick}, {Pid, _}, State) ->
|
||||
{Reply, NewState} = do_join(Pid, Nick, State),
|
||||
{reply, Reply, NewState};
|
||||
handle_call({nick_available, Nick}, _, State = #s{orators = Orators}) ->
|
||||
Reply = is_nick_available(Nick, Orators),
|
||||
{reply, Reply, State};
|
||||
handle_call(Unexpected, From, State) ->
|
||||
ok = tell("~p Unexpected call from ~tp: ~tp~n", [?MODULE, From, Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
-spec handle_cast(Message, State) -> {noreply, NewState}
|
||||
when Message :: term(),
|
||||
State :: state(),
|
||||
NewState :: state().
|
||||
%% @private
|
||||
%% The gen_server:handle_cast/2 callback.
|
||||
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_cast-2
|
||||
|
||||
handle_cast({relay, From, Message}, State = #s{orators = Orators}) ->
|
||||
do_relay(From, Message, Orators),
|
||||
{noreply, State};
|
||||
handle_cast(Unexpected, State) ->
|
||||
ok = tell("~p Unexpected cast: ~tp~n", [?MODULE, Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
-spec handle_info(Message, State) -> {noreply, NewState}
|
||||
when Message :: term(),
|
||||
State :: state(),
|
||||
NewState :: state().
|
||||
%% @private
|
||||
%% The gen_server:handle_info/2 callback.
|
||||
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_info-2
|
||||
|
||||
handle_info(Msg = {'DOWN', _Mon, process, _Pid, _Reason}, State) ->
|
||||
NewState = handle_down(Msg, State),
|
||||
{noreply, NewState};
|
||||
handle_info(Unexpected, State) ->
|
||||
ok = tell("~p Unexpected info: ~tp~n", [?MODULE, Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
|
||||
%%% OTP Service Functions
|
||||
|
||||
-spec code_change(OldVersion, State, Extra) -> Result
|
||||
when OldVersion :: {down, Version} | Version,
|
||||
Version :: term(),
|
||||
State :: state(),
|
||||
Extra :: term(),
|
||||
Result :: {ok, NewState}
|
||||
| {error, Reason :: term()},
|
||||
NewState :: state().
|
||||
%% @private
|
||||
%% The gen_server:code_change/3 callback.
|
||||
%% See: http://erlang.org/doc/man/gen_server.html#Module:code_change-3
|
||||
|
||||
code_change(_, State, _) ->
|
||||
{ok, State}.
|
||||
|
||||
|
||||
-spec terminate(Reason, State) -> no_return()
|
||||
when Reason :: normal
|
||||
| shutdown
|
||||
| {shutdown, term()}
|
||||
| term(),
|
||||
State :: state().
|
||||
%% @private
|
||||
%% The gen_server:terminate/2 callback.
|
||||
%% See: http://erlang.org/doc/man/gen_server.html#Module:terminate-2
|
||||
|
||||
terminate(_, _) ->
|
||||
ok.
|
||||
|
||||
|
||||
%%% internals
|
||||
|
||||
-spec do_join(Pid, Nick, State) -> {Reply, NewState}
|
||||
when Pid :: pid(),
|
||||
Nick :: string(),
|
||||
Reply :: ok | {error, Reason :: any()},
|
||||
NewState :: State.
|
||||
|
||||
do_join(Pid, Nick, State = #s{orators = Orators}) ->
|
||||
case ensure_can_join(Pid, Nick, Orators) of
|
||||
ok -> do_join2(Pid, Nick, State);
|
||||
Error -> {Error, State}
|
||||
end.
|
||||
|
||||
|
||||
do_join2(Pid, Nick, State = #s{orators = Orators}) ->
|
||||
_Monitor = erlang:monitor(process, Pid),
|
||||
NewOrator = #o{pid = Pid, nick = Nick},
|
||||
NewOrators = [NewOrator | Orators],
|
||||
NewState = State#s{orators = NewOrators},
|
||||
{ok, NewState}.
|
||||
|
||||
|
||||
-spec ensure_can_join(Pid, Nick, Orators) -> Result
|
||||
when Pid :: pid(),
|
||||
Nick :: string(),
|
||||
Orators :: [orator()],
|
||||
Result :: ok
|
||||
| {error, Reason},
|
||||
Reason :: any().
|
||||
% @private
|
||||
% ensures both Pid and Nick are unique
|
||||
|
||||
ensure_can_join(Pid, _ , [#o{pid = Pid} | _ ]) -> {error, already_joined};
|
||||
ensure_can_join(_ , Nick, [#o{nick = Nick} | _ ]) -> {error, {nick_taken, Nick}};
|
||||
ensure_can_join(Pid, Nick, [_ | Rest]) -> ensure_can_join(Pid, Nick, Rest);
|
||||
ensure_can_join(_ , _ , [] ) -> ok.
|
||||
|
||||
|
||||
-spec is_nick_available(Nick, Orators) -> boolean()
|
||||
when Nick :: string(),
|
||||
Orators :: [orator()].
|
||||
|
||||
is_nick_available(Nick, [#o{nick = Nick} | _ ]) -> false;
|
||||
is_nick_available(Nick, [_ | Rest]) -> is_nick_available(Nick, Rest);
|
||||
is_nick_available(_ , [] ) -> true.
|
||||
|
||||
|
||||
|
||||
-spec handle_down(Msg, State) -> NewState
|
||||
when Msg :: {'DOWN', Mon, process, Pid, Reason},
|
||||
Mon :: erlang:monitor(),
|
||||
Pid :: pid(),
|
||||
Reason :: any(),
|
||||
State :: state(),
|
||||
NewState :: State.
|
||||
|
||||
handle_down(Msg = {'DOWN', _, process, Pid, _}, State = #s{orators = Orators}) ->
|
||||
NewOrators = hdn(Msg, Pid, Orators, []),
|
||||
NewState = State#s{orators = NewOrators},
|
||||
NewState.
|
||||
|
||||
% encountered item, removing
|
||||
hdn(_, Pid, [#o{pid = Pid} | Rest], Acc) -> Rest ++ Acc;
|
||||
hdn(Msg, Pid, [Skip | Rest], Acc) -> hdn(Msg, Pid, Rest, [Skip | Acc]);
|
||||
hdn(Msg, _, [] , Acc) ->
|
||||
log("~tp: Unexpected message: ~tp", [?MODULE, Msg]),
|
||||
Acc.
|
||||
|
||||
|
||||
do_relay(Pid, Message, Orators) ->
|
||||
case lists:keyfind(Pid, #o.pid, Orators) of
|
||||
#o{nick = Nick} ->
|
||||
do_relay2(Nick, Message, Orators);
|
||||
false ->
|
||||
tell("~tp: Message received from outsider ~tp: ~tp", [?MODULE, Pid, Message]),
|
||||
error
|
||||
end.
|
||||
|
||||
% skip
|
||||
do_relay2(Nick, Msg, [#o{nick = Nick} | Rest]) ->
|
||||
do_relay2(Nick, Msg, Rest);
|
||||
do_relay2(Nick, Msg, [#o{pid = Pid} | Rest]) ->
|
||||
Pid ! {chat, {relay, Nick, Msg}},
|
||||
do_relay2(Nick, Msg, Rest);
|
||||
do_relay2(_, _, []) ->
|
||||
ok.
|
||||
@ -13,8 +13,8 @@
|
||||
%%% http://erlang.org/doc/design_principles/spec_proc.html
|
||||
%%% @end
|
||||
|
||||
-module(fd_httpd_client).
|
||||
-vsn("0.2.0").
|
||||
-module(fd_client).
|
||||
-vsn("0.1.0").
|
||||
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
-copyright("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
-license("BSD-2-Clause-FreeBSD").
|
||||
@ -52,11 +52,11 @@
|
||||
| {shutdown, term()}
|
||||
| term().
|
||||
%% @private
|
||||
%% How the fd_httpd_client_man or a prior fd_httpd_client kicks things off.
|
||||
%% This is called in the context of fd_httpd_client_man or the prior fd_httpd_client.
|
||||
%% How the fd_client_man or a prior fd_client kicks things off.
|
||||
%% This is called in the context of fd_client_man or the prior fd_client.
|
||||
|
||||
start(ListenSocket) ->
|
||||
fd_httpd_client_sup:start_acceptor(ListenSocket).
|
||||
fd_client_sup:start_acceptor(ListenSocket).
|
||||
|
||||
|
||||
-spec start_link(ListenSocket) -> Result
|
||||
@ -67,7 +67,7 @@ start(ListenSocket) ->
|
||||
| {shutdown, term()}
|
||||
| term().
|
||||
%% @private
|
||||
%% This is called by the fd_httpd_client_sup. While start/1 is called to iniate a startup
|
||||
%% This is called by the fd_client_sup. While start/1 is called to iniate a startup
|
||||
%% (essentially requesting a new worker be started by the supervisor), this is
|
||||
%% actually called in the context of the supervisor.
|
||||
|
||||
@ -86,7 +86,7 @@ start_link(ListenSocket) ->
|
||||
%% call to listen/3.
|
||||
|
||||
init(Parent, ListenSocket) ->
|
||||
ok = tell("~p Listening.~n", [self()]),
|
||||
ok = io:format("~p Listening.~n", [self()]),
|
||||
Debug = sys:debug_options([]),
|
||||
ok = proc_lib:init_ack(Parent, {ok, self()}),
|
||||
listen(Parent, Debug, ListenSocket).
|
||||
@ -98,7 +98,7 @@ init(Parent, ListenSocket) ->
|
||||
ListenSocket :: gen_tcp:socket().
|
||||
%% @private
|
||||
%% This function waits for a TCP connection. The owner of the socket is still
|
||||
%% the fd_httpd_client_man (so it can still close it on a call to fd_httpd_client_man:ignore/0),
|
||||
%% the fd_client_man (so it can still close it on a call to fd_client_man:ignore/0),
|
||||
%% but the only one calling gen_tcp:accept/1 on it is this process. Closing the socket
|
||||
%% is one way a manager process can gracefully unblock child workers that are blocking
|
||||
%% on a network accept.
|
||||
@ -110,12 +110,12 @@ listen(Parent, Debug, ListenSocket) ->
|
||||
{ok, Socket} ->
|
||||
{ok, _} = start(ListenSocket),
|
||||
{ok, Peer} = inet:peername(Socket),
|
||||
ok = tell("~p Connection accepted from: ~p~n", [self(), Peer]),
|
||||
ok = fd_httpd_client_man:enroll(),
|
||||
ok = io:format("~p Connection accepted from: ~p~n", [self(), Peer]),
|
||||
ok = fd_client_man:enroll(),
|
||||
State = #s{socket = Socket},
|
||||
loop(Parent, Debug, State);
|
||||
{error, closed} ->
|
||||
ok = tell("~p Retiring: Listen socket closed.~n", [self()]),
|
||||
ok = io:format("~p Retiring: Listen socket closed.~n", [self()]),
|
||||
exit(normal)
|
||||
end.
|
||||
|
||||
@ -135,34 +135,24 @@ loop(Parent, Debug, State = #s{socket = Socket, next = Next0}) ->
|
||||
Received = <<Next0/binary, Message/binary>>,
|
||||
case qhl:parse(Socket, Received) of
|
||||
{ok, Req, Next1} ->
|
||||
%% FIXME: unfuck received logic here
|
||||
%% handle_request should eventually call back into
|
||||
%% loop/3 or close the socket
|
||||
%%
|
||||
%% also loop/3 should be loop/1, because we need to pass
|
||||
%% the state down the callchains
|
||||
Next2 =
|
||||
case Next1 of
|
||||
none -> <<>>;
|
||||
Bin -> Bin
|
||||
end,
|
||||
Next2 = handle_request(Socket, Req, Next1),
|
||||
NewState = State#s{next = Next2},
|
||||
handle_request(Req, Parent, Debug, NewState);
|
||||
loop(Parent, Debug, NewState);
|
||||
Error ->
|
||||
%% should trigger bad request
|
||||
tell(error, "~p QHL parse error: ~tp", [?LINE, Error]),
|
||||
tell(error, "~p bad request:~n~ts", [?LINE, Received]),
|
||||
fd_httpd_utils:http_err(Socket, 400),
|
||||
fd_http_utils:http_err(Socket, 400),
|
||||
gen_tcp:shutdown(Socket, read_write),
|
||||
exit(normal)
|
||||
end;
|
||||
{tcp_closed, Socket} ->
|
||||
ok = tell("~p Socket closed, retiring.~n", [self()]),
|
||||
ok = io:format("~p Socket closed, retiring.~n", [self()]),
|
||||
exit(normal);
|
||||
{system, From, Request} ->
|
||||
sys:handle_system_msg(Request, From, Parent, ?MODULE, Debug, State);
|
||||
Unexpected ->
|
||||
ok = tell("~p Unexpected message: ~tp", [self(), Unexpected]),
|
||||
ok = io:format("~p Unexpected message: ~tp", [self(), Unexpected]),
|
||||
loop(Parent, Debug, State)
|
||||
end.
|
||||
|
||||
@ -220,44 +210,41 @@ system_replace_state(StateFun, State) ->
|
||||
|
||||
%%% http request handling
|
||||
|
||||
-spec handle_request(Request, Parent, Debug, State) -> no_return()
|
||||
when Request :: request(),
|
||||
Parent :: pid(),
|
||||
Debug :: [sys:dbg_opt()],
|
||||
State :: state().
|
||||
-spec handle_request(Sock, Request, Received) -> NewReceived
|
||||
when Sock :: gen_tcp:socket(),
|
||||
Request :: request(),
|
||||
Received :: binary(),
|
||||
NewReceived :: binary().
|
||||
|
||||
handle_request(Req = #request{method = Method, path = Path}, Parent, Debug, State)
|
||||
when Method =/= undefined,
|
||||
Path =/= undefined ->
|
||||
tell("~tp ~tp ~ts", [self(), Method, Path]),
|
||||
route(Method, Path, Req, Parent, Debug, State).
|
||||
handle_request(Sock, R = #request{method = M, path = P}, Received) when M =/= undefined, P =/= undefined ->
|
||||
tell("~tp ~tp ~ts", [self(), M, P]),
|
||||
route(Sock, M, P, R, Received).
|
||||
|
||||
|
||||
% hardcode routes first
|
||||
route(get, Route, Request, P, D, S = #s{socket = Sock}) ->
|
||||
|
||||
-spec route(Sock, Method, Route, Request, Received) -> NewReceived
|
||||
when Sock :: gen_tcp:socket(),
|
||||
Method :: get | post,
|
||||
Route :: binary(),
|
||||
Request :: request(),
|
||||
Received :: binary(),
|
||||
NewReceived :: binary().
|
||||
|
||||
route(Sock, get, Route, Request, Received) ->
|
||||
case Route of
|
||||
<<"/ws/echo">> -> ws_echo(Sock, Request);
|
||||
<<"/ws/webrtc">> ->
|
||||
ws_webrtc(Request, S);
|
||||
<<"/">> ->
|
||||
route_static(Sock, <<"/index.html">>),
|
||||
loop(P, D, S);
|
||||
_ ->
|
||||
route_static(Sock, Route),
|
||||
loop(P, D, S)
|
||||
<<"/ws/tetris">> -> ws_tetris(Sock, Request, Received);
|
||||
<<"/ws/echo">> -> ws_echo(Sock, Request) , Received;
|
||||
<<"/">> -> route_static(Sock, <<"/index.html">>) , Received;
|
||||
_ -> route_static(Sock, Route) , Received
|
||||
end;
|
||||
route(post, Route, Request, P, D, S = #s{socket = Sock}) ->
|
||||
route(Sock, post, Route, Request, Received) ->
|
||||
case Route of
|
||||
<<"/wfcin">> ->
|
||||
wfcin(Sock, Request),
|
||||
loop(P, D, S);
|
||||
_ ->
|
||||
fd_httpd_utils:http_err(Sock, 404),
|
||||
loop(P, D, S)
|
||||
<<"/wfcin">> -> wfcin(Sock, Request) , Received;
|
||||
_ -> fd_http_utils:http_err(Sock, 404) , Received
|
||||
end;
|
||||
route(_, _, _, P, D, S = #s{socket = Sock}) ->
|
||||
fd_httpd_utils:http_err(Sock, 404),
|
||||
loop(P, D, S).
|
||||
route(Sock, _, _, _, Received) ->
|
||||
fd_http_utils:http_err(Sock, 404),
|
||||
Received.
|
||||
|
||||
|
||||
|
||||
@ -266,13 +253,13 @@ route(_, _, _, P, D, S = #s{socket = Sock}) ->
|
||||
Route :: binary().
|
||||
|
||||
route_static(Sock, Route) ->
|
||||
respond_static(Sock, fd_httpd_sfc:query(Route)).
|
||||
respond_static(Sock, fd_sfc:query(Route)).
|
||||
|
||||
|
||||
|
||||
-spec respond_static(Sock, MaybeEty) -> ok
|
||||
when Sock :: gen_tcp:socket(),
|
||||
MaybeEty :: fd_httpd_sfc:maybe_entry().
|
||||
MaybeEty :: fd_sfc:maybe_entry().
|
||||
|
||||
respond_static(Sock, {found, Entry}) ->
|
||||
% -record(e, {fs_path :: file:filename(),
|
||||
@ -281,80 +268,86 @@ respond_static(Sock, {found, Entry}) ->
|
||||
% encoding :: encoding(),
|
||||
% contents :: binary()}).
|
||||
Headers0 =
|
||||
case fd_httpd_sfc_entry:encoding(Entry) of
|
||||
case fd_sfc_entry:encoding(Entry) of
|
||||
gzip -> [{"content-encoding", "gzip"}];
|
||||
none -> []
|
||||
end,
|
||||
Headers1 = [{"content-type", fd_httpd_sfc_entry:mime_type(Entry)} | Headers0],
|
||||
Headers1 = [{"content-type", fd_sfc_entry:mime_type(Entry)} | Headers0],
|
||||
Response = #response{headers = Headers1,
|
||||
body = fd_httpd_sfc_entry:contents(Entry)},
|
||||
fd_httpd_utils:respond(Sock, Response);
|
||||
body = fd_sfc_entry:contents(Entry)},
|
||||
fd_http_utils:respond(Sock, Response);
|
||||
respond_static(Sock, not_found) ->
|
||||
fd_httpd_utils:http_err(Sock, 404).
|
||||
fd_http_utils:http_err(Sock, 404).
|
||||
|
||||
|
||||
%% ------------------------------
|
||||
%% webrtc
|
||||
%% tetris
|
||||
%% ------------------------------
|
||||
|
||||
ws_webrtc(Request, State = #s{socket = Sock}) ->
|
||||
try
|
||||
case qhl_ws:handshake(Request) of
|
||||
-spec ws_tetris(Sock, Request, Received) -> NewReceived
|
||||
when Sock :: gen_tcp:socket(),
|
||||
Request :: request(),
|
||||
Received :: binary(),
|
||||
NewReceived :: binary().
|
||||
|
||||
ws_tetris(Sock, Request, Received) ->
|
||||
.
|
||||
|
||||
|
||||
|
||||
-spec ws_tetris2(Sock, Request, Received) -> NewReceived
|
||||
when Sock :: gen_tcp:socket(),
|
||||
Request :: request(),
|
||||
Received :: binary(),
|
||||
NewReceived :: binary().
|
||||
|
||||
ws_tetris2(Sock, Request, Received) ->
|
||||
%tell("~p: ws_tetris request: ~tp", [?LINE, Request]),
|
||||
case fd_ws:handshake(Request) of
|
||||
{ok, Response} ->
|
||||
fd_httpd_utils:respond(Sock, Response),
|
||||
% wait for username
|
||||
ok = fd_httpd_webrtc:join(),
|
||||
ws_webrtc_loop(State);
|
||||
fd_http_utils:respond(Sock, Response),
|
||||
{ok, TetrisPid} = fd_tetris:start_link(),
|
||||
ws_tetris_loop(Sock, TetrisPid, [], Received);
|
||||
Error ->
|
||||
tell("ws_webrtc: error: ~tp", [Error]),
|
||||
fd_httpd_utils:http_err(Sock, 400),
|
||||
exit(bad_request)
|
||||
end
|
||||
catch
|
||||
exit:Y:Z ->
|
||||
tell("~tp ws_webrtc: dying peacefully: ~tp:~tp", [self(), Y, Z]),
|
||||
gen_tcp:close(Sock);
|
||||
error:Y:Z ->
|
||||
tell(error, "~tp ws_webrtc: ERROR ~tp:~tp", [self(), Y, Z]),
|
||||
fd_httpd_utils:http_err(Sock, 500),
|
||||
error(internal_server_error)
|
||||
tell("ws_tetris: error: ~tp", [Error]),
|
||||
fd_http_utils:http_err(Sock, 400)
|
||||
end.
|
||||
|
||||
|
||||
-spec ws_webrtc_loop(state()) -> no_return().
|
||||
-spec ws_tetris_loop(Sock, Tetris, Frames, Received) -> NewReceived
|
||||
when Sock :: gen_tcp:socket(),
|
||||
Tetris :: pid(),
|
||||
Frames :: [fd_ws:frame()],
|
||||
Received :: binary(),
|
||||
NewReceived :: binary().
|
||||
|
||||
% we have no tcp bytes waiting to be parsed
|
||||
ws_webrtc_loop(State = #s{socket = Socket, next = <<>>}) ->
|
||||
ok = inet:setopts(Socket, [{active, once}]),
|
||||
ws_tetris_loop(Sock, Tetris, Frames, Received) ->
|
||||
tell("~p:ws_tetris_loop(Sock, ~p, ~p, ~p)", [?MODULE, Tetris, Frames, Received]),
|
||||
%% create tetris state
|
||||
case inet:setopts(Sock, [{active, once}]) of
|
||||
ok ->
|
||||
receive
|
||||
{tcp, Socket, Message} ->
|
||||
NewState = State#s{next = Message},
|
||||
ws_webrtc_loop(NewState);
|
||||
{webrtc, Message} ->
|
||||
ok = ws_webrtc_relay(Socket, s2c, Message),
|
||||
ws_webrtc_loop(State);
|
||||
{tcp_closed, Socket} ->
|
||||
ok = tell("~p Socket closed, retiring.~n", [self()]),
|
||||
exit(normal)
|
||||
{tcp, Sock, Bin} ->
|
||||
Rcv1 = <<Received/binary, Bin/binary>>,
|
||||
case fd_ws:recv(Sock, Rcv1, 3_000, Frames) of
|
||||
{ok, WsMsg, NewFrames, Rcv2} ->
|
||||
ok = fd_tetris:ws_msg(Tetris, WsMsg),
|
||||
ws_tetris_loop(Sock, Tetris, NewFrames, Rcv2);
|
||||
Error ->
|
||||
error(Error)
|
||||
end;
|
||||
% we have TCP bytes sitting and waiting to be parsed
|
||||
ws_webrtc_loop(State = #s{socket = Sock, next = Recv}) ->
|
||||
{ok, Message, NewRecv} = qhl_ws:recv_json(Sock, Recv, 5000),
|
||||
ok = ws_webrtc_relay(Sock, c2s, Message),
|
||||
ws_webrtc_loop(State#s{next = NewRecv}).
|
||||
|
||||
|
||||
% relay
|
||||
% message is a zj:value()
|
||||
ws_webrtc_relay(Socket, Direction, ZJValue) ->
|
||||
tell("~p ws_webrtc_relay ~p: ~p", [self(), Direction, ZJValue]),
|
||||
case Direction of
|
||||
c2s -> fd_httpd_webrtc:hi(ZJValue);
|
||||
s2c -> qhl_ws:send_json(Socket, ZJValue)
|
||||
end,
|
||||
ok.
|
||||
|
||||
|
||||
{tetris, Message} ->
|
||||
ok = log(info, "~p tetris: ~p", [self(), Message]),
|
||||
ok = fd_ws:send(Sock, {text, Message}),
|
||||
ws_tetris_loop(Sock, Tetris, Frames, Received);
|
||||
{tcp_closed, Sock} -> {error, tcp_closed};
|
||||
{tcp_error, Sock, Reason} -> {error, {tcp_error, Reason}}
|
||||
after 30_000 ->
|
||||
{error, timeout}
|
||||
end;
|
||||
{error, Reason} ->
|
||||
{error, {inet, Reason}}
|
||||
end.
|
||||
|
||||
%% ------------------------------
|
||||
%% echo
|
||||
@ -366,17 +359,17 @@ ws_echo(Sock, Request) ->
|
||||
catch
|
||||
X:Y:Z ->
|
||||
tell(error, "CRASH ws_echo: ~tp:~tp:~tp", [X, Y, Z]),
|
||||
fd_httpd_utils:http_err(Sock, 500)
|
||||
fd_http_utils:http_err(Sock, 500)
|
||||
end.
|
||||
|
||||
ws_echo2(Sock, Request) ->
|
||||
case qhl_ws:handshake(Request) of
|
||||
case fd_ws:handshake(Request) of
|
||||
{ok, Response} ->
|
||||
fd_httpd_utils:respond(Sock, Response),
|
||||
fd_http_utils:respond(Sock, Response),
|
||||
ws_echo_loop(Sock);
|
||||
Error ->
|
||||
tell("ws_echo: error: ~tp", [Error]),
|
||||
fd_httpd_utils:http_err(Sock, 400)
|
||||
fd_http_utils:http_err(Sock, 400)
|
||||
end.
|
||||
|
||||
ws_echo_loop(Sock) ->
|
||||
@ -384,15 +377,15 @@ ws_echo_loop(Sock) ->
|
||||
|
||||
ws_echo_loop(Sock, Frames, Received) ->
|
||||
tell("~p ws_echo_loop(Sock, ~tp, ~tp)", [self(), Frames, Received]),
|
||||
case qhl_ws:recv(Sock, Received, 5*qhl_ws:min(), Frames) of
|
||||
case fd_ws:recv(Sock, Received, 5*fd_ws:min(), Frames) of
|
||||
{ok, Message, NewFrames, NewReceived} ->
|
||||
tell("~p echo message: ~tp", [self(), Message]),
|
||||
% send the same message back
|
||||
ok = qhl_ws:send(Sock, Message),
|
||||
ok = fd_ws:send(Sock, Message),
|
||||
ws_echo_loop(Sock, NewFrames, NewReceived);
|
||||
Error ->
|
||||
tell(error, "ws_echo_loop: error: ~tp", [Error]),
|
||||
qhl_ws:send(Sock, {close, <<>>}),
|
||||
fd_ws:send(Sock, {close, <<>>}),
|
||||
error(Error)
|
||||
end.
|
||||
|
||||
@ -412,28 +405,28 @@ wfcin(Sock, #request{enctype = json,
|
||||
case wfc_read:expr(Input) of
|
||||
{ok, Expr, _Rest} ->
|
||||
case wfc_eval:eval(Expr, Ctx0) of
|
||||
{ok, noop, Ctx1} -> {fd_httpd_utils:jsgud("<noop>"), Ctx1};
|
||||
{ok, Sentence, Ctx1} -> {fd_httpd_utils:jsgud(wfc_pp:sentence(Sentence)), Ctx1};
|
||||
{error, Message} -> {fd_httpd_utils:jsbad(Message), Ctx0}
|
||||
{ok, noop, Ctx1} -> {fd_http_utils:jsgud("<noop>"), Ctx1};
|
||||
{ok, Sentence, Ctx1} -> {fd_http_utils:jsgud(wfc_pp:sentence(Sentence)), Ctx1};
|
||||
{error, Message} -> {fd_http_utils:jsbad(Message), Ctx0}
|
||||
end;
|
||||
{error, Message} ->
|
||||
{fd_httpd_utils:jsbad(Message), Ctx0}
|
||||
{fd_http_utils:jsbad(Message), Ctx0}
|
||||
end
|
||||
catch
|
||||
error:E:S ->
|
||||
ErrorMessage = unicode:characters_to_list(io_lib:format("parser crashed: ~p:~p", [E, S])),
|
||||
{fd_httpd_utils:jsbad(ErrorMessage), Ctx0}
|
||||
{fd_http_utils:jsbad(ErrorMessage), Ctx0}
|
||||
end,
|
||||
% update cache with new context
|
||||
ok = fd_wfcd_cache:set(Cookie, NewCtx),
|
||||
ok = fd_cache:set(Cookie, NewCtx),
|
||||
Body = zj:encode(RespObj),
|
||||
Response = #response{headers = [{"content-type", "application/json"},
|
||||
{"set-cookie", ["wfc=", Cookie]}],
|
||||
body = Body},
|
||||
fd_httpd_utils:respond(Sock, Response);
|
||||
fd_http_utils:respond(Sock, Response);
|
||||
wfcin(Sock, Request) ->
|
||||
tell("wfcin: bad request: ~tp", [Request]),
|
||||
fd_httpd_utils:http_err(Sock, 400).
|
||||
fd_http_utils:http_err(Sock, 400).
|
||||
|
||||
|
||||
|
||||
@ -443,9 +436,17 @@ wfcin(Sock, Request) ->
|
||||
Context :: wfc_eval_context:context().
|
||||
|
||||
ctx(#{<<"wfc">> := Cookie}) ->
|
||||
case fd_wfcd_cache:query(Cookie) of
|
||||
case fd_cache:query(Cookie) of
|
||||
{ok, Context} -> {Cookie, Context};
|
||||
error -> {Cookie, wfc_eval_context:default()}
|
||||
end;
|
||||
ctx(_) ->
|
||||
{fd_httpd_utils:new_cookie(), wfc_eval_context:default()}.
|
||||
{fd_http_utils:new_cookie(), wfc_eval_context:default()}.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -9,8 +9,8 @@
|
||||
%%% OTP should take care of for us.
|
||||
%%% @end
|
||||
|
||||
-module(fd_httpd_client_man).
|
||||
-vsn("0.2.0").
|
||||
-module(fd_client_man).
|
||||
-vsn("0.1.0").
|
||||
-behavior(gen_server).
|
||||
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
-copyright("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
@ -23,8 +23,6 @@
|
||||
code_change/3, terminate/2]).
|
||||
|
||||
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
|
||||
%%% Type and Record Definitions
|
||||
|
||||
|
||||
@ -94,7 +92,7 @@ echo(Message) ->
|
||||
when Result :: {ok, pid()}
|
||||
| {error, Reason :: term()}.
|
||||
%% @private
|
||||
%% This should only ever be called by fd_httpd_clients (the service-level supervisor).
|
||||
%% This should only ever be called by fd_clients (the service-level supervisor).
|
||||
|
||||
start_link() ->
|
||||
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
|
||||
@ -106,9 +104,8 @@ start_link() ->
|
||||
%% preparatory work necessary for proper function.
|
||||
|
||||
init(none) ->
|
||||
ok = tell("Starting fd_httpd_client_man."),
|
||||
ok = io:format("Starting.~n"),
|
||||
State = #s{},
|
||||
ok = tell("fd_httpd_client_man init state: ~tp", [State]),
|
||||
{ok, State}.
|
||||
|
||||
|
||||
@ -133,7 +130,7 @@ handle_call({listen, PortNum}, _, State) ->
|
||||
{Response, NewState} = do_listen(PortNum, State),
|
||||
{reply, Response, NewState};
|
||||
handle_call(Unexpected, From, State) ->
|
||||
ok = tell("~p Unexpected call from ~tp: ~tp~n", [self(), From, Unexpected]),
|
||||
ok = io:format("~p Unexpected call from ~tp: ~tp~n", [self(), From, Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
@ -155,7 +152,7 @@ handle_cast(ignore, State) ->
|
||||
NewState = do_ignore(State),
|
||||
{noreply, NewState};
|
||||
handle_cast(Unexpected, State) ->
|
||||
ok = tell("~p Unexpected cast: ~tp~n", [self(), Unexpected]),
|
||||
ok = io:format("~p Unexpected cast: ~tp~n", [self(), Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
@ -171,7 +168,7 @@ handle_info({'DOWN', Mon, process, Pid, Reason}, State) ->
|
||||
NewState = handle_down(Mon, Pid, Reason, State),
|
||||
{noreply, NewState};
|
||||
handle_info(Unexpected, State) ->
|
||||
ok = tell("~p Unexpected info: ~tp~n", [self(), Unexpected]),
|
||||
ok = io:format("~p Unexpected info: ~tp~n", [self(), Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
@ -228,10 +225,10 @@ do_listen(PortNum, State = #s{port_num = none}) ->
|
||||
{keepalive, true},
|
||||
{reuseaddr, true}],
|
||||
{ok, Listener} = gen_tcp:listen(PortNum, SocketOptions),
|
||||
{ok, _} = fd_httpd_client:start(Listener),
|
||||
{ok, _} = fd_client:start(Listener),
|
||||
{ok, State#s{port_num = PortNum, listener = Listener}};
|
||||
do_listen(_, State = #s{port_num = PortNum}) ->
|
||||
ok = tell("~p Already listening on ~p~n", [self(), PortNum]),
|
||||
ok = io:format("~p Already listening on ~p~n", [self(), PortNum]),
|
||||
{{error, {listening, PortNum}}, State}.
|
||||
|
||||
|
||||
@ -257,7 +254,7 @@ do_enroll(Pid, State = #s{clients = Clients}) ->
|
||||
case lists:member(Pid, Clients) of
|
||||
false ->
|
||||
Mon = monitor(process, Pid),
|
||||
ok = tell("Monitoring ~tp @ ~tp~n", [Pid, Mon]),
|
||||
ok = io:format("Monitoring ~tp @ ~tp~n", [Pid, Mon]),
|
||||
State#s{clients = [Pid | Clients]};
|
||||
true ->
|
||||
State
|
||||
@ -295,6 +292,6 @@ handle_down(Mon, Pid, Reason, State = #s{clients = Clients}) ->
|
||||
State#s{clients = NewClients};
|
||||
false ->
|
||||
Unexpected = {'DOWN', Mon, process, Pid, Reason},
|
||||
ok = tell("~p Unexpected info: ~tp~n", [self(), Unexpected]),
|
||||
ok = io:format("~p Unexpected info: ~tp~n", [self(), Unexpected]),
|
||||
State
|
||||
end.
|
||||
@ -2,8 +2,8 @@
|
||||
%%% front end web development lab Client Supervisor
|
||||
%%%
|
||||
%%% This process supervises the client socket handlers themselves. It is a peer of the
|
||||
%%% fd_httpd_client_man (the manager interface to this network service component),
|
||||
%%% and a child of the supervisor named fd_httpd_clients.
|
||||
%%% fd_client_man (the manager interface to this network service component),
|
||||
%%% and a child of the supervisor named fd_clients.
|
||||
%%%
|
||||
%%% Because we don't know (or care) how many client connections the server may end up
|
||||
%%% handling this is a simple_one_for_one supervisor which can spawn and manage as
|
||||
@ -13,8 +13,8 @@
|
||||
%%% http://erlang.org/doc/design_principles/sup_princ.html#id79244
|
||||
%%% @end
|
||||
|
||||
-module(fd_httpd_client_sup).
|
||||
-vsn("0.2.0").
|
||||
-module(fd_client_sup).
|
||||
-vsn("0.1.0").
|
||||
-behaviour(supervisor).
|
||||
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
-copyright("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
@ -35,9 +35,9 @@
|
||||
| {shutdown, term()}
|
||||
| term().
|
||||
%% @private
|
||||
%% Spawns the first listener at the request of the fd_httpd_client_man when
|
||||
%% Spawns the first listener at the request of the fd_client_man when
|
||||
%% fewd:listen/1 is called, or the next listener at the request of the
|
||||
%% currently listening fd_httpd_client when a connection is made.
|
||||
%% currently listening fd_client when a connection is made.
|
||||
%%
|
||||
%% Error conditions, supervision strategies and other important issues are
|
||||
%% explained in the supervisor module docs:
|
||||
@ -61,10 +61,10 @@ start_link() ->
|
||||
|
||||
init(none) ->
|
||||
RestartStrategy = {simple_one_for_one, 1, 60},
|
||||
Client = {fd_httpd_client,
|
||||
{fd_httpd_client, start_link, []},
|
||||
Client = {fd_client,
|
||||
{fd_client, start_link, []},
|
||||
temporary,
|
||||
brutal_kill,
|
||||
worker,
|
||||
[fd_httpd_client]},
|
||||
[fd_client]},
|
||||
{ok, {RestartStrategy, [Client]}}.
|
||||
@ -8,8 +8,8 @@
|
||||
%%% See: http://erlang.org/doc/apps/kernel/application.html
|
||||
%%% @end
|
||||
|
||||
-module(fd_httpd_clients).
|
||||
-vsn("0.2.0").
|
||||
-module(fd_clients).
|
||||
-vsn("0.1.0").
|
||||
-behavior(supervisor).
|
||||
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
-copyright("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
@ -32,17 +32,17 @@ start_link() ->
|
||||
|
||||
init(none) ->
|
||||
RestartStrategy = {rest_for_one, 1, 60},
|
||||
HttpClientMan = {fd_httpd_client_man,
|
||||
{fd_httpd_client_man, start_link, []},
|
||||
ClientMan = {fd_client_man,
|
||||
{fd_client_man, start_link, []},
|
||||
permanent,
|
||||
5000,
|
||||
worker,
|
||||
[fd_httpd_client_man]},
|
||||
HttpClientSup = {fd_httpd_client_sup,
|
||||
{fd_httpd_client_sup, start_link, []},
|
||||
[fd_client_man]},
|
||||
ClientSup = {fd_client_sup,
|
||||
{fd_client_sup, start_link, []},
|
||||
permanent,
|
||||
5000,
|
||||
supervisor,
|
||||
[fd_httpd_client_sup]},
|
||||
Children = [HttpClientSup, HttpClientMan],
|
||||
[fd_client_sup]},
|
||||
Children = [ClientSup, ClientMan],
|
||||
{ok, {RestartStrategy, Children}}.
|
||||
@ -1,6 +1,5 @@
|
||||
% @doc http utility functions
|
||||
-module(fd_httpd_utils).
|
||||
-vsn("0.2.0").
|
||||
-module(fd_http_utils).
|
||||
|
||||
-export([
|
||||
new_cookie/0,
|
||||
@ -8,10 +7,7 @@
|
||||
http_err/2,
|
||||
respond/2,
|
||||
fmtresp/1
|
||||
]).
|
||||
|
||||
-include("http.hrl").
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
])
|
||||
|
||||
|
||||
-spec new_cookie() -> Cookie
|
||||
@ -114,6 +110,6 @@ add_headers(Hs, Body) ->
|
||||
|
||||
default_headers(Body) ->
|
||||
BodySize = byte_size(iolist_to_binary(Body)),
|
||||
#{"Server" => "fewd 0.2.0",
|
||||
#{"Server" => "fewd 0.1.0",
|
||||
"Date" => qhl:ridiculous_web_date(),
|
||||
"Content-Length" => io_lib:format("~p", [BodySize])}.
|
||||
@ -1,46 +0,0 @@
|
||||
-module(fd_httpd).
|
||||
-vsn("0.2.0").
|
||||
-behaviour(supervisor).
|
||||
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
-copyright("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
-license("BSD-2-Clause-FreeBSD").
|
||||
|
||||
-export([start_link/0]).
|
||||
-export([init/1]).
|
||||
|
||||
|
||||
-spec start_link() -> {ok, pid()}.
|
||||
%% @private
|
||||
%% This supervisor's own start function.
|
||||
|
||||
start_link() ->
|
||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||
|
||||
|
||||
-spec init([]) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
|
||||
%% @private
|
||||
%% The OTP init/1 function.
|
||||
|
||||
init([]) ->
|
||||
RestartStrategy = {one_for_one, 1, 60},
|
||||
WebRTC = {fd_httpd_webrtc,
|
||||
{fd_httpd_webrtc, start_link, []},
|
||||
permanent,
|
||||
5000,
|
||||
worker,
|
||||
[fd_httpd_webrtc]},
|
||||
FileCache = {fd_httpd_sfc,
|
||||
{fd_httpd_sfc, start_link, []},
|
||||
permanent,
|
||||
5000,
|
||||
worker,
|
||||
[fd_httpd_sfc]},
|
||||
Clients = {fd_httpd_clients,
|
||||
{fd_httpd_clients, start_link, []},
|
||||
permanent,
|
||||
5000,
|
||||
supervisor,
|
||||
[fd_httpd_clients]},
|
||||
Children = [WebRTC, FileCache, Clients],
|
||||
%Children = [FileCache, Clients],
|
||||
{ok, {RestartStrategy, Children}}.
|
||||
@ -1,165 +0,0 @@
|
||||
% @doc webrtc peer pool
|
||||
-module(fd_httpd_webrtc).
|
||||
|
||||
-behavior(gen_server).
|
||||
|
||||
%% api (caller context)
|
||||
-export([
|
||||
join/0, hi/1
|
||||
]).
|
||||
|
||||
% startup/gen_server callbacks
|
||||
-export([
|
||||
% caller context
|
||||
start_link/0,
|
||||
% process context
|
||||
init/1, handle_call/3, handle_cast/2, handle_info/2,
|
||||
code_change/3, terminate/2
|
||||
]).
|
||||
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
|
||||
|
||||
-record(u, {pid :: pid()}).
|
||||
-type user() :: #u{}.
|
||||
|
||||
-record(s, {roster = [] :: [user()]}).
|
||||
-type state() :: #s{}.
|
||||
|
||||
|
||||
%%-----------------------------------------------------------------------------
|
||||
%% caller context
|
||||
%%-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
start_link() ->
|
||||
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
|
||||
|
||||
|
||||
|
||||
-spec join() -> ok | {error, any()}.
|
||||
|
||||
join() ->
|
||||
gen_server:call(?MODULE, join).
|
||||
|
||||
|
||||
|
||||
-spec hi(Message) -> ok
|
||||
when Message :: zj:value().
|
||||
|
||||
hi(Message) ->
|
||||
gen_server:cast(?MODULE, {hi, self(), Message}).
|
||||
|
||||
|
||||
|
||||
%%-----------------------------------------------------------------------------
|
||||
%% process context
|
||||
%%-----------------------------------------------------------------------------
|
||||
|
||||
%% gen_server callbacks
|
||||
|
||||
init(none) ->
|
||||
tell("starting fd_httpd_webrtc", []),
|
||||
InitState = #s{},
|
||||
{ok, InitState}.
|
||||
|
||||
|
||||
handle_call(join, _From = {PID, _Tag}, State) ->
|
||||
{Reply, NewState} = do_join(PID, State),
|
||||
{reply, Reply, NewState};
|
||||
handle_call(Unexpected, From, State) ->
|
||||
tell("~tp: unexpected call from ~tp: ~tp", [?MODULE, Unexpected, From]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
handle_cast(Unexpected, State) ->
|
||||
tell("~tp: unexpected cast: ~tp", [?MODULE, Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
handle_info({'DOWN', _Ref, process, PID, _Reason}, State) ->
|
||||
NewState = do_down(PID, State),
|
||||
{noreply, NewState};
|
||||
handle_info(Unexpected, State) ->
|
||||
tell("~tp: unexpected info: ~tp", [?MODULE, Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
code_change(_, State, _) ->
|
||||
{ok, State}.
|
||||
|
||||
terminate(_, _) ->
|
||||
ok.
|
||||
|
||||
|
||||
%%---------------------
|
||||
%% doers
|
||||
%%---------------------
|
||||
|
||||
-spec do_join(PID, State) -> {Reply, NewState}
|
||||
when PID :: pid(),
|
||||
State :: state(),
|
||||
Reply :: ok
|
||||
| {error, any()},
|
||||
NewState :: state().
|
||||
% @private join a user to a pool
|
||||
|
||||
do_join(PID, State = #s{roster = Users}) ->
|
||||
% see if pid is already there
|
||||
case lists:keymember(PID, #u.pid, Users) of
|
||||
true -> {error, already_joined};
|
||||
false -> do_join2(PID, State)
|
||||
end.
|
||||
|
||||
do_join2(PID, State = #s{roster = Users}) ->
|
||||
monitor(process, PID),
|
||||
NewUser = #u{pid = PID},
|
||||
NewRoster = usort([NewUser | Users]),
|
||||
NewState = State#s{roster = NewRoster},
|
||||
ok = whisper(whoami, PID),
|
||||
ok = gossip(roster, NewState),
|
||||
{ok, NewState}.
|
||||
|
||||
usort(Users) ->
|
||||
lists:keysort(#u.pid, Users).
|
||||
|
||||
|
||||
-spec do_down(PID, State) -> NewState
|
||||
when PID :: pid(),
|
||||
State :: state(),
|
||||
NewState :: state().
|
||||
% @private handle a user dying
|
||||
|
||||
do_down(PID, State = #s{roster = Users}) ->
|
||||
% remove from roster
|
||||
NewUsers = lists:keydelete(PID, #u.pid, Users),
|
||||
NewState = State#s{roster = NewUsers},
|
||||
% broadcast username
|
||||
ok = gossip(roster, NewState),
|
||||
NewState.
|
||||
|
||||
|
||||
% @doc send a message to a specific person
|
||||
whisper(whoami, PID) ->
|
||||
PID ! {webrtc, ["whoami", pidstr(PID)]},
|
||||
ok.
|
||||
|
||||
|
||||
% @doc send a message to everyone
|
||||
gossip(roster, State = #s{roster = Us}) ->
|
||||
Pids = [pidstr(PID) || #u{pid = PID} <- Us],
|
||||
gossip_json(["roster", Pids], State).
|
||||
|
||||
pidstr(PID) ->
|
||||
unicode:characters_to_list(io_lib:format("~tp", [PID])).
|
||||
|
||||
|
||||
-spec gossip_json(zj:value(), state()) -> ok.
|
||||
% @private gossip a message to everyone
|
||||
|
||||
gossip_json(Message, #s{roster = Users}) ->
|
||||
GossipTo =
|
||||
fun(#u{pid = PID}) ->
|
||||
PID ! {webrtc, Message}
|
||||
end,
|
||||
lists:foreach(GossipTo, Users).
|
||||
@ -1,6 +1,5 @@
|
||||
% @doc static file cache
|
||||
-module(fd_httpd_sfc).
|
||||
-vsn("0.2.0").
|
||||
-module(fd_sfc).
|
||||
|
||||
-behavior(gen_server).
|
||||
|
||||
@ -22,12 +21,12 @@
|
||||
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
|
||||
-type entry() :: fd_httpd_sfc_entry:entry().
|
||||
-type maybe_entry() :: {found, fd_httpd_sfc_entry:entry()} | not_found.
|
||||
-type entry() :: fd_sfc_entry:entry().
|
||||
-type maybe_entry() :: {found, fd_sfc_entry:entry()} | not_found.
|
||||
|
||||
|
||||
-record(s, {base_path = base_path() :: file:filename(),
|
||||
cache = fd_httpd_sfc_cache:new(base_path()) :: fd_httpd_sfc_cache:cache(),
|
||||
cache = fd_sfc_cache:new(base_path()) :: fd_sfc_cache:cache(),
|
||||
auto_renew = 0_500 :: pos_integer()}).
|
||||
%-type state() :: #s{}.
|
||||
|
||||
@ -63,14 +62,14 @@ start_link() ->
|
||||
%% gen_server callbacks
|
||||
|
||||
init(none) ->
|
||||
tell("starting fd_httpd_sfc"),
|
||||
tell("starting fd_sfc"),
|
||||
InitState = #s{},
|
||||
erlang:send_after(InitState#s.auto_renew, self(), auto_renew),
|
||||
{ok, InitState}.
|
||||
|
||||
|
||||
handle_call({query, Path}, _, State = #s{cache = Cache}) ->
|
||||
Reply = fd_httpd_sfc_cache:query(Path, Cache),
|
||||
Reply = fd_sfc_cache:query(Path, Cache),
|
||||
{reply, Reply, State};
|
||||
handle_call(Unexpected, From, State) ->
|
||||
tell("~tp: unexpected call from ~tp: ~tp", [?MODULE, Unexpected, From]),
|
||||
@ -107,6 +106,6 @@ terminate(_, _) ->
|
||||
%%-----------------------------------------------------------------------------
|
||||
|
||||
i_renew(State = #s{base_path = BasePath}) ->
|
||||
NewCache = fd_httpd_sfc_cache:new(BasePath),
|
||||
NewCache = fd_sfc_cache:new(BasePath),
|
||||
NewState = State#s{cache = NewCache},
|
||||
NewState.
|
||||
@ -1,7 +1,6 @@
|
||||
% @doc
|
||||
% cache data management
|
||||
-module(fd_httpd_sfc_cache).
|
||||
-vsn("0.2.0").
|
||||
-module(fd_sfc_cache).
|
||||
|
||||
-export_type([
|
||||
cache/0
|
||||
@ -14,7 +13,7 @@
|
||||
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
|
||||
-type cache() :: #{HttpPath :: binary() := Entry :: fd_httpd_sfc_entry:entry()}.
|
||||
-type cache() :: #{HttpPath :: binary() := Entry :: fd_sfc_entry:entry()}.
|
||||
|
||||
|
||||
-spec query(HttpPath, Cache) -> Result
|
||||
@ -22,7 +21,7 @@
|
||||
Cache :: cache(),
|
||||
Result :: {found, Entry}
|
||||
| not_found,
|
||||
Entry :: fd_httpd_sfc_entry:entry().
|
||||
Entry :: fd_sfc_entry:entry().
|
||||
|
||||
query(HttpPath, Cache) ->
|
||||
case maps:find(HttpPath, Cache) of
|
||||
@ -64,7 +63,7 @@ new2(BasePath) ->
|
||||
BAbsPath = unicode:characters_to_binary(AbsPath),
|
||||
HttpPath = remove_prefix(BBaseDir, BAbsPath),
|
||||
NewCache =
|
||||
case fd_httpd_sfc_entry:new(AbsPath) of
|
||||
case fd_sfc_entry:new(AbsPath) of
|
||||
{found, Entry} -> maps:put(HttpPath, Entry, AccCache);
|
||||
not_found -> AccCache
|
||||
end,
|
||||
@ -1,8 +1,7 @@
|
||||
% @doc non-servery functions for static file caching
|
||||
%
|
||||
% this spams the filesystem, so it's not "pure" code
|
||||
-module(fd_httpd_sfc_entry).
|
||||
-vsn("0.2.0").
|
||||
-module(fd_sfc_entry).
|
||||
|
||||
-export_type([
|
||||
encoding/0,
|
||||
@ -12,7 +12,7 @@
|
||||
%%% @end
|
||||
|
||||
-module(fd_sup).
|
||||
-vsn("0.2.0").
|
||||
-vsn("0.1.0").
|
||||
-behaviour(supervisor).
|
||||
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
-copyright("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
@ -36,17 +36,29 @@ start_link() ->
|
||||
|
||||
init([]) ->
|
||||
RestartStrategy = {one_for_one, 1, 60},
|
||||
WFCd = {fd_wfcd,
|
||||
{fd_wfcd, start_link, []},
|
||||
Clients = {fd_clients,
|
||||
{fd_clients, start_link, []},
|
||||
permanent,
|
||||
5000,
|
||||
supervisor,
|
||||
[fd_wfcd]},
|
||||
Httpd = {fd_httpd,
|
||||
{fd_httpd, start_link, []},
|
||||
[fd_clients]},
|
||||
Chat = {fd_chat,
|
||||
{fd_chat, start_link, []},
|
||||
permanent,
|
||||
5000,
|
||||
supervisor,
|
||||
[fd_httpd]},
|
||||
Children = [WFCd, Httpd],
|
||||
worker,
|
||||
[fd_chat]},
|
||||
FileCache = {fd_sfc,
|
||||
{fd_sfc, start_link, []},
|
||||
permanent,
|
||||
5000,
|
||||
worker,
|
||||
[fd_sfc]},
|
||||
Cache = {fd_cache,
|
||||
{fd_cache, start_link, []},
|
||||
permanent,
|
||||
5000,
|
||||
worker,
|
||||
[fd_cache]},
|
||||
Children = [Clients, Chat, FileCache, Cache],
|
||||
{ok, {RestartStrategy, Children}}.
|
||||
|
||||
88
src/fd_tetris.erl
Normal file
88
src/fd_tetris.erl
Normal file
@ -0,0 +1,88 @@
|
||||
% @doc tetris
|
||||
%
|
||||
% manages state for a single game of tetris
|
||||
%
|
||||
% sends parent process messages `{tetris, String}` where String is an encoded
|
||||
% JSON blob meant to be sent to the page script in /priv/static/js/ts/tetris.ts
|
||||
%
|
||||
% Refs:
|
||||
% 1. https://www.erlang.org/docs/24/man/gen_server
|
||||
-module(fd_tetris).
|
||||
|
||||
-behavior(gen_server).
|
||||
|
||||
-export([
|
||||
%% caller context
|
||||
start_link/0,
|
||||
%% process context
|
||||
%% gen_server callbacks
|
||||
init/1, handle_call/3, handle_cast/2, handle_info/2,
|
||||
code_change/3, terminate/2
|
||||
]).
|
||||
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
|
||||
|
||||
-record(s, {parent :: pid()}).
|
||||
|
||||
-type state() :: #s{}.
|
||||
|
||||
%%-----------------------------------------------------------------------------
|
||||
%% caller context below this line
|
||||
%%-----------------------------------------------------------------------------
|
||||
|
||||
-spec ws_msg(Tetris, Message) -> ok
|
||||
when Tetris :: pid(),
|
||||
Message :: fd_ws:ws_msg().
|
||||
|
||||
ws_msg(Tetris, Msg) ->
|
||||
gen_server:cast(Tetris, {ws_msg, Msg}).
|
||||
|
||||
|
||||
-spec start_link() -> {ok, pid()} | {error, term()}.
|
||||
start_link() ->
|
||||
gen_server:start_link(?MODULE, [self()], []).
|
||||
|
||||
|
||||
%%-----------------------------------------------------------------------------
|
||||
%% process context below this line
|
||||
%%-----------------------------------------------------------------------------
|
||||
|
||||
%% gen_server callbacks
|
||||
|
||||
-spec init(Args) -> {ok, State}
|
||||
when Args :: [Parent],
|
||||
Parent :: pid(),
|
||||
State :: state().
|
||||
|
||||
init([Parent]) ->
|
||||
tell("~tp:~tp starting fd_tetris with parent ~p", [?MODULE, self(), Parent]),
|
||||
self() ! {poop, 0},
|
||||
InitState = #s{parent = Parent},
|
||||
{ok, InitState}.
|
||||
|
||||
|
||||
handle_call(Unexpected, From, State) ->
|
||||
tell("~tp:~tp unexpected call from ~tp: ~tp", [?MODULE, self(), From, Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
handle_cast(Unexpected, State) ->
|
||||
tell("~tp:~tp unexpected cast: ~tp", [?MODULE, self(), Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
handle_info({poop, N}, State = #s{parent = Parent}) ->
|
||||
Parent ! {tetris, io_lib:format("poop~p", [N])},
|
||||
erlang:send_after(1_000, self(), {poop, N+1}),
|
||||
{noreply, State};
|
||||
handle_info(Unexpected, State) ->
|
||||
tell("~tp:~tp unexpected info: ~tp", [?MODULE, self(), Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
code_change(_, State, _) ->
|
||||
{ok, State}.
|
||||
|
||||
terminate(_, _) ->
|
||||
ok.
|
||||
@ -1,33 +0,0 @@
|
||||
-module(fd_wfcd).
|
||||
-vsn("0.2.0").
|
||||
-behaviour(supervisor).
|
||||
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
-copyright("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
-license("BSD-2-Clause-FreeBSD").
|
||||
|
||||
-export([start_link/0]).
|
||||
-export([init/1]).
|
||||
|
||||
|
||||
-spec start_link() -> {ok, pid()}.
|
||||
%% @private
|
||||
%% This supervisor's own start function.
|
||||
|
||||
start_link() ->
|
||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||
|
||||
|
||||
-spec init([]) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
|
||||
%% @private
|
||||
%% The OTP init/1 function.
|
||||
|
||||
init([]) ->
|
||||
RestartStrategy = {one_for_one, 1, 60},
|
||||
Cache = {fd_wfcd_cache,
|
||||
{fd_wfcd_cache, start_link, []},
|
||||
permanent,
|
||||
5000,
|
||||
worker,
|
||||
[fd_wfcd_cache]},
|
||||
Children = [Cache],
|
||||
{ok, {RestartStrategy, Children}}.
|
||||
@ -1,8 +1,7 @@
|
||||
% @doc websockets
|
||||
%
|
||||
% ref: https://datatracker.ietf.org/doc/html/rfc6455
|
||||
-module(qhl_ws).
|
||||
-vsn("0.2.0").
|
||||
-module(fd_ws).
|
||||
|
||||
-export_type([
|
||||
opcode/0,
|
||||
@ -16,12 +15,11 @@
|
||||
%% porcelain
|
||||
handshake/1,
|
||||
recv/3, recv/4,
|
||||
recv_strict/3, recv_json/3,
|
||||
send/2,
|
||||
send_dwim/2, send_json/2
|
||||
send/2
|
||||
]).
|
||||
|
||||
-include("http.hrl").
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
|
||||
|
||||
-define(MAX_PAYLOAD_SIZE, ((1 bsl 63) - 1)).
|
||||
@ -261,54 +259,6 @@ response_token(ChallengeToken) when is_binary(ChallengeToken) ->
|
||||
base64:encode(Sha1).
|
||||
|
||||
|
||||
-spec recv_json(Socket, Received, TimeoutMS) -> Result
|
||||
when Socket :: gen_tcp:socket(),
|
||||
Received :: binary(),
|
||||
TimeoutMS :: non_neg_integer(),
|
||||
Result :: {ok, zj:value(), Remainder}
|
||||
| {error, Reason},
|
||||
Remainder :: binary(),
|
||||
Reason :: any().
|
||||
% @doc
|
||||
% asserts response is text
|
||||
|
||||
recv_json(Sock, Recv, TimeoutMS) ->
|
||||
case recv_strict(Sock, Recv, TimeoutMS) of
|
||||
{ok, {Type, Payload}, NewRecv} when Type =:= text orelse
|
||||
Type =:= binary ->
|
||||
case zj:decode(Payload) of
|
||||
{ok, Value} ->
|
||||
io:format("~p value: ~p~n", [self(), Value]),
|
||||
{ok, Value, NewRecv};
|
||||
Error -> {error, {zj_decode, Error}}
|
||||
end;
|
||||
Error ->
|
||||
Error
|
||||
end.
|
||||
|
||||
|
||||
-spec recv_strict(Socket, Received, TimeoutMS) -> Result
|
||||
when Socket :: gen_tcp:socket(),
|
||||
Received :: binary(),
|
||||
TimeoutMS :: non_neg_integer(),
|
||||
Result :: {ok, Message, Remainder}
|
||||
| {error, Reason},
|
||||
Message :: ws_msg(),
|
||||
Remainder :: binary(),
|
||||
Reason :: any().
|
||||
% @doc
|
||||
% Almost equivalent to recv/3, but asserts resulting frames are empty
|
||||
|
||||
recv_strict(Sock, Recv, TimeoutMS) ->
|
||||
case recv(Sock, Recv, TimeoutMS) of
|
||||
{ok, Message, [], Remainder} ->
|
||||
{ok, Message, Remainder};
|
||||
Illegal = {ok, _, _NonEmptyFrames, _} ->
|
||||
{error, {bad_frame_stack, Illegal}};
|
||||
Error ->
|
||||
Error
|
||||
end.
|
||||
|
||||
|
||||
-spec recv(Socket, Received, TimeoutMS) -> Result
|
||||
when Socket :: gen_tcp:socket(),
|
||||
@ -654,15 +604,10 @@ recv_frame(Frame = #frame{payload_length = Len, payload = none}, Sock, Received,
|
||||
|
||||
%% factoring this out into a function to reduce repetition
|
||||
recv_frame_await(Frame, Sock, Received, Timeout) ->
|
||||
io:format("~p called: recv_frame_await(~p, ~p, ~p, ~p)~n",
|
||||
[self(), Frame, Sock, Received, Timeout]),
|
||||
case inet:setopts(Sock, [{active, once}]) of
|
||||
ok ->
|
||||
receive
|
||||
{tcp, Sock, Bin} ->
|
||||
io:format("~p calling: recv_frame(~p, ~p, ~p, ~p)~n",
|
||||
[self(),Frame, Sock, <<Received/bits, Bin/binary>>, Timeout]),
|
||||
recv_frame(Frame, Sock, <<Received/bits, Bin/binary>>, Timeout);
|
||||
{tcp, Sock, Bin} -> recv_frame(Frame, Sock, <<Received/bits, Bin/binary>>, Timeout);
|
||||
{tcp_closed, Sock} -> {error, tcp_closed};
|
||||
{tcp_error, Sock, Reason} -> {error, {tcp_error, Reason}}
|
||||
after Timeout ->
|
||||
@ -673,34 +618,6 @@ recv_frame_await(Frame, Sock, Received, Timeout) ->
|
||||
end.
|
||||
|
||||
|
||||
send_json(Socket, X) ->
|
||||
send_dwim(Socket, {json, X}).
|
||||
|
||||
|
||||
-spec send_dwim(Socket, Message) -> Result
|
||||
when Socket :: gen_tcp:socket(),
|
||||
Message :: string()
|
||||
| binary()
|
||||
| {json, zj:value()}
|
||||
| ws_msg(),
|
||||
Result :: ok
|
||||
| {error, Reason},
|
||||
Reason :: any().
|
||||
% @doc
|
||||
% equivalent to send/2 but assumes iolists/strings are meant to be `text`, and
|
||||
% naked binaries are meant to be `binary`
|
||||
%
|
||||
% lists are assumed to be unicode iolists and are converted to strings via
|
||||
% unicode:characters_to_list
|
||||
%
|
||||
% json is encoded as text and sent as such
|
||||
% @end
|
||||
|
||||
send_dwim(Socket, X) when is_list(X) -> send(Socket, {text, X});
|
||||
send_dwim(Socket, X) when is_binary(X) -> send(Socket, {binary, X});
|
||||
send_dwim(Socket, {json, X}) -> send(Socket, {text, zj:encode(X)});
|
||||
send_dwim(Socket, {Type, Payload}) -> send(Socket, {Type, Payload}).
|
||||
|
||||
|
||||
-spec send(Socket, Message) -> Result
|
||||
when Socket :: gen_tcp:socket(),
|
||||
@ -715,8 +632,11 @@ send_dwim(Socket, {Type, Payload}) -> send(Socket, {Type, Payload}).
|
||||
% @end
|
||||
|
||||
send(Socket, {Type, Payload}) ->
|
||||
log(info, "fd_ws: send(~tp, {~tp, ~tp})", [Socket, Type, Payload]),
|
||||
BPayload = payload_to_binary(Payload),
|
||||
log(info, "fd_ws: BPayload = ~tp", [BPayload]),
|
||||
Frame = message_to_frame(Type, BPayload),
|
||||
log(info, "fd_ws: Frame = ~tp", [Frame]),
|
||||
send_frame(Socket, Frame).
|
||||
|
||||
payload_to_binary(Bin) when is_binary(Bin) -> Bin;
|
||||
@ -755,6 +675,7 @@ message_to_frame(Control, Payload)
|
||||
|
||||
send_frame(Sock, Frame) ->
|
||||
Binary = render_frame(Frame),
|
||||
log(info, "send_frame: rendered frame: ~tp", [Binary]),
|
||||
gen_tcp:send(Sock, Binary).
|
||||
|
||||
|
||||
102
src/fd_wsp.erl
Normal file
102
src/fd_wsp.erl
Normal file
@ -0,0 +1,102 @@
|
||||
% @doc Abstracts a web socket into a process
|
||||
%
|
||||
% hands the TCP socket over to this process, also this process does the
|
||||
% handshake.
|
||||
%
|
||||
% this process sends back `{ws, self(), Message: fd_ws:ws_msg()}'
|
||||
%
|
||||
% for each websocket message it gets
|
||||
-module(fd_wsp).
|
||||
|
||||
-behavior(gen_server).
|
||||
|
||||
-export_type([
|
||||
|
||||
]).
|
||||
|
||||
-export([
|
||||
%% caller context
|
||||
handshake/0,
|
||||
start_link/0,
|
||||
|
||||
%% process context
|
||||
init/1, handle_call/3, handle_cast/2, handle_info/2,
|
||||
code_change/3, terminate/2
|
||||
]).
|
||||
|
||||
-include("http.hrl").
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
|
||||
-record(s, {socket :: gen_tcp:socket()})
|
||||
-type state() :: #s{}.
|
||||
|
||||
|
||||
%%-----------------------------------------------------------------------------
|
||||
%% caller context
|
||||
%%-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
-spec start_link(Socket, HandshakeReq, Received) -> Result
|
||||
when Socket :: gen_tcp:socket(),
|
||||
HandshakeReq :: request(),
|
||||
Received :: binary(),
|
||||
Result :: {ok, pid()}
|
||||
| {error, term()}.
|
||||
% @doc
|
||||
% starts a websocket and hands control of socket over to child process
|
||||
|
||||
start_link(Socket, HandshakeReq, Received) ->
|
||||
case gen_server:start_link(?MODULE, [Socket, HandshakeReq, Received], []) of
|
||||
{ok, PID} ->
|
||||
gen_tcp:controlling_process(Socket, PID),
|
||||
{ok, PID};
|
||||
Error ->
|
||||
Error
|
||||
end.
|
||||
|
||||
|
||||
%%-----------------------------------------------------------------------------
|
||||
%% process context below this line
|
||||
%%-----------------------------------------------------------------------------
|
||||
|
||||
%% gen_server callbacks
|
||||
|
||||
init([Socket, HandshakeReq, Received]) ->
|
||||
log("~p:~p init", [?MODULE, self()]),
|
||||
case fd_ws:handshake(HandshakeReq) of
|
||||
{ok, Response} ->
|
||||
ok = fd_http_utils:respond(Sock, Response),
|
||||
InitState = #s{socket = Socket},
|
||||
Error ->
|
||||
tell("~p:~p websocket handshake err: ~p", [?MODULE, self(), Error]),
|
||||
fd_http_utils:http_err(Socket, 400)
|
||||
Error
|
||||
end.
|
||||
|
||||
|
||||
handle_call(Unexpected, From, State) ->
|
||||
tell("~tp: unexpected call from ~tp: ~tp", [?MODULE, Unexpected, From]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
handle_cast(Unexpected, State) ->
|
||||
tell("~tp: unexpected cast: ~tp", [?MODULE, Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
handle_info({tcp, Sock, Bytes}, State = #s{socket = Sock}) ->
|
||||
handle_info(Unexpected, State) ->
|
||||
tell("~tp: unexpected info: ~tp", [?MODULE, Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
code_change(_, State, _) ->
|
||||
{ok, State}.
|
||||
|
||||
terminate(_, _) ->
|
||||
ok.
|
||||
|
||||
|
||||
%%-----------------------------------------------------------------------------
|
||||
%% internals
|
||||
%%-----------------------------------------------------------------------------
|
||||
@ -3,7 +3,7 @@
|
||||
%%% @end
|
||||
|
||||
-module(fewd).
|
||||
-vsn("0.2.0").
|
||||
-vsn("0.1.0").
|
||||
-behavior(application).
|
||||
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
-copyright("Peter Harpending <peterharpending@qpq.swiss>").
|
||||
@ -24,7 +24,7 @@
|
||||
%% Returns an {error, Reason} tuple if it is already listening.
|
||||
|
||||
listen(PortNum) ->
|
||||
fd_httpd_client_man:listen(PortNum).
|
||||
fd_client_man:listen(PortNum).
|
||||
|
||||
|
||||
-spec ignore() -> ok.
|
||||
@ -32,7 +32,7 @@ listen(PortNum) ->
|
||||
%% Make the server stop listening if it is, or continue to do nothing if it isn't.
|
||||
|
||||
ignore() ->
|
||||
fd_httpd_client_man:ignore().
|
||||
fd_client_man:ignore().
|
||||
|
||||
|
||||
-spec start(normal, term()) -> {ok, pid()}.
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
% @doc
|
||||
% porcelain wfc ops
|
||||
-module(wfc).
|
||||
-vsn("0.2.0").
|
||||
|
||||
-export_type([
|
||||
sentence/0
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
% @doc
|
||||
% bit matrices
|
||||
-module(wfc_bm).
|
||||
-vsn("0.2.0").
|
||||
|
||||
-export_type([
|
||||
bit/0,
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
-module(wfc_eval).
|
||||
-vsn("0.2.0").
|
||||
|
||||
-export_type([
|
||||
]).
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
-module(wfc_eval_context).
|
||||
-vsn("0.2.0").
|
||||
|
||||
-export_type([
|
||||
context/0
|
||||
|
||||
@ -7,7 +7,6 @@
|
||||
%
|
||||
% mathematically, this is a variable like "a", "b", "c", etc
|
||||
-module(wfc_ltr).
|
||||
-vsn("0.2.0").
|
||||
|
||||
-export_type([
|
||||
ltr/0
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
-module(wfc_pp).
|
||||
-vsn("0.2.0").
|
||||
|
||||
-export([
|
||||
eval_result/1,
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
-module(wfc_read).
|
||||
-vsn("0.2.0").
|
||||
|
||||
-export_type([
|
||||
]).
|
||||
|
||||
@ -6,7 +6,6 @@
|
||||
%
|
||||
% empty sentence is 0
|
||||
-module(wfc_sentence).
|
||||
-vsn("0.2.0").
|
||||
|
||||
-export_type([
|
||||
sentence/0
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
% @doc
|
||||
% sentence-fun <-> truth table logic
|
||||
-module(wfc_sftt).
|
||||
-vsn("0.2.0").
|
||||
|
||||
-export_type([
|
||||
sf/0, tt/0
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
% @doc
|
||||
% library of truth tables
|
||||
-module(wfc_ttfuns).
|
||||
-vsn("0.2.0").
|
||||
|
||||
-export_type([
|
||||
bit/0,
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
% @doc misc utility functions
|
||||
-module(wfc_utils).
|
||||
-vsn("0.2.0").
|
||||
|
||||
-export([err/2, str/2, emsg/2]).
|
||||
|
||||
|
||||
@ -13,7 +13,6 @@
|
||||
%
|
||||
% operations assume all inputs are valid
|
||||
-module(wfc_word).
|
||||
-vsn("0.2.0").
|
||||
|
||||
-export_type([
|
||||
word/0
|
||||
|
||||
694
src/zj.erl
Normal file
694
src/zj.erl
Normal file
@ -0,0 +1,694 @@
|
||||
%%% @doc
|
||||
%%% ZJ: The tiny JSON parser
|
||||
%%%
|
||||
%%% This module exports four functions and accepts no options.
|
||||
%%% @end
|
||||
|
||||
-module(zj).
|
||||
-vsn("1.1.2").
|
||||
-author("Craig Everett <zxq9@zxq9.com>").
|
||||
-copyright("Craig Everett <zxq9@zxq9.com>").
|
||||
-license("MIT").
|
||||
|
||||
-export([encode/1, decode/1,
|
||||
binary_encode/1, binary_decode/1]).
|
||||
|
||||
-export_type([value/0, bin_value/0]).
|
||||
|
||||
-type value() :: string()
|
||||
| number()
|
||||
| true
|
||||
| false
|
||||
| undefined
|
||||
| [value()]
|
||||
| #{string() := value()}.
|
||||
|
||||
|
||||
-type bin_value() :: binary()
|
||||
| number()
|
||||
| true
|
||||
| false
|
||||
| undefined
|
||||
| [bin_value()]
|
||||
| #{binary() := bin_value()}.
|
||||
|
||||
|
||||
|
||||
%%% Character constants
|
||||
|
||||
-define(BKSPC, 16#08).
|
||||
-define(H_TAB, 16#09).
|
||||
-define(NEW_L, 16#0A).
|
||||
-define(FORMF, 16#0C).
|
||||
-define(CAR_R, 16#0D).
|
||||
-define(SPACE, 16#20).
|
||||
|
||||
|
||||
%%% Interface Functions
|
||||
|
||||
-spec encode(term()) -> string().
|
||||
|
||||
%% @doc
|
||||
%% Take any convertable Erlang term and convert it to a JSON string.
|
||||
%%
|
||||
%% As JSON can only satirically be referred to as "a serialization format", it is
|
||||
%% almost impossible to map any interesting data between Erlang (or any other language)
|
||||
%% and JSON. For example, tuples do not exist in JSON, so converting an Erlang tuple
|
||||
%% turns it into a list (a JSON array). Atoms also do not exist, so atoms other than
|
||||
%% the ternay logic values `true', `false' and `null' become strings (those three
|
||||
%% remain as atoms, with the added detail that JSON `null' maps to Erlang
|
||||
%% `undefined').
|
||||
%%
|
||||
%% Unless care is taken to pick types that JSON can accurately express (integers,
|
||||
%% floats, strings, maps, lists, ternary logic atoms) it is not possible to guarantee
|
||||
%% (or even reasonable to expect) that `Term == decode(encode(Term))' will be true.
|
||||
%%
|
||||
%% This function crashes when it fails. Things that will cause a crash are trying to
|
||||
%% convert non-UTF-8 binaries to strings, use non-string values as object keys,
|
||||
%% encode an unaligned bitstring, etc.
|
||||
%%
|
||||
%% Note that Erlang terms are converted as type primitives, meaning that compound
|
||||
%% functional structures like GB-trees, dicts, sets, etc. will wind up having their
|
||||
%% underlying structures converted as-is which is almost never what you want. It is
|
||||
%% usually best to reduce compound values down to primitives (lists or maps) before
|
||||
%% running encode.
|
||||
%%
|
||||
%% The only unsupported Erlang pritmitive is bitstrings. Care has NOT been taken to
|
||||
%% ensure separation between actual binary data and binaries that are supposed to be
|
||||
%% interpreted as strings. The same is true of deep list data: it just comes out raw
|
||||
%% unless you flatten or convert it to a utf8 string with the unicode module.
|
||||
%%
|
||||
%% NOTE: If you need a serialization format that is less ambiguous and expresses more
|
||||
%% types consider using BERT (language-independent implementations of Erlang external
|
||||
%% binary format) instead: http://bert-rpc.org
|
||||
|
||||
encode(true) -> "true";
|
||||
encode(false) -> "false";
|
||||
encode(undefined) -> "null";
|
||||
encode([]) -> "[]";
|
||||
encode(T) when is_atom(T) -> quote(atom_to_list(T));
|
||||
encode(T) when is_float(T) -> float_to_list(T);
|
||||
encode(T) when is_integer(T) -> integer_to_list(T);
|
||||
encode(T) when is_pid(T) -> quote(pid_to_list(T));
|
||||
encode(T) when is_port(T) -> quote(port_to_list(T));
|
||||
encode(T) when is_function(T) -> quote(erlang:fun_to_list(T));
|
||||
encode(T) when is_reference(T) -> quote(ref_to_list(T));
|
||||
encode(T) -> unicode:characters_to_list(encode_value(T)).
|
||||
|
||||
|
||||
-spec decode(Stream) -> Result
|
||||
when Stream :: unicode:chardata(),
|
||||
Result :: {ok, value()}
|
||||
| {error, Parsed, Remainder}
|
||||
| {incomplete, Parsed, Remainder},
|
||||
Parsed :: value(),
|
||||
Remainder :: unicode:chardata()
|
||||
| unicode:external_chardata()
|
||||
| binary().
|
||||
%% @doc
|
||||
%% Take any IO data acceptable to the unicode module and return a parsed data structure.
|
||||
%% In the event of a parsing error whatever part of the structure could be successfully
|
||||
%% parsed will be returned along with the remainder of the string. Note that the string
|
||||
%% remainder may have been changed to a different form by unicode:characters_to_list/1.
|
||||
%% If the unicode library itself runs into a problem performing the initial conversion
|
||||
%% its error return (`error' or `incomplete') will be returned directly.
|
||||
|
||||
decode(Stream) ->
|
||||
case unicode:characters_to_list(Stream) of
|
||||
E when is_tuple(E) -> E;
|
||||
[16#FEFF | String] -> parse(seek(String));
|
||||
String -> parse(seek(String))
|
||||
end.
|
||||
|
||||
|
||||
-spec binary_encode(term()) -> binary().
|
||||
%% @doc
|
||||
%% A strict encoding routine that works very similarly to `encode/1' but with a few
|
||||
%% differences:
|
||||
%% ```
|
||||
%% - Lists and Strings are firmly separated:
|
||||
%% ALL lists are lists of discrete values, never strings.
|
||||
%% ALL binaries are always UTF-8 strings.
|
||||
%% An Erlang string or io_list will be encoded as JSON array.
|
||||
%% - This function generates a UTF-8 binary, not a list.
|
||||
%% - The burden is on the user to ensure that io_lists are collapsed to unicode
|
||||
%% binaries via `unicode:characters_to_binary/1' before passing in string values.
|
||||
%% - Erlang strings (lists) are still accepted as map/object keys.
|
||||
%% '''
|
||||
%%
|
||||
%% NOTE:
|
||||
%% Most cases are better served by `encode/1', as most code deals in strings and not
|
||||
%% arrays of integer values.
|
||||
%%
|
||||
%% Using this function requires a little bit more work up front (because ununified
|
||||
%% io_list() data will always be interpreted as a JSON array), but provides a way to
|
||||
%% reliably generate lists or strings in an unambiguous way in the special case where
|
||||
%% your code is generating both strings and lists of integer values that may overlap
|
||||
%% with valid UTF-8 codepoint values.
|
||||
|
||||
binary_encode(true) -> <<"true">>;
|
||||
binary_encode(false) -> <<"false">>;
|
||||
binary_encode(undefined) -> <<"null">>;
|
||||
binary_encode(T) when is_atom(T) -> <<"\"", (atom_to_binary(T, utf8))/binary, "\"">>;
|
||||
binary_encode(T) when is_float(T) -> float_to_binary(T);
|
||||
binary_encode(T) when is_integer(T) -> integer_to_binary(T);
|
||||
binary_encode(T) when is_pid(T) -> <<"\"", (list_to_binary(pid_to_list(T)))/binary, "\"">>;
|
||||
binary_encode(T) when is_port(T) -> <<"\"", (list_to_binary(port_to_list(T)))/binary, "\"">>;
|
||||
binary_encode(T) when is_function(T) -> <<"\"", (list_to_binary(erlang:fun_to_list(T)))/binary, "\"">>;
|
||||
binary_encode(T) when is_reference(T) -> <<"\"", (list_to_binary(ref_to_list(T)))/binary, "\"">>;
|
||||
binary_encode(T) -> unicode:characters_to_binary(b_encode_value(T)).
|
||||
|
||||
|
||||
-spec binary_decode(Stream) -> Result
|
||||
when Stream :: unicode:chardata(),
|
||||
Result :: {ok, bin_value()}
|
||||
| {error, Parsed, Remainder}
|
||||
| {incomplete, Parsed, Remainder},
|
||||
Parsed :: bin_value(),
|
||||
Remainder :: binary().
|
||||
%% @doc
|
||||
%% Almost identical in behavior to `decode/1' except this returns strings as binaries
|
||||
%% and arrays of integers as Erlang lists (which may also be valid strings if the
|
||||
%% values are valid UTF-8 codepoints).
|
||||
%%
|
||||
%% NOTE:
|
||||
%% This function returns map keys as binaries
|
||||
|
||||
binary_decode(Stream) ->
|
||||
case b_decode(Stream) of
|
||||
{error, Part, Rest} -> {error, Part, unicode:characters_to_binary(Rest)};
|
||||
Result -> Result
|
||||
end.
|
||||
|
||||
|
||||
|
||||
%%% Encoding Functions
|
||||
|
||||
encode_value(true) -> "true";
|
||||
encode_value(false) -> "false";
|
||||
encode_value(undefined) -> "null";
|
||||
encode_value(T) when is_atom(T) -> quote(atom_to_list(T));
|
||||
encode_value(T) when is_float(T) -> float_to_list(T);
|
||||
encode_value(T) when is_integer(T) -> integer_to_list(T);
|
||||
encode_value(T) when is_binary(T) -> maybe_string(T);
|
||||
encode_value(T) when is_list(T) -> maybe_array(T);
|
||||
encode_value(T) when is_map(T) -> pack_object(T);
|
||||
encode_value(T) when is_tuple(T) -> pack_array(tuple_to_list(T));
|
||||
encode_value(T) when is_pid(T) -> quote(pid_to_list(T));
|
||||
encode_value(T) when is_port(T) -> quote(port_to_list(T));
|
||||
encode_value(T) when is_function(T) -> quote(erlang:fun_to_list(T));
|
||||
encode_value(T) when is_reference(T) -> quote(ref_to_list(T)).
|
||||
|
||||
|
||||
maybe_string(T) ->
|
||||
L = binary_to_list(T),
|
||||
true = io_lib:printable_unicode_list(L),
|
||||
quote(L).
|
||||
|
||||
|
||||
maybe_array(T) ->
|
||||
case io_lib:printable_unicode_list(T) of
|
||||
true -> quote(T);
|
||||
false -> pack_array(T)
|
||||
end.
|
||||
|
||||
|
||||
quote(T) -> [$" | escape(T)].
|
||||
|
||||
escape([]) -> [$"];
|
||||
escape([$\b | T]) -> [$\\, $b | escape(T)];
|
||||
escape([$\f | T]) -> [$\\, $f | escape(T)];
|
||||
escape([$\n | T]) -> [$\\, $n | escape(T)];
|
||||
escape([$\r | T]) -> [$\\, $r | escape(T)];
|
||||
escape([$\t | T]) -> [$\\, $t | escape(T)];
|
||||
escape([$\" | T]) -> [$\\, $" | escape(T)];
|
||||
escape([$\\ | T]) -> [$\\, $\\ | escape(T)];
|
||||
escape([H | T]) -> [H | escape(T)].
|
||||
|
||||
|
||||
pack_array([]) -> "[]";
|
||||
pack_array([H | []]) -> [$[, encode_value(H), $]];
|
||||
pack_array([H | T]) -> [$[, encode_value(H), $,, encode_array(T), $]].
|
||||
|
||||
encode_array([H | []]) -> encode_value(H);
|
||||
encode_array([H | T]) -> [encode_value(H), $,, encode_array(T)].
|
||||
|
||||
|
||||
pack_object(M) ->
|
||||
case maps:to_list(M) of
|
||||
[] ->
|
||||
"{}";
|
||||
[{K, V} | T] when is_list(K) ->
|
||||
true = io_lib:printable_unicode_list(K),
|
||||
Init = [$", K, $", $:, encode_value(V)],
|
||||
[${, lists:foldl(fun pack_object/2, Init, T), $}];
|
||||
[{K, V} | T] when is_binary(K) ->
|
||||
Key = unicode:characters_to_list(K),
|
||||
true = io_lib:printable_unicode_list(Key),
|
||||
Init = [$", Key, $", $:, encode_value(V)],
|
||||
[${, lists:foldl(fun pack_object/2, Init, T), $}];
|
||||
[{K, V} | T] when is_float(K) ->
|
||||
Key = float_to_list(K),
|
||||
Init = [$", Key, $", $:, encode_value(V)],
|
||||
[${, lists:foldl(fun pack_object/2, Init, T), $}];
|
||||
[{K, V} | T] when is_integer(K) ->
|
||||
Key = integer_to_list(K),
|
||||
Init = [$", Key, $", $:, encode_value(V)],
|
||||
[${, lists:foldl(fun pack_object/2, Init, T), $}];
|
||||
[{K, V} | T] when is_atom(K) ->
|
||||
Init = [$", atom_to_list(K), $", $:, encode_value(V)],
|
||||
[${, lists:foldl(fun pack_object/2, Init, T), $}]
|
||||
end.
|
||||
|
||||
pack_object({K, V}, L) when is_list(K) ->
|
||||
true = io_lib:printable_unicode_list(K),
|
||||
[$", K, $", $:, encode_value(V), $, | L];
|
||||
pack_object({K, V}, L) when is_binary(K) ->
|
||||
Key = unicode:characters_to_list(K),
|
||||
true = io_lib:printable_unicode_list(Key),
|
||||
[$", Key, $", $:, encode_value(V), $, | L];
|
||||
pack_object({K, V}, L) when is_float(K) ->
|
||||
Key = float_to_list(K),
|
||||
[$", Key, $", $:, encode_value(V), $, | L];
|
||||
pack_object({K, V}, L) when is_integer(K) ->
|
||||
Key = integer_to_list(K),
|
||||
[$", Key, $", $:, encode_value(V), $, | L];
|
||||
pack_object({K, V}, L) when is_atom(K) ->
|
||||
[$", atom_to_list(K), $", $:, encode_value(V), $, | L].
|
||||
|
||||
|
||||
b_encode_value(true) -> <<"true">>;
|
||||
b_encode_value(false) -> <<"false">>;
|
||||
b_encode_value(undefined) -> <<"null">>;
|
||||
b_encode_value(T) when is_atom(T) -> [$", atom_to_binary(T, utf8), $"];
|
||||
b_encode_value(T) when is_float(T) -> float_to_binary(T);
|
||||
b_encode_value(T) when is_integer(T) -> integer_to_binary(T);
|
||||
b_encode_value(T) when is_binary(T) -> [$", b_maybe_string(T), $"];
|
||||
b_encode_value(T) when is_list(T) -> b_pack_array(T);
|
||||
b_encode_value(T) when is_map(T) -> b_pack_object(T);
|
||||
b_encode_value(T) when is_tuple(T) -> b_pack_array(tuple_to_list(T));
|
||||
b_encode_value(T) when is_pid(T) -> [$", list_to_binary(pid_to_list(T)), $"];
|
||||
b_encode_value(T) when is_port(T) -> [$", list_to_binary(port_to_list(T)), $"];
|
||||
b_encode_value(T) when is_function(T) -> [$", list_to_binary(erlang:fun_to_list(T)), $"];
|
||||
b_encode_value(T) when is_reference(T) -> [$", list_to_binary(ref_to_list(T)), $"].
|
||||
|
||||
|
||||
b_maybe_string(T) ->
|
||||
S = unicode:characters_to_binary(T),
|
||||
true = is_binary(S),
|
||||
S.
|
||||
|
||||
|
||||
b_pack_array([]) -> "[]";
|
||||
b_pack_array([H | []]) -> [$[, b_encode_value(H), $]];
|
||||
b_pack_array([H | T]) -> [$[, b_encode_value(H), $,, b_encode_array(T), $]].
|
||||
|
||||
b_encode_array([H | []]) -> b_encode_value(H);
|
||||
b_encode_array([H | T]) -> [b_encode_value(H), $,, b_encode_array(T)].
|
||||
|
||||
|
||||
b_pack_object(M) ->
|
||||
case maps:to_list(M) of
|
||||
[] ->
|
||||
"{}";
|
||||
[{K, V} | T] when is_list(K) ->
|
||||
true = io_lib:printable_unicode_list(K),
|
||||
Init = [$", K, $", $:, b_encode_value(V)],
|
||||
[${, lists:foldl(fun b_pack_object/2, Init, T), $}];
|
||||
[{K, V} | T] when is_binary(K) ->
|
||||
true = io_lib:printable_unicode_list(unicode:characters_to_list(K)),
|
||||
Init = [$", K, $", $:, b_encode_value(V)],
|
||||
[${, lists:foldl(fun b_pack_object/2, Init, T), $}];
|
||||
[{K, V} | T] when is_float(K) ->
|
||||
Key = float_to_list(K),
|
||||
Init = [$", Key, $", $:, b_encode_value(V)],
|
||||
[${, lists:foldl(fun b_pack_object/2, Init, T), $}];
|
||||
[{K, V} | T] when is_integer(K) ->
|
||||
Key = integer_to_list(K),
|
||||
Init = [$", Key, $", $:, b_encode_value(V)],
|
||||
[${, lists:foldl(fun b_pack_object/2, Init, T), $}];
|
||||
[{K, V} | T] when is_atom(K) ->
|
||||
Init = [$", atom_to_binary(K, utf8), $", $:, b_encode_value(V)],
|
||||
[${, lists:foldl(fun b_pack_object/2, Init, T), $}]
|
||||
end.
|
||||
|
||||
b_pack_object({K, V}, L) when is_list(K) ->
|
||||
true = io_lib:printable_unicode_list(K),
|
||||
[$", K, $", $:, b_encode_value(V), $, | L];
|
||||
b_pack_object({K, V}, L) when is_binary(K) ->
|
||||
true = io_lib:printable_unicode_list(unicode:characters_to_list(K)),
|
||||
[$", K, $", $:, b_encode_value(V), $, | L];
|
||||
b_pack_object({K, V}, L) when is_float(K) ->
|
||||
Key = float_to_list(K),
|
||||
[$", Key, $", $:, b_encode_value(V), $, | L];
|
||||
b_pack_object({K, V}, L) when is_integer(K) ->
|
||||
Key = integer_to_list(K),
|
||||
[$", Key, $", $:, b_encode_value(V), $, | L];
|
||||
b_pack_object({K, V}, L) when is_atom(K) ->
|
||||
[$", atom_to_list(K), $", $:, b_encode_value(V), $, | L].
|
||||
|
||||
|
||||
%%% Decode Functions
|
||||
|
||||
-spec parse(Stream) -> Result
|
||||
when Stream :: string(),
|
||||
Result :: {ok, value()}
|
||||
| {error, Extracted :: value(), Remaining :: string()}.
|
||||
%% @private
|
||||
%% The top-level dispatcher. This packages the top level value (or top-level error)
|
||||
%% for return to the caller. A very similar function (value/1) is used for inner
|
||||
%% values.
|
||||
|
||||
parse([${ | Rest]) ->
|
||||
case object(Rest) of
|
||||
{ok, Object, ""} -> {ok, Object};
|
||||
{ok, Object, More} -> polish(Object, seek(More));
|
||||
Error -> Error
|
||||
end;
|
||||
parse([$[ | Rest]) ->
|
||||
case array(Rest) of
|
||||
{ok, Array, ""} -> {ok, Array};
|
||||
{ok, Array, More} -> polish(Array, seek(More));
|
||||
Error -> Error
|
||||
end;
|
||||
parse([$" | Rest]) ->
|
||||
case string(Rest) of
|
||||
{ok, String, ""} -> {ok, String};
|
||||
{ok, String, More} -> polish(String, seek(More));
|
||||
Error -> Error
|
||||
end;
|
||||
parse([I | Rest]) when I == $-; $0 =< I, I =< $9 ->
|
||||
case number_int(Rest, [I]) of
|
||||
{ok, Number, ""} -> {ok, Number};
|
||||
{ok, Number, More} -> polish(Number, seek(More));
|
||||
Error -> Error
|
||||
end;
|
||||
parse("true" ++ More) ->
|
||||
polish(true, seek(More));
|
||||
parse("false" ++ More) ->
|
||||
polish(false, seek(More));
|
||||
parse("null" ++ More) ->
|
||||
polish(undefined, seek(More));
|
||||
parse(Other) ->
|
||||
{error, [], Other}.
|
||||
|
||||
|
||||
polish(Value, "") -> {ok, Value};
|
||||
polish(Value, More) -> {error, Value, More}.
|
||||
|
||||
|
||||
value([${ | Rest]) -> object(Rest);
|
||||
value([$[ | Rest]) -> array(Rest);
|
||||
value([$" | Rest]) -> string(Rest);
|
||||
value([I | Rest]) when I == $-; $0 =< I, I =< $9 -> number_int(Rest, [I]);
|
||||
value("true" ++ Rest) -> {ok, true, Rest};
|
||||
value("false" ++ Rest) -> {ok, false, Rest};
|
||||
value("null" ++ Rest) -> {ok, undefined, Rest};
|
||||
value(_) -> error.
|
||||
|
||||
|
||||
object([$} | Rest]) -> {ok, #{}, Rest};
|
||||
object(String) -> object(seek(String), #{}).
|
||||
|
||||
object([$} | Rest], Map) ->
|
||||
{ok, Map, Rest};
|
||||
object([$" | Rest], Map) ->
|
||||
case string(Rest) of
|
||||
{ok, Key, Remainder} -> object_value(seek(Remainder), Key, Map);
|
||||
{error, _, _} -> {error, Map, Rest}
|
||||
end;
|
||||
object(Rest, Map) ->
|
||||
{error, Map, Rest}.
|
||||
|
||||
object_value([$: | Rest], Key, Map) ->
|
||||
object_value_parse(seek(Rest), Key, Map);
|
||||
object_value(Rest, Key, Map) ->
|
||||
{error, maps:put(Key, undefined, Map), Rest}.
|
||||
|
||||
object_value_parse(String, Key, Map) ->
|
||||
case value(String) of
|
||||
{ok, Value, Rest} -> object_next(seek(Rest), maps:put(Key, Value, Map));
|
||||
{error, Value, Rest} -> {error, maps:put(Key, Value, Map), Rest};
|
||||
error -> {error, Map, String}
|
||||
end.
|
||||
|
||||
|
||||
object_next([$, | Rest], Map) -> object(seek(Rest), Map);
|
||||
object_next([$} | Rest], Map) -> {ok, Map, seek(Rest)};
|
||||
object_next(Rest, Map) -> {error, Map, Rest}.
|
||||
|
||||
|
||||
array([$] | Rest]) -> {ok, [], Rest};
|
||||
array(String) -> array(seek(String), []).
|
||||
|
||||
array([$] | Rest], List) ->
|
||||
{ok, lists:reverse(List), seek(Rest)};
|
||||
array(String, List) ->
|
||||
case value(String) of
|
||||
{ok, Value, Rest} -> array_next(seek(Rest), [Value | List]);
|
||||
{error, Value, Rest} -> {error, lists:reverse([Value | List]), Rest};
|
||||
error -> {error, lists:reverse(List), String}
|
||||
end.
|
||||
|
||||
array_next([$, | Rest], List) -> array(seek(Rest), List);
|
||||
array_next([$] | Rest], List) -> {ok, lists:reverse(List), seek(Rest)};
|
||||
array_next(Rest, List) -> {error, lists:reverse(List), Rest}.
|
||||
|
||||
|
||||
string(Stream) -> string(Stream, "").
|
||||
|
||||
string([$" | Rest], String) ->
|
||||
{ok, lists:reverse(String), Rest};
|
||||
string([$\\, $" | Rest], String) ->
|
||||
string(Rest, [$" | String]);
|
||||
string([$\\, $\\ | Rest], String) ->
|
||||
string(Rest, [$\\ | String]);
|
||||
string([$\\, $b | Rest], String) ->
|
||||
string(Rest, [?BKSPC | String]);
|
||||
string([$\\, $t | Rest], String) ->
|
||||
string(Rest, [?H_TAB | String]);
|
||||
string([$\\, $n | Rest], String) ->
|
||||
string(Rest, [?NEW_L | String]);
|
||||
string([$\\, $f | Rest], String) ->
|
||||
string(Rest, [?FORMF | String]);
|
||||
string([$\\, $r | Rest], String) ->
|
||||
string(Rest, [?CAR_R | String]);
|
||||
string([$\\, $u, A, B, C, D | Rest], String)
|
||||
when (($0 =< A andalso A =< $9) or ($A =< A andalso A =< $F) or ($a =< A andalso A =< $f))
|
||||
and (($0 =< B andalso B =< $9) or ($A =< B andalso B =< $F) or ($a =< B andalso B =< $f))
|
||||
and (($0 =< C andalso C =< $9) or ($A =< C andalso C =< $F) or ($a =< C andalso C =< $f))
|
||||
and (($0 =< D andalso D =< $9) or ($A =< D andalso D =< $F) or ($a =< D andalso D =< $f)) ->
|
||||
Char = list_to_integer([A, B, C, D], 16),
|
||||
string(Rest, [Char | String]);
|
||||
string(Stream = [$\\, $u | _], String) ->
|
||||
{error, String, Stream};
|
||||
string([$\\, Char | Rest], String)
|
||||
when Char == 16#20;
|
||||
Char == 16#21;
|
||||
16#23 =< Char, Char =< 16#5B;
|
||||
16#5D =< Char, Char =< 16#10FFFF ->
|
||||
string(Rest, [$\\, Char | String]);
|
||||
string([Char | Rest], String)
|
||||
when Char == 16#20;
|
||||
Char == 16#21;
|
||||
16#23 =< Char, Char =< 16#5B;
|
||||
16#5D =< Char, Char =< 16#10FFFF ->
|
||||
string(Rest, [Char | String]);
|
||||
string(Rest, String) ->
|
||||
{error, lists:reverse(String), Rest}.
|
||||
|
||||
|
||||
number_int([$. | Rest], String) ->
|
||||
number_float(Rest, [$. | String]);
|
||||
number_int([$e, Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_float_exp(Rest, [Char, $+, $e, $0, $. | String]);
|
||||
number_int([$E, Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_float_exp(Rest, [Char, $+, $e, $0, $. | String]);
|
||||
number_int([$e, $+, Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_float_exp(Rest, [Char, $+, $e, $0, $. | String]);
|
||||
number_int([$E, $+, Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_float_exp(Rest, [Char, $+, $e, $0, $. | String]);
|
||||
number_int([$e, $-, Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_float_exp(Rest, [Char, $-, $e, $0, $. | String]);
|
||||
number_int([$E, $-, Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_float_exp(Rest, [Char, $-, $e, $0, $. | String]);
|
||||
number_int([Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_int(Rest, [Char | String]);
|
||||
number_int(Rest, "-") ->
|
||||
{error, "", [$- | Rest]};
|
||||
number_int(Rest, String) ->
|
||||
{ok, list_to_integer(lists:reverse(String)), seek(Rest)}.
|
||||
|
||||
number_float([Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_float(Rest, [Char | String]);
|
||||
number_float([$E, Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_float_exp(Rest, [Char, $+, $e | String]);
|
||||
number_float([$e, Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_float_exp(Rest, [Char, $+, $e | String]);
|
||||
number_float([$E, $+, Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_float_exp(Rest, [Char, $+, $e | String]);
|
||||
number_float([$e, $+, Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_float_exp(Rest, [Char, $+, $e | String]);
|
||||
number_float([$E, $-, Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_float_exp(Rest, [Char, $-, $e | String]);
|
||||
number_float([$e, $-, Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_float_exp(Rest, [Char, $-, $e | String]);
|
||||
number_float(Rest, String) ->
|
||||
Target = lists:reverse(String),
|
||||
try
|
||||
Number = list_to_float(Target),
|
||||
{ok, Number, seek(Rest)}
|
||||
catch
|
||||
error:badarg -> {error, "", Target ++ Rest}
|
||||
end.
|
||||
|
||||
|
||||
number_float_exp([Char | Rest], String) when $0 =< Char, Char =< $9 ->
|
||||
number_float_exp(Rest, [Char | String]);
|
||||
number_float_exp(Rest, String) ->
|
||||
Target = lists:reverse(String),
|
||||
try
|
||||
Number = list_to_float(Target),
|
||||
{ok, Number, seek(Rest)}
|
||||
catch
|
||||
error:badarg -> {error, "", Target ++ Rest}
|
||||
end.
|
||||
|
||||
|
||||
seek([?H_TAB | Rest]) -> seek(Rest);
|
||||
seek([?NEW_L | Rest]) -> seek(Rest);
|
||||
seek([?CAR_R | Rest]) -> seek(Rest);
|
||||
seek([?SPACE | Rest]) -> seek(Rest);
|
||||
seek(String) -> String.
|
||||
|
||||
|
||||
b_decode(Stream) ->
|
||||
case unicode:characters_to_list(Stream) of
|
||||
E when is_tuple(E) -> E;
|
||||
[16#FEFF | String] -> binary_parse(seek(String));
|
||||
String -> binary_parse(seek(String))
|
||||
end.
|
||||
|
||||
-spec binary_parse(Stream) -> Result
|
||||
when Stream :: string(),
|
||||
Result :: {ok, bin_value()}
|
||||
| {error, Extracted :: bin_value(), Remaining :: binary()}.
|
||||
%% @private
|
||||
%% The top-level dispatcher. This packages the top level value (or top-level error)
|
||||
%% for return to the caller. A very similar function (b_value/1) is used for inner
|
||||
%% values.
|
||||
|
||||
binary_parse([${ | Rest]) ->
|
||||
case b_object(Rest) of
|
||||
{ok, Object, ""} -> {ok, Object};
|
||||
{ok, Object, More} -> b_polish(Object, seek(More));
|
||||
Error -> Error
|
||||
end;
|
||||
binary_parse([$[ | Rest]) ->
|
||||
case b_array(Rest) of
|
||||
{ok, Array, ""} -> {ok, Array};
|
||||
{ok, Array, More} -> b_polish(Array, seek(More));
|
||||
Error -> Error
|
||||
end;
|
||||
binary_parse([$" | Rest]) ->
|
||||
case string(Rest) of
|
||||
{ok, String, ""} ->
|
||||
case unicode:characters_to_binary(String) of
|
||||
E when is_tuple(E) -> E;
|
||||
Result -> {ok, Result}
|
||||
end;
|
||||
{ok, String, More} ->
|
||||
case unicode:characters_to_binary(String) of
|
||||
E when is_tuple(E) -> E;
|
||||
Result -> b_polish(Result, seek(More))
|
||||
end;
|
||||
Error ->
|
||||
Error
|
||||
end;
|
||||
binary_parse([I | Rest]) when I == $-; $0 =< I, I =< $9 ->
|
||||
case number_int(Rest, [I]) of
|
||||
{ok, Number, ""} -> {ok, Number};
|
||||
{ok, Number, More} -> b_polish(Number, seek(More));
|
||||
Error -> Error
|
||||
end;
|
||||
binary_parse("true" ++ More) ->
|
||||
b_polish(true, seek(More));
|
||||
binary_parse("false" ++ More) ->
|
||||
b_polish(false, seek(More));
|
||||
binary_parse("null" ++ More) ->
|
||||
b_polish(undefined, seek(More));
|
||||
binary_parse(Other) ->
|
||||
{error, [], Other}.
|
||||
|
||||
|
||||
b_polish(Value, "") -> {ok, Value};
|
||||
b_polish(Value, More) -> {error, Value, More}.
|
||||
|
||||
|
||||
b_value([${ | Rest]) -> b_object(Rest);
|
||||
b_value([$[ | Rest]) -> b_array(Rest);
|
||||
b_value([$" | Rest]) -> b_string(Rest);
|
||||
b_value([I | Rest]) when I == $-; $0 =< I, I =< $9 -> number_int(Rest, [I]);
|
||||
b_value("true" ++ Rest) -> {ok, true, Rest};
|
||||
b_value("false" ++ Rest) -> {ok, false, Rest};
|
||||
b_value("null" ++ Rest) -> {ok, undefined, Rest};
|
||||
b_value(_) -> error.
|
||||
|
||||
|
||||
b_string(Stream) ->
|
||||
case string(Stream) of
|
||||
{ok, String, More} ->
|
||||
case unicode:characters_to_binary(String) of
|
||||
E when is_tuple(E) -> E;
|
||||
Result -> {ok, Result, More}
|
||||
end;
|
||||
Error -> Error
|
||||
end.
|
||||
|
||||
|
||||
b_object([$} | Rest]) -> {ok, #{}, Rest};
|
||||
b_object(String) -> b_object(seek(String), #{}).
|
||||
|
||||
b_object([$} | Rest], Map) ->
|
||||
{ok, Map, Rest};
|
||||
b_object([$" | Rest], Map) ->
|
||||
case string(Rest) of
|
||||
{ok, Key, Remainder} ->
|
||||
b_object_value(seek(Remainder), unicode:characters_to_binary(Key), Map);
|
||||
{error, _, _} ->
|
||||
{error, Map, Rest}
|
||||
end;
|
||||
b_object(Rest, Map) ->
|
||||
{error, Map, Rest}.
|
||||
|
||||
b_object_value([$: | Rest], Key, Map) -> b_object_value_parse(seek(Rest), Key, Map);
|
||||
b_object_value(Rest, Key, Map) -> {error, maps:put(Key, undefined, Map), Rest}.
|
||||
|
||||
b_object_value_parse(String, Key, Map) ->
|
||||
case b_value(String) of
|
||||
{ok, Value, Rest} -> b_object_next(seek(Rest), maps:put(Key, Value, Map));
|
||||
{error, Value, Rest} -> {error, maps:put(Key, Value, Map), Rest};
|
||||
error -> {error, Map, String}
|
||||
end.
|
||||
|
||||
|
||||
b_object_next([$, | Rest], Map) -> b_object(seek(Rest), Map);
|
||||
b_object_next([$} | Rest], Map) -> {ok, Map, seek(Rest)};
|
||||
b_object_next(Rest, Map) -> {error, Map, Rest}.
|
||||
|
||||
|
||||
b_array([$] | Rest]) -> {ok, [], Rest};
|
||||
b_array(String) -> b_array(seek(String), []).
|
||||
|
||||
b_array([$] | Rest], List) ->
|
||||
{ok, lists:reverse(List), seek(Rest)};
|
||||
b_array(String, List) ->
|
||||
case b_value(String) of
|
||||
{ok, Value, Rest} -> b_array_next(seek(Rest), [Value | List]);
|
||||
{error, Value, Rest} -> {error, lists:reverse([Value | List]), Rest};
|
||||
error -> {error, lists:reverse(List), String}
|
||||
end.
|
||||
|
||||
b_array_next([$, | Rest], List) -> b_array(seek(Rest), List);
|
||||
b_array_next([$] | Rest], List) -> {ok, lists:reverse(List), seek(Rest)};
|
||||
b_array_next(Rest, List) -> {error, lists:reverse(List), Rest}.
|
||||
@ -1,11 +1,11 @@
|
||||
{name,"front end web development lab"}.
|
||||
{type,app}.
|
||||
{modules,[]}.
|
||||
{author,"Peter Harpending"}.
|
||||
{prefix,"fd"}.
|
||||
{desc,"Front End Web Dev in Erlang stuff"}.
|
||||
{package_id,{"otpr","fewd",{0,2,0}}}.
|
||||
{deps,[{"otpr","zj",{1,1,2}}]}.
|
||||
{author,"Peter Harpending"}.
|
||||
{package_id,{"otpr","fewd",{0,1,0}}}.
|
||||
{deps,[]}.
|
||||
{key_name,none}.
|
||||
{a_email,"peterharpending@qpq.swiss"}.
|
||||
{c_email,"peterharpending@qpq.swiss"}.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user