Last active
December 16, 2015 05:09
-
-
Save nowelium/5382439 to your computer and use it in GitHub Desktop.
non-blocking Timer
This file contains hidden or 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
Timer | |
t1 := Timer Interval clone | |
t1 setInterval(1) | |
t1 setCallback(method(timestamp, | |
"every 1 sec" println | |
)) | |
t1 start | |
t2 := Timer Interval clone | |
t2 setInterval(0.1) | |
t2 setCallback(method(timestamp, | |
"every 100 ms" println | |
)) | |
t2 start | |
Timer Interval Loop start |
This file contains hidden or 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
Thread | |
Socket | |
Server | |
Timer := Object clone | |
Timer do ( | |
counter := 0 | |
increment := method( | |
counter = counter + 1 | |
) | |
loop := method(endCallback, | |
while(Scheduler yieldingCoros size > 0, yield) | |
endCallback call | |
) | |
) | |
Timer Interval := Object clone do ( | |
Loop := nil | |
processId := System thisProcessPid | |
counter := 0 | |
thread := nil | |
id ::= nil | |
interval ::= 0.1 | |
callback ::= method() | |
init := method( | |
counter = Timer increment | |
id = "#{processId}.#{counter}" interpolate | |
) | |
start := method( | |
if(thread isNil not) then ( | |
Exception raise("timer still running") | |
) | |
Timer Interval Loop addListener(id, block(id, timestamp, | |
self @callback(timestamp) | |
)) | |
thread = Thread createThread(""" | |
id := "#{id}" | |
host := "localhost" | |
port := #{Timer Interval Loop port} | |
interval := #{interval} | |
socket := Socket clone | |
socket setHost(host) | |
socket setPort(port) | |
socket connect | |
if (socket isOpen) then ( | |
"client connected" println | |
) else ( | |
Exception raise("connection failed") | |
) | |
timestamp := Date now asNumberString | |
msg := "\#\{id\}:\#\{timestamp\}" | |
loop( | |
timestamp := Date now asNumberString | |
socket write(msg interpolate) | |
wait(interval) | |
) | |
""" interpolate) | |
) | |
) | |
Timer Interval Loop := Server clone do ( | |
setPort(11002) | |
listener := Map clone | |
addListener := method(id, callback, | |
listener atPut(id, callback) | |
) | |
Handler := Object clone do ( | |
handle := method(socket, server, | |
while(socket isOpen, | |
if(socket read) then ( | |
data := socket readBuffer | |
id := data beforeSeq(":") | |
timestamp := data afterSeq(":") | |
if(server listener hasKey(id)) then ( | |
callback := server listener at(id) | |
callback @call(id, timestamp asNumber) | |
) | |
) | |
socket readBuffer empty | |
) | |
socket close | |
) | |
) | |
handleSocket := method(socket, Handler clone @handle(socket, self)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment