Skip to content

Instantly share code, notes, and snippets.

@zackproser
Created July 21, 2015 05:53
Show Gist options
  • Save zackproser/9ee57f11edaedcde3109 to your computer and use it in GitHub Desktop.
Save zackproser/9ee57f11edaedcde3109 to your computer and use it in GitHub Desktop.
Demonstrates how to fork a process within an express server in response to a valid command.
/**
* Processes an authorized admin's attack request and launches the attack
*
* Handles tracking the attack child process at the app-level so it can be referenced later / stopped
*
* @param {String} requesting_admin - The phone number of the requesting admin
* @param {String} target - The phone number of the target to be attacked
*
* @return void
*/
handleAdminAttackRequest = function(requesting_admin, target) {
//Ensure target is not already being attacked (we have some degree of decency - no?)
if (!isTargetBeingAttacked(target)) {
//Fork a new attack process - passing the requesting admin's phone number and target phone number as string arguments
var CatfactsAttack = child_process.fork('./attack.js', [requesting_admin, target]);
//Handle messages sent back from child processes
CatfactsAttack.on('message', function(m){
switch(m.status){
case 'invalid_status':
CatfactsAttack.kill();
//Send invalid target sms back to admin
sendResponseSMS(m.requesting_admin, 'Oops! ' + target + ' doesn\'t appear to be a valid number. Attack NOT Launched!');
break;
case 'starting_attack':
//Tag the attack child_process with its target number
CatfactsAttack.target_number = m.child_target;
//Add child_process to app-level array of current attacks
beginTrackingAttack(CatfactsAttack);
//Send sms confirming attack back to admin
sendResponseSMS(m.requesting_admin, 'Attack Vector Confirmed: CatFacts Bombardment Underway! - Text: "downboy ' + m.child_target + '" to stop attack.');
break;
case 'exhausted':
//Remove number from app-level array of numbers being attacked
stopTrackingAttack(m.child_target);
//Send exhaustion notification sms back to admin
sendResponseSMS(m.requesting_admin, 'CatFacts Attack on ' + target + ' ran out of facts! Attack Complete.');
CatfactsAttack.kill();
break;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment