Class QuoteServer

  • All Implemented Interfaces:
    MembershipListener, MessageListener, Receiver, StateListener

    public class QuoteServer
    extends ReceiverAdapter
    Example of a replicated quote server. The server maintains state which consists of a list of quotes and their corresponding values. When it is started, it tries to reach other quote servers to get its initial state. If it does not receive any response after 5 seconds, it assumes it is the first server and starts processing requests.

    Any updates are multicast across the cluster

    Author:
    Bela Ban
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected Log log  
    • Constructor Summary

      Constructors 
      Constructor Description
      QuoteServer()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Map<java.lang.String,​java.lang.Float> getAllStocks()  
      float getQuote​(java.lang.String stock_name)  
      void getState​(java.io.OutputStream ostream)
      Allows an application to write a state through a provided OutputStream.
      static void main​(java.lang.String[] args)  
      void printAllStocks()  
      void setQuote​(java.lang.String stock_name, java.lang.Float value)  
      void setState​(java.io.InputStream istream)
      Allows an application to read a state through a provided InputStream.
      void start()  
      void viewAccepted​(View new_view)
      Called when a change in membership has occurred.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • log

        protected Log log
    • Constructor Detail

      • QuoteServer

        public QuoteServer()
    • Method Detail

      • viewAccepted

        public void viewAccepted​(View new_view)
        Description copied from interface: MembershipListener
        Called when a change in membership has occurred. No long running actions, sending of messages or anything that could block should be done in this callback. If some long running action needs to be performed, it should be done in a separate thread.

        Note that on reception of the first view (a new member just joined), the channel will not yet be in the connected state. This only happens when JChannel.connect(String) returns.

        Specified by:
        viewAccepted in interface MembershipListener
        Overrides:
        viewAccepted in class ReceiverAdapter
      • start

        public void start()
      • getQuote

        public float getQuote​(java.lang.String stock_name)
                       throws java.lang.Exception
        Throws:
        java.lang.Exception
      • setQuote

        public void setQuote​(java.lang.String stock_name,
                             java.lang.Float value)
      • getAllStocks

        public java.util.Map<java.lang.String,​java.lang.Float> getAllStocks()
      • printAllStocks

        public void printAllStocks()
      • getState

        public void getState​(java.io.OutputStream ostream)
                      throws java.lang.Exception
        Description copied from interface: StateListener
        Allows an application to write a state through a provided OutputStream. After the state has been written the OutputStream doesn't need to be closed as stream closing is automatically done when a calling thread returns from this callback.
        Specified by:
        getState in interface StateListener
        Overrides:
        getState in class ReceiverAdapter
        Parameters:
        ostream - the OutputStream
        Throws:
        java.lang.Exception - if the streaming fails, any exceptions should be thrown so that the state requester can re-throw them and let the caller know what happened
        See Also:
        OutputStream.close()
      • setState

        public void setState​(java.io.InputStream istream)
                      throws java.lang.Exception
        Description copied from interface: StateListener
        Allows an application to read a state through a provided InputStream. After the state has been read the InputStream doesn't need to be closed as stream closing is automatically done when a calling thread returns from this callback.
        Specified by:
        setState in interface StateListener
        Overrides:
        setState in class ReceiverAdapter
        Parameters:
        istream - the InputStream
        Throws:
        java.lang.Exception - if the streaming fails, any exceptions should be thrown so that the state requester can catch them and thus know what happened
        See Also:
        InputStream.close()
      • main

        public static void main​(java.lang.String[] args)