NobrElosztott rendszerek labor — Remoting/nobr

A VIK Wikiből
A lap korábbi változatát látod, amilyen (vitalap) 2012. október 21., 20:30-kor történt szerkesztése után volt. (Új oldal, tartalma: „{{GlobalTemplate|Infoszak|ElosztottLaborRemoting}} <style> code.pre { white-space:pre; display:block; } </style> __TOC__ Lásd még: [[IIRLaborRemoting|Integrált in…”)
(eltér) ← Régebbi változat | Aktuális változat (eltér) | Újabb változat→ (eltér)
Ugrás a navigációhoz Ugrás a kereséshez

Ez az oldal a korábbi SCH wiki-ről lett áthozva. Az eredeti változata itt érhető el.

Ha úgy érzed, hogy bármilyen formázási vagy tartalmi probléma van vele, akkor kérlek javíts rajta egy rövid szerkesztéssel.

Ha nem tudod, hogyan indulj el, olvasd el a migrálási útmutatót


<style> code.pre { white-space:pre; display:block; } </style>

Lásd még: Integrált információs rendszerek — Remoting labor

Szimpla összeadás

Előkészületek

  • New Project / Other / Solution: RemotingLab
  • Add project / C# Class Library: Common
  • Add project / C# Console Application: Server
    • Add reference / Projects: Common
    • Add reference / .NET: System.Runtime.Remoting
  • Add project / C# Console Application: Client
    • Add reference / Projects: Common
    • Add reference / .NET: System.Runtime.Remoting
  • Solution / Properties / Startup Project: Multiple, 1. Server, 2. Client, mindkettő Start

Common / Class1.cs

namespace Common
{
	 public interface IBusinessLogic {
		  int Add(int a, int b);
	 }
}

Server / Program.cs

namespace Server
{
	 class BusinessLogic : MarshalByRefObject, IBusinessLogic
	 {
		  public int Add(int a, int b) {
				return a + b;
		  }
	 }

	 class Program
	 {
		  static void Main(string[] args) {
				TcpChannel channel = new TcpChannel(9000);
				ChannelServices.RegisterChannel(channel, false);
				// SingleCall: minden híváskor új objektum példány jön létre, ami a hívás végén megszűnik
				RemotingConfiguration.RegisterWellKnownServiceType(
					 typeof(BusinessLogic), "IBusinessLogic", WellKnownObjectMode.SingleCall);
				// A szerver csak addig tud aktiválni, amíg fut
				Console.ReadLine();
		  }
	 }
}

Client / Program.cs

static void Main(string[] args) {
	 IBusinessLogic ibl = (IBusinessLogic)Activator.
		  GetObject(typeof(IBusinessLogic), "tcp://localhost:9000/IBusinessLogic");
	 Console.WriteLine(ibl.Add(10, 3));
}

Chat

Common / Class1.cs

namespace Common
{
	 public interface IChatClient {
		  // OneWay: aszinkron hívás
		  [OneWay]
		  void Show(string name, string message);
	 }

	 public interface IChatServer {
		  void LogIn(string name, IChatClient client);
		  void LogOut(string name);
		  void Send(string name, string message);
	 }
}

Server / Program.cs

namespace Server
{
	 class ChatServer: MarshalByRefObject, IChatServer
	 {
		  Dictionary<string, IChatClient> clients = new Dictionary<string, IChatClient>();
		  ReaderWriterLock rwl = new ReaderWriterLock();

		  public void LogIn(string name, IChatClient client)
		  {
				rwl.AcquireWriterLock(100);
				clients[name] = client;
				rwl.ReleaseLock();
		  }

		  public void LogOut(string name)
		  {
				rwl.AcquireWriterLock(100);
				clients.Remove(name);
				rwl.ReleaseLock();
		  }

		  public void Send(string name, string message)
		  {
				rwl.AcquireReaderLock(100);
				foreach (string key in clients.Keys)
					 if (key != name)
						  clients[key].Show(name, message);
				rwl.ReleaseLock();
		  }

		  // a szerver akkor se szűnjön meg, ha sokáig nem érkezik kérés
		  public override object InitializeLifetimeService()
		  {
				return null;
		  }
	 }

	 class Program
	 {
		  static void Main(string[] args) {
				// callback engedélyezése
				BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
				provider.TypeFilterLevel = TypeFilterLevel.Full;
				IDictionary props = new Hashtable();
				props["port"] = 9000;

				TcpChannel channel = new TcpChannel(props, null, provider);
				ChannelServices.RegisterChannel(channel);
				// Singleton: egy példány jön létre és megtartja az állapotát a hívások között
				RemotingConfiguration.RegisterWellKnownServiceType(
					 typeof(ChatServer), "IChatServer", WellKnownObjectMode.Singleton);
				// A szerver csak addig tud aktiválni, amíg fut
				Console.ReadLine();
		  }
	 }
}

Client / Program.cs

namespace Client
{
	 class ChatClient : MarshalByRefObject, IChatClient
	 {
		  public void Show(string name, string message) {
				Console.WriteLine("{0} : {1}", name, message);
		  }

		  // a kliens akkor se szűnjön meg, ha sokáig nem érkezik kérés
		  public override object InitializeLifetimeService() {
				return null;
		  }
	 }

	 class Program
	 {
		  static void Main(string[] args) {
				// A kliens a szerverrel felépített TCP kapcsolaton keresztül hívható
				TcpChannel channel = new TcpChannel(0);
				ChannelServices.RegisterChannel(channel);

				IChatServer ics = (IChatServer)Activator.
					 GetObject(typeof(IChatServer), "tcp://lab52000:9000/IChatServer");

				ics.LogIn(args[0], new ChatClient());
				for (string msg = ""; msg != "bye"; )
					 ics.Send(args[0], msg = Console.ReadLine());
				ics.LogOut(args[0]);
		  }
	 }
}

-- Peti - 2006.04.21.