Guest User

Untitled

a guest
Jan 17th, 2012
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. package jforex;
  2.  
  3. import com.dukascopy.api.*;
  4.  
  5. import akka.actor.*;
  6. import akka.actor.Actor.*;
  7. import akka.event.*;
  8.  
  9. import com.typesafe.config.ConfigFactory;
  10.  
  11. @RequiresFullAccess
  12. @Library("akka-actor-2.0-M2.jar:akka-remote-2.0-M2.jar:netty-3.2.6.Final.jar:protobuf-java-2.4.1.jar:scala-library-2.9.1.jar")
  13.  
  14. public class JfxPing implements IStrategy {
  15.  
  16.   public class Client implements Runnable {
  17.  
  18.     /** Client configuration */
  19.     public static final String akkaConf = "\n"+
  20.       "\n" +
  21.       "ping {\n" +
  22.         "\n" +
  23.         "akka {\n" +
  24.           "\n" +
  25.           "actor {\n" +
  26.             "\n" +
  27.             "provider = \"akka.remote.RemoteActorRefProvider\"\n" +
  28.             "\n" +
  29.           "}\n" +
  30.           "\n" +
  31.           "remote {\n" +
  32.             "\n" +
  33.             "transport = \"akka.remote.netty.NettyRemoteSupport\"\n" +
  34.             "\n" +
  35.             "server {\n" +
  36.               "\n" +
  37.               "hostname = \"127.0.0.1\"\n" +
  38.               "port = 3555\n" +
  39.               "\n" +
  40.             "}\n" +
  41.             "\n" +
  42.           "}\n" +
  43.           "\n" +
  44.           "cluster.nodename = \"pong\"\n" +
  45.           "\n" +
  46.         "}\n" +
  47.       "}\n";
  48.  
  49.     /** Server configuration */
  50.     public static final String serverPath = "akka://[email protected]:2552/user/pong";
  51.  
  52.     /** Client playing field */
  53.     public class PingField extends UntypedActor {
  54.  
  55.       LoggingAdapter log = Logging.getLogger(getContext().system(), this);
  56.  
  57.       /** Max. game rounds */
  58.       int rounds = -1;
  59.  
  60.       /** Mailbox */
  61.       @Override
  62.       public void onReceive(Object message) throws Exception {
  63.  
  64.         // String based messages
  65.         if (message instanceof String) {
  66.  
  67.           String text = (String) message;
  68.  
  69.           // Counter
  70.           if(text.equals("PONG")) {
  71.  
  72.             console.getOut().println(text);
  73.  
  74.             // Check game over
  75.             if(rounds != -1)
  76.               rounds--;
  77.             if(rounds == 0) {
  78.  
  79.               // Tell it
  80.               getSender().tell("GAME OVER");
  81.               getSender().tell("SHUTDOWN");
  82.  
  83.               // Shutdown all
  84.               getContext().stop(getSelf());
  85.               context.stop();
  86.  
  87.             }
  88.  
  89.             // A little break
  90.             Thread.sleep(1000);
  91.  
  92.             // Continue the game
  93.             getSender().tell("PING", getSelf());
  94.  
  95.           // Unsupported message
  96.           } else
  97.             console.getOut().println("Protocol not support: " + text);
  98.  
  99.         // Unsupported message
  100.         } else
  101.           console.getOut().println("Protocol not support: " + message);
  102.       }
  103.     }
  104.  
  105.     public void run() {
  106.  
  107.       // Read client configuration
  108.       system = ActorSystem.create("Client",
  109.                                   ConfigFactory.parseString(akkaConf).getConfig("ping"));
  110.  
  111.       // Init client
  112.       ping = system.actorOf(new Props(new UntypedActorFactory() {
  113.  
  114.         public UntypedActor create() {
  115.  
  116.           return new PingField();
  117.  
  118.         }
  119.  
  120.       } ), "ping");
  121.  
  122.       // Connect to server
  123.       pong = system.actorFor(serverPath);
  124.  
  125.       // Send ping
  126.       pong.tell("PING", ping);
  127.  
  128.     }
  129.  
  130.   }
  131.  
  132.   public void onStart(IContext context) throws JFException {
  133.  
  134.     // Init JForex.
  135.     this.engine = context.getEngine();
  136.     this.console = context.getConsole();
  137.     this.history = context.getHistory();
  138.     this.context = context;
  139.     this.indicators = context.getIndicators();
  140.     this.userInterface = context.getUserInterface();
  141.  
  142.     Client client= new Client();
  143.     new Thread(client).start();
  144.  
  145.   }
  146.  
  147.   public void onAccount(IAccount account) throws JFException {
  148.   }
  149.  
  150.   public void onMessage(IMessage message) throws JFException {
  151.   }
  152.  
  153.   public void onStop() throws JFException {
  154.   }
  155.  
  156.   public void onTick(Instrument instrument, ITick tick) throws JFException {
  157.  
  158.  
  159.   }
  160.    
  161.   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
  162.   }
  163.  
  164.  
  165.   /** JForex objects. */
  166.   private IEngine engine;
  167.   static private IConsole console;
  168.   private IHistory history;
  169.   private IContext context;
  170.   private IIndicators indicators;
  171.   private IUserInterface userInterface;
  172.  
  173.   /** Akka objects */
  174.  
  175.   private ActorSystem system;
  176.   private ActorRef ping;
  177.   private ActorRef pong;
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment