Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

EBinder.java

Go to the documentation of this file.
00001 /***
00002  * Jonathan: an Open Distributed Processing Environment 
00003  * Copyright (C) 1999-2000 France Telecom R&D
00004  * 
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  * 
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  * 
00019  * Release: 2.0
00020  *
00021  * Contact: jonathan@objectweb.org
00022  *
00023  * Author: Frédéric Dang Tran
00024  *
00025  */
00026 
00027 package org.objectweb.jonathan.libs.binding.echannel;
00028 
00029 
00030 import org.objectweb.jonathan.apis.presentation.*;
00031 import org.objectweb.jonathan.apis.protocols.*;
00032 import org.objectweb.jonathan.apis.binding.*;
00033 import org.objectweb.jonathan.apis.kernel.*;
00034 import org.objectweb.jonathan.apis.stub_factories.*;
00035 import org.objectweb.jonathan.apis.resources.*;
00036 
00037 import org.objectweb.jonathan.libs.kernel.JContextFactory;
00038 import org.objectweb.jonathan.libs.protocols.multicast_ip.*;
00039 import org.objectweb.jonathan.libs.protocols.rtp.*;
00040 import org.objectweb.jonathan.libs.helpers.MessageHelpers;
00041 
00042 
00050 public class EBinder implements NamingContext {
00051    
00052    static JContextFactory context_factory = new JContextFactory();
00053 
00054    ChunkFactory chunk_factory;
00055    MarshallerFactory marshaller_factory;
00056         
00057         RTPProtocol rtp_protocol;       
00058    MulticastIpProtocol mcast_protocol;
00059         NamingContext context;
00060    StubFactory stub_factory;
00061 
00071    public EBinder(ChunkFactory chunk_factory,
00072                   MarshallerFactory marshaller_factory,
00073                   NamingContext context,
00074                   StubFactory stub_factory) {
00075                 this.chunk_factory=chunk_factory;
00076                 this.marshaller_factory=marshaller_factory;
00077                 this.context=context;
00078       this.stub_factory = stub_factory;
00079         
00080                 mcast_protocol=new MulticastIpProtocol(marshaller_factory);
00081                 rtp_protocol=new RTPProtocol(mcast_protocol.getMtu());
00082         }
00083 
00092    public Identifier export(Object itf, Context hints) {
00093       throw new InternalException("Meaningless operation.");
00094    }
00095 
00103    public void bindConsumer(Session_Low consumer,EId channel) 
00104       throws JonathanException  {
00105                 ProtocolGraph protocol_graph = channel.getProtocolGraph();
00106                 protocol_graph.export(consumer);
00107         }
00108 
00118    public Identifier decode(byte[] data,int offset,int length) 
00119       throws JonathanException {
00120                 return new EId(data, offset, length);
00121         }
00122 
00130    public Identifier decode(UnMarshaller u)
00131       throws JonathanException {
00132                 return new EId(u);
00133         }
00134 
00145    public EId newId(String address, int port, String type)      
00146       throws JonathanException  {
00147                 return new EId(address,port,type);
00148         }
00149 
00156    public class EId implements Identifier {
00157                 String address;
00158                 int port;
00159                 String type;
00160 
00161                 SessionIdentifier session_id;
00162       ProtocolGraph protocol_graph;
00163       
00171       public EId(String address, int port, String type) {
00172                         this.address=address;
00173                         this.port=port;
00174                         this.type=type;
00175                 }
00176 
00186       public EId(byte[] encoded,int offset,int length)
00187          throws JonathanException {
00188          Chunk chunk = new Chunk(encoded,offset,offset + length);
00189          UnMarshaller unmarshaller =
00190             marshaller_factory.newUnMarshaller(chunk,0);
00191          boolean little_endian = unmarshaller.readBoolean();
00192          unmarshaller.setByteOrder(little_endian);
00193          init(unmarshaller);
00194          unmarshaller.close();
00195                 }
00196       
00204       public EId(UnMarshaller unmarshaller)
00205          throws JonathanException {
00206          init(unmarshaller);
00207                 }
00208 
00209       private void init(UnMarshaller unmarshaller) 
00210          throws JonathanException {
00211          address = unmarshaller.readString8();
00212          port = unmarshaller.readInt();
00213                         type= unmarshaller.readString8();
00214       }
00215 
00222       public SessionIdentifier getSessionIdentifier() throws JonathanException {
00223                         if(session_id==null) {
00224                                 SessionIdentifier mcast_session_id = 
00225                mcast_protocol.newSessionIdentifier(address,port);
00226                                 session_id = rtp_protocol.newSessionIdentifier(mcast_session_id);
00227                         }
00228                         return session_id;
00229                 }
00230 
00237       public ProtocolGraph getProtocolGraph() throws JonathanException {
00238                         if(protocol_graph==null) {
00239                                 ProtocolGraph mcast_protocol_graph = 
00240                mcast_protocol.newProtocolGraph(address,port);
00241                                 protocol_graph = rtp_protocol.newProtocolGraph(mcast_protocol_graph);
00242                         }
00243                         return protocol_graph;
00244                 }
00245 
00246                 public Object bind(Identifier[] ref,Context hints)
00247                         throws JonathanException {
00248          boolean new_hints = false;
00249          if (hints == null) {
00250             hints = context_factory.newContext();
00251             new_hints = true;
00252          }
00253          hints.addElement("interface_type",String.class,type,(char) 0);
00254          Object stub = stub_factory.newStub(getSessionIdentifier(),ref,hints);
00255          if (new_hints) {
00256             hints.release();
00257          }
00258          return stub;
00259       }
00260       
00261                 
00262                 public NamingContext getContext() {
00263                         return EBinder.this.context;
00264                 }
00265                 
00266                 public byte[] encode() throws JonathanException {
00267          Marshaller marshaller = marshaller_factory.newMarshaller();
00268          marshaller.writeBoolean(marshaller.isLittleEndian());
00269          encode(marshaller);
00270          byte[] encoded = MessageHelpers.copy(marshaller);
00271          marshaller.close();
00272          return encoded;
00273       }
00274       
00275       public void encode(Marshaller marshaller) throws JonathanException {
00276          marshaller.writeString8(address);
00277          marshaller.writeInt(port);
00278          marshaller.writeString8(type);
00279       }
00280    
00281                 public Object resolve() {
00282                         return null;
00283                 }       
00284                 
00285                 public boolean isValid() {
00286                         return true; 
00287                 }
00288                 
00289                 public void unexport() {
00290                 }
00291         }
00292 }

Generated at Fri May 31 19:23:32 2002 for Jonathan by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001