-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathReceiver.py
executable file
·55 lines (37 loc) · 1.69 KB
/
Receiver.py
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
#!/usr/bin/env python
import os
from time import sleep
from sys import stdout, exit
import argparse
ENDLINE = '\x92'
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group( required = True )
group.add_argument( '--directory', '-d', help = 'Use all files in this directory in the order "ls" returns them (BEWARE: MUST BE WRITABLE)')
group.add_argument( '--files', '-f', help = 'Use the listed files as in the order they are given', nargs = '*')
args = parser.parse_args()
# print args
files = args.files
if args.directory :
files = [ (args.directory + x) for x in os.listdir( args.directory ) ]
# print files
Delivered = False
def tick(file) : # Returns the "Tick bit" (First file's Owner Read bit)
return (os.stat(file)[0] % 512) ^ 512 # as boolean
def consume_char( char ) :
stdout.write(str(char)) # Print Char without newlines
stdout.flush() # Flush the output stream
last_state = tick(files[0]) # Gets the state of Tick Bit
while not Delivered :
cur_state = tick(files[0]) # Saves the current Tick Bit
while cur_state == last_state : # If the current and previous Tick bits are same
sleep (0.1) # Wait a while
cur_state = tick(files[0]) # Get new Tick bit in case it changed
# Spin-Lock until Tick bit Changes
for f in files : # Tick happened - New data in file privileges!
mod = (os.stat(f)[0]) # Get the privilege int
char = chr(mod % 256) # Strip it to 1 byte and save it as ASCII Character
if char == (ENDLINE) : # If it is the Special Character
Delivered = True # The Message has been delivered
break
consume_char( char ) # Consume char in some way. Default: print it.
last_state = cur_state