Beispiel #1
0
        public RequestToCancel createRequestForMessage(BMessage msg, BAsyncResultIF <BMessage> asyncResult)
        {
            ERequestDirection requestDirection = ERequestDirection.FORWARD;
            int timeout = this.timeoutMillisClient;

            // Reverse HTTP request (long-poll)?
            bool isReverse = (msg.header.flags & BMessageHeader.FLAG_RESPONSE) != 0;

            if (isReverse)
            {
                requestDirection = ERequestDirection.REVERSE;
                timeout          = System.Threading.Timeout.Infinite; // timeout controlled by server, 10min by default.
            }

            RequestToCancel r = new RequestToCancel(this,
                                                    requestDirection,
                                                    msg.header.messageId,
                                                    msg.buf,
                                                    null,
                                                    0L,
                                                    0L,
                                                    timeoutMillisClient,
                                                    timeout,
                                                    asyncResult);

            addRequest(r);
            return(r);
        }
Beispiel #2
0
        public void recv(BServer server, BMessage msg, BAsyncResultIF <BMessage> asyncResult)
        {
            if (log.isDebugEnabled())
            {
                log.debug("recv(");
            }

            BInput bin       = getInput(msg.header, msg.buf);
            Object methodObj = bin.load();

            if (log.isDebugEnabled())
            {
                log.debug("methodObj=" + methodObj);
            }

            MethodResult methodResult = new MethodResult(this, asyncResult, bin);

            BTargetId clientTargetId = bin.header.targetId;

            if (log.isDebugEnabled())
            {
                log.debug("clientTargetId=" + clientTargetId);
            }

            if (log.isDebugEnabled())
            {
                log.debug("server.recv");
            }
            server.recv(clientTargetId, methodObj, methodResult);

            if (log.isDebugEnabled())
            {
                log.debug(")recv");
            }
        }
Beispiel #3
0
        public void send <T>(Object obj, BAsyncResultIF <T> asyncResult)
        {
            if (log.isDebugEnabled())
            {
                log.debug("send(" + obj);
            }
            try {
                BOutput bout = getOutput();

                if (log.isDebugEnabled())
                {
                    log.debug("obj -> message");
                }
                bout.store(obj);
                BMessage msg = bout.toMessage();

                if (log.isDebugEnabled())
                {
                    log.debug("wire.send");
                }
                BAsyncResultIF <BMessage> outerResult = new MyAsyncResultRelogin <T>(this, (BMethodRequest)obj, asyncResult);
                wire.send(msg, outerResult);
            }
            catch (Exception e) {
                asyncResult.setAsyncResult(default(T), e);
            }
        }
Beispiel #4
0
        public void negotiateProtocolClient(BAsyncResultIF <Boolean> asyncResult)
        {
            if (log.isDebugEnabled())
            {
                log.debug("negotiateProtocolClient(");
            }

            ByteBuffer buf  = ByteBuffer.allocate(BNegotiate.NEGOTIATE_MAX_SIZE);
            BNegotiate nego = new BNegotiate(apiDesc);

            nego.write(buf);
            buf.flip();

            BAsyncResultIF <BMessage> outerResult = new MyNegoAsyncResult(this, asyncResult);

            if (log.isDebugEnabled())
            {
                log.debug("wire.send");
            }
            BMessageHeader header = new BMessageHeader();

            header.messageId = wire.makeMessageId();
            BMessage msg = new BMessage(header, buf, null);

            wire.send(msg, outerResult);
            if (log.isDebugEnabled())
            {
                log.debug(")negotiateProtocolClient");
            }
        }
Beispiel #5
0
            public void setAsyncResult(ByteBuffer buf, Exception ex)
            {
                if (log.isDebugEnabled())
                {
                    log.debug("setAsyncResult" + this + "(buf=" + buf + ", ex=" + ex);
                }
                if (Interlocked.Increment(ref isOpen) == 1)
                {
                    try
                    {
                        if (ex == null && buf != null && buf.remaining() != 0)
                        {
                            BMessageHeader header = new BMessageHeader();

                            bool nego = BNegotiate.isNegotiateMessage(buf);
                            if (nego)
                            {
                                BNegotiate negoResponse = new BNegotiate();
                                negoResponse.read(buf);

                                header.messageId = messageId;

                                BTransport utransport = wire.getClientUtilityRequests().getTransport();
                                utransport.applyNegotiate(negoResponse);
                            }
                            else
                            {
                                header.read(buf);
                            }

                            BMessage msg = buf != null ? new BMessage(header, buf, null) : null;
                            if (log.isDebugEnabled())
                            {
                                log.debug("asyncResult.set");
                            }
                            asyncResult.setAsyncResult(msg, ex);
                        }
                        else
                        {
                            asyncResult.setAsyncResult(null, ex);
                        }
                    }
                    catch (Exception e)
                    {
                        asyncResult.setAsyncResult(null, e);
                    }
                }
                if (log.isDebugEnabled())
                {
                    log.debug(")setAsyncResult");
                }
            }
Beispiel #6
0
        private void internalSend(BMessage msg, BAsyncResultIF<BMessage> asyncResult)
        {
		    ByteBuffer obuf = ByteBuffer.allocate(msg.buf.remaining());
		    obuf.put(msg.buf);
		    obuf.flip();

            putStreams(msg.streams, asyncResult);

            BMessageHeader header = new BMessageHeader();
            header.read(obuf);
            BMessage omsg = new BMessage(header, obuf, null);
            asyncResult.setAsyncResult(omsg, null);
        }
Beispiel #7
0
        private void internalSend(BMessage msg, BAsyncResultIF <BMessage> asyncResult)
        {
            ByteBuffer obuf = ByteBuffer.allocate(msg.buf.remaining());

            obuf.put(msg.buf);
            obuf.flip();

            putStreams(msg.streams, asyncResult);

            BMessageHeader header = new BMessageHeader();

            header.read(obuf);
            BMessage omsg = new BMessage(header, obuf, null);

            asyncResult.setAsyncResult(omsg, null);
        }
Beispiel #8
0
 public LongPoll(HServerR pthis, BMessage methodResult)
 {
     if (log.isDebugEnabled())
     {
         log.debug("LongPoll(" + methodResult);
     }
     this.pthis = pthis;
     if (methodResult != null)
     {
         this.methodResult = methodResult;
     }
     else
     {
         BOutput outp = pthis.transport.getOutput();
         outp.header.flags |= BMessageHeader.FLAG_RESPONSE;
         outp.store(null);                 // irgendwas, damit auch der Header in den ByteBuffer geschrieben wird.
         this.methodResult = outp.toMessage();
     }
     if (log.isDebugEnabled())
     {
         log.debug(")LongPoll");
     }
 }
Beispiel #9
0
        } // class LongPoll

        protected void sendLongPoll(BMessage obj)
        {
            if (log.isDebugEnabled())
            {
                log.debug("sendLongPollInWorkerThread(" + obj);
            }
            lock (mutex)
            {
                if (!isDone)
                {
                    LongPoll lp = new LongPoll(this, obj);
                    lp.run();
                }
            }
            //lock(this)
            //{
            //    currentLongPoll_access_sync = lp;
            //    Monitor.PulseAll(this);
            //}
            if (log.isDebugEnabled())
            {
                log.debug(")sendLongPollInWorkerThread");
            }
        }
Beispiel #10
0
 public virtual void sendR(BMessage msg, BAsyncResultIF <BMessage> asyncResult)
 {
     internalSend(msg, asyncResult);
 }
Beispiel #11
0
            public void setAsyncResult(BMessage msg, Exception ex)
            {
                if (log.isDebugEnabled())
                {
                    log.debug("setAsyncResult(" + msg + ", ex=" + ex);
                }
                bool relogin = false;

                try
                {
                    if (ex != null)
                    {
                        // BYPS relogin error? (HTTP 401)
                        relogin = transport.internalIsReloginException(ex, transport.getObjectTypeId(methodRequest));
                        if (!relogin)
                        {
                            if (log.isDebugEnabled())
                            {
                                log.debug("return ex");
                            }
                            innerResult.setAsyncResult(default(T), ex);
                        }
                    }
                    else
                    {
                        if (log.isDebugEnabled())
                        {
                            log.debug("obj <- message");
                        }
                        BInput bin = transport.getInput(msg.header, msg.buf);
                        T      obj = (T)bin.load();

                        if (log.isDebugEnabled())
                        {
                            log.debug("return obj=" + obj);
                        }
                        innerResult.setAsyncResult(obj, null);
                    }
                }
                catch (Exception e)
                {
                    if (log.isDebugEnabled())
                    {
                        log.debug("exception=" + e);
                    }
                    try
                    {
                        relogin = transport.internalIsReloginException(e, transport.getObjectTypeId(methodRequest));
                    }
                    catch (Exception ignored) {
                        if (log.isDebugEnabled())
                        {
                            log.debug("ignored exception=", ignored);
                        }
                    }

                    if (!relogin)
                    {
                        if (log.isDebugEnabled())
                        {
                            log.debug("return ex");
                        }
                        innerResult.setAsyncResult(default(T), e);
                    }
                }

                if (log.isDebugEnabled())
                {
                    log.debug("relogin="******")setAsyncResult");
                }
            }
Beispiel #12
0
        public void negotiateProtocolClient(BAsyncResultIF <Boolean> asyncResult)
        {
            if (log.isDebugEnabled())
            {
                log.debug("negotiateProtocolClient(");
            }
            if (log.isDebugEnabled())
            {
                log.debug("negotiateActive=" + negotiateActive);
            }

            // Check that we do not run into recursive authentication requests.
            lock (asyncResultsWaitingForAuthentication)
            {
                // Already have an active negotiation request?
                if (negotiateActive)
                {
                    // Are there threads waiting?
                    if (asyncResultsWaitingForAuthentication.Count != 0)
                    {
                        // Most likely slow or recursive authentication
                        BException ex = new BException(BExceptionC.FORBIDDEN,
                                                       "Authentication procedure failed. Server returned 401 for every request. "
                                                       + "A common reason for this error is slow authentication handling.");
                        // ... or calling a function that requires authentication in BAuthentication.authenticate() - see. TestRemoteWithAuthentication.testAuthenticateBlocksRecursion
                        asyncResult.setAsyncResult(false, ex);
                        return;
                    }
                    else
                    {
                        // Correction: if no threads are waiting then there cannot be an aktive negotiation request.
                        negotiateActive = true;
                    }
                }
                else
                {
                    // Now, this is the active negotiation request.
                    negotiateActive = true;
                }
            }


            ByteBuffer buf  = ByteBuffer.allocate(BNegotiate.NEGOTIATE_MAX_SIZE);
            BNegotiate nego = new BNegotiate(apiDesc);

            nego.write(buf);
            buf.flip();

            BAsyncResultIF <BMessage> outerResult = new MyNegoAsyncResult(this, asyncResult);

            if (log.isDebugEnabled())
            {
                log.debug("wire.send");
            }
            BMessageHeader header = new BMessageHeader();

            header.messageId = wire.makeMessageId();
            BMessage msg = new BMessage(header, buf, null);

            wire.send(msg, outerResult);
            if (log.isDebugEnabled())
            {
                log.debug(")negotiateProtocolClient");
            }
        }
Beispiel #13
0
        protected void internalSend(BMessage msg, BAsyncResultIF <BMessage> asyncResult)
        {
            if (log.isDebugEnabled())
            {
                log.debug("send(" + msg + ", asyncResult=" + asyncResult);
            }

            // Convert the BMessage into single RequestToCancel objects.
            // One RequestToCancel is created for msg.buf.
            // For each stream in msg.streams further RequestToCancel objects are created.

            int nbOfRequests = 1 + (msg.streams != null ? msg.streams.Count : 0);
            List <RequestToCancel> requests = new List <RequestToCancel>(nbOfRequests);

            // If the BMessage contains streams, the given asyncResult is wrapped into an
            // outerResult in order to pass only the first exception to the caller.
            BAsyncResultIF <BMessage> outerResult = asyncResult;

            if (nbOfRequests > 1)
            {
                if (log.isDebugEnabled())
                {
                    log.debug("wrap asyncResult");
                }
                outerResult = new AsyncResultAfterAllRequests(this, msg.header.messageId, asyncResult, nbOfRequests);
            }

            // Create RequestToCancel for msg.buf
            RequestToCancel req = createRequestForMessage(msg, outerResult);

            requests.Add(req);

            // Create RequestToCancel objects for each stream.
            if (msg.streams != null)
            {
                foreach (BContentStream stream in msg.streams)
                {
                    req = createRequestForPutStream(stream, outerResult);
                    requests.Add(req);
                }
            }

            // Execute the RequestToCancel objects in the thread pool
            if (log.isDebugEnabled())
            {
                log.debug("put requests into thread pool");
            }
            foreach (RequestToCancel r in requests)
            {
                if (!executeRequest(r))
                {
                    cancelMessage(msg.header.messageId);
                    break;
                }
            }

            if (log.isDebugEnabled())
            {
                log.debug(")send");
            }
        }
Beispiel #14
0
 public override void sendR(BMessage msg, BAsyncResultIF <BMessage> asyncResult)
 {
     internalSend(msg, asyncResult);
 }
Beispiel #15
0
        public BMessage toMessage()
        {
            BMessage msg = new BMessage(header, toByteBuffer(), getStreamRequests());

            return(msg);
        }
Beispiel #16
0
 public virtual void sendR(BMessage msg, BAsyncResultIF<BMessage> asyncResult)
 {
     internalSend(msg, asyncResult);
 }
Beispiel #17
0
 public BMessage toMessage()
 {
     BMessage msg = new BMessage(header, toByteBuffer(), getStreamRequests());
     return msg;
 }