1 /*
2  *  Avis event router.
3  *
4  *  Copyright (C) 2008 Matthew Phillips <avis@mattp.name>
5  *
6  *  This program is free software: you can redistribute it and/or
7  *  modify it under the terms of the GNU General Public License
8  *  version 3 as published by the Free Software Foundation.
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 GNU
13  *  General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.avis.federation;
19 
20 import org.avis.config.Options;
21 import org.avis.router.Router;
22 import org.avis.router.SimpleClient;
23 import org.avis.subscription.parser.ParseException;
24 
25 import static java.net.InetAddress.getLocalHost;
26 
27 import static org.avis.federation.JUTestFederation.PORT1;
28 import static org.avis.federation.JUTestFederation.PORT2;
29 import static org.avis.federation.TestUtils.waitForConnect;
30 import static org.avis.util.Collections.set;
31 
32 public class StandardFederatorSetup
33 {
34   public Router server1;
35   public Router server2;
36 
37   public SimpleClient client1;
38   public SimpleClient client2;
39 
40   public Connector connector;
41   public Acceptor acceptor;
42 
StandardFederatorSetup()43   public StandardFederatorSetup ()
44     throws Exception
45   {
46     this (new Options (FederationOptionSet.OPTION_SET));
47   }
48 
StandardFederatorSetup(FederationClasses classes1, FederationClasses classes2)49   public StandardFederatorSetup (FederationClasses classes1,
50                                  FederationClasses classes2)
51     throws Exception
52   {
53     this (classes1, classes2, new Options (FederationOptionSet.OPTION_SET));
54   }
55 
StandardFederatorSetup(Options options)56   public StandardFederatorSetup (Options options)
57     throws Exception
58   {
59     this (new FederationClasses (defaultClass ()),
60           new FederationClasses (defaultClass ()),
61           options);
62   }
63 
defaultClass()64   public static FederationClass defaultClass ()
65     throws ParseException
66   {
67     return new FederationClass ("require (federated)",
68                                 "require (federated)");
69   }
70 
StandardFederatorSetup(FederationClasses classes1, FederationClasses classes2, Options options)71   public StandardFederatorSetup (FederationClasses classes1,
72                                  FederationClasses classes2,
73                                  Options options)
74     throws Exception
75   {
76     server1 = new Router (JUTestFederation.PORT1);
77     server2 = new Router (JUTestFederation.PORT2);
78 
79     EwafURI ewafURI = new EwafURI ("ewaf://localhost:" + (PORT1 + 1));
80 
81     acceptor =
82       new Acceptor (server2, "server2", classes2, set (ewafURI), options);
83 
84     connector =
85       new Connector (server1, "server1", ewafURI,
86                      classes1.classFor (getLocalHost ()), options);
87 
88     waitForConnect (connector);
89 
90     client1 = new SimpleClient ("client1", "localhost", PORT1);
91     client2 = new SimpleClient ("client2", "localhost", PORT2);
92 
93     client1.connect ();
94     client2.connect ();
95 
96     client1.subscribe ("require (federated) && from == 'client2'");
97     client2.subscribe ("require (federated) && from == 'client1'");
98   }
99 
close()100   public void close ()
101     throws Exception
102   {
103     client1.close ();
104     client2.close ();
105 
106     connector.close ();
107     acceptor.close ();
108 
109     server1.close ();
110     server2.close ();
111   }
112 }