NET Remoting
Transcription
NET Remoting
Plan .Net Remoting 1. Introduction 2. Modèle de programmation 3. Caractéristiques techniques Lionel Seinturier 3.1. Invocation 3.2. Paramètres 3.3. Activation de serveurs INRIA Futurs Lille – Projet JACQUARD Université Pierre & Marie Curie 4. Configuration [email protected] 5. Fonctionnalités avancées 6. Comparaison 6/9/04 .Net Remoting 1 Lionel Seinturier .Net Remoting 1. Introduction 2 2. Modèle de programmation .Net Remoting Principe Solution Microsoft pour interopérabilité client/serveur entre objets .Net distants client + serveur + proxy - successeur de DCOM (trop complexe) - très proche de Java RMI et de CORBA/IIOP .Net génère dynamiquement un objet proxy requête rés eau proxy Install new Serveur ! l'interface (méthodes) de l'objet serveur doit être connue côté client rés eau réponse prog. serveur ! 3 programmes - client.exe - serveur.exe (classe Serveur + classe Install) - itf.dll - contient l'interface du seveur - partagée par client.exe et serveur.exe masquée au sein d'un appel de méthode .Net Remoting Client - mêmes méthodes que l'objet serveur - chaque méthode redirige l'appel vers l'objet serveur Principe : interaction client/serveur prog. client Lionel Seinturier 3 Lionel Seinturier .Net Remoting 4 Lionel Seinturier 2. Modèle de programmation 2. Modèle de programmation Classe Serveur Installation objet(s) serveur(s) • namespace System.Remoting • classe Serveur : hérite de MarshalByRefObject • méthodes publiques Concept de channel = canal de communication entre le client et le serveur 2 existants à la base using ItfPartagee; namespace CoteServeur { class Serveur : MarshalByRefObject, IServeur { public string SayHello() { return "Hello world"; } } } - TCP avec un format de message binaire - HTTP avec un format de message SOAP (XML) Possibilités de créer de nouveaux channels et/ou de nouveaux formats namespace ItfPartagee { public interface IServeur { string SayHello(); } } .Net Remoting - IIOP - SMTP - … (voir sur Internet) 5 Lionel Seinturier .Net Remoting 2. Modèle de programmation 6 Lionel Seinturier 2. Modèle de programmation Installation objet(s) serveur(s) Classe Client using using using using using ItfPartagee; ItfPartagee; System.Runtime.Remoting; System.Runtime.Remoting.Channels; System.Runtime.Remoting.Channels.Tcp; namespace CoteClient { class Install { static void main(string[] args) { namespace CoteServeur { class Install { static void main(string[] args) { IServeur proxy = (IServeur) Activator.GetObject( typeof(IServeur), "tcp://localhost:1704/hello" ); TcpChannel chan = new TcpChannel(1704); ChannelServices.RegisterChannel(chan); string ret = proxy.SayHello(); RemotingConfiguration.RegisterWellKnownServiceType( typeof(Serveur), "hello", // URI WellKnownObjectMode.SingleCall ); } } } Rq : channel HTTP http://localhost:1704/hello Console.ReadLine(); } } } Rq : ≠ JavaRMI, CORBA : port choisi, pas de service de noms (rq: en programmer un) .Net Remoting 7 Lionel Seinturier .Net Remoting 8 Lionel Seinturier 3. Caractéristiques 3.1 Invocations 3 types d'invocations 3.1. Invocation Client 3.2. Paramètres Serveur Synchrone 3.3. Activation de serveurs Client Serveur Semi-synchrone (ou asynchrone avec retour) Client Serveur Asynchrone (ou oneway) Invocation synchrone Cas par défaut .Net Remoting 9 Lionel Seinturier .Net Remoting 3.1 Invocations 10 Lionel Seinturier 3.1 Invocations Invocation semi-synchrone (asynchrone avec retour) Invocation asynchrone (oneway) Aucun changement côté serveur Côté serveur • attribut méthode [OneWay()] dans l'interface et la classe serveur • 1 seul message d'appel, pas de retour (! pas de type de retour autre que void) Côté client Notion de delegate : ~pointeur de fonction Côté client • idem semi-synchrone (BeginInvoke) • EndInvoke retourne toujours immédiatement (avant réception côté serveur) • méthode pour déclencher l'appel : BeginInvoke • méthode pour récupérer le résultat : EndInvoke delegate string SayHello(); SayHelloDelegate shd = new SayHelloDelegate(proxy.SayHello); IAsyncResult res = shd.BeginInvoke(null,null); ... string ret = shd.EndInvoke(res); namespace ItfPartagee { public interface IServeur { [OneWay()] void Ping(); } } Rq : SayHelloDelegate générée par le framework .Net Remoting 11 Lionel Seinturier .Net Remoting using ItfPartagee; namespace CoteServeur { class Serveur : MarshalByRefObject, IServeur { [OneWay()] public void Ping() { Console.WriteLine("Ping() reçu"); } } } 12 Lionel Seinturier 3.2 Paramètres 3.2 Paramètres 2 types de passage de paramètres Passage de paramètre par référence - types de base (int, float, string, …) : par valeur - objets : par valeur (sérialisation) ou par référence Transmission d'un objet d'une classe qui hérite de MarshalByRefObject Client Serveur # # class Serveur: MarshalByRefObject, IServeur { ... } Client # new # # class ServeurFactory: MarshalByRefObject, IServeurFactory { public IServeur NewServeur() { return new Serveur(); } } Serveur new # # Client ServeurFactory # new Serveur # # .Net Remoting 13 Lionel Seinturier .Net Remoting 3.2 Paramètres 14 3.3 Activation de serveurs Passage de paramètre par valeur 2 types d'activations Transmission d'un objet d'une classe associée à l'attribut Serializable • par le serveur • par le client [Serializable] public class Personne { string nom; public Personne(string nom) { this.nom=nom; } public string GetNom() { return nom; } } class Entreprise: MarshalByRefObject, IEntreprise { public Personne NewPersonne() { return new Personne(); Client } } # .Net Remoting 15 Activation par le serveur 2 possibilités pour chaque classe d'objet • singleton : 1 seule instance • single call : 1 instance par invocation Entreprise # new Personne # Lionel Seinturier # Lionel Seinturier RemotingConfiguration.RegisterWellKnownServiceType( typeof(Serveur), "hello", // URI WellKnownObjectMode.Singleton ); ou .SingleCall .Net Remoting 16 Lionel Seinturier 3.3 Activation de serveurs 4. Configuration Activation par le client 2 outils • 1 instance par client • le client demande explicitement la création de(s) l'instance(s) • soapsuds • fichiers XML Côté serveur soapsuds RemotingConfiguration.ApplicationName = "MyServer"; RemotingConfiguration.RegisterActivatedServiceType( typeof(Serveur) ); But : éviter l'écriture de l'assembly partagée (itf.dll) entre le client et le serveur ! soapsuds utilitaire qui génère les informations à partir du serveur Côté client RemotingConfiguration.RegisterActivatedClientType( typeof(Serveur), "tcp://localhost:1704/SayHello" ); Serveur proxy = new Serveur(); string ret = proxy.SayHello(); .Net Remoting 17 soapsuds -url:http://localhost:1704/hello?wsdl –oa:itf.dll Lionel Seinturier .Net Remoting 4.2 Fichiers XML 18 4.2 Fichiers XML Fichiers XML Installation objet serveur But : éviter de programmer les paramètres de configuration (channel, port, URI, …) class Start { static void Main(string[] args) { RemotingConfiguration.Configure("remoting.xml"); <configuration> <system.runtime.remoting> <application> <service> <wellknown type = "Serveur, Serveur" <!-- classe, assembly --> mode = "SingleCall" objectUri = "hello" /> </service> <channels> <channel ref="http" port="1704" /> </channels> </application> </system.runtime.remoting> </configuration> .Net Remoting 19 Lionel Seinturier Lionel Seinturier Console.WriteLine("Serveur pret"); Console.ReadLine(); } } Avantage : dynamicité i.e. pas de recompil. en cas changement param. dans XML Inconvénient : aléas de la "programmation en XML" .Net Remoting 20 Lionel Seinturier 5. Fonctionnalités avancées 5.1 Ramasse-miettes Ramasse-miettes (garbage collection) Pb : perte de message de déréférencement ? Solution connue : ≈ ping Constat : objets factory créant des objets accesibles à distance class ServeurFactory : MarshalByRefObject, IServeurFactory { public IServeur NewCreate() { return new Serveur(); Client } } # Serveur Factory new class Serveur : MarshalByRefObject, IServeur { ... } ## # Pb : libérer les objets (ressources) non utilisés par les clients Difficulté : environnement réparti Solution connue (DCOM) : comptage de référence Pb : pas forcément possible en environnement HTTP/firewall Solution : mécanisme de bail (lease) • chaque objet crée est associé à un bail (par défaut : 10min) • à chaque appel de méthode, bail renouvelé (par défaut : 2min) éventuellement appels explicite à ILease.Renew() • à l'expiration du bail, le lease manager interroge les sponsors (clients précédents) pour connaître l'état de l'objet ! si pas de demande de renouvellement, à la poubelle .Net Remoting 21 Lionel Seinturier .Net Remoting 5.1 Ramasse-miettes surcharge de la méthode InitializeLifetimeServices hérité de MarshalByRefObject using System.Runtime.Remoting.Lifetime; class Serveur : MarshalByRefObject, IServeur { public override object InitializeLifetimeService() { ILease lease = (ILease) base.InitializeLifetimeService(); lease.InitialLeaseTime = TimeSpan.FromMilliseconds(10); lease.SponsorshipTimeout = TimeSpan.FromMilliseconds(10); lease.RenewOnCallTime = TimeSpan.FromMilliseconds(10); return lease; } } class Install { static void main(string[] args) { LifetimeServices.LeaseManagerPollTime = TimeSpan.FromMilliseconds(10); ... 23 Lionel Seinturier 6. Comparaison Personnalisation des baux .Net Remoting 22 .Net Java RMI CORBA IIOP principe def. interface dist. serveur noms c/s C#, VB, … non c/s Java oui c/s CORBA IDL oui invoc. sync. invoc. semi-sync. invoc. async. invoc. statique invoc. dyn. oui oui oui oui non oui non non oui non oui oui oui oui oui param. par valeur param. par ref. oui oui oui oui oui oui gen. dyn. proxy activation par serveur oui oui oui (1.5) oui non oui singleton/singlecall [ oui non activation par client Lionel Seinturier .Net Remoting 24 permanente/à la demande ] non Lionel Seinturier 6. Comparaison protocole données ramasse-miettes personnalisation .Net Remoting .Net Java RMI CORBA IIOP TCP, HTTP IIOP, … bin, SOAP, … oui JRMP, IIOP GIOP, IIOP bin oui (JRMP) bin non Channels Formatters RMISocket Factory Portable Interceptors 25 Lionel Seinturier