58 | | public static void main(String[] args) { |
59 | | StreamChannelFactory factory = new StreamChannelFactory(System.in, System.out); |
60 | | PollService service = new PollService(factory, 1); |
61 | | service.start(); |
62 | | |
63 | | Random random = new Random(); |
64 | | while (true) { |
65 | | SolutionMessage msg = service.getSolution(); |
66 | | // parse the message and retrieve the variables there |
67 | | try { |
68 | | service.sendQuality(msg, random.nextDouble()); |
69 | | } catch (IOException e) { |
70 | | break; |
| 61 | public class RandomStreamingPollEvaluator { |
| 62 | |
| 63 | public static void main(String[] args) { |
| 64 | StreamChannelFactory factory = new StreamChannelFactory(System.in, System.out); |
| 65 | PollService service = new PollService(factory, 1); |
| 66 | service.start(); |
| 67 | |
| 68 | Random random = new Random(); |
| 69 | while (true) { |
| 70 | SolutionMessage msg = service.getSolution(); |
| 71 | // parse the message and retrieve the variables there |
| 72 | try { |
| 73 | service.sendQuality(msg, random.nextDouble()); |
| 74 | } catch (IOException e) { |
| 75 | break; |
| 76 | } |
78 | | This code shows a concrete example of how to use the !PollService and the ease of integrating this into an external java application. |
| 84 | This code shows a concrete example of how to use the !PollService and the ease of integrating this into an external java application. Using a !PushService is slightly different as can be seen in the following example. As mentioned it depends on the application which of the two possibilities is more suited. |
| 85 | |
| 86 | {{{#!java |
| 87 | public class RandomSocketPushEvaluator { |
| 88 | private PushService service; |
| 89 | |
| 90 | public static void main(String[] args) { |
| 91 | RandomSocketPushEvaluator main = new RandomSocketPushEvaluator(); |
| 92 | main.run(); |
| 93 | System.out.println("Service is running, terminate by pressing <Return> or <Enter>."); |
| 94 | System.console().readLine(); |
| 95 | main.terminate(); |
| 96 | } |
| 97 | |
| 98 | private void run() { |
| 99 | ServerSocketChannelFactory factory = new ServerSocketChannelFactory(8843); |
| 100 | service = new PushService(factory, 1, new RandomEvaluator()); |
| 101 | service.start(); |
| 102 | } |
| 103 | |
| 104 | private void terminate() { |
| 105 | service.stop(); |
| 106 | } |
| 107 | |
| 108 | private class RandomEvaluator implements IEvaluationService { |
| 109 | Random random; |
| 110 | |
| 111 | public RandomEvaluator() { |
| 112 | random = new Random(); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public double evaluate(SolutionMessage msg) { |
| 117 | return random.nextDouble(); |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | } |
| 122 | }}} |