summaryrefslogtreecommitdiffstats
path: root/sandbox/axis2-1.4/demos/workpool-distributed/src/main/java/node/WorkpoolNode.java
blob: 86557548afc9280c19a730b2e5da7cccbb286d51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package node;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import javax.xml.namespace.QName;

import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.assembly.Service;
import org.apache.tuscany.sca.contribution.Contribution;
import org.apache.tuscany.sca.contribution.DeployedArtifact;
import org.apache.tuscany.sca.contribution.service.impl.ContributionServiceImpl;
import org.apache.tuscany.sca.domain.SCADomain;
import org.apache.tuscany.sca.node.NodeManagerInitService;
import org.apache.tuscany.sca.node.SCANode;
import org.apache.tuscany.sca.node.SCANodeFactory;
import org.apache.tuscany.sca.node.impl.SCANodeImpl;
import java.net.URI;

import workpool.WorkerManager;
import workpool.WorkerManagerImpl;
import workpool.WorkpoolManager;
import workpool.WorkpoolService;
import workpool.WorkpoolServiceImpl;

/**
 * This client program shows how to run a distributed SCA node. In this case a
 * calculator node has been constructed specifically for running the calculator
 * composite. Internally it creates a representation of a node and associates a
 * distributed domain with the node. This separation is made different
 * implementations of the distributed domain can be provided.
 */
public class WorkpoolNode {

    public static void main(String[] args) throws Exception {

        // Check that the correct arguments have been provided
        if (null == args || args.length < 4) {
            System.err
                    .println("Useage: java WorkpoolNode domainname nodename iterTest workerNo");
            System.exit(1);
        }
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String domainName = args[0];
        String nodeName = args[1];
        long iterations = Long.parseLong(args[2]);
        long jobsNo = Long.parseLong(args[3]);
        long workerNo = Long.parseLong(args[4]);
        ClassLoader cl = WorkpoolNode.class.getClassLoader();

        SCANodeFactory nodeFactory = SCANodeFactory.newInstance();
        SCANode node = nodeFactory.createSCANode(null, domainName);
        node.addContribution(nodeName, cl.getResource(nodeName + "/"));
        node.addToDomainLevelComposite(new QName("http://sample", "Workpool"));
        node.start();
        // nodeA is the head node and runs some tests while all other nodes
        // simply listen for incoming messages

        FileReader rules = new FileReader("workerRules.drl");
        StringBuffer buffer = new StringBuffer();

        BufferedReader br = new BufferedReader(rules);
        String ruleString;
        do {
            ruleString = br.readLine();
            if (ruleString != null) {
                buffer.append(ruleString + "\n");
            }
        } while (ruleString != null);

        if (nodeName.equals("nodeA")) {
            // do some application stuff
            WorkpoolService workpoolService = node.getDomain().getService(
                    WorkpoolService.class, "WorkpoolServiceComponent");
            workpoolService.start();
            NodeManagerInitService nodeInit = node.getDomain().getService(
                    NodeManagerInitService.class,
                    "WorkpoolManagerComponent/NodeManagerInitService");
            nodeInit.setNode(node);
            WorkpoolManager workpoolManager = node.getDomain().getService(
                    WorkpoolManager.class,
                    "WorkpoolManagerComponent/WorkpoolManager");
            workpoolManager.setWorkpoolReference(node.getDomain()
                    .getServiceReference(WorkpoolService.class,
                            "WorkpoolServiceComponent"));
            workpoolManager.setCycleTime(8000);
            workpoolManager.acceptRules(buffer.toString());
            workpoolManager.start();
            int items[] = { 3, 4, 5, 6, 3, 6, 3, 5, 9, 5, 6 };

            double x = 398349;

            for (int i = 0; i < jobsNo; ++i)
                workpoolService.submit(new TestJob(x, iterations, items));

            TestJob j = new TestJob(-1.0, true);
            for (int i = 0; i < workerNo + 1; ++i) {
                j.setEOS();
                workpoolService.submit(j);
            }

        }
        try {
            if (nodeName.equals("nodeB")) {
                NodeManagerInitService serviceNodeB = node
                        .getDomain()
                        .getService(NodeManagerInitService.class,
                                "WorkerManagerNodeBComponent/NodeManagerInitService");
                serviceNodeB.setNode(node);
            }
            if (nodeName.equals("nodeC")) {
                NodeManagerInitService workerManagerC = node
                        .getDomain()
                        .getService(NodeManagerInitService.class,
                                "WorkerManagerNodeCComponent/NodeManagerInitService");
                workerManagerC.setNode(node);
            }
            if (nodeName.equals("nodeD")) {
                NodeManagerInitService workerManagerD = node
                        .getDomain()
                        .getService(NodeManagerInitService.class,
                                "WorkerManagerNodeDComponent/NodeManagerInitService");
                workerManagerD.setNode(node);
            }
            if (nodeName.equals("nodeE")) {
                NodeManagerInitService workerManagerE = node
                        .getDomain()
                        .getService(NodeManagerInitService.class,
                                "WorkerManagerNodeEComponent/NodeManagerInitService");
                workerManagerE.setNode(node);
            }

            System.out.println("Node started (press enter to shutdown)");
            String buff;
            for (;;) {
                try {
                    buff = in.readLine();
                    if (buff == null)
                        break;
                    System.out.print(in.readLine());
                } catch (IOException ex) {
                    break; // Exit thread.
                }
            }
            // stop the node and all the domains in it
            node.stop();
            node.destroy();
            System.exit(0);
        } catch (Exception ex) {
            System.err.println("Exception in node - " + ex.getMessage());
            ex.printStackTrace(System.err);
        }
    }
}