Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

133 рядки
4.0 KiB

  1. #! /usr/bin/python
  2. import os, struct, sys, random
  3. import gd
  4. from cpushare.proto_const import *
  5. from cpushare.exceptions import CompilationError
  6. # Our input data
  7. source = False
  8. result = False
  9. todo = 0
  10. # Parse command line
  11. if len(sys.argv) < 4:
  12. print 'Usage: %s <buy_order.cpu> -<mode> [image list]' % (sys.argv[0],)
  13. sys.exit(-1)
  14. mode = sys.argv[2][1]
  15. done = 0
  16. source = sys.argv[3:]
  17. result = [False] * len(source)
  18. class buy_state_machine_class(object):
  19. buy_api = '0.0.0'
  20. w, h = 0, 0
  21. index = -1
  22. answer = ''
  23. def __init__(self, protocol):
  24. self.protocol = protocol
  25. self.handler = self.stringReceived
  26. def start(self):
  27. global todo, source
  28. if todo >= len(source):
  29. return # We're finished... FIXME: is the transaction kept stuck?
  30. self.index = todo
  31. todo += 1
  32. gd.gdMaxColors = 256 * 256 * 256
  33. im = gd.image(source[self.index])
  34. # Send argument count
  35. self.protocol.sendString(PROTO_SECCOMP_FORWARD + chr(3))
  36. # Send arguments
  37. msg = chr(3)
  38. msg += "bytecode\0"
  39. msg += "-1\0"
  40. msg += source[self.index] + "\0"
  41. self.protocol.sendString(PROTO_SECCOMP_FORWARD + msg)
  42. # Send image size
  43. (self.w, self.h) = (w, h) = im.size()
  44. print 'sending %s (%dx%d)' % (source[self.index], w, h)
  45. msg = chr(w / 256) + chr(w % 256) + chr(h / 256) + chr(h % 256)
  46. self.protocol.sendString(PROTO_SECCOMP_FORWARD + msg)
  47. # Send image contents
  48. msg = ''
  49. for y in range(h):
  50. for x in range(w):
  51. p = im.getPixel((x, y))
  52. rgb = im.colorComponents(p)
  53. msg += chr(rgb[1]) # use green coordinate
  54. self.protocol.sendString(PROTO_SECCOMP_FORWARD + msg)
  55. print '%s bytes sent' % len(msg)
  56. def stringReceived(self, string):
  57. control = string[0]
  58. data = string[1:]
  59. if control == PROTO_SECCOMP_FORWARD:
  60. self.answer += data
  61. if ord(self.answer[-1]) != 0:
  62. return
  63. global done, result
  64. result[self.index] = self.answer[:-1]
  65. print self.answer[:-1],
  66. self.protocol.sendString(PROTO_SECCOMP_SUCCESS)
  67. done += 1
  68. if done < len(source):
  69. return
  70. from twisted.internet import reactor
  71. reactor.stop()
  72. elif control == PROTO_SECCOMP_SIGNAL:
  73. print 'Checkpoint starting'
  74. elif control == PROTO_LOG:
  75. print repr(data)
  76. else:
  77. if control == PROTO_SECCOMP_SUCCESS:
  78. pass
  79. elif control == PROTO_SECCOMP_FAILURE:
  80. status = struct.unpack('!i', data)[0]
  81. exit_code = status >> 8
  82. signal = status & 0xff
  83. s = 'Seccomp failure: status %d, exit_code %d, signal %d.' % \
  84. (status, exit_code, signal)
  85. print s
  86. else:
  87. s = 'Unknown failure %d - %s' % (ord(control), repr(string))
  88. self.protocol.sendString(PROTO_SECCOMP_FAILURE + s)
  89. print s
  90. self.protocol.sendString(PROTO_SECCOMP_FAILURE)
  91. self.protocol.transport.loseConnection()
  92. def connectionLost(self):
  93. pass
  94. # Build local module
  95. from new import module
  96. m = module('cpushare_buy')
  97. # Create bytecode -- i686 only
  98. from cpushare.seccomp_gen import seccomp_gen_class
  99. m.seccomp_gen_hash = {}
  100. m.seccomp_gen_hash['i686'] = seccomp_gen_class('bytecode', 'i686')
  101. m.seccomp_gen_hash['i686'].heap_kbytes = 1
  102. m.seccomp_gen_hash['i686'].stack_kbytes = 1
  103. # Estimate of the max number of seconds that the sell client
  104. # will take to checkpoint and send its state back to us
  105. m.checkpoint_sec = 10
  106. # Our buying state machine
  107. m.buy_state_machine_class = buy_state_machine_class
  108. # Append our module to the global list of modules
  109. sys.modules['cpushare_buy'] = m
  110. # Build a new command line and run twistd
  111. sys.argv = [sys.argv[0], '-q', '-n', 'cpushare', '--order', sys.argv[1]]
  112. from twisted.scripts.twistd import run
  113. run()