Remoting - Is server running?

O

Olie

I have a number of applications that need to access the same object at
different times and any one of these programs may be running at any one
time. I do not have a specific application that could act as the
server. So I want to give each application the capability of being the
server and the first one to start gets the job.

Unfortunately to do this I need some way of detecting whether a server
is already running. I was hoping that Activator.GetObject() might
return null if there is no server but it just returns a usless object
that throws an exception every time you try and use it.

Is there any way without using exceptions (which are slow and horrible)
to find out if another server is running?

I have posted my code bellow to help you understand what I mean. Both
functions are calling by any program wishing to access the remoted
object.

I also have the same problem with Channels.

Thanks for any help!

Olie

public void StartService(string Name,Type type)
{
if (_ServerChannel == null)
{
try
{
BinaryClientFormatterSinkProvider ClientProvider =
new BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider ServerProvider =
new BinaryServerFormatterSinkProvider();
System.Collections.Hashtable Properties = new
System.Collections.Hashtable();

ServerProvider.TypeFilterLevel =
System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
Properties["port"] = xxxx;
Properties["name"] = "xxxx";
Properties["typeFilterLevel"] = "Full";
_ServerChannel = new TcpChannel(Properties,
ClientProvider, ServerProvider);

ChannelServices.RegisterChannel(_ServerChannel);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}

RemotingConfiguration.RegisterWellKnownServiceType(type,
Name, WellKnownObjectMode.Singleton);
}

public void ConnectToService(string Url, string Name, Type
type, ref XxxxService Endpoint)
{
if (_Channel == null)
{
BinaryClientFormatterSinkProvider ClientProvider = new
BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider ServerProvider = new
BinaryServerFormatterSinkProvider();
System.Collections.Hashtable Properties = new
System.Collections.Hashtable();

ServerProvider.TypeFilterLevel =
System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
Properties["port"] = 0;
_Channel = new TcpChannel(Properties, ClientProvider,
ServerProvider);

ChannelServices.RegisterChannel(_Channel);
}

Endpoint = (AcwService)Activator.GetObject(type, "tcp://" +
Url + ":xxxx/" + Name);

Register(Endpoint);
}
 
M

Michael Bray

I have a number of applications that need to access the same object at
different times and any one of these programs may be running at any one
time. I do not have a specific application that could act as the
server. So I want to give each application the capability of being the
server and the first one to start gets the job.

I couldn't quite tell from your post if you meant that all applications are
running on the same machine (as would perhaps be implied in the above
paragraph) or if they are running on different machines (as would be
implied by the fact that you are trying to use remoting to synchronize.)

If they are all running on one machine and you are just using remoting to
connect to a local object, then perhaps a named mutex would work better.
The url below describes using named mutexes to implement single-instance
application instantiation, but you could easily adapt it so that several
applications run with knowledge of whether your other applications are
running.

http://odetocode.com/Blogs/scott/archive/2004/08/20/401.aspx

-mdb
 
O

Olie

Perhaps why you are getting confused is that the software is designed
to both work locally and remotly.

I also believe that remoting supports other features such as events
which mutexs would not.
 
L

Leon Lambert

I have a similar situation but not exactly the same. In my case i can
have multiple servers and many multiple clients. I used UDP for everyone
to find out about each other. The servers identify themselves
periodically. They can also respond to an immediate request for
identification from any client.
In your case you can have your server try to start and do the following
logic
1) Try to open udp channel. If this fail a server is already running on
this machine.
2) Send an identification request over UDP to see if a server responds.
If no response become the servers.

Hope this helps
Leon Lambert
I have a number of applications that need to access the same object at
different times and any one of these programs may be running at any one
time. I do not have a specific application that could act as the
server. So I want to give each application the capability of being the
server and the first one to start gets the job.

Unfortunately to do this I need some way of detecting whether a server
is already running. I was hoping that Activator.GetObject() might
return null if there is no server but it just returns a usless object
that throws an exception every time you try and use it.

Is there any way without using exceptions (which are slow and horrible)
to find out if another server is running?

I have posted my code bellow to help you understand what I mean. Both
functions are calling by any program wishing to access the remoted
object.

I also have the same problem with Channels.

Thanks for any help!

Olie

public void StartService(string Name,Type type)
{
if (_ServerChannel == null)
{
try
{
BinaryClientFormatterSinkProvider ClientProvider =
new BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider ServerProvider =
new BinaryServerFormatterSinkProvider();
System.Collections.Hashtable Properties = new
System.Collections.Hashtable();

ServerProvider.TypeFilterLevel =
System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
Properties["port"] = xxxx;
Properties["name"] = "xxxx";
Properties["typeFilterLevel"] = "Full";
_ServerChannel = new TcpChannel(Properties,
ClientProvider, ServerProvider);

ChannelServices.RegisterChannel(_ServerChannel);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}

RemotingConfiguration.RegisterWellKnownServiceType(type,
Name, WellKnownObjectMode.Singleton);
}

public void ConnectToService(string Url, string Name, Type
type, ref XxxxService Endpoint)
{
if (_Channel == null)
{
BinaryClientFormatterSinkProvider ClientProvider = new
BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider ServerProvider = new
BinaryServerFormatterSinkProvider();
System.Collections.Hashtable Properties = new
System.Collections.Hashtable();

ServerProvider.TypeFilterLevel =
System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
Properties["port"] = 0;
_Channel = new TcpChannel(Properties, ClientProvider,
ServerProvider);

ChannelServices.RegisterChannel(_Channel);
}

Endpoint = (AcwService)Activator.GetObject(type, "tcp://" +
Url + ":xxxx/" + Name);

Register(Endpoint);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top