Last active
January 4, 2016 17:39
-
-
Save IyadAssaf/8655039 to your computer and use it in GitHub Desktop.
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
#---------# | |
# Traffic # | |
#---------# | |
require 'coffee-script' | |
# debugging | |
trafficReport = 1 | |
color = require("ansi-color").set | |
# Queue array for connection counting | |
trafficJam = [] | |
exports.createQueue = (id) -> | |
trafficJam.push new traffic id | |
return trafficJam[trafficJam.length-1] | |
exports.extendQueue = (id) -> | |
for car in trafficJam | |
if car.id is id | |
console.log 'Extend: SUCCESSFUL' | |
return car | |
exports.createQueue id | |
exports.releaseQueueWithID = (id) -> | |
i = 0 | |
for car in trafficJam | |
i++ | |
if car.id == id | |
car.clearJobs() | |
trafficJam.splice i-1, 1 | |
break | |
exports.queueDoesExist = (id) -> | |
for car in trafficJam | |
if car.id == id | |
return 1 | |
return 0 | |
# ------- # | |
# Signals # | |
# ------- # | |
socketArray = [] | |
isNew = (id) -> | |
i = -1 | |
for socket in socketArray | |
if socket.id == id | |
i = i + 1 | |
return i | |
return -1 | |
exports.connect = (session_id) -> | |
isNewID = isNew session_id | |
if isNewID is -1 | |
socketObj = | |
id: session_id | |
count: 1 | |
socketArray.push socketObj | |
report(color('CONNECT: Creating a queue, count: 1, sessionID: ' + session_id , "green_bg")) | |
return exports.createQueue session_id | |
else | |
socketArray[isNewID].count++ | |
report(color('CONNECT: Extending a queue, count: ' + socketArray[isNewID].count + ', sessionID: ' + session_id, "green_bg")) | |
return exports.extendQueue session_id | |
exports.disconnect = (session_id) -> | |
isNewID = isNew session_id | |
if isNewID isnt -1 | |
socketArray[isNewID].count-- | |
report(color('DISCONNECT: The count is now ' + socketArray[isNewID].count + ', sessionID: ' + session_id, "red_bg")) | |
if socketArray[isNewID].count is 0 | |
socketArray.splice(isNewID, 1) | |
report(color('DISCONNECT: Releasing the queue' , "red_bg")) | |
exports.releaseQueueWithID session_id | |
else | |
report(color('DISCONNECT: This id hasnt been used', "red_bg")) | |
print = () -> | |
for obj in socketArray | |
report(color(JSON.stringify(obj, null, 4), "green_bg")) | |
# | |
# Debugging console | |
# | |
report = (message) -> | |
if trafficReport | |
console.log message | |
{EventEmitter} = require 'events' | |
class traffic extends EventEmitter | |
trafficQueue = [] | |
#init | |
constructor: (@id) -> | |
console.log 'Constructor' | |
@emit 'test', 'test' | |
@on 'processJobReturn', (completedJob) => | |
console.log 'processJobReturn' | |
@emit 'completedJob', completedJob | |
if trafficQueue[0] | |
trafficQueue.splice 0, 1 | |
if trafficQueue[0] | |
@executeProcess() | |
null | |
null | |
addLocalJob: (job, argument) -> | |
trafficQueue.push | |
job: job | |
args: argument | |
if trafficQueue.length is 1 | |
if trafficQueue[0] | |
@executeProcess() | |
null | |
executeProcess: () -> | |
if trafficQueue[0] | |
console.log 'Executing job: ' + trafficQueue[0].job | |
@emit 'processJob', trafficQueue[0] | |
null | |
clearJobs: () -> | |
console.log 'Clear jobs' | |
trafficQueue = [] | |
null |
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
# example usage | |
require 'coffee-script' | |
traffic = require 'traffic' | |
traffic.addJob 'job', 'some arguments' | |
traffic.on 'processJob', (data) -> | |
console.log 'process job: ' + data | |
traffic.emit 'processJobReturn', data | |
traffic.on 'completedJob', (job) -> | |
console.log 'Completed job: ' + job |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment