Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

139 rader
4.2 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]
  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 mode, todo, source
  28. gd.gdMaxColors = 256 * 256 * 256
  29. while todo < len(source):
  30. try:
  31. self.index = todo
  32. im = gd.image(source[self.index])
  33. break
  34. except:
  35. todo += 1
  36. self.result = "Error\n"
  37. if todo >= len(source):
  38. return # We're finished... FIXME: isn't the transaction stuck?
  39. todo += 1
  40. # Send argument count
  41. self.protocol.sendString(PROTO_SECCOMP_FORWARD + chr(3))
  42. # Send arguments
  43. msg = chr(3)
  44. msg += "bytecode\0"
  45. msg += mode + "\0"
  46. msg += source[self.index] + "\0"
  47. self.protocol.sendString(PROTO_SECCOMP_FORWARD + msg)
  48. # Send image size
  49. (self.w, self.h) = (w, h) = im.size()
  50. print 'sending %s (%dx%d)' % (source[self.index], w, h)
  51. msg = chr(w / 256) + chr(w % 256) + chr(h / 256) + chr(h % 256)
  52. self.protocol.sendString(PROTO_SECCOMP_FORWARD + msg)
  53. # Send image contents
  54. msg = ''
  55. for y in range(h):
  56. for x in range(w):
  57. p = im.getPixel((x, y))
  58. rgb = im.colorComponents(p)
  59. msg += chr(rgb[1]) # use green coordinate
  60. self.protocol.sendString(PROTO_SECCOMP_FORWARD + msg)
  61. print '%s bytes sent' % len(msg)
  62. def stringReceived(self, string):
  63. control = string[0]
  64. data = string[1:]
  65. if control == PROTO_SECCOMP_FORWARD:
  66. self.answer += data
  67. if ord(self.answer[-1]) != 0:
  68. return
  69. global done, result
  70. result[self.index] = self.answer[:-1]
  71. print self.answer[:-1],
  72. self.protocol.sendString(PROTO_SECCOMP_SUCCESS)
  73. done += 1
  74. if done < len(source):
  75. return
  76. from twisted.internet import reactor
  77. reactor.stop()
  78. elif control == PROTO_SECCOMP_SIGNAL:
  79. print 'Checkpoint starting'
  80. elif control == PROTO_LOG:
  81. print repr(data)
  82. else:
  83. if control == PROTO_SECCOMP_SUCCESS:
  84. pass
  85. elif control == PROTO_SECCOMP_FAILURE:
  86. status = struct.unpack('!i', data)[0]
  87. exit_code = status >> 8
  88. signal = status & 0xff
  89. s = 'Seccomp failure: status %d, exit_code %d, signal %d.' % \
  90. (status, exit_code, signal)
  91. print s
  92. else:
  93. s = 'Unknown failure %d - %s' % (ord(control), repr(string))
  94. self.protocol.sendString(PROTO_SECCOMP_FAILURE + s)
  95. print s
  96. self.protocol.sendString(PROTO_SECCOMP_FAILURE)
  97. self.protocol.transport.loseConnection()
  98. def connectionLost(self):
  99. pass
  100. # Build local module
  101. from new import module
  102. m = module('cpushare_buy')
  103. # Create bytecode -- i686 only
  104. from cpushare.seccomp_gen import seccomp_gen_class
  105. m.seccomp_gen_hash = {}
  106. m.seccomp_gen_hash['i686'] = seccomp_gen_class('bytecode', 'i686')
  107. m.seccomp_gen_hash['i686'].heap_kbytes = 1
  108. m.seccomp_gen_hash['i686'].stack_kbytes = 1
  109. # Estimate of the max number of seconds that the sell client
  110. # will take to checkpoint and send its state back to us
  111. m.checkpoint_sec = 10
  112. # Our buying state machine
  113. m.buy_state_machine_class = buy_state_machine_class
  114. # Append our module to the global list of modules
  115. sys.modules['cpushare_buy'] = m
  116. # Build a new command line and run twistd
  117. sys.argv = [sys.argv[0], '-q', '-n', 'cpushare', '--order', sys.argv[1]]
  118. from twisted.scripts.twistd import run
  119. run()