Re: Singleton Objects
From: pSm (pSm_at_discussions.microsoft.com)
Date: 11/01/04
- Next message: Ken Kolda: "Re: Singleton Objects"
- Previous message: Ken Kolda: "Re: Singleton Objects"
- In reply to: Ken Kolda: "Re: Singleton Objects"
- Next in thread: Ken Kolda: "Re: Singleton Objects"
- Reply: Ken Kolda: "Re: Singleton Objects"
- Reply: Ken Kolda: "Re: Singleton Objects"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 1 Nov 2004 13:19:01 -0800
Ken,
Here's the server 'Type' -->
--------------------------------------------------------------------------------------------
Imports System.Diagnostics
Public Class clsRemoteType
Inherits MarshalByRefObject
Private arrIDs(10) As String
Private Shared lastid As String
Private Shared message As String
Private count As Integer = 0
Public evt As New System.Diagnostics.EventLog("RemoteLog.txt")
Public Sub clsRemoteType()
evt.WriteEntry("APP", "Remote Type Created")
End Sub
Protected Overrides Sub finalize()
evt.WriteEntry("APP", "Remote Type Destroyed" + CStr(DateTime.Now))
End Sub
Public Sub send(ByVal id As String, ByVal msg As String)
Dim i As Integer
Dim blnFound As Boolean
For i = 0 To count
If (arrIDs(i) = id) Then
'Found - Already present no need to add
blnFound = True
Exit For
End If
Next
If (Not blnFound) Then
arrIDs(count) = id
count = count + 1
End If
lastid = id
message = msg
evt.WriteEntry("APP", "Message written -" + message +
CStr(DateTime.Now))
End Sub
Public Function receive() As String
Return (lastid + " : " + message)
End Function
End Class
--------------------------------------------------------------------------------------------
The Host -->
Imports System
Imports System.Runtime.Remoting
Module Module1
Sub Main()
Try
RemotingConfiguration.Configure("../RemotingHost.exe.config")
Console.Write("Waiting for Connections to arrive --")
Console.ReadLine()
Catch ex As Exception
Console.Write("Exception Occured..")
End Try
End Sub
End Module
-------------------------------------------------------------------------------------------
The Server 'Config'
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown
mode="Singleton"
type="clsRemoteType, RemoteType.clsRemoteType"
objectUri="RemoteTypeURI.rem"
/>
</service>
<channels>
<channel ref="http" port="8989"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
----------------------------------------------------------------------------------------------
The client -->
Code when a message is sent :
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSend.Click
Dim obj As New RemoteType.clsRemoteType
obj.send("ABC", txtMsg.Text)
End Sub
Code that polls ( a timer is used)
Private Sub tmPollMsg_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmPollMsg.Tick
Dim obj As New RemoteType.clsRemoteType
Dim str As String
str = obj.receive()
If Trim(str) <> ":" And lastmessage <> str Then
lblMsg.Text = lblMsg.Text + str + vbCrLf
lastmessage = str
End If
End Sub
------------------------------------------------------------------------------------------
Client Config -->
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown
type="clsRemoteType, RemoteType.clsRemoteType"
url="http://FLL020361:8989/RemoteTypeURI.rem"
/>
</client>
</application>
</system.runtime.remoting>
</configuration>
---------------------------***************-------------------------------------
Thanks
"Ken Kolda" wrote:
> If the class is exposed as a singleton, the Message value should be shared
> across all clients. The fact that you're seeing the object destroyed after
> each call from a client implies this is SingleCall instead of Singleton
> (although even then I wouldn't expect the finalizer to run right away unless
> your forcing garbage collection). You want to post some code if you still
> have issues.
>
> Ken
>
>
> "pSm" <pSm@discussions.microsoft.com> wrote in message
> news:B31F2E03-2A5C-4E32-B9EC-FC7EB29E84D4@microsoft.com...
> > Hi Sam,
> > Thanks for the post , but I am not really looking on how to
> implement
> > the chat program, but understanding/clearing my remoting concepts.
> >
> > To re-iterate, I would like to know if my understanding that if the object
> > is singleton, and the message property is shared, every user would have
> > access to the same message value, is correct or not ?
> >
> > Thanks,
> >
> >
> > "Sam Santiago" wrote:
> >
> > > Here's a remoting Chat example that uses events to accomplish what you
> want.
> > > Also, if you are seeing the finalizer run each time you might have
> defined a
> > > SingleCall SAO vs. a Singleton SAO. Check out this example for more
> info:
> > >
> > > http://support.microsoft.com/default.aspx?scid=kb;en-us;312114
> > >
> > > Thanks,
> > >
> > > Sam
> > >
> > > --
> > > _______________________________
> > > Sam Santiago
> > > ssantiago@n0spam-SoftiTechture.com
> > > http://www.SoftiTechture.com
> > > _______________________________
> > > "pSm" <pSm@discussions.microsoft.com> wrote in message
> > > news:1DD1BB89-DC6E-422A-A8E7-C0BB2F05A273@microsoft.com...
> > > > Hi,
> > > > I was trying to write a basic chat client/server. I used remoting for
> > > this.
> > > > The server type uses a shared property message to keep track of the
> last
> > > sent
> > > > message. All the clients poll the server every 10ms to see if a new
> > > message
> > > > is arrived.
> > > >
> > > > The server type is singleton. My guess was since the object is
> singleton
> > > and
> > > > message is shared, the last mesage posted by anyone would be visible
> to
> > > > anybody else.
> > > >
> > > > For example, say A says 'Hi' . When B polls for the message, he should
> see
> > > > the 'Hi'. But what I see is, every client is seeing only the message
> that
> > > he
> > > > has posted and nothing else. So, if A says 'Hi' and B says 'Bye', A
> only
> > > sees
> > > > 'Hi' and B only sees 'Bye'. Can anyone help me understand what's wrong
> ?
> > > >
> > > > Second question is, after I saw that this was happening, I tried
> finding
> > > out
> > > > if the object is getting destroyed. So I put in a finalizer and some
> logs
> > > in
> > > > it. I see the finalizer run after every call from the client.Why is
> this
> > > so ?
> > >
> > >
> > >
>
>
>
- Next message: Ken Kolda: "Re: Singleton Objects"
- Previous message: Ken Kolda: "Re: Singleton Objects"
- In reply to: Ken Kolda: "Re: Singleton Objects"
- Next in thread: Ken Kolda: "Re: Singleton Objects"
- Reply: Ken Kolda: "Re: Singleton Objects"
- Reply: Ken Kolda: "Re: Singleton Objects"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|