- I got megaphone to receive packets.
- I modified megaphone to send packets.
- I noted that data was not getting delivered to ECM.
Last time, I resolved to create a "heartbeat" style message between megaphone and ECM. In the course of doing this I noted that, if I tried to connect ECM to megaphone after a crash of ECM, data would not get delivered. That is, if I start up ejabberd/megaphone and then connect ECM to it, data gets delivered to ECM. If I try reconnecting ECM to megaphone, things don't work.
The nodejs side of the heartbeat does most of the work. When ECM first connects to megaphone, it sets up an interval timer. Each time the interval timer fires, ECM sends another heartbeat. The systems distinguish heartbeats from regular messages by using connection ID 0 for heartbeat messages.
Here is the (modified) code for ECM that starts up the heartbeat process:
me.socket.connect(config.server.port, config.server.host, function () {
console.log ("now connected to " + config.server.host + ":" + config.server.port);
setInterval(me.sendHeartBeat, 5000);
});
This fragment only includes the code for the connect. The function to send the heartbeat is also pretty simple:
me.sendHeartBeat = function()
{
var hbmsg =
"0000000041|"
+ "00000000000000000000|"
+ "heartbeat";
me.socket.write(hbmsg);
};
The code for receiving a message had to be modified to check for heartbeat messages:
me.processPacket = function ()
{
console.log("packet: " + me.packet);
if (me.packet && me.packet.complete)
{
if (me.packet.connectionId == 0)
{
console.log("heartbeat");
}
else
{
var connectionId = me.packet.connectionId;
var intCID = parseInt(connectionId, 10);
var exchange = me.sockets.get(intCID);
console.log("got exchange: " + exchange + " about to send reply");
exchange.sendReply(me.packet.content);
me.packet = null;
}
}
}
The megaphone side is simpler than the ECM side. It just looks for messages with a connection ID of 0 and immediately sends a response for them:
process_request(ConnectionID, Data) ->
if
ConnectionID == 0 ->
Response = "heartbeat",
?MODULE:send(ConnectionID, Response);
true ->
NewData = parse_packet(Data),
ResponseData = ejabberd_http_bind:process_request(NewData, {{127,0,0,1}, 1234}),
Response = response_data_to_response(ResponseData),
?MODULE:send(ConnectionID, Response)
end.
So now everything is peachy. Except that the ECM side simply prints out "packet: [object]" and "heartbeat" over and over again. Next time I will try to figure out why this is.
No comments:
Post a Comment