'Callback function for switching state and waking up main thread.
This function is the only function witch can be called asynchronous.
The ready can switch Connection to three states:
WAIT_LEN if request was oneway.
SEND_ANSWER if request was processed in normal way.
CLOSED if request throws unexpected exception.
The one wakes up main thread.'

@locked
def ready(self, all_ok, message):

    assert (self.status == WAIT_PROCESS)
    if (not all_ok):
        self.close()
        self.wake_up()
        return
    self.len = ''
    if (len(message) == 0):
        self.message = ''
        self.status = WAIT_LEN
    else:
        self.message = (struct.pack('!i', len(message)) + message)
        self.status = SEND_ANSWER
    self.wake_up()
