001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of Granite Data Services.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or modify
008     *   it under the terms of the GNU Library General Public License as published by
009     *   the Free Software Foundation; either version 2 of the License, or (at your
010     *   option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful, but
013     *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014     *   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015     *   for more details.
016     *
017     *   You should have received a copy of the GNU Library General Public License
018     *   along with this library; if not, see <http://www.gnu.org/licenses/>.
019     */
020    package org.granite.client.messaging.channel.amf;
021    
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.io.UnsupportedEncodingException;
025    import java.net.URI;
026    
027    import org.granite.client.configuration.Configuration;
028    import org.granite.client.messaging.channel.AsyncToken;
029    import org.granite.client.messaging.channel.RemotingChannel;
030    import org.granite.client.messaging.codec.AMF0MessagingCodec;
031    import org.granite.client.messaging.codec.MessagingCodec;
032    import org.granite.client.messaging.messages.ResponseMessage;
033    import org.granite.client.messaging.messages.responses.AbstractResponseMessage;
034    import org.granite.client.messaging.transport.DefaultTransportMessage;
035    import org.granite.client.messaging.transport.Transport;
036    import org.granite.client.messaging.transport.TransportMessage;
037    import org.granite.messaging.amf.AMF0Body;
038    import org.granite.messaging.amf.AMF0Message;
039    import org.granite.messaging.amf.AMF3Object;
040    
041    import flex.messaging.messages.AcknowledgeMessage;
042    import flex.messaging.messages.Message;
043    
044    /**
045     * @author Franck WOLFF
046     */
047    public class AMFRemotingChannel extends AbstractAMFChannel implements RemotingChannel {
048            
049            protected final MessagingCodec<AMF0Message> codec;
050            protected volatile int index = 1;
051            
052            public AMFRemotingChannel(Transport transport, Configuration configuration, String id, URI uri, int maxConcurrentRequests) {
053                    super(transport, id, uri, maxConcurrentRequests);
054                    
055                    this.codec = new AMF0MessagingCodec(configuration);
056            }
057    
058            @Override
059            protected TransportMessage createTransportMessage(AsyncToken token) throws UnsupportedEncodingException {
060                    AMF0Message amf0Message = new AMF0Message();
061                    for (Message message : convertToAmf(token.getRequest())) {
062                            AMF3Object data = new AMF3Object(message);
063                        AMF0Body body = new AMF0Body("", "/" + (index++), new Object[]{data}, AMF0Body.DATA_TYPE_AMF3_OBJECT);
064                        amf0Message.addBody(body);
065                    }
066                    return new DefaultTransportMessage<AMF0Message>(token.getId(), false, clientId, null, amf0Message, codec);
067            }
068    
069            @Override
070            protected ResponseMessage decodeResponse(InputStream is) throws IOException {
071                    final AMF0Message amf0Message = codec.decode(is);
072                    final int messagesCount = amf0Message.getBodyCount();
073                    
074                    AbstractResponseMessage response = null, previous = null;
075                    
076                    for (int i = 0; i < messagesCount; i++) {
077                            AMF0Body body = amf0Message.getBody(i);
078                            
079                            if (!(body.getValue() instanceof AcknowledgeMessage))
080                                    throw new RuntimeException("Message should be an AcknowledgeMessage: " + body.getValue());
081                            
082                            AcknowledgeMessage message = (AcknowledgeMessage)body.getValue();
083                            AbstractResponseMessage current = convertFromAmf(message);
084                            
085                            if (response == null)
086                                    response = previous = current;
087                            else {
088                                    previous.setNext(current);
089                                    previous = current;
090                            }
091                    }
092                    
093                    return response;
094            }
095    }