IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

CORBA Discussion :

Probleme de cast


Sujet :

CORBA

  1. #1
    Membre averti Avatar de _Xavier_
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    311
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations forums :
    Inscription : Mai 2009
    Messages : 311
    Points : 390
    Points
    390
    Par défaut Probleme de cast
    Bonjour,

    J'ai deux classes, ParkingImpl qui invoque des méthodes de MairieImpl après avoir initialiser la connexion. Pour l'instant le teste se fait sur une seule machine.

    ParkingImpl
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    package fr.corba.parking;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    import org.omg.CORBA.Context;
    import org.omg.CORBA.ContextList;
    import org.omg.CORBA.DomainManager;
    import org.omg.CORBA.ExceptionList;
    import org.omg.CORBA.NVList;
    import org.omg.CORBA.NamedValue;
    import org.omg.CORBA.Object;
    import org.omg.CORBA.Policy;
    import org.omg.CORBA.Request;
    import org.omg.CORBA.SetOverrideType;
    import org.omg.CosNaming.NamingContext;
    import org.omg.PortableServer.POA;
    import org.omg.PortableServer.POAHelper;
    
    import ParkingAuto.Historique;
    import ParkingAuto.Localisation;
    import ParkingAuto.Mairie;
    import ParkingAuto.MairieHelper;
    import ParkingAuto.Parking;
    import ParkingAuto.ParkingPOA;
    
    public class ParkingImpl extends ParkingPOA implements Parking {
    
    	private int numParking;
    	private String nomParking;
    	private int nbPlacesTotal;
    	private int nbPlacesoqp;
    	private int prixHoraire;
    	private Historique[] historiques;
    	private boolean libre = true;
    	private Localisation localisation;
    	private int cmp = 0; // compteur pour le tableau Historique
    	public static Mairie mairie;
    	private String[] args;
    	
    	public ParkingImpl(String nom, Localisation loc,String[] args){
    		this.localisation = loc;
    		this.nomParking = nom;
    		this.args = args;
    		this.initMairie();
    	}
    	
    	public ParkingImpl(){
    		
    	}
    	
    	public Historique[] getHistorique() {
    		// TODO Auto-generated method stub
    		return this.historiques;
    	}
    	
    	/**
    	 * Initialise la connexion avec la mairie
    	 */
    	private void initMairie(){
    		try {
    
    			// Intialisation de l'orb
    			org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
    
    
    	
    
    	        // Saisie du nom de l'objet (si utilisation du service de nommage)
    	        System.out.println("Quel objet Corba voulez-vous contacter ?");
    	        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	        String idObj = in.readLine();
    
    	        // Recuperation du naming service
    	        org.omg.CosNaming.NamingContext nameRoot =
    	        		org.omg.CosNaming.NamingContextHelper.narrow(orb.string_to_object("corbaloc:iiop:1.2@172.18.37.77:2001/NameService"));
    
    	        // Construction du nom a rechercher
    	        org.omg.CosNaming.NameComponent[] nameToFind = new org.omg.CosNaming.NameComponent[1];
    	         nameToFind[0] = new org.omg.CosNaming.NameComponent(idObj,"");
    
    	        // Recherche aupres du naming service
    	        org.omg.CORBA.Object distantMairie = nameRoot.resolve(nameToFind);
    	        System.out.println("Objet '" + idObj + "' trouve aupres du service de noms. IOR de l'objet :");
    	        System.out.println(orb.object_to_string(distantMairie));
    	
    	        // Utilisation directe de l'IOR (SAUF utilisation du service de nommage)
    	        // org.omg.CORBA.Object distantMairie = orb.string_to_object("IOR:000...");
    	        // Casting de l'objet CORBA au type convertisseur euro
    	         mairie = MairieHelper.narrow(distantMairie);
    	        // traitement 
    	         
    		}
    		catch (Exception e) {
    			e.printStackTrace();
    		}
    
    		
    	}
    	/**
    	 * Envoie le dernier mouvemen de véhicule (entrée ou sortie)
    	 */
    	
    	private void sendHistorique(){
    		mairie.addInfoParking(historiques[cmp],this.numParking);		
    	}
    
    	public Mairie getMairie(){
    		return this.mairie;
    	}
    	
    	public void entrerParrking(String dateArrivee){
    		this.nbPlacesoqp++;
    		if (this.nbPlacesoqp==this.nbPlacesTotal){
    			libre= false;			
    		}
    		this.historiques[cmp] = new HistoriqueImpl(dateArrivee,Historique.ENTREE);
    		sendHistorique();
    		cmp ++;
    	}
    	
    	public void sortirParrking(String dateSortie) {
    		// TODO Auto-generated method stub
    		this.nbPlacesoqp--;
    		if (this.nbPlacesoqp<this.nbPlacesTotal){
    			libre= true;			
    		}
    		this.historiques[cmp] = new HistoriqueImpl(dateSortie,Historique.SORTIE);
    		sendHistorique();
    		cmp ++;
    	}
    	public Localisation getLocalisation() {
    		// TODO Auto-generated method stub
    		return this.localisation;
    	}
    
    	public int getNbPlacesTotal() {
    		// TODO Auto-generated method stub
    		return this.nbPlacesTotal;
    	}
    
    	public int getNbPlacesoqp() {
    		// TODO Auto-generated method stub
    		return this.nbPlacesoqp;
    	}
    
    	public String getNomParking() {
    		// TODO Auto-generated method stub
    		return this.nomParking;
    	}
    
    	public int getPrixHoraire() {
    		// TODO Auto-generated method stub
    		return this.prixHoraire;
    	}
    
    	public void setLocalisation(Localisation locP) {
    		// TODO Auto-generated method stub
    		this.localisation =locP;
    	}
    
    	public void setNbPlacesTotal(int nbPlacesTotal) {
    		// TODO Auto-generated method stub
    		this.nbPlacesTotal = nbPlacesTotal;
    	}
    
    	public void setNbPlacesoqp(int nbPlacesoqp) {
    		// TODO Auto-generated method stub
    		this.nbPlacesoqp = nbPlacesoqp;
    	}
    
    	public void setNomParking(String nomParking) {
    		// TODO Auto-generated method stub
    		this.nomParking = nomParking;
    	}
    
    	public void setPrixHoraire(int prixHoraire) {
    		// TODO Auto-generated method stub
    		this.prixHoraire = prixHoraire;
    	}
    
    	public Request _create_request(Context ctx, String operation,
    			NVList arg_list, NamedValue result) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	public Request _create_request(Context ctx, String operation,
    			NVList arg_list, NamedValue result, ExceptionList exclist,
    			ContextList ctxlist) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	public Object _duplicate() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	public DomainManager[] _get_domain_managers() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	public Object _get_interface_def() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	public Policy _get_policy(int policy_type) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	public int _hash(int maximum) {
    		// TODO Auto-generated method stub
    		return 0;
    	}
    
    	public boolean _is_a(String repositoryIdentifier) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    	public boolean _is_equivalent(Object other) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    	public boolean _non_existent() {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    	public void _release() {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public Request _request(String operation) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	public Object _set_policy_override(Policy[] policies,
    			SetOverrideType set_add) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    	public int getNumParking() {
    		// TODO Auto-generated method stub
    		return this.numParking;
    	}
    	public void setNumParking(int numParking) {
    		this.numParking = numParking;
    		// TODO Auto-generated method stub
    		
    	}
     
    	public void start(){
    		
    		try {
    		        // Intialisation de l'ORB
    		        //************************
    		        org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
    
    		        // Gestion du POA
    		        //****************
    		        // Recuperation du POA
    		        POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
    
    		        // Creation du servant
    		        //*********************
    
    		        // Activer le servant au sein du POA et recuperer son ID
    		        byte[] parkingId = rootPOA.activate_object(this);
    
    		        // Activer le POA manager
    		        rootPOA.the_POAManager().activate();
    
    
    		        // Enregistrement dans le service de nommage
    		        //*******************************************
    		        // Recuperation du naming service
    		        NamingContext nameRoot=org.omg.CosNaming.NamingContextHelper.narrow(orb.string_to_object("corbaloc:iiop:1.2@172.18.37.77:2001/NameService"));
    
    		        // Construction du nom a enregistrer
    		        org.omg.CosNaming.NameComponent[] nameToRegister = new org.omg.CosNaming.NameComponent[1];
    		        nameToRegister[0] = new org.omg.CosNaming.NameComponent(this.nomParking,"");
    
    		        // Enregistrement de l'objet CORBA dans le service de noms
    		        nameRoot.rebind(nameToRegister,rootPOA.servant_to_reference(this));
    		        System.out.println("==> Nom '"+ this.nomParking + "' est enregistre dans le service de noms.");
    		
    		        String IORServant = orb.object_to_string(rootPOA.servant_to_reference(this));
    		        System.out.println("L'objet possede la reference suivante :");
    		        System.out.println(IORServant);
    
    		        // Lancement de l'ORB et mise en attente de requete
    		        //**************************************************
    		        orb.run();
    
    		    }
    			catch (Exception e) {
    				e.printStackTrace();
    			}
    		} 
    	public static void main(String[] args) throws IOException {
    		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    		System.out.println("Donner le nom du parking : ");
    		String nom = in.readLine();	    
    	    System.out.println("Coordonnées X : ");
    	    int x = Integer.parseInt(in.readLine());
    	    System.out.println("Coordonnées Y : ");
    	    int y = Integer.parseInt(in.readLine());
    	    
    		ParkingImpl parking = new ParkingImpl(nom, new LocalisationImpl(x,y),args);
    		Mairie mairie = parking.getMairie();
    		System.out.println("Enregistrent du parking au niveau de la mairie"); 
    		mairie.addNewParking(parking);
    		System.out.println("Ajout du parking au niveau de la mairie");
    		
    		// Lancement du serveur de parking
    		parking.start();
    
    	}
    	
    }
    Le NamingService marche bien, le serveur de Mairie est lancée sans erreur. En revanche à l'execution de ParkingImpl le système génére une exception au niveau de la ligne rouge en gras :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Exception in thread "main"
    
    java.lang.ClassCastException: fr.corba.parking.ParkingImpl cannot be cast to org.omg.CORBA.portable.ObjectImpl
    at ParkingAuto.ParkingHelper.write(
    
    ParkingHelper.java:82)
    at ParkingAuto._MairieStub.addNewParking(
    
    _MairieStub.java:236)
    at ups.corba.parking.ParkingImpl.main(
    
    ParkingImpl.java:320)
    Je voudrais savoir ce qui peut être à l'origine de l'erreur.

    Merci

  2. #2
    Membre averti
    Homme Profil pro
    Architecte technique
    Inscrit en
    Septembre 2006
    Messages
    219
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2006
    Messages : 219
    Points : 302
    Points
    302
    Par défaut
    Bonjour,

    d'après ce que je comprends, Mairie et Parking sont des interfaces CORBA. Et l'appel mairie.addNewParking(parking) est donc un appel CORBA avec un objet CORBA de type Parking comme paramètre.

    Je vois 2 problèmes dans ton code:
    1. La classe ParkingImpl ne devrait pas implémenter l'interface Parking. L'héritage de ParkingPOA est suffisant. l'interface Parking générée est en fait utilisée côté client (c'est le stub). Par conséquent, ParkingImpl ne devrait pas ré-implémenter toutes les opérations _create_request(), _duplicate() ..., mais uniquement les opérations définies dans l'IDL.

    2. Dans l'appel mairie.addNewParking(parking), le paramètre passé est de type ParkingImpl. Ce n'est donc pas un objet CORBA, mais un Servant (i.e. un objet d'implémentation d'un objet CORBA). Ton code compile grâce à l'héritage de ParkingImpl vers Parking, mais il ne devrait pas si tu enlève cet héritage.
      L'objet CORBA doit être créé et activer pour l'envoyer comme paramètre. La manière la plus simple de faire cela est le code suivant:
      Code : Sélectionner tout - Visualiser dans une fenêtre à part
      Parking parkingObj = parking._this(orb);
      Cet appel crée l'objet CORBA, le lie au Servant qui l'implémente et l'active. L'ORB doit donc être initialisé avant la création de l'objet CORBA.
      Puis ton appel CORBA devient:
      Code : Sélectionner tout - Visualiser dans une fenêtre à part
      mairie.addNewParking(parkingObj);

  3. #3
    Membre averti Avatar de _Xavier_
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    311
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations forums :
    Inscription : Mai 2009
    Messages : 311
    Points : 390
    Points
    390
    Par défaut
    Merci CorbAddict

Discussions similaires

  1. Probleme de cast de string en int
    Par Oberown dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 26/04/2006, 11h48
  2. Réponses: 12
    Dernier message: 25/07/2005, 14h49
  3. [VB.NET][dataset][datagrid] probleme de Cast
    Par graphicsxp dans le forum Windows Forms
    Réponses: 3
    Dernier message: 05/05/2005, 14h18
  4. Probleme de cast de parametres
    Par John Fullspeed dans le forum Langage
    Réponses: 3
    Dernier message: 14/10/2004, 08h43
  5. probleme de cast
    Par gaut dans le forum C++
    Réponses: 9
    Dernier message: 06/08/2004, 18h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo