Package main

Source Code of main.Server$PublierClients

/**
*     This file is part of eCommerce.
*
*    eCommerce is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    eCommerce is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with Foobar; if not, write to the Free Software
*    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*
*    2007 Matej Hausenblas matej.hausenblas@gmail.com
*    Ait Elhaj brahim brahima@gmail.com
*
*/
package main;

import interfaces.catalogue.IMetierCatalogue;
import interfaces.clients.IMetierClients;
import interfaces.commandes.IMetierCommandes;
import interfaces.exceptions.MetierException;
import interfaces.produits.IMetierProduits;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import util.Util;

import metier.catalogue.MetierCatalogue;
import metier.clients.MetierClients;
import metier.commandes.MetierCommandes;
import metier.produits.MetierProduits;

public class Server {
 
  private int port = 1099;
  private String host = "localhost";
  private String URL_RMI = "rmi://"+host+":"+port+"/";
  private Registry registry;
 
  public static String PORT = "port";
  public static String HOST_RMI = "host_rmi";
  public static String PROPERTY_FILE = "config_rmi.properties";
 
  /**
   * Classe representant le serveur de Nom
   *
   */
  private class ServeurNom extends Thread{
   
    public void run(){
      System.out.println("Starting Registry server...");
     
      // demarrage d'un serveur re RMI Registry
      try {
       
        port = Util.lireIntProperty(PROPERTY_FILE, PORT);
        host = Util.lireStringProperty(PROPERTY_FILE, HOST_RMI);
        System.out.println("[PORT] ="+port);
        System.out.println("[HOST_BDD] = "+host);
       
        registry = LocateRegistry.createRegistry(port);
       
        System.out.println("Registry cree");
      } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(0);
      }
    }
  }
 
 
  private class PublierCatalogue extends Thread{
    public void run(){
      try{
//            registration du metier Catalogue.
          String name = "MetierCatalogue";
            IMetierCatalogue metierCatalogue = new MetierCatalogue();
            IMetierCatalogue stubCatalog =
                (IMetierCatalogue) UnicastRemoteObject.exportObject(metierCatalogue, port);
            System.out.println("Registration de "+name);
           
            String urlMetier = URL_RMI+name;
            //registry.rebind("localhost/"+name, stubCatalog);           
            registry.rebind(urlMetier, stubCatalog);
            //           
            //registry.rebind(urlMetier, metierCatalogue);
            Naming.rebind(urlMetier, metierCatalogue);
           
            System.out.println(name+" registre...");
      } catch (RemoteException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.exit(1);
      }catch(MalformedURLException mfe){
        mfe.printStackTrace();
      }
    }
  }
 
  private class PublierClients extends Thread{
    public void run(){
      try{
        System.out.println("coucou 1");
//            registration du metier Clients.
            String name = "MetierClients";
            IMetierClients metierClients = new MetierClients();
           
            System.out.println("coucou 2");           
            IMetierClients stubClient =
                (IMetierClients) UnicastRemoteObject.exportObject(metierClients, port);
            System.out.println("Registration de "+name);
           
            String urlMetier = URL_RMI+name;
            registry.rebind(urlMetier, stubClient);           
           
            Naming.rebind(urlMetier, metierClients);
           
            System.out.println(name+" registre...");
    }catch(MetierException me){
      System.out.println("Erreur Metier");
      System.exit(1);
    } catch (RemoteException e) {
      e.printStackTrace();
    }catch(MalformedURLException mfe){
      mfe.printStackTrace();
    }
    }
  }
 
  private class PublierCommandes extends Thread{
    public void run(){
      try{
//            registration du metier Commandes.
            String name = "MetierCommandes";
            IMetierCommandes metierCommandes = new MetierCommandes();           
            IMetierCommandes stubCde =
                (IMetierCommandes) UnicastRemoteObject.exportObject(metierCommandes, port);
            System.out.println("Registration de "+name);
           
           
            String urlMetier = URL_RMI+name;
            registry.rebind(urlMetier, stubCde);      
           
           
            Naming.rebind(urlMetier, metierCommandes);
           
            System.out.println(name+" registre...");   
    }catch(MetierException me){
      System.out.println("Erreur Metier");
      System.exit(1);
    } catch (RemoteException e) {
      e.printStackTrace();
    }catch(MalformedURLException mfe){
      mfe.printStackTrace();
    }

    }
  }
 
 
 
  private class PublierProduits extends Thread{
    public void run(){
      try{
          // registration du metier produits.
            String name = "MetierProduits";
            IMetierProduits metierProduits = new MetierProduits();
           
           
            IMetierProduits stubProd =
                (IMetierProduits) UnicastRemoteObject.exportObject(metierProduits, port);
            System.out.println("Registration de "+name);
           
            String urlMetier = URL_RMI+name;
            registry.rebind(urlMetier, stubProd);
           
           
            Naming.rebind(urlMetier, metierProduits);
           
           
            System.out.println(name+" registre...");
      }catch(MetierException me){
        System.out.println("Erreur Metier");
        System.exit(1);
      } catch (RemoteException e) {
        e.printStackTrace();
      }catch(MalformedURLException mfe){
        mfe.printStackTrace();
      }
                
       
    }
  }
 
  /**
   * Demarrage du serveur, avec tous ses fils
   *
   */
  public void startServer() throws Exception{
           // demarrage du serveur de nom dans un nouveau fil.
            Thread sn = new ServeurNom();
            sn.run();
           
            // recuperation de la connexion de RMI Registry, actuellement locale.
//            try {
//          registry = LocateRegistry.getRegistry(host);
//        } catch (RemoteException e) {
//          e.printStackTrace();
//          throw new Exception("Remote Exception");
//        }
       
            System.out.println("Starting met1");
        Thread met1 = new PublierCatalogue();
        met1.run();
       
        System.out.println("met1 demarre");
       
        System.out.println("Starting met2");
        Thread met2 = new PublierClients();
        met2.run();
        System.out.println("met2 demarre");
       
        Thread met3 = new PublierCommandes();
        met3.run();
       
        Thread met4 = new PublierProduits();
        met4.run();
       
             
              System.out.println("Metier bound");
   
  }
 
 
  private void publier() {
    try{
     
      registry = LocateRegistry.createRegistry(port);
     
      IMetierCatalogue metierCatalogue = new MetierCatalogue();
      /*IMetierClients metierClients = new MetierClients();
      IMetierCommandes metierCommandes = new MetierCommandes();
      IMetierProduits metierProduits = new MetierProduits();*/
     
      //System.out.println("Chargement des stubs ...");
      IMetierCatalogue stubCatalog =
                (IMetierCatalogue) UnicastRemoteObject.exportObject(metierCatalogue, port);
            /*IMetierClients stubClient =
                (IMetierClients) UnicastRemoteObject.exportObject(metierClients, port);
            IMetierCommandes stubCde =
                (IMetierCommandes) UnicastRemoteObject.exportObject(metierCommandes, port);
            IMetierProduits stubProd =
                (IMetierProduits) UnicastRemoteObject.exportObject(metierProduits, port);*/
     
      // System.out.println("Stubs chargés ...");
     
      System.out.println("Tentative de lancement du serveur RMI sur "+URL_RMI+" ...");
      // Publication
      Naming.rebind(URL_RMI+"metierCatalogue",metierCatalogue );
      /*Naming.rebind(URL_RMI+"metierClient",metierClients );
      Naming.rebind(URL_RMI+"metierCommandes", metierCommandes);     
      Naming.rebind(URL_RMI+"metierProduits",metierProduits);*/
     
      System.out.print("Serveur RMI lancé avec succès");
     
    }/*catch(MetierException me){
      System.out.println("Erreur Metier");
      System.exit(1);
    }*/ catch (RemoteException e) {
      e.printStackTrace();
    }catch(MalformedURLException mfe){
      mfe.printStackTrace();
    }
  }

  /**
   * Demarrage du serveur en autonome.
   * @param args
   */
  public static void main(String[] args) {
    Server s = new Server();
    //s.publier();
    try {
     
      s.startServer();
      System.out.println("Server started.");
      while(true){ Thread.sleep(1000000);}
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}
TOP

Related Classes of main.Server$PublierClients

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.