1 24 25 package org.objectweb.dream.message.codec; 26 27 import java.io.DataInput ; 28 import java.io.DataOutput ; 29 import java.io.IOException ; 30 import java.util.Iterator ; 31 32 import org.objectweb.dream.AbstractComponent; 33 import org.objectweb.dream.message.ExtensibleMessage; 34 import org.objectweb.dream.message.ExtensibleMessageNC; 35 import org.objectweb.dream.message.Message; 36 import org.objectweb.dream.message.MessageAlreadyExistException; 37 import org.objectweb.dream.message.MessageNC; 38 import org.objectweb.dream.message.MessageTypeNC; 39 import org.objectweb.dream.message.manager.MessageManager; 40 import org.objectweb.dream.util.Error; 41 import org.objectweb.fractal.api.NoSuchInterfaceException; 42 import org.objectweb.fractal.api.control.IllegalBindingException; 43 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 44 45 49 public abstract class AbstractMessageCodecDataStream extends AbstractComponent 50 implements 51 MessageCodec 52 { 53 54 protected MessageManager messageManagerItf; 55 56 60 65 protected Message doDecode(DataInput input) throws IOException 66 { 67 Message message = doDecodeSingleMessage(input); 68 if (message instanceof MessageNC) 69 { 70 boolean hasSubMessage = input.readBoolean(); 71 if (hasSubMessage) 72 { 73 if (!(message instanceof ExtensibleMessage)) 74 { 75 throw new IOException ( 76 "Invalid decoded message, must implement ExtensibleMessage."); 77 } 78 ExtensibleMessage extensibleMessage = (ExtensibleMessage) message; 80 while (hasSubMessage) 81 { 82 Message subMessage = doDecode(input); 83 extensibleMessage.addSubMessage(subMessage); 84 hasSubMessage = input.readBoolean(); 85 } 86 } 87 88 hasSubMessage = input.readBoolean(); 89 if (hasSubMessage) 90 { 91 if (!(message instanceof ExtensibleMessageNC)) 92 { 93 throw new IOException ( 94 "Invalid decoded message, must implements ExtensibleMessageNC."); 95 } 96 ExtensibleMessageNC extensibleMessageNC = (ExtensibleMessageNC) message; 98 while (hasSubMessage) 99 { 100 String subMessageName = input.readUTF(); 101 Message subMessage = doDecode(input); 102 try 103 { 104 extensibleMessageNC.addSubMessage(subMessageName, subMessage); 105 } 106 catch (MessageAlreadyExistException e) 107 { 108 Error.bug(logger, e); 109 } 110 hasSubMessage = input.readBoolean(); 111 } 112 } 113 } 114 else 115 { 116 boolean hasSubMessage = input.readBoolean(); 117 if (hasSubMessage) 118 { 119 if (!(message instanceof ExtensibleMessage)) 120 { 121 throw new IOException ( 122 "Invalid decoded message, must implement ExtensibleMessage."); 123 } 124 ExtensibleMessage extensibleMessage = (ExtensibleMessage) message; 125 while (hasSubMessage) 126 { 127 Message subMessage = doDecode(input); 128 extensibleMessage.addSubMessage(subMessage); 129 hasSubMessage = input.readBoolean(); 130 } 131 } 132 } 133 return message; 134 } 135 136 144 protected abstract Message doDecodeSingleMessage(DataInput input) 145 throws IOException ; 146 147 152 protected void doEncode(Message message, DataOutput output) 153 throws IOException 154 { 155 doEncodeSingleMessage(message, output); 156 if (message instanceof MessageNC) 157 { 158 MessageNC messageNC = (MessageNC) message; 159 Iterator iterator = messageNC.getUnnamedSubMessageIterator(); 160 while (iterator.hasNext()) 161 { 162 output.writeBoolean(true); 163 Message subMessage = (Message) iterator.next(); 164 doEncode(subMessage, output); 165 } 166 output.writeBoolean(false); 167 MessageTypeNC messageTypeNC = (MessageTypeNC) messageNC.getMessageType(); 168 iterator = messageTypeNC.getSubMessageNamesIterator(); 169 while (iterator.hasNext()) 170 { 171 output.writeBoolean(true); 172 String subMessageName = (String ) iterator.next(); 173 output.writeUTF(subMessageName); 174 doEncode(messageNC.getSubMessage(subMessageName), output); 175 } 176 output.writeBoolean(false); 177 } 178 else 179 { 180 Iterator iterator = message.getSubMessageIterator(); 181 while (iterator.hasNext()) 182 { 183 output.writeBoolean(true); 184 Message subMessage = (Message) iterator.next(); 185 doEncode(subMessage, output); 186 } 187 output.writeBoolean(false); 188 } 189 } 190 191 199 protected abstract void doEncodeSingleMessage(Message message, 200 DataOutput output) throws IOException ; 201 202 206 209 public String [] listFc() 210 { 211 return new String []{MessageManager.ITF_NAME}; 212 } 213 214 218 public synchronized void bindFc(String clientItfName, Object serverItf) 219 throws NoSuchInterfaceException, IllegalBindingException, 220 IllegalLifeCycleException 221 { 222 if (clientItfName.equals(MessageManager.ITF_NAME)) 223 { 224 messageManagerItf = (MessageManager) serverItf; 225 } 226 super.bindFc(clientItfName, serverItf); 227 } 228 229 } | Popular Tags |