-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsyncServer
More file actions
executable file
·84 lines (68 loc) · 2.15 KB
/
syncServer
File metadata and controls
executable file
·84 lines (68 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env coffee
# Synchronize multiples server for sharing resources
# Include third libraries
async = require 'async'
express = require 'express'
bodyParser = require 'body-parser'
# Indicate if any client is processing or not
actualClient = false
# Registered clients, name -> actual generation
clients = []
# Server
httpServer = express()
# Number of request process at same time
PARALELL_TASKS = 1
# Default port
port = 8081
# Queue for the requests
requestQueue = {}
processRequest = (task, callback) ->
action = task.action
answer = false
generation = task.generation
project = task.project
switch action
when 'take'
# Update the generation value for the project
found = false
for client in clients
if project is client.project
client.generation = generation
found = true
# Insert the project is not found
if not found
clients.push
generation: generation
project : project
# Search next client
if not actualClient and clients.length > 1
minGeneration = Number.MAX_VALUE
nextProject = null
for client in clients
if minGeneration > client.generation
minGeneration = client.generation
nextProject = client.project
if nextProject == project
actualClient = project
answer = true
when 'leave'
actualClient = false
answer = true
callback answer
# Read POST content
httpServer.use bodyParser.urlencoded extended: true
# Handle POST requests
httpServer.post '/', (request, response) ->
newTask =
action : request.body.action
generation : (parseInt request.body.generation, 10) or 0
project : request.body.project
requestQueue.push newTask,
# Callback when the task finished
(answer) ->
response.send JSON.stringify answer
# Start HTTP server in port
httpServer.listen port
requestQueue = async.queue processRequest, PARALELL_TASKS
console.log 'Sync Server listening on port ' + port
console.log 'Start time :' + new Date