• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to get Clients CPU information on Server using Java RMI

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have java RMI Client Server programme, I want to see Clients CPU Usage, Memory, HDD Space and Computer ID on Server, how can I do that,I will really appreciate if someone provide me the codes as I am not good in java, I am giving the programme in which I have to put the additional codes, I hope this will work.
What are we trying to create?
During runtime there are three interacting entities involved in an RMI application. These are:
�� A client which is the process that is invoking a method on a remote object.
�� A server which is the process that owns the remote object. The remote object is an ordinary object in the address space of the server process
�� The Object Registry, which is a name server that relates objects with names. Objects are registered with the Object Registry. Once an object has been registered, you can use the Object Registry to obtain access to a remote object using the name of that object.
Basically, the programmer has to explicitly define a remote interface for all remote methods invocated by a client. In order to invoke a method of a remote object, a client has to perform a lookup of the RMI Registry on the server where the remote object resides. The RMI Registry will return a reference to the server object. Before the RMI application can be run, the server's RMI Registry has to be started up. Server objects have to be registered with the RMI Registry. A skeleton for each remote object handles incoming invocations. You'll notice in this tutorial that we don't have to get our hands dirty with sockets or TCP/IP, or even URL's, when we are programming using RMI. All these details are purposefully hidden from the applications programmer.
5. Getting started: the remote interface and the remote class
For our Hello World example, we first need a Remote class and its corresponding Remote interface. We will call these Hello and HelloInterface, respectively. It is not necessary for both the Client and the Server to have access to the definition of the Remote class. The Server requires the definition of both the Remote class and the Remote interface, but the Client only uses the Remote interface. Roughly speaking, the Remote interface represents the type of an object handle, while the Remote class represents the type of an object. If a remote object is being used remotely, its type must be declared to be the type of the Remote interface, not the type of the Remote class.
The following figure shows the contents of HelloInterface.java. It contains the definition of the remote interface that is provided by the server. Any methods specified here are remotely accessible by any client located in a different machine:
/***************************************************************
* Remote Interface for the "Hello, world!" example
*****************************************************************/
import java.rmi.*;
public interface HelloInterface extends Remote {
/**
* Remotely invocable method, say().
*/
public String say() throws RemoteException;
}
2
Note that the interface extends the java.rmi.Remote interface -- all interfaces used in RMI must do this.
An interface, such as the one defined above, is similar to a pure virtual class in C++ in that it defines the methods (and the arguments to the methods) that will be available in a class that implements it; however, it doesn't actually implement any of the methods. The above class contains only one method say(); the method takes no arguments and returns an object of type String. Note that method throws a java.rmi.RemoteException - every method in a remote object's interface must specify this exception in its "throws" clause; this exception is a superclass of all exceptions that can be thrown.
We now need to create a class that implements the above interface. In this example, the implementation is found in Hello.java which is listed below:
/*********************************************************************
* Remote class implementation for the "Hello, world!" RMI example.
**********************************************************************/
import java.rmi.*;
import java.rmi.server.*;
public class Hello extends UnicastRemoteObject implements HelloInterface {
private String message;
/**
* Construct a remote object. msg is the message of the
* remote object, such as "Hello, world!".
*/
public Hello (String msg) throws RemoteException {
message = msg;
}
/**
* Implementation of the remotely invocable method, say().
* Returns the message of the remote object, such as "Hello, world!".
*/
public String say() throws RemoteException {
return message;
}
}
This class must extend java.rmi.UnicastRemoteObject. Objects of such a class exist in the address space of the server and can be invoked remotely. While there are other ways to define a Remote class, this is the simplest way to ensure that objects of a class can be used as remote objects. In our example, the only method that needs to be implemented is say(), however, it can have methods that are not in its Remote interface which can only be invoked locally.
Exercise 1
Compile the above two source files using the SDK command line compiler, javac. Make sure that you compile the interface class first. You now need to run the special RMI
3
interface compiler on the .class file of the implementation class. To do this type a line similar to the following:
rmic Hello
This step generates some additional Java classes. They are the stubs and skeletons used by RMI. You only need to run rmic on the class that implements your RMI interface (in this case Hello.class) Also note that the rmic compiler runs on a .class file, not a .java file.
6. Creating the client and server applications
Your client and server applications will be simple. We will look at the client first. Your client program must have a means of first getting hold of a remote object. The Object Registry enables your client program to do this. It allows you to obtain a remote object using only the name of the remote object. This name is made up the following constituent parts (rather like a URL):
�� The Internet name (or address) of the machine that is running the Object Registry with which the remote object is being registered.
�� The port to which the Object Registry is listening. This has a default value 1099
�� The local name of the remote object within the Object Registry.
Here is our example client program, HelloClient.java:
/***************************************************************
* Client program for the "Hello, world!" RMI example.
*****************************************************************/
import java.rmi.Naming;
public class HelloClient
{
public static void main (String[] argv) {
try {
HelloInterface hello = // your PC address here
(HelloInterface) Naming.lookup ("//IP Number/Hello");
System.out.println (hello.say());
}
catch (Exception e){
System.out.println ("HelloClient exception: " + e);}
}
}
The Naming.lookup method obtains an object handle from the RMI Object Registry running on the machine with IP number (you need to find the IP number of your machine like 134.83.128.5) and listening to the default port 1099. This is the only remotely 'networky' type command we have to issue for this entire application. Note that the result of Naming.lookup must be cast to the type of the Remote interface - in our case a HelloInterface object. After that, all the client does is invoke the remote method say() and print the returned value (a String, remember) to the command interface.
4 Now for the server application, HelloServer.java is given below:
At least one remote object must be registered with the Object Registry. The statement for this is: Naming.rebind (objectName, object); where object is the remote object being registered, and objectName is the String that names the remote object. The Object Registry only accepts requests to bind and unbind objects running on the same machine, so it is never necessary to specify the name of the machine when one is registering an object.
Exercise 2
You are now almost ready to run the application. You will use the same machine as both client and server - so you need to work out the IP number of your machine and then enter that in the correct place in the client application source code. Once you have done this then compile the client and server source files.
You now need to run the start the Object Registry, and leave it running in the background, on the server machine. To do this type the following command:
start rmiregistry
This should fire up a new Command Window and give you the usual Java JDK messages - so long as no error messages crop up everything should be OK and the registry should have started. You could also start the registry from your server code using a line something like LocateRegistry.createRegistry(1234) to start it up and listening on port 1234 (remember it has a default port number of 1099).
Now you need to start the server:
java HelloServer
/***************************************************************
* Server program for the "Hello, world!" RMI example.
*****************************************************************/
import java.rmi.Naming;
public class HelloServer
{
public static void main (String[] argv)
{
try {
Naming.rebind ("Hello", new Hello ("Hello, world!"));
System.out.println ("Hello Server is ready.");
}
catch (Exception e) {
System.out.println ("Hello Server failed: " + e);
}
}
}
56
Now, from a separate command window, run the client application:
java HelloClient
Hello, world!
If you see the message shown above then all has gone fine - and you have implemented your first RMI application.
7. Which are client and which are server classes?
Remember that it is not necessary for both the Client and the Server to have access to the definition of the Remote class. The Server requires the definition of both the Remote class and the Remote interface, but the Client only uses the Remote interface. Remember as well, that a skeleton for a remote object is a server-side entity that contains a method which dispatches calls to the actual remote object implementation. A stub is a proxy for a remote object which is responsible for forwarding method invocations on remote objects to the server where the actual remote object implementation resides. A client's reference to a remote object, therefore, is actually a reference to a local stub.
Exercise 3
Create two directories, one called client and one called server and put only the files needed for each in the relevant directory and run the application. Make sure you know which files reside where ! Once you have done this, try running the client and server on separate machines.
8. Lab Tasks
Test the sample.
Write a Java RMI Client/Server application with one client and two RMI objects. Each RMI object has a method. The two methods from the two RMI objects will be used by the client. You can implement the methods as you like, but make sure they are used in the client.
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, let me do your homework for you....
Forget it. Either take your F in the class or do some work for yourself. If you are up to RMI in a java class and you're as lost as you sound, you need to start over or put in some serious time studying. If you have a small problem with the program, or something you can't get to work the way you expect, thats one thing. But don't come asking other people to bail you out so you can just turn in their work.
Why in the time it took you to type in all the guidelines, you could have read the first 2 pages of this classic post and at least have SOME idea of what you are supposed to be doing.
Do you even understand what your guide lines actually say? It shouldn't be too muddled for you to at least pick it out.
Ah never mind. Go back to slacking off in your mom's basement.
Jeez, i just wasted 4 minutes looking up that link for you. I bet you won't even go look at it....
[ January 22, 2004: Message edited by: Chris Shepherd ]
 
Shiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic