?
Solved

client - server problem

Posted on 2003-03-20
9
Medium Priority
?
156 Views
Last Modified: 2010-03-31
I am developing an online auction using java rmi, and want to simulate the auctioneers instrustions on the client side, so that when a user places a bid, a text label within an applet representing the auctionners instruction will then ask for the next amount, etc.

What I am having problems with is how to relay the "going once", "going twice" and "gone" instrustions in that these will have to appear a set period of time after the final bid has been placed, so i will need a while loop or something on the server which  contantly checks to see if the required amount of time has passed. My question is how do i do with while also allowing the server to recieve bids, and prefrom other operations?
0
Comment
Question by:danBosh
  • 5
  • 3
9 Comments
 
LVL 2

Expert Comment

by:glottis
ID: 8179180
multi threading.
on thread will be listing for peoples bid and the other thread will keep a timer on when two say going once, twice sold.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8179217
Use threading at the server-side.

In your main () function, use a ServerSocket object:

class MainServer
{
  public static void main ( String args[] )
    throws Exception
  {
    ServerSocket ss = new ServerSocket ( <port-no> ) ;
    Socket s = ss.accept () ; // accept a new connection from a client
    new ServerThread ( s ) ; // start a new thread to serve this client dedicatedly

  } // end of main ()

} // class definition over

class ServerThread extends Thread
{
  Socket s ;
  BufferedReader br ;
  PrintWriter pw ;

  ServerThread ( Socket s )
  {
    try
    {
      this.s = s ;  
      br = new BufferedReader ( new InputStreamReader ( s.getInputStream () ) ) ;
      pw = new PrintWriter ( s.getOutputStream (), true ) ;

    } // end of try block

    catch ( Exception e )
    {
      System.out.println ( "\n Exception: " + e ) ;
      return ;

    } // end of catch block

    start () ; // new thread

  } // end of constructor ()

  public void run ()
  {
    try
    {
      while ( true )
      {
        // put entire functionality of the server here
        // use pw.println () to send data,
        // br.readLine () to read

        if ( <some-exiting-condition> ) // like, the client wishes to log out
          return ; // kill this thread

      } // end while
     
    } // end of try block

    catch ( Exception e ) // client gets disconnected by, say, a power-failure
    {
      System.out.println ( "\n Exception: " + e ) ;
      return ; // kill this thread

    } // end of catch block

  } // end of run ()

} // class definition over


Hope that helps as a building block!

Mayank.

     
0
 

Author Comment

by:danBosh
ID: 8179763
i counldnt really do it like that because where you say "put entire functionality of the server here" would not be possible because my server has many methods used for the auction and rmi communication?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 8179833
Maybe you can define those methods inside the ServerThread class and call them from the run () method??

Mayank.
0
 

Author Comment

by:danBosh
ID: 8179930
my server class looks like this, i just need a method like which will be checked approx every 0.1secs:
 
if((lastBid-currenttime)>30)updateLabel("going once")
if((lastBid-currenttime)>40)updateLabel("going twice")
if((lastBid-currenttime)>50)updateLabel("gone")




import java.rmi.*;
import java.rmi.server.*;
import java.net.*;
import java.util.*;

public class Auctioneer extends UnicastRemoteObject implements AuctionInterface
{
    //private String minBid;
    private String currentBid;
    private String product;
    private String highestBidder;

    // To hold the registered clients
    private Vector clientList = null;
    double delays [];
    int price=0;

    public Auctioneer(String product)throws RemoteException//minBid removed
    {
       super();
       setProduct(product);
       clientList = new Vector();
       currentBid="0";
       highestBidder="fhfd";
    }

    public String getCurrentBid() throws RemoteException
    {
        return currentBid;
    }
   
    public String bosh()
    {
          return "bosh";
      }

    /*public String getMinBid() throws RemoteException
    {
        return minBid;
    }
    */
   
    public String getHighestBidder()throws RemoteException
    {
          //System.out.println("New Highest Bidder"+highestBidder);
          return highestBidder;
      }

    public String getProduct() throws RemoteException
    {
        return product;
    }
   
    public String getInstruction() throws RemoteException
    {
        return "Do I hear #"+nextPrice();
    }
   
    public int nextPrice()
    {
          if(price<100)
            {
                  return price+5;
            }
            else if(price<500)
            {
                  return price+25;
            }
            else
            {
                  return price+=50;
            }
      }
   
    public void incrementPrice(String clientName)throws RemoteException
    {
   
   
          Updateable thingToUpdate = null;
          if(clientName !=getHighestBidder())
          {
                price = Integer.parseInt(currentBid);//.trim() possible?
          
              if(price<100)
                  {
                        price+=5;
                  }
                  else if(price<500)
                  {
                        price+=25;
                  }
                  else
                  {
                        price+=50;
                  }
                  currentBid = Integer.toString(price);
                  highestBidder=clientName;
            
                  System.out.println("currentBid: "+currentBid);
                  System.out.println("Highest Bidder: "+highestBidder);
                  
                  for(Enumeration clients = clientList.elements();clients.hasMoreElements();)
              {
                    System.out.println("NOTIFYING");
                thingToUpdate = (Updateable) clients.nextElement();
                //thingToNotify.notify(new Integer(0));
                 thingToUpdate.update(new Integer(0));
              }
          }
          else
          {
                
        }         
      }
      
      
      public void delay() throws RemoteException
      {
            Updateable thingToUpdate = null;
            delays = new double[clientList.size()];
            double start=0;
            double total=0;
            int c=0;
            
            for(Enumeration clients = clientList.elements();clients.hasMoreElements();)
        {
              thingToUpdate = (Updateable) clients.nextElement();
              
              
              for(int i=0;i<3;i++)
              {
                    start = System.currentTimeMillis();
                    thingToUpdate.dummy();
                    total+= (System.currentTimeMillis()-start);
              }
              delays[c]=(total/3.0);
              total=0.0;
              c++;
        }
            
            
      }      

      
    public void setProduct (String p)
    {
        product = p;
    }

    public void registerForUpdate(Updateable n) throws RemoteException
    {
        clientList.addElement(n);
        delay();
        for(int i=0;i<delays.length;i++)
        {
              System.out.println("Client Delay in Milli Seconds "+delays[i]);
          }
    }

    public static void main(String [] args)
    {
          System.out.println("SERVER STARTED");
        if(args.length == 0)
        {
            System.out.println("USAGE: java <product name>");
            System.exit(0);
        }
   
       
        System.setSecurityManager(new RMISecurityManager());
       
        try
        {
            Auctioneer thisOne = new Auctioneer(args[0]);//args 1 removed
            Naming.rebind(args[0], thisOne);
           
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}


0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8179968
Something like this:

class TimeChecker extends Thread
{
  Date initialTime ;
  Date currentTime ;
  int timeOutLimit = 60000 ; // 60 seconds or 60 milliseconds

  TimeChecker () // oonstructor ()
  {
    initialTime = new Date () ;
    start () ;

  } // end of constructor ()

  public void run ()
  {
    currentTime = new Date () ;
   
    if ( currentTime.getTime () - initialTime.getTime () > timeOutLimit )
    // over 60 seconds

  } // end of run ()

} // class definition over

// put a TimeChecker object in the Auctioneer class - it will spawn a new thread and automatically keep checking the time

public class Auctioneer extends UnicastRemoteObject implements AuctionInterface
{
  public Auctioneer ( String product )
    throws RemoteException
  {
    super () ;
    new TimeChecker () ;
....
....

  } // end of constructor ()

} // class definition over

   
Hope that helps!

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8179972
Declare timeOutLimit as 'long' above.

Mayank.
0
 

Author Comment

by:danBosh
ID: 8244581
mayankeagle

i tried this and it just runs the tread once an then carries on as normal?
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 204 total points
ID: 8244614
Try something like this:

public void run ()
{
  try
  {
    while ( true )
    {
      currentTime = new Date () ;
   
      if ( currentTime.getTime () - initialTime.getTime () > timeOutLimit )
        // over time-out limit - do your thing & terminate the thread by a break/ return statement

      else
        Thread.sleep ( 1000 ) ; // sleep for 1 second and again try

    } // end while

  } // end of try block

  catch ( Exception e )
  {
    System.out.println ( "\n\t Exception: " + e ) ;

  } // end of exception

} // end of run ()


Mayank.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question