Beispiel #1
0
    def test_make_synchronized(self):
        doneSignal = CountDownLatch(10)

        class SynchedRunnable(Runnable):
            i = 0

            def run(self):
                self.i += 1
                doneSignal.countDown()

            run = synchronize.make_synchronized(run)

        runner = SynchedRunnable()
        for _ in xrange(10):
            Thread(runner).start()
        doneSignal. await ()
        self.assertEquals(10, runner.i)
Beispiel #2
0
class ChildSocket(_socketobject):
    
    def __init__(self):
        super(ChildSocket, self).__init__()
        self.activity_latch = CountDownLatch(1)

    def _unlatch(self):
        print "Unlatched"
        self.activity_latch.countDown()

    def _wait_on_latch(self):
        print "Waiting for activity on this child socket"
        self.activity_latch.await()
        #self.__class__ = _socketobject        
        print "Latch released"

    # FIXME raise exception for accept, listen, bind, connect, connect_ex

    # All ops that allow us to characterize the mode of operation of
    # this socket as being either Start TLS or SSL when connected

    def send(self, data):
        print "Child send", data
        if self.activity_latch.getCount():
            self._post_connect()
            self._unlatch()
        return super(ChildSocket, self).send(data)

    def recv(self, bufsize, flags=0):
        print "Child recv", bufsize
        if self.activity_latch.getCount():
            self._post_connect()
            self._unlatch()
        return super(ChildSocket, self).recv(bufsize, flags)

    # Presumably we would only close/shutdown immediately under exceptional situations;
    # regardless release the latch

    def close(self):
        if self.activity_latch.getCount():
            self._post_connect()
            self._unlatch()
        super(ChildSocket, self).close()

    def shutdown(self, how):
        if self.activity_latch.getCount():
            self._post_connect()
            self._unlatch()
        super(ChildSocket, self).shutdown(how)
Beispiel #3
0
 def fetch(self):
     """ generated source for method fetch """
     totalFetchDataSize = self.calcDataSize(len(self.variables))
     if totalFetchDataSize == 0:
         raise RuntimeException("no data to fetch")
     if totalFetchDataSize > dataSizeLimitForFetch:
         raise RuntimeException("exceed the max data limit for fetch")
     dataSet = GridDataSet(self.meta)
     latch = CountDownLatch(len(self.variables) * self.tRange.getSize() * self.zRange.getSize())
     exceptions = ConcurrentLinkedQueue()
     counter = AtomicInteger()
     taskCount = 0
     for variable in variables:
         dataSet.addVariable(variable, Grid4D(buffer_, self.meta.getDataType(), self.getOrigin(), self.getShape()))
         # 
         #  * not thread-safe
         #  
         while t < tRange.getEnd():
             # 
             #  * not thread-safe
             #  
             # 
             #  * not thread-safe
             #  
             while z < zRange.getEnd():
                 # 
                 #  * not thread-safe
                 #  
                 self.addTask(counter, data, curPos, variable, t, z, latch, exceptions)
                 curPos += xRange.getSize() * yRange.getSize() * meta.getDataType().getSize()
                 taskCount += 1
                 z += 1
             t += 1
     latch.await()
     if not exceptions.isEmpty():
         raise exceptions.peek()
     if counter.get() != taskCount:
         raise RuntimeException("not all task success")
     return dataSet
Beispiel #4
0
	def call(self):
		#CountdownLatch
		latch = CountDownLatch(1)

		#Read History.txt into TextArea
		br = BufferedReader(FileReader(self.hist_file))

		#Runnable Inner Class
		class HistoryTaskRunnable (Runnable):
			def __init__(self, textArea, br):
				self.textArea = textArea
				self.br = br
				self.sbf = StringBuffer()
			#@Override
			def run(self):
				while True:
					line = self.br.readLine()
					if line != None:
						self.sbf.append(line + "\n")
						# self.textArea.appendText(line + "\n") - Very slow
					else:
						break
				#Add Text to TextArea
				self.textArea.setText(self.sbf.toString())
				self.textArea.appendText("") #Used to trigger event handler
				#Close Buffered Reader
				br.close()

		#Run Later
		Platform.runLater(HistoryTaskRunnable(self.textArea, br))
			
		#Set Starting positions
		textArea.setScrollLeft(Double.MAX_VALUE)
		textArea.setScrollTop(Double.MAX_VALUE)

		#Make the Application Thread wait
		latch.await()
Beispiel #5
0
 def __init__(self):
     super(ChildSocket, self).__init__()
     self.activity_latch = CountDownLatch(1)
Beispiel #6
0
    def __init__(self, sock, keyfile=None, certfile=None, server_side=False, cert_reqs=CERT_NONE,
                 ssl_version=PROTOCOL_SSLv23, ca_certs=None,
                 do_handshake_on_connect=True, suppress_ragged_eofs=True, npn_protocols=None, ciphers=None,
                 server_hostname=None, _context=None):
        # TODO ^^ handle suppress_ragged_eofs
        self.sock = sock
        self.do_handshake_on_connect = do_handshake_on_connect
        self._sock = sock._sock  # the real underlying socket

        # FIXME in CPython, a check like so is performed - but this is
        # not quite correct, based on tests. We should revisit to see
        # if we can make this work as desired.

        # if do_handshake_on_connect and self._sock.timeout == 0:
        #     raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")

        self._connected = False
        if _context:
            self._context = _context
        else:
            if server_side and not certfile:
                raise ValueError("certfile must be specified for server-side "
                                 "operations")
            if keyfile and not certfile:
                raise ValueError("certfile must be specified")
            if certfile and not keyfile:
                keyfile = certfile
            self._context = SSLContext(ssl_version)
            self._context.verify_mode = cert_reqs
            if ca_certs:
                self._context.load_verify_locations(ca_certs)
            if certfile:
                self._context.load_cert_chain(certfile, keyfile)
            if npn_protocols:
                self._context.set_npn_protocols(npn_protocols)
            if ciphers:
                self._context.set_ciphers(ciphers)
            self.keyfile = keyfile
            self.certfile = certfile
            self.cert_reqs = cert_reqs
            self.ssl_version = ssl_version
            self.ca_certs = ca_certs
            self.ciphers = ciphers

        if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
            raise NotImplementedError("only stream sockets are supported")

        if server_side and server_hostname:
            raise ValueError("server_hostname can only be specified "
                             "in client mode")
        if self._context.check_hostname and not server_hostname:
            raise ValueError("check_hostname requires server_hostname")
        self.server_side = server_side
        self.server_hostname = server_hostname
        self.suppress_ragged_eofs = suppress_ragged_eofs

        self.ssl_handler = None
        # We use _sslobj here to support the CPython convention that
        # an object means we have handshaked. It is used by existing code
        # in the wild that looks at this ostensibly internal attribute.
        
        # FIXME CPython uses _sslobj to track the OpenSSL wrapper
        # object that's implemented in C, with the following
        # properties:
        #
        # 'cipher', 'compression', 'context', 'do_handshake',
        # 'peer_certificate', 'pending', 'read', 'shutdown',
        # 'tls_unique_cb', 'version', 'write'
        self._sslobj = self   # setting to self is not quite right

        self.engine = None

        if self.do_handshake_on_connect and self._sock.connected:
            log.debug("Handshaking socket on connect", extra={"sock": self._sock})
            if isinstance(self._sock, ChildSocket):
                # Need to handle child sockets differently depending
                # on whether the parent socket is wrapped or not.
                #
                # In either case, we cannot handshake here in this
                # thread - it must be done in the child pool and
                # before the child is activated.
                #
                # 1. If wrapped, this is going through SSLSocket.accept

                if isinstance(self._sock.parent_socket, SSLSocket):
                    # already wrapped, via `wrap_child` function a few lines below
                    log.debug(
                        "Child socket - will handshake in child loop type=%s parent=%s",
                        type(self._sock), self._sock.parent_socket,
                        extra={"sock": self._sock})
                    self._sock._make_active()

                # 2. If not, using code will be calling SSLContext.wrap_socket
                #    *after* accept from an unwrapped socket

                else:
                    log.debug("Child socket will wrap self with handshake", extra={"sock": self._sock})
                    setup_handshake_latch = CountDownLatch(1)

                    def setup_handshake():
                        handshake_future = self.do_handshake()
                        setup_handshake_latch.countDown()
                        return handshake_future

                    self._sock.ssl_wrap_self = setup_handshake
                    self._sock._make_active()
                    setup_handshake_latch.await()
                    log.debug("Child socket waiting on handshake=%s", self._handshake_future, extra={"sock": self._sock})
                    self._sock._handle_channel_future(self._handshake_future, "SSL handshake")
            else:
                self.do_handshake()

        if hasattr(self._sock, "accepted_children"):
            def wrap_child(child):
                log.debug(
                    "Wrapping child socket - about to handshake! parent=%s",
                    self._sock, extra={"sock": child})
                child._wrapper_socket = self.context.wrap_socket(
                    _socketobject(_sock=child),
                    do_handshake_on_connect=self.do_handshake_on_connect,
                    suppress_ragged_eofs=self.suppress_ragged_eofs,
                    server_side=True)
                if self.do_handshake_on_connect:
                    # this handshake will be done in the child pool - initChannel will block on it
                    child._wrapper_socket.do_handshake()
            self._sock.ssl_wrap_child_socket = wrap_child