-2

i am programming a virtual network.
I have a central Client class which implements my whole network and allows to send/receive data to/from an specific other Client in the network. I now wan't to use a single instance of the Client class for ALL programs using the network. I have an interface for the programs to handle incoming packets, and a wrapper for sending, so I would appreciate another solution than "run a local TCP server".

My question now is: How can I have a single instance of a class running for all Programs using it in Java?

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
th3falc0n
  • 1,389
  • 1
  • 12
  • 33
  • Sounds like you should make a common Java library with the Client class in it, that you add to each separate Java program's build path... But @MrSmith42 is right, there's no question. – Philip Whitehouse Jan 12 '13 at 21:03
  • I am sorry, the question was indirectly in the sentence "I now wan't to use a single instance of the Client class for ALL programs using the network." – th3falc0n Jan 12 '13 at 21:06

1 Answers1

0

This can be easily done by using RMI, RMI-IIOP or CORBA features that are available in Java. The mentioned technologies allow to create a single instance of object on a single machine (server) and export it to other computers (you say you need network) as a remote interface. In other hosts, you will have a stub class with the same interface your "real" instance implements, and will be able to call methods on it. These calls will be at the end remotely invoked on the single instance of your servant. To may opinion, this exactly that you are asking for.

From newer, more interesting approaches, Google Protocol Buffers looks like a good thing to look at, while you need more custom code with Protocol Buffers. Web services also allow to export the interface and are more suitable to pass through firewalls (other here listed approaches are more suitable for talking between your own servers you can configure how you want).

In case you need to ensure that only one instance of your program runs on a given operating system, this can be done as discussed in this question. If you program is listening on fixed ports, second instance probably will not start properly anyway as the ports are already taken; all you need is to catch exception when you try to bind the socked and terminate.

Community
  • 1
  • 1
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • I think I was a little wrong with my english. I don't want to have a single instance in a network. I wan't a single instance of my network client on a local PC. I am actually trying to program a network like Tor. – th3falc0n Jan 12 '13 at 21:14
  • Who prevents you from having a single instance, then? – Audrius Meškauskas Jan 12 '13 at 21:16
  • I don't know how to do it :/ – th3falc0n Jan 12 '13 at 21:18
  • In your instance class, declare public static final MyInstance instance = new MyInstance() and then access MyInstance.instance where you need to access that object. – Audrius Meškauskas Jan 12 '13 at 21:21
  • I just tried that and just like I expected that opens one instance per program. But for my purpose there may be only a single instance of the Client on one System. – th3falc0n Jan 12 '13 at 21:29
  • So you need to know that there is no other application instance running on the same computer. This have already been asked. I am adding the link to the bottom of my answer. – Audrius Meškauskas Jan 12 '13 at 21:32
  • Im sorry but you are wrong again, there may be multiple applications on one system, but they have to share the same Client class, because it provides the access to the virtual network. – th3falc0n Jan 12 '13 at 21:45
  • Then my INITIAL suggestion is good. Start the built-in RMI or CORBA name service with the first instance of you application, create and bind your object to it. On every start check maybe this service is running. If it is running, obtain the object reference from there, otherwise use the local instance. You need an extra functionality to migrate the object if the first started program (primary server) shuts down. – Audrius Meškauskas Jan 12 '13 at 21:52
  • Thank you for your patience. I thought that that would only work on a single instance per System, i will try that. – th3falc0n Jan 12 '13 at 21:57