Code Examples
A repository of 155 code examples for BeepBeep
PushMachineA.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 java.util.Scanner;
21 
22 import network.CompoundObject;
23 import ca.uqac.lif.cep.Connector;
24 import ca.uqac.lif.cep.ProcessorException;
25 import ca.uqac.lif.cep.functions.ApplyFunction;
26 import ca.uqac.lif.cep.http.HttpUpstreamGateway;
27 import ca.uqac.lif.cep.serialization.JsonSerializeString;
28 import ca.uqac.lif.cep.tmf.QueueSource;
29 
30 /**
31  * This is the same example as {@link PushLocalSerialize}, but with the
32  * "Machine A" and "Machine B" parts of the chain split into two independent
33  * programs, with some user interaction added. You are advised to first
34  * go through the {@link PushLocalSerialize} example.
35  * This file corresponds to Machine A.
36  * <p>
37  * You can actually run this example on two physical machines if you
38  * wish. Simply start {@link PushMachineA} and {@link PushMachineB} on
39  * two computers, and use the IP address of Machine B in the URL you give
40  * to Machine A.
41  *
42  * @author Sylvain HallĂ©
43  */
44 public class PushMachineA
45 {
46  public static void main(String[] args) throws ProcessorException, InterruptedException
47  {
48  // Ask a few infos about the remote end of the process
49  System.out.println("Hello, I am Machine A (upstream).");
50  System.out.print("Enter the URL to push on Machine B: ");
51  Scanner sc = new Scanner(System.in);
52  String url = sc.nextLine();
53 
54  // Fill a queue source of dummy compound objects
55  QueueSource source = new QueueSource();
56  source.addEvent(new CompoundObject(0, "foo", null));
57  source.addEvent(new CompoundObject(0, "foo", new CompoundObject(6, "z", null)));
58 
59  // Connect this queue to a serializer and an upstream gateway
60  ApplyFunction serialize = new ApplyFunction(new JsonSerializeString());
61  HttpUpstreamGateway up_gateway = new HttpUpstreamGateway(url);
62  Connector.connect(source, serialize);
63  Connector.connect(serialize, up_gateway);
64 
65  // Start the gateway
66  up_gateway.start();
67 
68  /* Push events out. Here, we simply cycle through the dummy
69  * events we put in the queue source a few times. A press on Enter
70  * triggers the source to push a new event downstream. */
71  for (int i = 0; i < 5; i++)
72  {
73  System.out.println("Press Enter to send a new object to Machine B. Type q to quit.");
74  String line = sc.nextLine();
75  if (line.startsWith("q") || line.startsWith("Q"))
76  break;
77  source.push();
78  }
79  sc.close();
80 
81  // Stop the gateway
82  up_gateway.stop();
83  }
84 
85 }
Send events from one processor to another over a network.
This is the same example as PushLocalSerialize, but with the "Machine A" and "Machine B" parts of the...
A dummy object used to show serialization.