Code Examples
A repository of 155 code examples for BeepBeep
PushLocalSimple.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;
19 
20 import ca.uqac.lif.cep.Connector;
21 import ca.uqac.lif.cep.ProcessorException;
22 import ca.uqac.lif.cep.Pushable;
23 import ca.uqac.lif.cep.http.HttpDownstreamGateway;
24 import ca.uqac.lif.cep.http.HttpUpstreamGateway;
25 import ca.uqac.lif.cep.io.Print;
26 import ca.uqac.lif.jerrydog.RequestCallback.Method;
27 
28 /**
29  * In this example, Machine A and Machine B are actually the same host; they
30  * just listen to different TCP ports on the same computer. Read this program
31  * first before looking that the other files in this package.
32  * <p>
33  * <img
34  * src="./doc-files/network/httppush/PushLocalSimple.png"
35  * alt="Processor graph">
36  * @author Sylvain HallĂ©
37  *
38  */
39 public class PushLocalSimple
40 {
41  public static void main(String[] args) throws ProcessorException, InterruptedException
42  {
43  ///
44  HttpUpstreamGateway up_gateway =
45  new HttpUpstreamGateway("http://localhost:12144/push");
46 
47  /* We now move on to Machine B, which is responsible for receiving character
48  * strings and converting them back into objects. This is the mirror process of
49  * what was just done. So, the first step is to create an
50  * {@link HttpDownstreamGateway}. The gateway is instructed to listen to incoming
51  * requests on port 12144, to respond to requests made at the page "/push", and
52  * send through an HTTP <code>POST</code> request. */
53  HttpDownstreamGateway dn_gateway =
54  new HttpDownstreamGateway(12144, "/push", Method.POST);
55 
56  /* Just so that we can see something, we plug a {@link Print} processor at
57  * the end; it will print to the standard output whatever object it receives
58  * from upstream. */
59  Print print = new Print();
60  Connector.connect(dn_gateway, print);
61 
62  /* Since the gateways are actually mini-web servers, the servers need to
63  * be launched so that they can actually communicate. This is done by
64  * calling the {@link Processor#start() start()} method on both
65  * processors. Look out, as there can be only one server on a machine
66  * listening to a given TCP port; if you have another instance of this
67  * program already running, the call to start will throw an Exception. */
68  up_gateway.start();
69  dn_gateway.start();
70 
71  /* We are now ready to push events and see what happens. First, we get
72  * a handle on the {@link Pushable} of the very first processor of the
73  * chain, <code>serialize</code> (which resides on Machine A). */
74  Pushable p = up_gateway.getPushableInput();
75 
76  /* Let's push some dummy object. After the call to push, the standard
77  * output should display the contents of that object. */
78  p.push("foo");
79 
80  /* Sleep a little so you have time to look at the console... */
81  Thread.sleep(1000);
82 
83  /* Let's push again. You know the drill. */
84  p.push("bar");
85 
86  /* Once everything is done, we have to stop the servers to free the
87  * TCP ports on your machine. This is done by calling
88  * {@link Processor#stop() stop()} on both processors. */
89  up_gateway.stop();
90  dn_gateway.stop();
91 
92  /* That's all folks! */
93  ///
94  }
95 }
In this example, Machine A and Machine B are actually the same host; they just listen to different TC...