Plan.Net Remoting 1. Introduction Lionel Seinturier INRIA Futurs Lille Projet JACQUARD Université Pierre & Marie Curie Lionel.Seinturier@lifl.fr 3. Caractéristiques techniques 3.1. Invocation 3.2. Paramètres 3.3. Activation de serveurs 4. Configuration 5. Fonctionnalités avancées 6/9/04.Net Remoting 1 Lionel Seinturier 6. Comparaison.Net Remoting 2 Lionel Seinturier 1. Introduction.Net Remoting Principe Solution Microsoft pour interopérabilité client/serveur entre objets.net distants - successeur de DCOM (trop complexe) - très proche de Java RMI et de CORBA/IIOP Principe : interaction client/serveur client + serveur + proxy.net génère dynamiquement un objet proxy - mêmes méthodes que l'objet serveur - chaque méthode redirige l'appel vers l'objet serveur proxy rés eau Install prog. client masquée au sein d'un appel de méthode requête rés eau réponse prog. serveur! l'interface (méthodes) de l'objet serveur doit être connue côté client! 3 programmes - client.exe - serveur.exe (classe + classe Install) - itf.dll - contient l'interface du seveur - partagée par client.exe et serveur.exe.net Remoting 3 Lionel Seinturier.Net Remoting 4 Lionel Seinturier
Classe namespace System.Remoting classe : hérite de MarshalByRefObject méthodes publiques namespace Cote { class : MarshalByRefObject, I { public string SayHello() { return "Hello world"; namespace ItfPartagee { public interface I { string SayHello(); Installation objet(s) serveur(s) Concept de channel = canal de communication entre le client et le serveur 2 existants à la base - 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 -IIOP -SMTP - (voir sur Internet).Net Remoting 5 Lionel Seinturier.Net Remoting 6 Lionel Seinturier Installation objet(s) serveur(s) using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; namespace Cote { class Install { static void main(string[] args) { TcpChannel chan = TcpChannel(1704); ChannelServices.RegisterChannel(chan); RemotingConfiguration.RegisterWellKnownServiceType( typeof(), "hello", // URI WellKnownObjectMode.SingleCall ); Console.ReadLine(); Classe namespace Cote { class Install { static void main(string[] args) { I proxy = (I) Activator.GetObject( typeof(i), "tcp://localhost:1704/hello" ); string ret = proxy.sayhello(); Rq : channel HTTP http://localhost:1704/hello 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 types d'invocations 3.1 Invocations 3.1. Invocation 3.2. Paramètres 3.3. Activation de serveurs Synchrone Semi-synchrone (ou asynchrone avec retour) Asynchrone (ou oay) Invocation synchrone Cas par défaut.net Remoting 9 Lionel Seinturier.Net Remoting 10 Lionel Seinturier 3.1 Invocations Invocation semi-synchrone (asynchrone avec retour) Aucun changement côté serveur Côté client Notion de delegate : ~pointeur de fonction méthode pour déclencher l'appel : BeginInvoke méthode pour récupérer le résultat : EndInvoke Invocation asynchrone (oay) 3.1 Invocations 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 idem semi-synchrone (BeginInvoke) EndInvoke retourne toujours immédiatement (avant réception côté serveur) delegate string SayHello(); SayHelloDelegate shd = SayHelloDelegate(proxy.SayHello); IAsyncResult res = shd.begininvoke(null,null);... string ret = shd.endinvoke(res); Rq : SayHelloDelegate générée par le framework namespace ItfPartagee { public interface I { [OneWay()] void Ping(); namespace Cote { class : MarshalByRefObject, I { [OneWay()] public void Ping() { Console.WriteLine("Ping() reçu");.net Remoting 11 Lionel Seinturier.Net Remoting 12 Lionel Seinturier
2 types de passage de paramètres 3.2 Paramètres - types de base (int, float, string, ) : par valeur - objets : par valeur (sérialisation) ou par référence 3.2 Paramètres Passage de paramètre par référence Transmission d'un objet d'une classe qui hérite de MarshalByRefObject class : MarshalByRefObject, I {... class Factory: MarshalByRefObject, IFactory { public I New() { return (); Factory.Net Remoting 13 Lionel Seinturier.Net Remoting 14 Lionel Seinturier Passage de paramètre par valeur 3.2 Paramètres 2 types d'activations 3.3 Activation de serveurs Transmission d'un objet d'une classe associée à l'attribut Serializable [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 Personne(); Entreprise Personne par le serveur par le client Activation par le serveur 2 possibilités pour chaque classe d'objet singleton : 1 seule instance single call : 1 instance par invocation RemotingConfiguration.RegisterWellKnownServiceType( typeof(), "hello", // URI WellKnownObjectMode.Singleton ); ou.singlecall.net Remoting 15 Lionel Seinturier.Net Remoting 16 Lionel Seinturier
Activation par le client 3.3 Activation de serveurs 1 instance par client le client demande explicitement la création de(s) l'instance(s) 2 outils soapsuds fichiers XML 4. Configuration Côté serveur RemotingConfiguration.ApplicationName = "MyServer"; RemotingConfiguration.RegisterActivatedServiceType( typeof() ); Côté client RemotingConfiguration.RegisterActivatedType( typeof(), "tcp://localhost:1704/sayhello" ); proxy = (); string ret = proxy.sayhello(); soapsuds 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 soapsuds -url:http://localhost:1704/hello?wsdl oa:itf.dll.net Remoting 17 Lionel Seinturier.Net Remoting 18 Lionel Seinturier Fichiers XML 4.2 Fichiers XML Installation objet serveur 4.2 Fichiers XML But : éviter de programmer les paramètres de configuration (channel, port, URI, ) <configuration> <system.runtime.remoting> <application> <service> <wellknown type = ", " <!-- classe, assembly --> mode = "SingleCall" objecturi = "hello" /> </service> <channels> <channel ref="http" port="1704" /> </channels> </application> </system.runtime.remoting> </configuration> class Start { static void Main(string[] args) { RemotingConfiguration.Configure("remoting.xml"); Console.WriteLine(" 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 19 Lionel Seinturier.Net Remoting 20 Lionel Seinturier
5. Fonctionnalités avancées Ramasse-miettes (garbage collection) Constat : objets factory créant des objets accesibles à distance class Factory : MarshalByRefObject, IFactory { public I NewCreate() { return (); class : MarshalByRefObject, I {... Pb : libérer les objets (ressources) non utilisés par les clients Difficulté : environnement réparti Solution connue (DCOM) : comptage de référence Factory 5.1 Ramasse-miettes Pb : perte de message de déréférencement? Solution connue : ping 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.Re() à 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 22 Lionel Seinturier Personnalisation des baux 5.1 Ramasse-miettes surcharge de la méthode InitializeLifetimeServices hérité de MarshalByRefObject using System.Runtime.Remoting.Lifetime; class : MarshalByRefObject, I { public override object InitializeLifetimeService() { ILease lease = (ILease) base.initializelifetimeservice(); lease.initialleasetime = TimeSpan.FromMilliseconds(10); lease.sponsorshiptimeout = TimeSpan.FromMilliseconds(10); lease.reoncalltime = TimeSpan.FromMilliseconds(10); return lease; class Install { static void main(string[] args) { LifetimeServices.LeaseManagerPollTime = TimeSpan.FromMilliseconds(10);....Net Remoting 23 Lionel Seinturier 6. Comparaison.Net Java RMI CORBA IIOP principe c/s c/s c/s def. interface dist. C, VB, Java CORBA IDL serveur noms non oui oui invoc. sync. oui oui oui invoc. semi-sync. oui non oui invoc. async. oui non oui invoc. statique oui oui oui invoc. dyn. non non oui param. par valeur oui oui oui param. par ref. oui oui oui gen. dyn. proxy oui oui (1.5) non activation par serveur oui oui oui singleton/singlecall [ permanente/à la demande ] activation par client oui non non.net Remoting 24 Lionel Seinturier
6. Comparaison.Net Java RMI CORBA IIOP protocole TCP, HTTP JRMP, IIOP GIOP, IIOP IIOP, données bin, SOAP, bin bin ramasse-miettes oui oui (JRMP) non personnalisation Channels RMISocket Portable Interceptors Formatters Factory.Net Remoting 25 Lionel Seinturier