Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 4 votes, 4.25 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-18-2004, 02:06 PM   PM User | #1
rynox
New Coder

 
Join Date: Aug 2002
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
rynox is an unknown quantity at this point
Java: How to Create Interface

I'm working on a multi-threaded server project and I created two classes:

1. Class that will open a server socket and loop through accepting sockets. Each time it accepts a connection it spawns a new thread on class #2.

2. This class, in the run() method will create an inputstream from the server socket and read each line coming through the server socket.

What I want to do is in CLASS #1 implement an interface that will invoke a certain method in class #1 each time a line is entered in the server socket. How do you do this?

Here is condensed class #1:
Code:
public Library() {
	ServerSocket serverSocket=null;
	boolean listening=true;
	try {
		serverSocket=new ServerSocket(801);
		while(listening) new rServer(serverSocket.accept()).start();
		serverSocket.close();
	} catch(Exception e) { System.exit(0); }
}
static public void main(String[] args) { new Library(); }
public void processCommand(String cmd, Thread thread){ }
Here is the run() method of class #2.
Code:
public void run() {
	try {
		DataInputStream in=new DataInputStream(s.getInputStream());
		DataOutputStream out=new DataOutputStream(s.getOutputStream());
		out.writeChars("Number of connections: ");
		out.writeChars(java.lang.String.valueOf(Thread.activeCount()-2)+"\r\n");
		String line=null;
		while(true) {
			Thread.yield();
			line=in.readLine();
			System.out.print("Thread ");
			System.out.print(Thread.currentThread());
			System.out.println(" :" + line);
			command(line);
			if(line.equalsIgnoreCase("exit")) {
				s.close();
				Thread.currentThread().stop();
			}
		}
	} catch(Exception e) {
		System.out.println(e.getMessage());
	}
}
See everything works, I just want to invoke method processCommand(String,Thread) each time a line comes in. I think I need to create an interface and implement on class #1, but I've never done that before. How?
rynox is offline   Reply With Quote
Old 11-18-2004, 05:43 PM   PM User | #2
rynox
New Coder

 
Join Date: Aug 2002
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
rynox is an unknown quantity at this point
Solved, guess I was barking up the wrong tree with the interfaces, I really needed to create an abstract class. Here's what I did.

I created an abstract class:
Code:
public abstract class rServerCommands {
	public abstract void command(String cmd,Thread thr);
}
Then, in my first class I extened the abstract class:
Code:
public class Library extends rServerCommands {...
 ...while(listening) new rServer(serverSocket.accept(),this).start();
Then, in the constructor of class 2:
Code:
public rServer(Socket s, rServerCommands obj) {...
...
obj.command(...
This way the main class, class 1, can pass itself as an argument to class 2. Since class 1 is a subclass of abstract rServerCommands, class 2 doesn't need to know what class it is, just that it is a rServerCommands class and can invoke the method command(String...etc).
rynox is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:32 AM.


Advertisement
Log in to turn off these ads.