Bài giảng Công nghệ lập trình tiên tiến - Net Remoting

Net Remoting Definition:

• Application which is located in another application domain or process can communicate with another by using .Net Remoting.

• Net Remoting allows processes to share the objects. It can call the method and can access the properties of an objects that are:


pptx73 trang | Chuyên mục: Lập trình trực quan | Chia sẻ: dkS00TYs | Ngày: 08/06/2015 | Lượt xem: 1372 | Lượt tải: 2download
Tóm tắt nội dung Bài giảng Công nghệ lập trình tiên tiến - Net Remoting, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
emote Object Build Host/Server application Build client application Create a new instance of remote object by using new While you do this, Remoting system creates the proxy object of Remotable object Remoting system receives that call and routes it to the server It then processes the request and return the result to the proxy, which intern return it to the client application. Note • Remote object should inherit “MarshalbyRefObject” class. • A client needs to obtain proxy, should activate remote Object by Calling CreateInstance, GetObject or by using new key word. • Local object has to be passed as parameter when making remote calls. It should passed by value. • This object must be serialized. Remoting clients A clients wants to use a remote object Well-known objects exist at a URL • Client obtains proxy using Activator.GetObject • Client can also obtain proxy using new Client activated object factory exists at a URL • Client requests factory to instantiate object and return a proxy to it using Activator.CreateInstance • Client can also make same request using new Activator.GetObject GetObject returns a proxy for the served at the specified location No network traffic until first method call! – Proxy built on client from metadata – Server activates object on first method call 2 Examples TcpChannel & HttpChannel ExamplesTcpChannel -Name solution : TcpChannelRemoting It has 3 projects: 	1) ProxyObject class Library project 	2) ServerTier (Winform application) 	3) ClientTier (Winform application) 	 See next slide…. Create TcpChannelRemoting solution: Choose Visual Studio Solutions Type name: TcpChannelRemoting Now, I add a ProxyObject class Library project into this solution. Right click on solution / add / New Project Choose Class Library Enter name: ProxyObject then click Ok button And then add new class : MyProxy Enter name: MyProxy.cs and click Add button using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ProxyObject { public class MyProxy:MarshalByRefObject { } } Inherit MarshalByRefObject as below: Modify class MyProxy: Add new 2 attributes: 	m_strName which string type to store 	current name of client request 	m_arr which ArrayList type to store list 	name from client request public void MakeRequest(string s) public string GetInfor() Use to set current name and add this name into m_arr Return current name, number of request and all name client request in m_arr public class MyProxy:MarshalByRefObject { private string m_strName = ""; private ArrayList m_arr=new ArrayList(); public MyProxy(){ } public void MakeRequest(string s) { this.m_strName = s; this.m_arr.Add(s); } public string GetInfor() { string sInfor = "Hello "+this.m_strName +"\n"; sInfor += " You are client " + this.m_arr.Count + "\n"; sInfor += " ---List client request---\n "; for (int i = 0; i < this.m_arr.Count; i++){ sInfor += this.m_arr[i] + "\n"; } return sInfor; } } Create ServerTier Winform application project Similar create ProxyObject project, but in this case, you choose Windows forms Application You create UI as design Control Name Text TextBox txtServerName Localhost TextBox txtPort 9999 RadioButton radSingleCall SingleCall RadioButton radSingleTon SingleTon Button btnStart Start Server Button btnStop Stop Server Label lblStatus Status Add Reference ProxyObject -Right click on References/ Add reference… Add Reference System.Runtime.Remoting Right click References/ Add Reference… OK!!! Coding ServerTier: using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using ProxyObject; TcpChannel tcpChannel = null;//Member variable name private void btnStart_Click(object sender, EventArgs e) {//Event for button Start Server StartServer(); } private void btnStop_Click(object sender, EventArgs e) {//Event for button Stop Server StopServer(); } Using… private void StopServer() { if (ChannelServices.GetChannel("tcp") != null) { ChannelServices.UnregisterChannel(tcpChannel); lblStatus.Text = "Server is Stopped!"; } } ChannelServices.GetChannel("tcp") 	Check Channel is available ChannelServices.UnregisterChannel(tcpChannel) 	Unregister channel private void StartServer() {StopServer();// Call Stop server int port = Int32.Parse(txtPort.Text);//Get Port //Create TcpChannel tcpChannel = new TcpChannel(port); //Get WellKnownObjectMode WellKnownObjectMode mode=radSingleTon.Checked ? 	WellKnownObjectMode.Singleton : 	WellKnownObjectMode.SingleCall; Type type=typeof(MyProxy);//Get type object //Register Channel ChannelServices.RegisterChannel(tcpChannel,false); //Register Remoting object to Remoting framework RemotingConfiguration.RegisterWellKnownServiceType 	(type, "MYPROXY_URI", mode); lblStatus.Text = "Server is Starting...” 	+mode.ToString(); } In client Tier, we will use MYPROXY_URI Create ClientTier Similar create ServerTier Control Name Text TextBox txtServerName Localhost TextBox txtPort 9999 TextBox txtName Enter name client Label lblSatus Status connect Button btnGetInfor Get Infor Button btnConnect Connect RichTextBox richInfor Get infor from server Add Reference to ProxyObject & System.Runtime.Remoting as the same ServerTier Coding ClientTier using System.Runtime.Remoting; using ProxyObject; MyProxy myproxy; private void btnConnect_Click 	(object sender, EventArgs e) { string s= txtServerName.Text;//Server name string p = txtPort.Text;//Port string url = 	"tcp://" + s+ ":" + p+ "/MYPROXY_URI"; RemotingConfiguration.RegisterWellKnownClientType 	(typeof(MyProxy), url); lblStatus.Text = "Connected!"; } MYPROXY_URI is defined from server private void btnGetInfor_Click 	(object sender, EventArgs e) { myproxy = new MyProxy(); myproxy.MakeRequest(txtName.Text); richInfor.Text = myproxy.GetInfor(); } Before slide, we write code for button Connect, Now we code for button Get infor: Final, now we Batch Build Solution and run .exe Choose Batch Build… from menu Build Checked all and then click Rebuild Run ClientTier.exe Also, We open ServerTier release folder to run ServerTier.exe What’s problem with SingleCall??? Summary: SingleTon SingleCall RegisterChannel RegisterWellKnownServiceType RegisterWellKnownClientType And … Examples HttpChannel Make public Chatting Application Server Chat UI Client Chat UI public class ChatInformation:MarshalByRefObject { private ArrayList clients = new ArrayList(); private string chatSession = ""; public void AddClient(string name) { if (name != null) { lock (clients) { clients.Add(name); } } } Create ProxyObject, it has ChatInformation class public void RemoveClient(string name) { if (clients != null) { lock (clients) { clients.Remove(name); } } } public void AddText(string text) { if (text != null) { lock (chatSession) { chatSession += text; } } } public ArrayList GetClients() { return clients; } public string ChatSession() { return chatSession; } } Create ServerChatUI Project, you must add full reference as the same example1 using ProxyObject; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; Control Name Text TextBox txtServerName Localhost TextBox txtPort 9999 Button txtStart Start Server Button txtStop Stop Server Label lblStatus Status HttpChannel httpChanel; private void btnStart_Click(object sender, EventArgs e) { //Create httpChannel with Port httpChanel = new HttpChannel(Int32.Parse(txtPort.Text)); //Register httpchannel ChannelServices.RegisterChannel(httpChanel, false); //Register remote object to remote framework RemotingConfiguration.RegisterWellKnownServiceType (typeof(ChatInformation), "CHATCHAT", WellKnownObjectMode.Singleton); lblStatus.Text= "Server is starting...."; } private void btnStop_Click(object sender, EventArgs e) { if (ChannelServices.GetChannel("http") != null) { ChannelServices.UnregisterChannel(httpChanel); lblStatus.Text = "Server is Stopped!"; } } CHATCHAT will be use in the client UI Create ClientChatUI Project, you must add full reference as the same example1 Control Name Text TextBox txtServer Localhost TextBox txtPort 9999 TextBox txtName User name RichTextBox richChat Enter chat RichTextBox richHistory History chat ListBox listUser List user login Button btnLogin Login Button btnSend Send Chat Button btnClear Clear Chat Button btnLogout Logout We use multi Threading to code Client Chat using ProxyObject; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using System.Threading; private ChatInformation chatInfor; private string userName = ""; private Thread threadChat; Create Member variables…: Next slide to see detail coding… private void btnLogin_Click 	(object sender, EventArgs e) { string URL = "http://" + txtServer.Text + ":" + 	txtPort.Text + "/CHATCHAT"; RemotingConfiguration.RegisterWellKnownClientType 	(typeof(ChatInformation), URL); 	chatInfor = new ChatInformation(); 	userName = txtName.Text; 	chatInfor.AddClient(userName); 	threadChat = new Thread(new 	ThreadStart(PollServer)); 	threadChat.Start(); } Write code for Login button See PollServer method in the next slide… int selectedIndex = 0; private void PollServer() { for (; ; ) {Thread.Sleep(1000); 	ArrayList clients = chatInfor.GetClients(); 	selectedIndex = listUser.SelectedIndex; 	listUser.Items.Clear(); 	for (int i = 0; i < clients.Count; i++) 	{listUser.Items.Add(clients[i].ToString()); 	} 	listUser.SelectedIndex = selectedIndex; 	string sessionText = chatInfor.ChatSession(); 	richHistory.Clear(); 	richHistory.Text = sessionText; } } private void btnLogout_Click 	(object sender, EventArgs e) { chatInfor.RemoveClient(userName); listUser.Items.Clear(); listUser.Items.Add("No longer logged in..."); threadChat.Abort(); } private void btnClear_Click 	(object sender, EventArgs e) {richChat.Clear(); } private void btnSend_Click 	(object sender, EventArgs e) { 	chatInfor.AddText(userName 	 	+":\n"+richChat.Text+"\n\n"); } END 74 HO CHI MINH UNIVERSITY OF INDUSTRY 

File đính kèm:

  • pptx5-Week10_Net Remoting.pptx
Tài liệu liên quan