Code Examples
A repository of 155 code examples for BeepBeep
TwinPrimesB.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2017 Sylvain HallĂ©
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package network.httppush.twinprimes;
19 
20 import java.math.BigInteger;
21 
24 
25 import static ca.uqac.lif.cep.Connector.INPUT;
26 import static ca.uqac.lif.cep.Connector.LEFT;
27 import static ca.uqac.lif.cep.Connector.RIGHT;
28 import static ca.uqac.lif.cep.Connector.OUTPUT;
29 import ca.uqac.lif.cep.Connector;
30 import ca.uqac.lif.cep.ProcessorException;
31 import ca.uqac.lif.cep.UtilityMethods;
32 import ca.uqac.lif.cep.functions.ApplyFunction;
33 import ca.uqac.lif.cep.functions.FunctionTree;
34 import ca.uqac.lif.cep.functions.StreamVariable;
35 import ca.uqac.lif.cep.http.HttpDownstreamGateway;
36 import ca.uqac.lif.cep.io.Print;
37 import ca.uqac.lif.cep.tmf.Filter;
38 import ca.uqac.lif.cep.tmf.Fork;
39 import ca.uqac.lif.jerrydog.RequestCallback.Method;
40 
41 /**
42  * The code for Machine B in the twin prime example.
43  * @author Sylvain HallĂ©
44  */
45 public class TwinPrimesB
46 {
47  public static void main(String[] args) throws ProcessorException
48  {
49  /* First, we create an HttpDownstreamGateway to receive strings
50  * from Machine A. */
51  ///
52  HttpDownstreamGateway dn_gateway = new HttpDownstreamGateway(12312, "/bigprime", Method.POST);
53 
54  /* The next step is to convert the string received from the gateway
55  * back into a BigInteger. We then increment this number by 2. We do
56  * this with a special function. */
57  ApplyFunction big_int_plus_2 = new ApplyFunction(new FunctionTree(
58  new BigIntegerFunctions.IncrementBigInteger(new BigInteger("2")),
59  new FunctionTree(StringToBigInteger.instance, StreamVariable.X)));
60  Connector.connect(dn_gateway, big_int_plus_2);
61 
62  /* Fork the output */
63  Fork fork = new Fork(2);
64  Connector.connect(big_int_plus_2, fork);
65 
66  /* Primality check for n+2... */
67  ApplyFunction is_prime = new ApplyFunction(IsPrime.instance);
68  Connector.connect(fork, LEFT, is_prime, INPUT);
69 
70  /* Join both forks into a filter. What comes out is a stream of numbers
71  * of the form n+2, such that both n and n+2 are prime. */
72  Filter filter = new Filter();
73  Connector.connect(fork, RIGHT, filter, LEFT);
74  Connector.connect(is_prime, OUTPUT, filter, RIGHT);
75 
76  /* Print n+2 */
77  Print print = new Print();
78  Connector.connect(filter, print);
79 
80  /* All set! Start the gateway so it can listen to requests from
81  * Machine A. */
82  dn_gateway.start();
83  ///
84 
85  /* Loop indefinitely */
86  System.out.println("Machine B listening for requests. Every number displayed below");
87  System.out.println("is the second of a twin prime pair. Press Ctrl+C to end.");
88  while (true)
89  {
90  UtilityMethods.pause(10000);
91  }
92  }
93 }
Send events from one processor to another over a network.
The code for Machine B in the twin prime example.
Contains a few utility functions for manipulating Java&#39;s BigInteger objects.
Use HTTP gateways in push mode.
Compute twin primes by distributing the computation across two machines over a network.