mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2026-06-01 10:12:18 +00:00
Update socket-erlang example
This commit is contained in:
@@ -134,19 +134,24 @@ Command.prototype.pipeSocket = function(pipeInfo,options) {
|
||||
|
||||
Command.prototype.pipeSocketErlang = function(pipeInfo,options) {
|
||||
var self = this,
|
||||
encoding = pipeInfo.encoding || "utf8",
|
||||
net = require("net"),
|
||||
socket = new net.Socket(),
|
||||
accumulator = Buffer.alloc(0);
|
||||
socket.connect(pipeInfo.port,pipeInfo.host || 8081,function() {
|
||||
self.log("Socket connection",pipeInfo.port,pipeInfo.host);
|
||||
var dataBytes = Buffer.from(options.data,encoding);
|
||||
// Write 32-bit big endian message length
|
||||
var lengthBytes = Buffer.alloc(4);
|
||||
lengthBytes.writeUInt32BE(options.data.length,0)
|
||||
console.log("Wring butes",options.data.length)
|
||||
lengthBytes.writeUInt32BE(dataBytes.length + 1,0)
|
||||
console.log("Writing bytes",dataBytes.length + 1);
|
||||
socket.write(lengthBytes);
|
||||
// Write 8-bit type
|
||||
var typeByte = Buffer.alloc(1);
|
||||
typeByte.writeUInt8(1,0);
|
||||
socket.write(typeByte);
|
||||
socket.write(options.data);
|
||||
// Write data
|
||||
socket.write(dataBytes);
|
||||
});
|
||||
socket.on("error",function(e) {
|
||||
self.log("Socket error",e)
|
||||
@@ -154,14 +159,18 @@ console.log("Wring butes",options.data.length)
|
||||
socket.on("data",function(data) {
|
||||
console.log("Received data",data.length)
|
||||
accumulator = Buffer.concat([accumulator,data]);
|
||||
while(accumulator.length > 5) {
|
||||
var length = accumulator.readInt32BE(0),
|
||||
type = accumulator.readUInt8(4);
|
||||
if(accumulator.length > (length + 5)) {
|
||||
var data = accumulator.toString("utf8",5,length + 5);
|
||||
while(accumulator.length > 4) {
|
||||
var length = accumulator.readInt32BE(0);
|
||||
if(accumulator.length >= (length + 4)) {
|
||||
if(length < 2) {
|
||||
throw "ERROR: Incoming message length field is less than 2";
|
||||
}
|
||||
var type = accumulator.readUInt8(4),
|
||||
dataLength = length - 1,
|
||||
data = accumulator.toString(encoding,5,dataLength + 5);
|
||||
console.log("Got message",length,type)
|
||||
self.processIncomingData(data,pipeInfo);
|
||||
accumulator = accumulator.slice(length + 5);
|
||||
accumulator = accumulator.slice(length + 4);
|
||||
socket.end();
|
||||
return self.callback(null);
|
||||
} else {
|
||||
|
||||
@@ -18,8 +18,6 @@ exports.path = /^\/commands\/$/;
|
||||
|
||||
exports.handler = function(request,response,state) {
|
||||
// Check we're enabled
|
||||
|
||||
console.log("wikiInfo",$tw.boot.wikiInfo)
|
||||
if(!($tw.boot.wikiInfo.config || {})["allow-remote-commands"]) {
|
||||
response.writeHead(404);
|
||||
response.end();
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env escript
|
||||
|
||||
%% run with sh ./recase_erl.sh
|
||||
|
||||
-mode(compile).
|
||||
|
||||
main(_) ->
|
||||
{ok, Listen} = gen_tcp:listen(8081, [binary,{packet,4},
|
||||
{active,true}]),
|
||||
spawn(fun() -> par_connect(Listen) end),
|
||||
receive after infinity -> void end.
|
||||
|
||||
par_connect(Listen) ->
|
||||
{ok, Socket} = gen_tcp:accept(Listen),
|
||||
io:format("connected ~n"),
|
||||
spawn(fun() -> par_connect(Listen) end),
|
||||
loop(Socket).
|
||||
|
||||
loop(Socket) ->
|
||||
receive
|
||||
{tcp,Socket,Bin} ->
|
||||
io:format("received ~p bytes~n",[size(Bin)]),
|
||||
Return = recase_binary(Bin),
|
||||
io:format("sending: ~p bytes~n",[size(Return)]),
|
||||
gen_tcp:send(Socket, Return),
|
||||
loop(Socket);
|
||||
Other ->
|
||||
io:format("received ~p~n",[Other])
|
||||
end.
|
||||
|
||||
recase_binary(<<1,B/binary>>) ->
|
||||
L = binary_to_list(B),
|
||||
L1 = [recase(I) || I<- L],
|
||||
B1 = list_to_binary(tl(L1)),
|
||||
<<1,B1/binary>>.
|
||||
|
||||
recase(I) when I >= $a, I =< $z -> I - $a + $A;
|
||||
recase(I) when I >= $A, I =< $Z -> I - $A + $a;
|
||||
recase(I) -> I.
|
||||
@@ -29,18 +29,22 @@ server.on("connection", function(sock) {
|
||||
sock.on("data",function(data) {
|
||||
console.log("DATA " + sock.remoteAddress + ": " + data.length);
|
||||
accumulator = Buffer.concat([accumulator,Buffer.from(data)]);
|
||||
while(accumulator.length > 5) {
|
||||
var length = accumulator.readInt32BE(0),
|
||||
type = accumulator.readUInt8(4);
|
||||
if(accumulator.length > (length + 5)) {
|
||||
var data = accumulator.toString("utf8",5,length + 5);
|
||||
accumulator = accumulator.slice(length + 5);
|
||||
while(accumulator.length > 4) {
|
||||
var length = accumulator.readInt32BE(0);
|
||||
if(accumulator.length >= (length + 4)) {
|
||||
if(length < 2) {
|
||||
throw "ERROR: Incoming message length field is less than 2";
|
||||
}
|
||||
var type = accumulator.readUInt8(4),
|
||||
dataLength = length - 1,
|
||||
data = accumulator.toString("latin1",5,dataLength + 5);
|
||||
accumulator = accumulator.slice(length + 4);
|
||||
// Recase it
|
||||
console.log("MESSAGE",length,type);
|
||||
var recasedData = recase(data);
|
||||
var recasedData = Buffer.from(recase(data),"latin1");
|
||||
// Send it back
|
||||
var lengthBytes = Buffer.alloc(4);
|
||||
lengthBytes.writeUInt32BE(recasedData.length,0)
|
||||
lengthBytes.writeUInt32BE(recasedData.length + 1,0)
|
||||
console.log("RESPONSE",1,recasedData.length)
|
||||
sock.write(lengthBytes);
|
||||
var typeByte = Buffer.alloc(1);
|
||||
|
||||
@@ -36,10 +36,17 @@ Output:
|
||||
This demo requires the example task `reverser.js 8081` to be running in a separate command window.
|
||||
|
||||
<$button>
|
||||
<$action-sendmessage $message="tm-execute-job" 1="--pipe" 2="reverser" 3="[[Alice in Wonderland]]" statusTitle="JobStatus"/>
|
||||
<$action-sendmessage $message="tm-execute-job" 1="--pipe" 2="reverser" 3="[[Sample Text]]" statusTitle="JobStatus"/>
|
||||
Reverser
|
||||
</$button>
|
||||
|
||||
This demo requires the example task `./recaser.js 8081` or `./recase_erl.sh` to be running in a separate command window.
|
||||
|
||||
<$button>
|
||||
<$action-sendmessage $message="tm-execute-job" 1="--pipe" 2="recaser" 3="[[Sample Text]]" statusTitle="JobStatus"/>
|
||||
Recaser
|
||||
</$button>
|
||||
|
||||
!! Demo Output
|
||||
|
||||
The output generated by the demos appears here:
|
||||
|
||||
@@ -79,6 +79,7 @@
|
||||
"type": "socket-erlang",
|
||||
"host": "127.0.0.1",
|
||||
"port": 8081,
|
||||
"encoding": "latin1",
|
||||
"input": {
|
||||
"format": "rendered-text"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user