aspose file tools*
The moose likes Distributed Java and the fly likes Dynamic Switching between method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "Dynamic Switching between method" Watch "Dynamic Switching between method" New topic
Author

Dynamic Switching between method

singh gaurav
Ranch Hand

Joined: Mar 28, 2010
Posts: 42
It is Possible in java to switch between methods dyanamically, I have a method which create a client and this client holds a connection to a server, and server reset connection after some period of time automatically, to save this connection client, we use a hello () method periodically, to call hello method periodically we have a method inside method a infinite loop is running and calling hello () after some time periodically. By using this client we have to perform some operations declared in some other methods, for invoking other operation methods i have to stop method hello (), and perform some operations,Then again to save our client and connection to server we have to call hello (),

Class xyz{
method Create client ()
{
Return client
}
Method say hello()
{
For(infinite loop)
{
Client.hello()
}
}
Method m1(){client.some work}
Method m2(){client.somework}
.
.
.
Method mN(){client.some work}
}
Here I have to switch between method sayhello() and m1() m2() m3()……mN() dynamically and method create client() called just once
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
I believe the concept you are looking for is State Machine.

A commonly used architecture to select the next thing to do based on the current state of the user. Easily implemented in Java with a switch/case structure. I like to call mine "doTheNextThing"

Bill
singh gaurav
Ranch Hand

Joined: Mar 28, 2010
Posts: 42
@William Brogden ,Hi William thanks for your suggestion ,i am grateful, but how cand i design a code for a condition which not known , my program executed continuously ,and some time of execution i have to stop some method execution and start another one.
Please suggest me a way .
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Exactly what happens when you "stop some method execution" ?

singh gaurav
Ranch Hand

Joined: Mar 28, 2010
Posts: 42
OOh William you are so great , i am blessed with your experience.

But how to stop a method execution dynamically,
and how to resume.

Thanks a lot.

Gaurav

ctgaurav@gmail.com
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
To "stop a method execution dynamically"

Usually internal logic in the method detects a reason to stop - executes "return"

You may have to redesign in order to do this smoothly.

Bill



singh gaurav
Ranch Hand

Joined: Mar 28, 2010
Posts: 42
@Bill can i use multithreading to implement this??
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
The usual precautions which are standard practice in Java would of course be applicable if you want more than one Thread executing.

Bill
singh gaurav
Ranch Hand

Joined: Mar 28, 2010
Posts: 42
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.openrtk.idl.epprtk.epp_Command;
import org.openrtk.idl.epprtk.epp_Exception;
import org.openrtk.idl.epprtk.epp_Greeting;
import org.openrtk.idl.epprtk.epp_Response;
import org.openrtk.idl.epprtk.epp_Result;
import org.openrtk.idl.epprtk.epp_XMLException;
import org.openrtk.idl.epprtk.contact.epp_ContactInfoReq;
import org.openrtk.idl.epprtk.contact.epp_ContactInfoRsp;
import com.tucows.oxrs.epprtk.rtk.EPPClient;
import com.tucows.oxrs.epprtk.rtk.transport.EPPTransportException;
import com.tucows.oxrs.epprtk.rtk.xml.EPPContactInfo;
public class ClientTest {
static EPPClient my_client;
static int timeout = 1000*60;
static epp_Greeting greeting;
static String epp_host_name = "dfgfds";
static int epp_host_port = df;
static String epp_client_id = "dfg";
static String epp_password = "dgdg";
//Creating EPP Client
public static EPPClient getClient()
{
try
{
my_client = new EPPClient(epp_host_name, epp_host_port, epp_client_id, epp_password);
my_client.setEPPTimeout(timeout);
my_client.setLang("en-US");
greeting = my_client.connectAndGetGreeting();
//my_client.login(epp_client_id,epp_password);
if(greeting != null)
{
System.out.println("Greeting Recieved");
}
else
{
System.out.println("No Greeting Receieved");
}
}
catch ( Exception xcp )
{
System.err.println("Exception! ["+xcp.getClass().getName()+"] ["+xcp.getMessage()+"]");
xcp.printStackTrace();
}

return my_client;
}
public static EPPClient useClient(int x)
{
EPPClient client = getClient();
if(x==1)
{
int i;
System.out.println("Hold Client Alive");
for(i=5;i>0;i++)
{
try
{
client.hello();
greeting = client.connectAndGetGreeting();
Thread.sleep(1000*60); // 1 Min delay
}
catch (InterruptedException ie)
{
System.out.println(ie.getMessage());
} catch (epp_Exception e) {
e.printStackTrace();
} catch (epp_XMLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (EPPTransportException e) {
e.printStackTrace();
}
if(my_client != null)
{System.out.println("Client Alive");}
}
}
if(x==2)
System.out.println("Returning EPP Client");
return client;

}
public static void contactInfo(EPPClient xy)//2.3.4
{
try
{
xy.login(epp_client_id,epp_password);
epp_Command command_data ;
System.out.println(" EPP Contact INFO command");
String default_contact_id = "OTE-C5";
epp_ContactInfoReq contact_info_request = new epp_ContactInfoReq();
command_data = new epp_Command();
command_data.m_client_trid = getClientTrid(epp_client_id);
contact_info_request.m_cmd = command_data;
contact_info_request.m_id = default_contact_id;
EPPContactInfo contact_info = new EPPContactInfo();
contact_info.setRequestData(contact_info_request);
contact_info = (EPPContactInfo) xy.processAction(contact_info);
epp_ContactInfoRsp contact_info_response = contact_info.getResponseData();
epp_Response response = contact_info_response.m_rsp;
if(response != null)
{
epp_Result[] results = response.m_results;
// epp_AuthInfo contact_auth_info = contact_info_response.m_auth_info;
System.out.println("ContactInfo results: ["+results[0].m_code+"] ["+results[0].m_msg+"]");
System.out.println(" Contact ID: ["+contact_info_response.m_id+"]");
System.out.println(" Contact Name: ["+contact_info_response.m_addresses[0].m_name+"]");
System.out.println(" Contact Organization: ["+contact_info_response.m_addresses[0].m_org+"]");
System.out.println(" Contact Address Street1: ["+contact_info_response.m_addresses[0].m_address.m_street1+"]");
System.out.println(" Contact Address Street2: ["+contact_info_response.m_addresses[0].m_address.m_street2+"]");
System.out.println(" Contact Address City: ["+contact_info_response.m_addresses[0].m_address.m_city+"]");
System.out.println(" Contact Address State/Province: ["+contact_info_response.m_addresses[0].m_address.m_state_province+"]");
System.out.println(" Contact Address Postal Code: ["+contact_info_response.m_addresses[0].m_address.m_postal_code+"]");
System.out.println(" Contact Address Country: ["+contact_info_response.m_addresses[0].m_address.m_country_code+"]");
System.out.println(" Contact Voice: ["+contact_info_response.m_voice.m_value+"]");
System.out.println(" Contact Voice Extension: ["+contact_info_response.m_voice.m_extension+"]");//extension)
System.out.println(" Fax: ["+contact_info_response.m_fax.m_value+":"+contact_info_response.m_fax.m_extension+"]");
System.out.println(" Contact EMail: ["+contact_info_response.m_email+"]");
System.out.println(" Auth INFO: ["+contact_info_response.m_auth_info+"]");
System.out.println("End of the EPP Contact Info command");
System.out.println();
System.out.println("****************"); System.out.println();
}
else
{
System.out.println("No Response For ontact INFO command");
}
}
catch(epp_XMLException xcp)
{
System.err.println("epp_XMLException! ["+xcp.m_error_message+"]");
}
catch ( epp_Exception xcp )
{
System.err.println("epp_Exception!");
epp_Result[] results = xcp.m_details;
System.err.println("\tresults: "+results[0]+"]");
}
catch ( Exception xcp )
{
System.err.println("Contact Info failed! ["+xcp.getClass().getName()+"] ["+xcp.getMessage()+"]");
xcp.printStackTrace();
}

}
protected static String getClientTrid(String epp_client_id)
{
return "ABC:"+epp_client_id+":"+System.currentTimeMillis();
}
public static void main (String []args)
{
System.out.println("Enter your Choice '1'- Hold '2'- Execute Command ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int choice =0;
try {
choice =Integer.parseInt( br.readLine());
} catch (IOException ioe) {
System.out.println("IO error !");
System.exit(1);
}
useClient(choice);
EPPClient y = useClient(choice);
contactInfo(y);
}
}


in this code i have to switch between two methods at run time useClient(int x) and contactInfo(EPPClient xy) at run time.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
What in the world do you expect Ranchers to do with that monster listing?

It isn't even formatted as Code, making it hard to read even in the unlikely event that somebody wanted to.

Bill
singh gaurav
Ranch Hand

Joined: Mar 28, 2010
Posts: 42
singh gaurav wrote:import

in this code i have to switch between two methods at run time useClient(int x) and contactInfo(EPPClient xy) at run time.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
i have to switch between two methods at run time useClient(int x) and contactInfo(EPPClient xy) at run time.


I think you mean choose between. Right now in main you execute useClient twice then contactInfo - what is supposed to happen?

On the basis of what will you make that choice?

Incidentally practically all of the code you present has absolutely nothing to do with your problem - you need to develop the ability to boil down problems to the simplest possible statement if you want to get help.

Bill
singh gaurav
Ranch Hand

Joined: Mar 28, 2010
Posts: 42
@William :

Dear William consider a scenario , you have a connection (object- encapsulated a authenticated session) to a server. by using connection object you have to send some commands to server and taking response from server, to make this connection alive ,persists you have to call a method periodical in a infinite loop, in mean time if you want to execute a command by using connection ,you have to stop infinite loop method, and start executing other method using same connection object connection. and when commands are completed you have to recall infinite loop method to make connection alive continuously.

Here my problem is how switch between methods (infinite loop method and other command methods how to stop a infinite loop method by run time argument) i cant use recursion methods,can't call a method inside a method).

Hope you got my point .

Regards
Gaurav
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
you have to call a method periodical in a infinite loop,


Why? The only loop should be the one that waits for a new command.

Seems to me that each separate command can be interpreted and generate a job to be executed by some Thread. No infinite loop there.

I think you got off on the wrong path a long while ago and need to back up.

See the "Command" design pattern for inspiration.

Bill
singh gaurav
Ranch Hand

Joined: Mar 28, 2010
Posts: 42
@Bill,
Understand my need to call a method in loop, to persist connection .
singh gaurav
Ranch Hand

Joined: Mar 28, 2010
Posts: 42
@BIll See This CODE
>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
If you think I am going to try to make sense out of 263 lines of code in terms of what you think you need to be doing, surprise, I am not about to.

Like I said earlier - I think you got off on the wrong foot long ago and your assumptions of how you want things to work are not helping.

You need to back away from existing code and try to diagram the flow of control and data. Perhaps UML or some other modeling technique will help.

I suspect you are not following my advice to read about design patterns such as "Command"

Take the advice of a really old programmer - sometimes you need to throw away version 0.1 and start fresh.

Bill

singh gaurav
Ranch Hand

Joined: Mar 28, 2010
Posts: 42
yes Bill i got your point........ i will go for command design pattern
 
wood burning stoves
 
subject: Dynamic Switching between method