001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2012 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    
021    package org.granite.client.messaging.channel.amf;
022    
023    import java.io.IOException;
024    import java.io.InputStream;
025    import java.io.UnsupportedEncodingException;
026    import java.net.URI;
027    
028    import org.granite.client.configuration.Configuration;
029    import org.granite.client.configuration.DefaultConfiguration;
030    import org.granite.client.messaging.channel.AsyncToken;
031    import org.granite.client.messaging.channel.RemotingChannel;
032    import org.granite.client.messaging.codec.AMF0MessagingCodec;
033    import org.granite.client.messaging.codec.MessagingCodec;
034    import org.granite.client.messaging.messages.ResponseMessage;
035    import org.granite.client.messaging.messages.responses.AbstractResponseMessage;
036    import org.granite.client.messaging.transport.DefaultTransportMessage;
037    import org.granite.client.messaging.transport.Transport;
038    import org.granite.client.messaging.transport.TransportMessage;
039    import org.granite.messaging.amf.AMF0Body;
040    import org.granite.messaging.amf.AMF0Message;
041    import org.granite.messaging.amf.AMF3Object;
042    
043    import flex.messaging.messages.AcknowledgeMessage;
044    import flex.messaging.messages.Message;
045    
046    /**
047     * @author Franck WOLFF
048     */
049    public class AMFRemotingChannel extends AbstractAMFChannel implements RemotingChannel {
050            
051            protected final MessagingCodec<AMF0Message> codec;
052            protected volatile int index = 1;
053            
054            public AMFRemotingChannel(Transport transport, String id, URI uri) {
055                    this(transport, id, uri, 5);
056            }
057            
058            public AMFRemotingChannel(Transport transport, String id, URI uri, int maxConcurrentRequests) {
059                    this(transport, DefaultConfiguration.getInstance(), id, uri, maxConcurrentRequests);
060            }
061            
062            public AMFRemotingChannel(Transport transport, Configuration configuration, String id, URI uri, int maxConcurrentRequests) {
063                    super(transport, id, uri, maxConcurrentRequests);
064                    
065                    this.codec = newMessagingCodec(configuration);
066            }
067    
068            protected MessagingCodec<AMF0Message> newMessagingCodec(Configuration configuration) {
069                    return new AMF0MessagingCodec(configuration);
070            }
071    
072            @Override
073            protected TransportMessage createTransportMessage(AsyncToken token) throws UnsupportedEncodingException {
074                    AMF0Message amf0Message = new AMF0Message();
075                    for (Message message : convertToAmf(token.getRequest())) {
076                            AMF3Object data = new AMF3Object(message);
077                        AMF0Body body = new AMF0Body("", "/" + (index++), new Object[]{data}, AMF0Body.DATA_TYPE_AMF3_OBJECT);
078                        amf0Message.addBody(body);
079                    }
080                    return new DefaultTransportMessage<AMF0Message>(token.getId(), false, clientId, null, amf0Message, codec);
081            }
082    
083            @Override
084            protected ResponseMessage decodeResponse(InputStream is) throws IOException {
085                    final AMF0Message amf0Message = codec.decode(is);
086                    final int messagesCount = amf0Message.getBodyCount();
087                    
088                    
089                    AbstractResponseMessage response = null, previous = null;
090                    
091                    for (int i = 0; i < messagesCount; i++) {
092                            AMF0Body body = amf0Message.getBody(i);
093                            
094                            
095                            if (!(body.getValue() instanceof AcknowledgeMessage))
096                                    throw new RuntimeException("Message should be an AcknowledgeMessage: " + body.getValue());
097                            
098                            AcknowledgeMessage message = (AcknowledgeMessage)body.getValue();
099                            AbstractResponseMessage current = convertFromAmf(message);
100                            
101                            if (response == null)
102                                    response = previous = current;
103                            else {
104                                    previous.setNext(current);
105                                    previous = current;
106                            }
107                    }
108                    
109                    return response;
110            }
111    }