concurrent programming ideas needed!

Hi... I'm pretty new to Java; at uni, we are usually always given C programing assignements due to the nature of the course being very much low-level programming! but for this module, distributed systems, we have to learn about different distributed methods available! ...and obviously, one of the main ones, being RMI.

The assignment is to write a simple RMI chat server/client application, where Clients send messages to the server and the server will send these messaes on to the clients!

OK, so i have understood OO, and i have created an application using RMI that creates an rmi server, and allows clients to connect, the clients can send messages to the server and the server display them - which works nicely!!!

So... my problem is to find a way that the client can listen for messages that were sent to the server by other clients! ...I have looked at threads, so that i can just do a continous loop listening for changes in the message variable, if it changed, then it could be displayed, or by using a flag that gets sent / unset when a new message is recieved! But im pretty stuck as to the way to implement this because it says my class needs to be abstract!!!

Any ideas... please no solutions because its for me to work it out! but the idea of the module is not to do amazing code, but to understand how RMI works!!!! Thanks!!!

[1374 byte] By [bobthebullet990a] at [2007-11-26 18:17:37]
# 1
I don't think you need threads. The client should expose a callback interface. The server should loop over all registered clients and invoke a method on the callback interface (e.g. appendText).Kaj
kajbja at 2007-7-9 5:51:14 > top of Java-index,Java Essentials,Java Programming...
# 2

Ok... I have had a quick read up on javaworld... but can I implement two interfaces for one class... or do I add the callback method to my current interface? So, as said by javaworld...

public interface InterestingEvent

{

// This is just a regular method so it can return something or

// take arguments if you like.

public void interestingEvent ();

}

so, for my app, my interface would become...

public interface ChatInterface extends java.rmi.Remote {

// Function to get current time

public String getTime() throws RemoteException;

// Function for clients to login

public int login(String userID) throws RemoteException;

// Function for clients to send msg

public int sendMsg(String msg) throws RemoteException;

// Function for clients to read msg

public String getMsg() throws RemoteException;

// This is just a regular method so it can return something or

// take arguments if you like.

public void interestingEvent ();

}

So this would also work? thanks!

bobthebullet990a at 2007-7-9 5:51:14 > top of Java-index,Java Essentials,Java Programming...
# 3
One class can implement several interfaces, but you don't need to create a new one in this case.Btw. The interestingEvent should take an argumetn (the event)Kaj
kajbja at 2007-7-9 5:51:14 > top of Java-index,Java Essentials,Java Programming...
# 4

Did you use sockets? Any way your server should be having a method that returns new messages say in an array or vector. Your clients should request for new messages via this method. What do u consider to be a new message. A meassage that has not been read by any other client or what? if so then u need threads that will contend for this method as a resource. The first thread to read the message should mark it as read. Dont mean to confuse u.

thanks

Sq2000

Sq2000a at 2007-7-9 5:51:14 > top of Java-index,Java Essentials,Java Programming...
# 5

Did you use sockets? Any way your server should be having a method that returns new messages say in an array or vector. Your clients should request for new messages via this method. What do u consider to be a new message. A meassage that has not been read by any other client or what? if so then u need threads that will contend for this method as a resource. The first thread to read the message should mark it as read. Dont mean to confuse u.

thanks

Sq2000

Sq2000a at 2007-7-9 5:51:14 > top of Java-index,Java Essentials,Java Programming...
# 6

OK!!! SO i can just add it to my current interace!

Can i just ask... the Java events..... if an event occurs all functions with an event argument are invoked? or does it find out the id of the component that triggered the event and call the compononents listener function? is that correct?

Thanks!!!

bobthebullet990a at 2007-7-9 5:51:14 > top of Java-index,Java Essentials,Java Programming...
# 7

Well... I'm not sure... I didn't setup a socket myself! ...I setup the RMI server on port 9000 and added to the registry, the client can then connect to this server by finding it in the RMI registry!

try{

// Start the rmiregistry to run on port 9000

LocateRegistry.createRegistry(9000);

// Create an instance of the chat server

ChatServer svr = new ChatServer();

// Add the server instance to the registry

Naming.rebind("rmi://localhost:9000/ChatServer",svr);

}

catch(Exception e) {

System.out.println(e);

}

Oh... and ye... what I would call an event is when any client connected with the server sends a message to the server. The other connected clients should then display this message!

bobthebullet990a at 2007-7-9 5:51:14 > top of Java-index,Java Essentials,Java Programming...
# 8
> Did you use sockets? It's an RMI question so your answer does not apply(?)
kajbja at 2007-7-9 5:51:14 > top of Java-index,Java Essentials,Java Programming...
# 9

> OK!!! SO i can just add it to my current interace!

>

> Can i just ask... the Java events..... if an event

> occurs all functions with an event argument are

> invoked? or does it find out the id of the component

> that triggered the event and call the compononents

> listener function? is that correct?

>

> Thanks!!!

I dont' understand your question.

Lets say that you have two clients, client1 and client2.

client1 has a text, and invokes e.g. distributeText on the server. The server implementation for that method should iterate over all registered clients and invoke e.g. appendText on each client. (client1 can then filter out that message or display it)

Kaj

kajbja at 2007-7-9 5:51:14 > top of Java-index,Java Essentials,Java Programming...