001: /*
002: * DDS (Data Distribution Service) for JacORB
003: *
004: * Copyright (C) 2005 , Ahmed yehdih <ahmed.yehdih@gmail.com>, fouad
005: * allaoui <fouad.allaoui@gmail.com>, Didier Donsez (didier.donsez@ieee.org)
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Library General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Library General Public License for more details.
016: *
017: * You should have received a copy of the GNU Library General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: *
022: * Coontact: Ahmed yehdih <ahmed.yehdih@gmail.com>, fouad allaoui
023: * <fouad.allaoui@gmail.com>, Didier Donsez (didier.donsez@ieee.org)
024: * Contributor(s)
025: *
026: **/
027: package org.jacorb.dds;
028:
029: import java.util.Iterator;
030: import java.util.Vector;
031:
032: import org.jacorb.dds.ThreadSubscriber;
033: import org.jacorb.dds.DomainParticipantImpl;
034: import org.jacorb.events.EventChannelImpl;
035: import org.omg.CosNaming.NameComponent;
036: import org.omg.CosNaming.NamingContextExt;
037: import org.omg.CosNaming.NamingContextExtHelper;
038: import org.omg.dds.DomainParticipant;
039: import org.omg.dds.DomainParticipantFactory;
040: import org.omg.dds.DomainParticipantFactoryPOA;
041: import org.omg.dds.DomainParticipantHelper;
042: import org.omg.dds.RETCODE_OK;
043: import org.omg.dds.RETCODE_PRECONDITION_NOT_MET;
044:
045: /**
046: * The sole purpose of this class is to allow the creation and destruction of
047: * DomainParticipant objects. DomainParticipantFactory itself has no factory. It
048: * is either a pre-existing singleton object that can be accessed by means of
049: * the get_instance class operation on the DomainParticipantFactory.
050: */
051: public class DomainParticipantFactoryImpl extends
052: DomainParticipantFactoryPOA {
053:
054: private org.omg.CORBA.ORB orb;
055:
056: private org.omg.PortableServer.POA poa;
057:
058: private org.omg.dds.DomainParticipantQos defaultqos;
059:
060: private ThreadSubscriber Consummer;
061:
062: private Vector allParticipant;
063:
064: private DomainParticipantFactoryImpl impl;
065:
066: private DomainParticipantFactory ref;
067:
068: /**
069: * Sets the pOA attribute of this object
070: *
071: * @param poa
072: * The new pOA value
073: */
074: public void setPOA(org.omg.PortableServer.POA poa) {
075: this .poa = poa;
076: }
077:
078: /**
079: * Sets the oRB attribute of this object
080: *
081: * @param orb
082: * The new oRB value
083: */
084: public void setORB(org.omg.CORBA.ORB orb) {
085: this .orb = orb;
086: }
087:
088: public DomainParticipantFactoryImpl(org.omg.CORBA.ORB orb,
089: org.omg.PortableServer.POA poa) {
090: this .orb = orb;
091: this .poa = poa;
092: try {
093: NamingContextExt nc = NamingContextExtHelper.narrow(orb
094: .resolve_initial_references("NameService"));
095: EventChannelImpl channel = new EventChannelImpl(orb, poa);
096: org.omg.CORBA.Object o = poa.servant_to_reference(channel);
097: /* event channel used by event service */
098: nc.rebind(nc.to_name("eventchannel"), o);
099: } catch (Exception e) {
100:
101: }
102: allParticipant = new Vector();
103: /* thread send message for all suscriber */
104: Consummer = new ThreadSubscriber(orb, poa);
105: Consummer.start();
106: }
107:
108: /**
109: * This operation creates a new DomainParticipant object. The
110: * DomainParticipant signifies that the calling application intends to join
111: * the Domain identified by the domainId argument.
112: */
113: public org.omg.dds.DomainParticipant create_participant(
114: int domainId, org.omg.dds.DomainParticipantQos qos,
115: org.omg.dds.DomainParticipantListener a_listener) {
116:
117: org.omg.dds.DomainParticipant ref = null;
118: org.jacorb.dds.DomainParticipantImpl impl = new org.jacorb.dds.DomainParticipantImpl(
119: domainId, qos, a_listener);
120: impl.setORB(orb);
121: impl.setPOA(poa);
122:
123: try {
124: // get the root naming context
125:
126: ref = has_domainId(domainId);
127: if (ref != null)
128: return ref;
129: org.omg.CORBA.Object objRef = orb
130: .resolve_initial_references("NameService");
131: // Use NamingContextExt which is part of the Interoperable
132: // Naming Service (INS) specification.
133: NamingContextExt ncRef = NamingContextExtHelper
134: .narrow(objRef);
135: // get object reference from the servant (and implicitly register
136: // it)
137: org.omg.CORBA.Object oref = poa.servant_to_reference(impl);
138: ref = org.omg.dds.DomainParticipantHelper.narrow(oref);
139:
140: if (ncRef != null) {
141: // bind the Object Reference in Naming
142: NameComponent path[] = ncRef.to_name(new Integer(
143: domainId).toString());
144: ncRef.rebind(path, ref);
145: allParticipant.add(ref);
146: Consummer.add(ref);
147: }
148: } catch (Exception e) {
149: }
150: return ref;
151: }
152:
153: /**
154: * This operation deletes an existing DomainParticipant. This operation can
155: * only be invoked if all domain entities belonging to the participant have
156: * already been deleted.
157: *
158: * @param a_participant
159: * @return RETCODE_OK.value if succes Otherwise the error
160: * PRECONDITION_NOT_MET is returned.
161: */
162: public int delete_participant(
163: org.omg.dds.DomainParticipant a_participant) {
164:
165: try {
166: boolean delete_ok = ((DomainParticipantImpl) poa
167: .reference_to_servant(a_participant)).isDeletable();
168: if (delete_ok) {
169: org.omg.CORBA.Object objRef = orb
170: .resolve_initial_references("NameService");
171: // Use NamingContextExt which is part of the Interoperable
172: // Naming Service (INS) specification.
173: NamingContextExt ncRef = NamingContextExtHelper
174: .narrow(objRef);
175: String id = new Integer(a_participant.get_domain_id())
176: .toString();
177: NameComponent path[] = ncRef.to_name(id);
178: ncRef.unbind(path);
179: } else {
180: return RETCODE_PRECONDITION_NOT_MET.value;
181: }
182: } catch (Exception e) {
183: System.err.println("ERROR: " + e);
184: e.printStackTrace(System.out);
185: }
186:
187: return RETCODE_OK.value;
188: }
189:
190: /**
191: * @param domainId
192: * @return a participant has a same domainId
193: */
194: public DomainParticipant has_domainId(int domainId) {
195: Iterator it = allParticipant.iterator();
196: DomainParticipant temp;
197: while (it.hasNext()) {
198: temp = (DomainParticipant) it.next();
199: if (temp.get_domain_id() == domainId)
200: return temp;
201: }
202: return null;
203: }
204:
205: /**
206: * @param domainId
207: * @return a Participant has a same domaiId
208: */
209: public org.omg.dds.DomainParticipant lookup_participant(int domainId) {
210:
211: org.omg.dds.DomainParticipant ref = null;
212: try {
213: org.omg.CORBA.Object objRef = orb
214: .resolve_initial_references("NameService");
215: // Use NamingContextExt instead of NamingContext. This is
216: // part of the Interoperable naming Service.
217: NamingContextExt ncRef = NamingContextExtHelper
218: .narrow(objRef);
219: ref = DomainParticipantHelper.narrow(ncRef
220: .resolve_str(new Integer(domainId).toString()));
221: }
222:
223: catch (Exception e) {
224: System.err.println("ERROR: " + e);
225: e.printStackTrace(System.out);
226: }
227: return ref;
228: }
229:
230: /**
231: * @param qos
232: * @return
233: */
234: public int set_default_participant_qos(
235: org.omg.dds.DomainParticipantQos qos) {
236:
237: this .defaultqos = qos;
238: return 0;
239: }
240:
241: public void get_default_participant_qos(
242: org.omg.dds.DomainParticipantQosHolder qos) {
243:
244: qos.value = this .defaultqos;
245: }
246:
247: /**
248: * @return Returns the orb.
249: */
250: public org.omg.CORBA.ORB getOrb() {
251: return orb;
252: }
253:
254: /**
255: * @return Returns the poa.
256: */
257: public org.omg.PortableServer.POA getPoa() {
258: return poa;
259: }
260: }
|