Last active
August 29, 2015 14:16
-
-
Save ElectricImpSampleCode/f65c685694e8e9610872 to your computer and use it in GitHub Desktop.
Code examples for agent.send() troubleshooting guide.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server.settimeoutpolicy(SUSPEND_ON_ERROR, WAIT_FOR_ACK, 10); | |
// Program does some work... | |
foreach (count, blob in buffer) { | |
// Every send will not return until the server has ACK’d | |
// Assume no timeout for this example | |
local outcome = agent.send(“process.data”, blob); | |
server.log(“Blob %u sent to server”, count + 1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function increaseBuffer(dataToSend) { | |
local oldSize = 0; | |
local newSize = dataToSend.len(); | |
if (newSize > 3456) local oldSize = imp.setsendbuffersize(newSize); | |
server.log("Buffer increased to " + newSize + " bytes from " + oldSize + " bytes"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server.settimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 10); | |
dataToSend <- null; | |
retryTime <- 600; // Ten minutes | |
// Code at some point will place the data it needs to send into dataToSend and then call | |
// transmitter(), a wrapper for agent.send() and related calls, to send it. | |
function transmitter() { | |
// Make sure we have data to send | |
if (dataToSend == null) return; | |
if (server.isconnected()) { | |
// At this point we have WiFi connected so try to add dataToSend to TCP send buffer | |
local outcome = agent.send(“message”, dataToSend); | |
if (outcome == 0) { | |
// Record successful addition of data to impOS buffer by zero-ing dataToSend | |
// This can be used to indicate dataToSend is free for more data | |
dataToSend = null; | |
} else { | |
// Couldn’t add dataToSend to the buffer OR | |
// WiFi has been lost OR some other failure | |
// RETURN_ON_ERROR in force so we need to re-connect before | |
// attempting to add dataToSend to the buffer again | |
// Reschedule transmitter() | |
imp.wakeup(retryTime, transmitter); | |
} | |
} else { | |
// WiFi is disconnected, so attempt to reconnect as per RETURN_ON_ERROR | |
// Whatever the outcome, we come back to transmitter(). | |
// We can't schedule transmitter() directly as server.connect()'s | |
// callback expects a single parameter to take outcome indicator integer | |
server.connect(function(reason) { transmitter(); } ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment