org.siddhi.sample.benchmark.server.MultiClientServer Maven / Gradle / Ivy
/**
* Licensed 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 org.siddhi.sample.benchmark.server;
import org.siddhi.core.SiddhiManager;
import org.siddhi.core.exception.InvalidQueryException;
import org.siddhi.core.exception.ProcessorInitializationException;
import org.siddhi.core.exception.SiddhiException;
import org.siddhi.sample.benchmark.Symbols;
import org.siddhi.sample.benchmark.query.types.QueryProvider;
import org.siddhi.sample.benchmark.query.types.SimpleStockQuoteVWAPQueryProvider;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
public class MultiClientServer { // listens to multiple clients to process events
private int port;
private int limit;
private boolean eventBased = true;
private static final int DEFAULT_PORT = 4444;
private static final int DEFAULT_LIMIT = 300;
private static boolean hasDefaultPort = true;
private static boolean hasDefaultLimit = true;
private SiddhiManager siddhiManager;
private QueryProvider currentQueryProvider = null;
public MultiClientServer() {
this.port = DEFAULT_PORT;
this.limit = DEFAULT_LIMIT;
this.eventBased = true;
siddhiManager = new SiddhiManager();
}
public MultiClientServer(int port,int limit,boolean eventBased) {
this.port = port;
this.limit = limit;
this.eventBased = eventBased;
siddhiManager = new SiddhiManager();
}
public static void main(String[] args) {
int port = 0;
int limit = 0;
boolean eventBased = true;
try {
for (int i = 0; i < args.length; i++) { // filter the parameters passed to the main method from the server.sh
if ("-port".equals(args[i])) {
i++;
port = Integer.valueOf(args[i]);
hasDefaultPort = false;
}
else if ("-limit".equals(args[i])) {
i++;
limit = Integer.valueOf(args[i]);
hasDefaultLimit = false;
} else if ("-eventbs".equals(args[i])) {
i++;
eventBased = Boolean.valueOf(args[i]);
RunningTimeStats.EVENT_BASED = eventBased;
}
}
if (hasDefaultPort && hasDefaultLimit) {
new MultiClientServer().startServer();
} else if(!hasDefaultPort && !hasDefaultLimit){
new MultiClientServer(port,limit,eventBased).startServer();
}
} catch(Exception e) {
System.out.println("Syntax Error of input format....");
}
}
private void startServer() {
System.out.println((new StringBuilder("Server accepting connections on port ")).append(port).append(".").toString());
ServerSocketChannel serverSocketChannel = null;
try {
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(port)); // binds the address from the given port to listen from the created channel
while (true) {
new ServerThread(serverSocketChannel.accept(), getCurrentQueryProvider(),limit,eventBased).start(); //As this is for benchmark, always runs the current query which het from query provider for any connected client.
}
} catch (Exception e) {
System.out.println("Error receiving data from client");
}
}
private QueryProvider getCurrentQueryProvider()
throws InvalidQueryException, ProcessorInitializationException, SiddhiException { // always returns one query provider to process events from any number of connected clients
if(currentQueryProvider == null) {
currentQueryProvider = new SimpleStockQuoteVWAPQueryProvider
(siddhiManager, "CSEStream", Symbols.SYMBOLS.length);
return currentQueryProvider;
} else {
return currentQueryProvider;
}
}
}