Problem with designing threads & abstraction
Hi.
I'm dealing a small program that has a SocketServer.
I have a specific simple protocol that I'm implementing by myself.
For the server I'm openeing a thread for every new session = socket
(for a user connecting to the server).
The problem is with the protocol design:
I want to have an abstract class for "Message" - a protocol message between the server and
the client.
Tryed to create inside a "Session" class (which identifies each connection = independant socket)
an abstract class for Message, so each session can instantiate new messages for every request (=message).
The problem - something with the generics doesn't seem to work.
These are the relevant headers:
Session.java
-------------------
Code :
public class Session <M extends Message> extends Thread {
.
.
public abstract class Message {
public abstract String treatMessage(InetAddress ip, int port);
}
.
.
}
Introduce.java
-------------------
Code :
public class Introduce extends Message {
}
inside "Introduce" class I get error messages from eclipse:
"no enclosing instance of type Session<M> is available due to some intermediate constructor invocation."
"Session.Message is a raw type. References to generic type Session<M>. Message should be parametrized."
The design is in this way because all sessions share the same database, and this is the design I found
for this situation (inner class). If any new good idea for design I'd love to hear..
thanks..
Re: Problem with designing threads & abstraction
Java does not allow multiple inheritance. You can't extend more than one class.
Re: Problem with designing threads & abstraction
how am I extending more than one class ?
you mean the inner class which extends "Message" while the whole class extends "Session" ?
and do you have any other idea for that?
Re: Problem with designing threads & abstraction
public class Session <M extends Message> extends Thread {
Re: Problem with designing threads & abstraction
Quote:
Originally Posted by
gilme
Hi.
I'm dealing a small program that has a SocketServer.
I have a specific simple protocol that I'm implementing by myself.
For the server I'm openeing a thread for every new session = socket
(for a user connecting to the server).
The problem is with the protocol design:
I want to have an abstract class for "Message" - a protocol message between the server and
the client.
Tryed to create inside a "Session" class (which identifies each connection = independant socket)
an abstract class for Message, so each session can instantiate new messages for every request (=message).
The problem - something with the generics doesn't seem to work.
These are the relevant headers:
Session.java
-------------------
Code :
public class Session <M extends Message> extends Thread {
.
.
public abstract class Message {
public abstract String treatMessage(InetAddress ip, int port);
// Did you ever create InetAddress in Message, or is in any super classes it might have?
// did you define port in Message or superclass?
// is there a way to use polymorphism to make a reference variable of superclass type point to subclass type if needed?
// what are the <> for?
// why is Message being defined inside of Session if it extends it instead of, if anything, Session being defined within Message?
}
.
.
}
Introduce.java
-------------------
Code :
public class Introduce extends Message {
// were any parameters overridden from Message and not referenced with super?
// did you have ever define a method called treatMessage in Introduce, with same parameters and everything?
}
inside "Introduce" class I get error messages from eclipse:
"no enclosing instance of type Session<M> is available due to some intermediate constructor invocation."
"Session.Message is a raw type. References to generic type Session<M>. Message should be parametrized."
The design is in this way because all sessions share the same database, and this is the design I found
for this situation (inner class). If any new good idea for design I'd love to hear..
thanks..
What class is extending what?
Also, in theory, could you do this,
public class superClass
{
public class subClass extends superClass
{
public class subClassTwo extends subClass
{
}
}
}
}
Also, you need to define class InetMessage, unless it's predefined, don't know on that one.
If it's not predefined, then perhaps you could do this:
public class superClass extends InetMessage
{
Re: Problem with designing threads & abstraction
Wait, I''m not sure you can define a class as an inner class inside of the class it''s extending.
However, I think you can have two inner classes like this:
public class innnerClassOne{
}
public class innerClassTwo extends innerClassOne
{
}
Re: Problem with designing threads & abstraction
Your class declaration is fine.
Code :
public class Session<M extends Message> extends Thread
However you need to create a new file called Message.java and put your Message class in there instead which should solve your problem.
Code :
public abstract class Message {
public abstract String treatMessage(InetAddress ip, int port);
}
// Json