Hi I have a class with two methods referring a session object. I want to modify it and make it work to handle two sessions running parallely. So based if a new session comes within a session I want to assign the new session to another variable. You know I don't want to modify the class severely. I just want to use a boolean variable that checks the status and assign the session to one of the two variables and the res of the logic will run smoothly. Here is the frameowrk of my class

Code java:

Publicclass MyClass
{private IoSession clientSession =null;private IoSession serverSession =null;private IoSession newServerSession =null;publicvoid passToServer( RtspMessage message ){//I include the following assignment statment and if statement to make is work for two sessions/* IoSession serverSession =null;
if (this.sessionWithinASession==true)
{
serverSession=this.newServerSession; //This is my question
}
else
{
serverSession=this.serverSession; //This is my question. I want the variable serverSession (the local one to point to the two global variables). So, tell me if I'm wrong and how I can implement.
...
if ( serverSession == null ) {
RtspRequest request = (RtspRequest) message;
try {
connectServerSide( request.getUrl() );
} catch ( IOException e ) {
log.error( e );
// closeAll();
} finally {
if ( serverSession == null )
return; //I know connectServerSide Creates a new serversession and assigned it to the external one but it terminates b/c this is not referring the global one
}
}
}
private void connectServerSide( URL url ) throws IOException
{
//This is to also another code that i included for the same reason that I mentioned above
IoSession serverSession =null;
if (this.sessionWithinASession)
serverSession=this.newServerSession;
else
serverSession=this.serverSession;
......
// Create TCP/IP connector.
SocketConnector connector = new SocketConnector();
// Start communication.
log.debug( "Trying to connect to '" + host + "' " + port + " URL is "+url);
try {
ConnectFuture future = connector.connect(
new InetSocketAddress( host, port ), new ServerSide(),
new RtspServerFilters() );
future.join();
serverSession = future.getSession(); //I suppose this will modify the local and also the global and was thinking that by the time it gets back to the previous method serverSession will not be null
} catch ( UnresolvedAddressException e ) {
return;
}
}

So as you see the connect server side creates a session and should assign to the proper global variable. But in PassToServer method after the control gets back serverSession (which I think is refering to either the global serversession and global newserversession is still null). I hope my question is clear. Basically my question is how can I point an object from a method using the same type of object and modify the pointer and get the effect on the global object.

Sorry I'm new to this forum and thought that the posting I made was not the proper forum and made the other posting. It is not that I want to be smart.

October 19th, 2010, 11:17 AM

aussiemcgr

Re: How to create a pointer type of effect for objects

Ok, well I say this a lot. There is no good reason to have local variables and global variables have the same name. That sort of thing just creates the kind of confusion you are having here. With that said, your attempt to set the global variable to be equal to the local variable is flipped. This:

Code java:

serverSession=this.serverSession;

should be:

Code java:

this.serverSession=serverSession;

The keyword this is used to reference the class you are in, not the method you are in. Based on basic programming, the variable you are setting is on the left of the equals sign and the value you are assigning said variable is on the right of the equals sign. Which means, your call to this.serverSession needs to be on the left, as you are attempting to assign that value.

My best advice to you is to not have local and global variables with the same name, this can cause some confusion in your programs and serves no benefit. What changing the local variable name would look like is: Instead of this:

Code java:

Publicclass MyClass
{private IoSession clientSession =null;private IoSession serverSession =null;private IoSession newServerSession =null;publicvoid passToServer( RtspMessage message ){//I include the following assignment statment and if statement to make is work for two sessions/* IoSession serverSession =null;
if (this.sessionWithinASession==true)
{
this.serverSession=newServerSession; //This is my question
}
else
{
this.serverSession=serverSession; //This is my question. I want the variable serverSession (the local one to point to the two global variables). So, tell me if I'm wrong and how I can implement.
...

You would have this:

Code java:

publicclass MyClass
{private IoSession clientSession =null;private IoSession serverSession =null;private IoSession newServerSession =null;publicvoid passToServer( RtspMessage message ){//I include the following assignment statment and if statement to make is work for two sessions
IoSession serverSession2 =null;if(sessionWithinASession==true){
serverSession=newServerSession;//This is my question}else{
serverSession=serverSession2;//This is my question. I want the variable serverSession (the local one to point to the two global variables). So, tell me if I'm wrong and how I can implement.
...

Going from that, I have more questions:
1) Where is the variable sessionWithinASession?
2) If it is a boolean, why bother compare to true? If you are checking if boolean sessionWithinASession is true, you only need to say: if (sessionWithinASession) as sessionWithinASession will return a true or false value, which is what an if statement looks for.
3) Inside the if statement I mentioned above, are you attempting to set the global variable serverSession or the local variable serverSession?
4) Why do you have 3 IoSessions? What is the purpose of having 3 instead of 2? 1 for the client and 1 for the server.

October 19th, 2010, 12:41 PM

Darryl.Burke

Re: How to create a pointer type of effect for objects

Quote:

Originally Posted by aussiemcgr

Ok, well I say this a lot. There is no good reason to have local variables and global variables have the same name.

Agree. With the exception of method or constructor parameters that correspond to the field whose value is to be set from them.

Quote:

your attempt to set the global variable to be equal to the local variable is flipped. This:

Code java:

serverSession=this.serverSession;

should be:

Code java:

this.serverSession=serverSession;

Actually, it isn't and it shouldn't. The OP is apparently conditionally setting the local variable equal to one of two instance fields.

And in Java, the nearest thing to a global variable is a static (and usually final) class field, or an interface field. An instance field is, well, an instance field, but may equally well be referred to just as a field.

db

October 20th, 2010, 03:46 AM

zelalem

Re: How to create a pointer type of effect for objects

Hi first of all sorry for the late reply. It is b/c of time difference. I thank you for your advice. And here are my responses to your questions:

Quote:

Originally Posted by aussiemcgr

Ok, well I say this a lot. There is no good reason to have local variables and global variables have the same name. That sort of thing just creates the kind of confusion you are having here. With that said, your attempt to set the global variable to be equal to the local variable is flipped. This:

Code java:

serverSession=this.serverSession;

should be:

Code java:

this.serverSession=serverSession;

You know I didn't want to change the implementation of the original code. That is the reason why I used the same variable name. Anyway, you are right, it creates confusion. The assignment should also be different. BTW, let me explain what I'm trying to do (as you have also asked me why I need three I/O sessions). The application I'm trying to modify is a streaming application (a proxy) and I want to change the media I get from the server based on a certain condition. The proxy just mediates b/n them. So, when the condition fulfils, I want to create another connection to the server while the client gets the previously established stream and once the second session is established (and teh setup is complete), i'll stop the first one and start accepting the second one and send on the new media on the same client session to the client. I want to use the same connection to the client and don't want to establish a new connection. This will create a good user experience and also you can apply the application to any kind of client. I hope I made it clear (my idea).

Quote:

Going from that, I have more questions:
1) Where is the variable sessionWithinASession?
2) If it is a boolean, why bother compare to true? If you are checking if boolean sessionWithinASession is true, you only need to say: if (sessionWithinASession) as sessionWithinASession will return a true or false value, which is what an if statement looks for.

yes it is a silly mistake. I think i mkodifed that later.

Quote:

3) Inside the if statement I mentioned above, are you attempting to set the global variable serverSession or the local variable serverSession?
4) Why do you have 3 IoSessions? What is the purpose of having 3 instead of 2? 1 for the client and 1 for the server.

I explained it above.

But you know my problem is I want to use this local variable in two different methods (PassToServer and ConnectServerSide) and want to affect either serverSession or newServerSession. So, this is my biggest proble. I'll try to modify the code as per you sugegstion and come back to you.

Thanks again.

October 20th, 2010, 03:53 AM

zelalem

Re: How to create a pointer type of effect for objects

Quote:

Originally Posted by Darryl.Burke

The OP is apparently conditionally setting the local variable equal to one of two instance fields.And in Java, the nearest thing to a global variable is a static (and usually final) class field, or an interface field. An instance field is, well, an instance field, but may equally well be referred to just as a field.
db

Thanks. This is a good information. By the way as I said earlier, I want to modify the local variable hoping to affect the global (as a side effect).