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.messaging.channel.AsyncToken;
028 import org.granite.client.messaging.channel.RemotingChannel;
029 import org.granite.client.messaging.codec.JMFAMF0MessagingCodec;
030 import org.granite.client.messaging.codec.MessagingCodec;
031 import org.granite.client.messaging.jmf.ClientSharedContext;
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 JMFAMFRemotingChannel extends AbstractAMFChannel implements RemotingChannel {
048
049 protected final MessagingCodec<AMF0Message> codec;
050 protected volatile int index = 1;
051
052 public JMFAMFRemotingChannel(Transport transport, ClientSharedContext sharedContext, String id, URI uri, int maxConcurrentRequests) {
053 super(transport, id, uri, maxConcurrentRequests);
054
055 this.codec = new JMFAMF0MessagingCodec(sharedContext);
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 AMF0Body body = new AMF0Body("", "/" + (index++), new Object[]{message}, AMF0Body.DATA_TYPE_AMF3_OBJECT);
063 amf0Message.addBody(body);
064 }
065 return new DefaultTransportMessage<AMF0Message>(token.getId(), false, clientId, null, amf0Message, codec);
066 }
067
068 @Override
069 protected ResponseMessage decodeResponse(InputStream is) throws IOException {
070 final AMF0Message amf0Message = codec.decode(is);
071 final int messagesCount = amf0Message.getBodyCount();
072
073 AbstractResponseMessage response = null, previous = null;
074
075 for (int i = 0; i < messagesCount; i++) {
076 AMF0Body body = amf0Message.getBody(i);
077
078 if (!(body.getValue() instanceof AMF3Object))
079 throw new RuntimeException("Message should be an AMF3Object: " + body.getValue());
080
081 AMF3Object bodyObject = (AMF3Object)body.getValue();
082 if (!(bodyObject.getValue() instanceof AcknowledgeMessage))
083 throw new RuntimeException("Message should be an AcknowledgeMessage: " + bodyObject.getValue());
084
085 AcknowledgeMessage message = (AcknowledgeMessage)bodyObject.getValue();
086 AbstractResponseMessage current = convertFromAmf(message);
087
088 if (response == null)
089 response = previous = current;
090 else {
091 previous.setNext(current);
092 previous = current;
093 }
094 }
095
096 return response;
097 }
098 }