Initial populations

This commit is contained in:
2021-11-16 11:19:21 +07:00
parent cdac14e7fd
commit bee21d279a
2168 changed files with 849329 additions and 0 deletions
@@ -0,0 +1,60 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p;
public class MessageBean {
public static String REPLYBACK="reply";
public static String TYPE_REQUEST="request";
public static String TYPE_RESPONSE="response";
public static String TYPE_INFO="info";
private String sender="";
private String type=TYPE_INFO;
private String command="";
private String reply="";
public String getSender() {
if(sender.equals("")){
sender=P2PService.getComputerName();
}
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
public String getReply() {
return reply;
}
public void setReply(String reply) {
this.reply = reply;
}
}
@@ -0,0 +1,151 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p;
import java.net.URISyntaxException;
import java.security.MessageDigest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.config.Config;
import net.jxta.document.AdvertisementFactory;
import net.jxta.id.IDFactory;
import net.jxta.peer.PeerID;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.pipe.PipeID;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.protocol.PipeAdvertisement;
public class P2PAdvertisement {
public final static String PIPEIDSTR = "urn:jxta:uuid-59616261646162614E50472050325033C0C1DE89719B456691A596B983BA0E1004";
private String taskpip="taskpip";
private Logger log = LogManager.getLogger(P2PAdvertisement.class.getName());
public P2PAdvertisement () {
if(Config.getValue("p2p.taskpip")!=null && !Config.getValue("p2p.taskpip").equals("") ){
taskpip= Config.getValue("p2p.taskpip");
}
}
public P2PAdvertisement (boolean flag_helper) {
//taskpip="taskpip_helper";
//if(Config.getValue("p2p.taskpip.helper")!=null && !Config.getValue("p2p.taskpip.helper").equals("") ){
// taskpip= Config.getValue("p2p.taskpip.helper");
//}
if(Config.getValue("p2p.taskpip")!=null && !Config.getValue("p2p.taskpip").equals("") ){
taskpip= Config.getValue("p2p.taskpip");
}
}
public PipeAdvertisement getPipeAdvertisement(String computername,PeerGroup pg) {
//if(Config.getValue("p2p.taskpip")!=null && !Config.getValue("p2p.taskpip").equals("") ){
// taskpip= Config.getValue("p2p.taskpip");
//}
log.debug("creating advertishment for computer:"+computername+" pg:"+pg.getPeerGroupName()+" peerrname:"+pg.getPeerName());
PipeID pipeID = null;
try {
pipeID =createPipeID(pg.getPeerGroupID(),computername,taskpip);
log.debug("Pipe Advert ID:"+pipeID);
} catch (URISyntaxException use) {
use.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
PipeAdvertisement advertisement = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());
advertisement.setPipeID(pipeID);
advertisement.setType(PipeService.PropagateType);
advertisement.setName(computername);
return advertisement;
}
public PipeAdvertisement getPipeAdvertisement(PipeID pipeID, PeerGroup pg) {
PipeAdvertisement advertisement = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());
advertisement.setPipeID(pipeID);
advertisement.setType(PipeService.PropagateType);
return advertisement;
}
public PeerAdvertisement getPeerAdvertisement(String computername,PeerGroup pg) throws Exception {
PeerID peerID = null;
// if(Config.getValue("p2p.taskpip")!=null && !Config.getValue("p2p.taskpip").equals("") ){
// taskpip= Config.getValue("p2p.taskpip");
// }
log.debug("creating advertishment for computer:"+computername+" pg:"+pg.getPeerGroupName()+" peerrname:"+pg.getPeerName());
try {
peerID =createPeerID(pg.getPeerGroupID(),computername+"peer",taskpip);
} catch (URISyntaxException use) {
use.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
PeerAdvertisement advertisement = (PeerAdvertisement) AdvertisementFactory.newAdvertisement(PeerAdvertisement.getAdvertisementType());
//PipeID pipeID =MD4ID.createPipeID(pg.getPeerGroupID(),computername,"taskpip");
advertisement.setPeerID(IDFactory.newPeerID(PeerGroupID.worldPeerGroupID));
//advertisement.setPeerID(peerID);
advertisement.setPeerGroupID(IDFactory.newPeerGroupID(PeerGroupID.defaultNetPeerGroupID));
advertisement.setName(computername);
return advertisement;
}
private final byte[] generateHash(String clearTextID, String function) throws Exception {
String id;
if (function == null) {
id = clearTextID;
} else {
id = clearTextID + "" + function;
}
byte[] buffer = id.getBytes();
MessageDigest algorithm = null;
algorithm = MessageDigest.getInstance("MD5");
// Generate the digest.
algorithm.reset();
algorithm.update(buffer);
byte[] digest1 = algorithm.digest();
return digest1;
}
private final PipeID createPipeID( PeerGroupID peerGroupID
, String clearTextID
, String function) throws Exception {
byte[] digest = generateHash(clearTextID, function);
return IDFactory.newPipeID(peerGroupID, digest );
}
private final PeerID createPeerID(PeerGroupID peerGroupID
, String clearTextID
, String function) throws Exception {
byte[] digest = generateHash(clearTextID, function);
return IDFactory.newPeerID(peerGroupID, digest );
}
}
@@ -0,0 +1,360 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p;
import java.io.File;
import java.net.InetAddress;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.config.Config;
import net.jxta.discovery.DiscoveryEvent;
import net.jxta.discovery.DiscoveryListener;
import net.jxta.discovery.DiscoveryService;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeService;
import net.jxta.platform.NetworkConfigurator;
import net.jxta.platform.NetworkManager;
import net.jxta.protocol.DiscoveryResponseMsg;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.protocol.PipeAdvertisement;
public class P2PService extends Thread implements DiscoveryListener {
public final static String MESSAGE_NAME_SPACE = "PipeTaskMsg";
private static transient NetworkManager manager=null;
protected static HashMap machineList=new HashMap();
//protected static Vector messages=new Vector();
public static Vector messages=new Vector();
//private static long discoveryWaitTime=1000L;
private static transient DiscoveryService discovery=null;
private static PipeService pipeService=null;
//private static InputPipe inputPipe = null;
//private static PipeAdvertisement pipeAdv;
private static PeerGroup netPeerGroup=null;
private static Date startedTime=null;
//private static PeerAdvertisement peerAdv=null;
private Logger log=LogManager.getLogger(P2PService.class.getName());
private static String defaultP2PGroup="DiscoveryServer";
private static String defaultP2PCache=""; // addition because there is a possibility that .cache folder cause an error when restarting .. bug when restarting (20140411)
public static void setCache(){
// Addition because there is a possibility that .cache folder cause an error when restarting .. bug when restarting (20140411)
if(Config.getValue("p2p.cachepath")!=null && !Config.getValue("p2p.cachepath").equals("") ){
defaultP2PCache=Config.getValue("p2p.cachepath")+".cache";
} else{
defaultP2PCache=".cache";
}
}
public static void removeCache(){
setCache();
String cachePath=defaultP2PCache;
File tempFile = new File(cachePath);
try {
if(tempFile.isDirectory()){
FileUtils.deleteDirectory(tempFile);
System.out.println("removing .Cache Directory: " + cachePath);
} else {
System.out.println(".Cache Directory Does Not Exits");
}
} catch (Exception e) {
// e.printStackTrace();
// System.out.println("P2PService.Removing .cache :Error::"+e.getMessage());
}
}
public static NetworkManager getManager(){
Logger log = LogManager.getLogger(P2PService.class.getName());
if(P2PService.manager==null){
try {
if(Config.getValue("p2p.groupname")!=null && !Config.getValue("p2p.groupname").equals("") ){
defaultP2PGroup= Config.getValue("p2p.groupname");
}
String manname=defaultP2PGroup+getComputerName();
setCache();
String cachePath=defaultP2PCache;
System.out.println("set .Cache Directory: " + cachePath);
P2PService.manager = new NetworkManager(NetworkManager.ConfigMode.ADHOC, manname, new File(new File(cachePath), manname).toURI());
if(Config.getValue(Config.P2P_NO_MULTICAST)!=null
&& Config.getValue(Config.P2P_NO_MULTICAST).equalsIgnoreCase("true")){
NetworkConfigurator configurator=P2PService.manager.getConfigurator();
try{
configurator.setUseMulticast(false);
configurator.setHttpEnabled(true);
configurator.setHttpIncoming(true);
//configurator.setTcpEnabled(true);
//configurator.setTcpIncoming(true);
log.info("Disabling IP Multicast");
}catch(Exception e){
log.error("Error while configuring network, e:"+e.getMessage());
}
}
//Logger log= Logger.getLogger(P2PService.class);
PeerGroup pg= P2PService.manager.startNetwork();
log.debug("pg: group_name:"+pg.getPeerGroupName());
log.debug("pg: peer_peer:"+pg.getPeerName());
log.debug("pg: peer name:"+pg.getPeerAdvertisement().getName());
//log.debug("pg: pipe advertisment:"+));
log.debug("pg: getPeerID unique value:"+pg.getPeerID().getUniqueValue());
} catch (Exception e) {
e.printStackTrace();
System.out.println("----------------------- P2PService.getManager() :Error::"+e.getMessage());
//System.exit(-1);
}
}
return P2PService.manager;
}
public static void stopNetwork(){
if(P2PService.manager!=null){
try {
P2PService.manager.stopNetwork();
P2PService.discovery=null;
P2PService.netPeerGroup=null;
P2PService.manager=null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static PeerGroup getPeerGroup(){
if(netPeerGroup==null){
netPeerGroup = getManager().getNetPeerGroup();
}
return netPeerGroup;
}
public static DiscoveryService getDiscoveryService() {
if(discovery==null){
discovery = getPeerGroup().getDiscoveryService();
}
return discovery;
}
public static PipeService getPipeService() {
if(pipeService==null){
pipeService = getPeerGroup().getPipeService();
}
return pipeService;
}
public static PipeService getNewPipeService() {
pipeService = getPeerGroup().getPipeService();
return pipeService;
}
/*
public static PeerAdvertisement getPeerAdvertisement() {
if(peerAdv==null){
P2PAdvertisement ad=new P2PAdvertisement();
try{
peerAdv = ad.getPeerAdvertisement(getComputerName(),getPeerGroup());
}catch(Exception e){
e.printStackTrace();
}
}
return peerAdv;
}
*/
/*
public void run() {
try{
netPeerGroup = getManager().getNetPeerGroup();
// get the discovery service
discovery = netPeerGroup.getDiscoveryService();
pipeService = netPeerGroup.getPipeService();
long waittime = 10 * 1000L;
String computername=getComputerName();
P2PAdvertisement ad=new P2PAdvertisement();
PeerAdvertisement mdadv = ad.getPeerAdvertisement(computername,netPeerGroup);
discovery.publish(mdadv,DiscoveryService.INFINITE_LIFETIME,DiscoveryService.INFINITE_LIFETIME);
discovery.remotePublish(mdadv,DiscoveryService.INFINITE_LIFETIME);
pipeAdv = ad.getPipeAdvertisement(computername,netPeerGroup);
inputPipe = pipeService.createInputPipe(pipeAdv, new IncomingMessage(null) );
log.debug("----------->publishing client node:"+computername);
//while (true) {
// try {
// // System.out.println("Sleeping for :" + waittime);
// Thread.sleep(waittime);
// log.debug("thread sleeping "+waittime);
// } catch (Exception e) {// ignored
// }
//}
}catch(Exception e){
log.error("p2p thread couldn't be started, task scheduler load balancing might not work in this computer");
}
}
*/
public static Date getPeerStartedTime(){
return startedTime;
}
//protected static void setPeerStarted(){
public static void setPeerStarted(){
startedTime=new Date();
}
/**
* @deprecated
* @param clientname
* @param message
* @throws Exception
*/
public static void sendMessage(String clientname, String message) throws Exception {
try{
manager.getNetPeerGroup();
PipeAdvertisement pipeAdv = new P2PAdvertisement().getPipeAdvertisement(clientname,netPeerGroup);
//OutgoingMessageCompose omc=new OutgoingMessageCompose(){
//};
//OutgoingMessage ogM=new OutgoingMessage(null,message);
//pipeService = P2PService.getPipeService();
//pipeService.createOutputPipe(pipeAdv,ogM);
}catch(Exception e){
e.printStackTrace();
}
}
private static String THIS_HOST_NAME=null;
public synchronized static String getComputerName() {
String computername="[unknown]";
try{
if(P2PService.THIS_HOST_NAME==null){
computername=InetAddress.getLocalHost().getHostName();
P2PService.THIS_HOST_NAME=computername;
}else{
computername=P2PService.THIS_HOST_NAME;
}
}catch(Exception e){
e.printStackTrace();
}
return computername;
}
public Map getClientList() {
DiscoveryService discovery = P2PService.getDiscoveryService();
new Thread("Adv poll") {
public void run() {
int sleepy=3000;
DiscoveryService discovery = P2PService.getDiscoveryService();
discovery.addDiscoveryListener(new P2PService());
P2PService.getDiscoveryService().getRemoteAdvertisements(null,DiscoveryService.PEER,null,null,1);
log.debug("Adv poll created");
try {
sleep(sleepy);
}
catch(InterruptedException e) {}
}
}.start();
try {
sleep(1000);
} catch(InterruptedException e) {}
return machineList;
}
public static Map getClientListFromMemory() {
return machineList;
}
public void discoveryEvent(DiscoveryEvent ev) {
if(Config.getValue("p2p.groupname")!=null && !Config.getValue("p2p.groupname").equals("") ){
defaultP2PGroup= Config.getValue("p2p.groupname");
}
DiscoveryResponseMsg res = ev.getResponse();
PeerAdvertisement adv;
Enumeration en = ev.getSearchResults();
log.debug("discoveryEvent() called");
if (en != null) {
while (en.hasMoreElements()) {
adv = (PeerAdvertisement) en.nextElement();
log.debug(" adv.getName(): "+adv.getName());
if(adv.getName().startsWith(defaultP2PGroup)){
//String s = adv.getName().replace("DiscoveryServer","");
//PipeAdvertisement av=new P2PAdvertisement().getPipeAdvertisement(s,P2PService.getManager().getNetPeerGroup());
//P2PService.machineList.put(av.getPipeID(),s);
}else{
PipeAdvertisement av=new P2PAdvertisement().getPipeAdvertisement(adv.getName(),P2PService.getManager().getNetPeerGroup());
P2PService.machineList.put(av.getPipeID(),adv.getName());
}
}
}
//System.out.println("Module specific id :"+res.getPeerAdvertisement());
}
}
@@ -0,0 +1,389 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.listener;
import java.io.StringReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;
import org.apache.commons.digester.Digester;
import org.apache.jcs.JCS;
import org.apache.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.p2p.MessageBean;
import com.fourelementscapital.scheduler.p2p.msg.MessageNames;
import com.fourelementscapital.scheduler.p2p.msg.ReceiveMessage;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.pipe.PipeMsgEvent;
import net.jxta.pipe.PipeMsgListener;
public class IncomingMessage implements PipeMsgListener {
private IncomingMessageCallBack imi=null;
public IncomingMessage(IncomingMessageCallBack imi ){
this.imi=imi;
}
private Logger log = LogManager.getLogger(IncomingMessage.class.getName());
public final static String MESSAGE_NAME_SPACE = "PipeTaskMsg";
//protected static Vector<MessageBean> messages=new Vector<MessageBean>();
public static Vector<MessageBean> messages=new Vector<MessageBean>();
//protected static TreeMap<String,String> cachedPeers=new TreeMap<String,String>();
private static JCS cachedPeers=null;
protected static Map<String,Integer> executingpeers=Collections.synchronizedMap(new TreeMap<String,Integer>());
protected static Map<String,Map> executingpeerstime=Collections.synchronizedMap(new TreeMap<String,Map>());
protected static TreeMap<String,String> cachedStatistics=new TreeMap<String,String>();
protected static TreeMap<String,String> cachedRPackages=new TreeMap<String,String>();
protected static TreeMap<String,String> cachedPeerQueueStat=new TreeMap<String,String>();
/*
private static ExecutorService incomingService= Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
public void pipeMsgEvent(PipeMsgEvent event) {
Future fu=incomingService.submit(
new Callable<String>() {
public String call(){
//return new Integer(new LoadBalancingHSQLQueue().startedIfNotStarted1(sc_id,tri_time,peer));
IncomingMessage im=new IncomingMessage(this.imi);
im.pipeMsgEvent2(this.event);
return "";
}
private IncomingMessageCallBack imi=null;
private PipeMsgEvent event=null;
public Callable<String> init(IncomingMessageCallBack imi,PipeMsgEvent event){
this.imi=imi;
this.event=event;
return this;
}
}.init(this.imi, event));
}
*/
public void pipeMsgEvent(PipeMsgEvent event) {
log.debug("message received...");
try{
if(this.imi!=null){this.imi.before(event);}
Message msg;
try {
msg = event.getMessage();
if (msg == null) {
log.error("message is null");
return;
}
} catch (Exception e) {
e.printStackTrace();
return;
}
// get all the message elements
Message.ElementIterator en = msg.getMessageElements();
if (!en.hasNext()) {
log.error("en has no elements");
return;
}
// get the message element in the name space PipeClient.MESSAGE_NAME_SPACE
MessageElement msgElement = msg.getMessageElement(null, MESSAGE_NAME_SPACE);
MessageBean mb=null;
String xmsg=msgElement.toString();
try{
mb= parseMessage(xmsg);
log.debug("message from :"+mb.getSender()+" msg:"+mb.getCommand());
}catch(Exception e){
//e.printStackTrace();
log.error("Error 1:"+e.getMessage());
}
if(this.imi!=null){this.imi.after(event);}
if(mb!=null){
HashMap attachments=new HashMap();
while(en.hasNext()){
MessageElement me=(MessageElement)en.next();
if(!me.getElementName().equals(MESSAGE_NAME_SPACE)){
//attachmentName=me.getElementName();
//attachment=me.toString();
attachments.put(me.getElementName(),me.toString());
}
}
//new P2PPipeLog().logIncoming(mb);
try{
if(attachments.get(MessageNames.MESSAGE_BEAN_NAME)!=null ){
new ReceiveMessage(attachments).process();
}else{
/*
IncomingMessageParser imp= new IncomingMessageParser(mb,msgElement);
imp.setAttachement(attachments);
imp.parse();
*/
}
}catch(Exception e){
log.error("Error while msg:"+msgElement.toString()+" Error:"+e.getMessage());
}
}
}catch(Exception e){
// log.error("Error Msg:"+e.getMessage());
log.error("Error while parsing or getting data:"+e.getMessage());
e.printStackTrace();
}
}
public static Vector<MessageBean> getMessages(){
return messages;
}
private MessageBean parseMessage(String msg) throws Exception {
Digester digester = new Digester();
digester.setValidating(false);
digester.addObjectCreate("message", MessageBean.class);
digester.addBeanPropertySetter("message/type");
digester.addBeanPropertySetter("message/command");
digester.addBeanPropertySetter("message/sender");
digester.addBeanPropertySetter("message/reply");
MessageBean ve= (MessageBean) digester.parse(new StringReader(msg));
return ve;
}
//0 to delete
//-1 delete all
//
public static int ACTION_PEER_ADD=1;
public static int ACTION_PEER_REMOVE=0;
public static int ACTION_PEER_REMOVEALL=-1;
/*
public synchronized static void peersUpdate(int action, String peer,String status){
if(action==ACTION_PEER_REMOVE && cachedPeers.containsKey(peer)) cachedPeers.remove(peer);
if(action==ACTION_PEER_REMOVEALL) cachedPeers.clear();
if(action==ACTION_PEER_ADD) cachedPeers.put(peer,status);
}
*/
public synchronized static void peersUpdate(String peer,String status){
try{
if(cachedPeers==null){
cachedPeers=JCS.getInstance("cachedPeers$$");
}
IElementAttributes att= cachedPeers.getDefaultElementAttributes();
att.setMaxLifeSeconds(4);
cachedPeers.put(peer, status,att);
}catch(Exception e){
}
//if(action==ACTION_PEER_REMOVE && cachedPeers.containsKey(peer)) cachedPeers.remove(peer);
//if(action==ACTION_PEER_REMOVEALL) cachedPeers.clear();
//if(action==ACTION_PEER_ADD) cachedPeers.put(peer,status);
}
public synchronized static void updateExecutingPeers(String peer,String status, int scheduler_id) {
if(status.equalsIgnoreCase("BUSY")&& scheduler_id>0){
executingpeers.put(peer, scheduler_id);
}
if(status.equalsIgnoreCase("NOBUSY")){
executingpeers.remove(peer);
}
}
/**
* @deprecated
* @param peer
* @param status
* @param scheduler_id
* @param tr_time
* this method should be removed eventually once all the HSQL supported peer
*/
public synchronized static void updateExecutingPeersTime(String peer,String status, int scheduler_id,long tr_time) {
if(status.equalsIgnoreCase("BUSY")&& scheduler_id>0){
Map tm;
if(executingpeerstime.get(peer)==null){
tm=Collections.synchronizedMap(new TreeMap());
}else{
tm=(Map)executingpeerstime.get(peer);
}
if(tm!=null){
tm.put(scheduler_id, tr_time);
executingpeerstime.put(peer, tm);
}
}
if(status.equalsIgnoreCase("NOBUSY")){
executingpeerstime.remove(peer);
}
}
/**
* @deprecated
* @param peer
* @param scheduler_id
* @param trigger_timg
* this method should be removed eventually once all the HSQL supported peer
*/
public synchronized static void updateFinishedPeersTime(String peer, int scheduler_id, long trigger_timg) {
if(scheduler_id>0){
Map tm;
if(executingpeerstime.get(peer)!=null){
tm=(Map)executingpeerstime.get(peer);
if(tm!=null && tm.get(scheduler_id)!=null && ((Long)tm.get(scheduler_id)).longValue()==trigger_timg || trigger_timg==0){
if(tm!=null){
tm.remove(scheduler_id);
}
}
if(tm!=null && tm.keySet().size()==0){
executingpeerstime.remove(peer);
}
}
}
}
public synchronized static void updatePeerStatistics(String peer,String status) {
cachedStatistics.put(peer, status);
}
public synchronized static Map getPeerStatistics() {
return cachedStatistics;
}
public synchronized static void updatePeerQueueStat(String peer,String status) {
cachedPeerQueueStat.put(peer, status);
}
public synchronized static Map getPeerQueueStat() {
return cachedPeerQueueStat;
}
public synchronized static void updatePeerRPackages(String peer,String pkgs) {
cachedRPackages.put(peer, pkgs);
}
public synchronized static Map getPeerRPackages() {
return cachedRPackages;
}
public static Map<String,Integer> getExecutingPeers(){
return executingpeers;
}
public static synchronized Map<String,Map> getExecutingPeersTime(){
return executingpeerstime;
}
public static HashMap getCachedPeers(){
HashMap rtn=null;
if(cachedPeers!=null){
rtn=cachedPeers.getMatching("^[A-Za-z0-9]+$");
}
return (rtn!=null)?rtn:new HashMap(); //all alpha numeric keys.
}
private static JCS cachedPeerResp4Task=null;
/**
* This method is to store the executing task in cache that expires
* after certain period, so that scheduler kicks out the task from the queue, after 5 minutes of non-response of the task of the peer.
* @param scheduler_id
* @param trigger_time
*/
public synchronized static void peerRespRecencyOnTask(int scheduler_id, long trigger_time){
try{
if(cachedPeerResp4Task==null){
cachedPeerResp4Task=JCS.getInstance("cachedPeerResp4Task$$$");
}
IElementAttributes att= cachedPeerResp4Task.getDefaultElementAttributes();
att.setMaxLifeSeconds(180); //3 minutes
String value=scheduler_id+"_"+trigger_time;
//if(cachedPeerResp4Task.get(value)!=null) cachedPeerResp4Task.remove(value);
cachedPeerResp4Task.put(value,"true",att);
}catch(Exception e){
Logger log = LogManager.getLogger(IncomingMessage.class.getName());
log.error("Error:"+e.getMessage());
}
}
/**
* this method to check wheather the executing task is responded by the peer
* to kick out the task from the queue, after 5 minutes of non-response of the task of the peer.
* @param scheduler_id
* @param trigger_time
* @return
*/
public synchronized static boolean isRespRecencyOnTask(int scheduler_id, long trigger_time){
String value=scheduler_id+"_"+trigger_time;
if(cachedPeerResp4Task!=null && cachedPeerResp4Task.get(value)!=null){
return true;
}
return false;
}
}
@@ -0,0 +1,17 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.listener;
import net.jxta.pipe.PipeMsgEvent;
public class IncomingMessageCallBack {
public void before(PipeMsgEvent event){}
public void after(PipeMsgEvent event){}
}
@@ -0,0 +1,156 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.listener;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.p2p.MessageBean;
import com.fourelementscapital.scheduler.p2p.msg.CommunicationTrace;
import net.jxta.document.Advertisement;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.StringMessageElement;
import net.jxta.pipe.OutputPipe;
import net.jxta.pipe.OutputPipeEvent;
import net.jxta.pipe.OutputPipeListener;
public class OutgoingMessage implements OutputPipeListener{
private static int maxDebugErrCounter=0;
private OutgoingMessageCallBack omc=null;
public final static String MESSAGE_NAME_SPACE = "PipeTaskMsg";
private MessageBean smsg;
private Logger log = LogManager.getLogger(OutgoingMessage.class.getName());
private String destination=null;
private boolean messageSent=false;
private Map<String,String> attachments=null;
//private String attachmentName=null;
public OutgoingMessage(OutgoingMessageCallBack omc, MessageBean msg, String destination){
this.omc=omc;
this.smsg=msg;
this.destination=destination;
if(this.omc!=null){
this.omc.setDestination(destination);
}
}
public void setAttachment(Map<String,String> attachments) {
this.attachments=attachments;
}
public void outputPipeEvent(OutputPipeEvent event) {
//log.debug("message sent...cmd:"+this.smsg.getCommand());
OutputPipe outputPipe = event.getOutputPipe();
if(this.omc!=null){this.omc.before(event);}
Message msg;
try {
Advertisement adv=outputPipe.getAdvertisement();
//log.debug("message sent...cmd:"+this.smsg.getCommand()+", adv:type:");
msg = new Message();
StringMessageElement sme = new StringMessageElement(MESSAGE_NAME_SPACE, getXML(this.smsg), null);
msg.addMessageElement(null, sme);
if(attachments!=null){
for(Iterator<String> it=attachments.keySet().iterator();it.hasNext();){
String ky=it.next();
String val=attachments.get(ky);
if(val!=null){
StringMessageElement sme1=new StringMessageElement(ky,val,null);
msg.addMessageElement(null, sme1);
}
}
//System.out.println("~~~~~~~~~~~~OutgoingMessage.attachment:"+msg.toString());
}
//boolean msgsent=false;
//tries 5 times, maximum waits upto 400 //nano seconds
int numofTries=(omc!=null)?omc.numberOfTries:OutgoingMessageCallBack.PRIORITY_ZERO;
Date start=new Date();
for(int i=0;(i<numofTries && !this.messageSent);i++){
boolean taskvalided=true;
if(this.omc!=null && !this.omc.validateBeforeSend()){
taskvalided=false;
}
if(this.omc==null || taskvalided){
this.messageSent=outputPipe.send(msg);
}else{
i=numofTries;
//exit the loop;
}
log.debug("Msg:sent to client:getMessageElements:"+msg.getMessageElements().toString()+" this.messageSent:"+this.messageSent);
if(!this.messageSent && taskvalided){
try{
//log.error("Output pipe of "+this.destination+" is not responding waiting "+40*i+" ms" +" ~~Msg:"+getXML(this.smsg));
Thread.sleep(50);
}catch(Exception e){}
}
}
long diff=new Date().getTime()-start.getTime();
if(!this.messageSent && OutgoingMessage.maxDebugErrCounter<1000){
//display only 1000 errors, this will be reset on restarting the server/peer to avoid too many messsages in error log.
log.error("Message couldn't be sent to peer "+this.destination+" Tried:"+numofTries+"times Took:"+diff+"ms Msg:"+getXML(this.smsg));
OutgoingMessage.maxDebugErrCounter++;
}
if(this.messageSent && attachments!=null){
new CommunicationTrace(this.destination).outgoing(this.attachments);
}
//log.debug("Msg:"+getXML(this.smsg));
if(this.smsg!=null){
//new P2PPipeLog().logOutgoing(this.smsg,this.destination);
}
} catch (IOException e) {
e.printStackTrace();
//System.exit(-1);
//ClientErrorMgmt.reportError(e, "To Peer:"+ this.destination+" command:"+this.smsg.getCommand());
}finally{
if(this.omc!=null){this.omc.after(event);}
if(this.omc!=null && !this.messageSent){this.omc.onFail(event,this.smsg,this.destination);}
}
//msg=null;
//outputPipe=null;
}
private String getXML(MessageBean msg){
String rtn="<?xml version=\"1.0\"?><message>";
rtn+="<sender>"+msg.getSender()+"</sender>";
rtn+="<reply>"+msg.getReply()+"</reply>";
rtn+="<type>"+msg.getType()+"</type>";
rtn+="<command>"+msg.getCommand()+"</command>";
rtn+="</message>";
return rtn;
}
}
@@ -0,0 +1,69 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.listener;
import java.util.Map;
import net.jxta.pipe.OutputPipeEvent;
import com.fourelementscapital.scheduler.p2p.MessageBean;
import com.fourelementscapital.scheduler.p2p.msg.PostMessage;
public abstract class OutgoingMessageCallBack {
public static int PRIORITY_ZERO=1;
public static int PRIORITY_LOW=5;
public static int PRIORITY_NORMAL=10;
public static int PRIORITY_HIGH=50;
public static int PRIORITY_VERY_HIGH=100;
public static int PRIORITY_VERY_VERY_HIGH=1000;
protected String tuid=null;
private String destination=null;
private String scheduler_id;
private Map data=null;
private PostMessage postmessage=null;
public OutgoingMessageCallBack(){}
public OutgoingMessageCallBack(PostMessage pm){
this.postmessage=pm;
}
public OutgoingMessageCallBack(String tuid, Map data){this.tuid=tuid;this.data=data;}
protected static int PRIORITY_LOOP_FREQ=40;
protected int numberOfTries=PRIORITY_ZERO;
public void before(OutputPipeEvent pipe){}
public void after(OutputPipeEvent pipe){}
public void onFail(OutputPipeEvent pipe, MessageBean mbean, String destination){}
public boolean validateBeforeSend(){return true;}
public void setPriority(int priority){
this.numberOfTries=priority;
}
public String getTuid(){
return this.tuid;
}
public String getDestination(){
return this.destination;
}
protected void setDestination(String destination){
this.destination=destination;
}
protected Map getData(){
return this.data;
}
protected PostMessage getPostMessage(){
return this.postmessage;
}
}
@@ -0,0 +1,127 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.listener;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.fourelementscapital.scheduler.config.Config;
public class P2PPipeLog {
private static String nextLineChar="\r\n";
private Vector loggable=new Vector();
public P2PPipeLog() {
//loggable.add("\\bEXECUTETASK\\W+(?:\\w+\\W+){1,5}?CONFIRM\\b"); //client receives from server
//loggable.add("\\bEXECUTETASK\\W+(?:\\w+\\W+){1,6}?CONFIRM\\b"); //client receives from server
//loggable.add("\\bEXECUTETASK\\W+(?:\\w+\\W+){1,5}?TENDER\\b"); //client receives from server
//loggable.add("\\bEXECUTETASK\\W+(?:\\w+\\W+){1,4}?FINISHED\\b"); //server receives from client
//loggable.add("\\bEXECUTETASK\\W+(?:\\w+\\W+){1,4}?BID\\b"); //server receives from client
//loggable.add("^EXECUTEFAILED"); //server receives from client
//loggable.add("^RESTART_PEER"); //server receives from client
//loggable.add("^STATISTICS"); //server receives from client
//
//loggable.add("\\bEXECUTETASK\\W+(?:\\w+\\W+){1,4}?TENDER\\b"); //server receives from client
//loggable.add("^R_PACKAGES");
//loggable.add("^R_PACKAGES_DATA");
//loggable.add("^EXECUTESCRIPT");
}
//public void logIncoming(MessageBean mb) {
//if(validate(mb.getCommand())){
// String msg="From:"+mb.getSender()+" Comm:"+mb.getCommand();
//reportLog(msg);
//}
//}
//public void logOutgoing(MessageBean mb, String destination) {
//if(validate(mb.getCommand())){
// String msg="To:"+destination+" Comm:"+mb.getCommand();
// reportLog(msg);
///}
//}
private boolean validate(String s){
boolean rtn=false;
for(Iterator<String> it=this.loggable.iterator();(it.hasNext() && !rtn);){
Pattern p = Pattern.compile(it.next(), Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(s);
rtn= m.find();
}
return rtn;
}
public static void sendMsg(String msg, String toPeer) {
reportLog("To--->"+toPeer+" :"+msg);
}
public static void receiveMsg(String msg, String fromPeer) {
reportLog("<---------From "+fromPeer+" :"+msg);
}
private static void reportLog( String msg){
msg=(msg==null)?"":msg;
String root=Config.getString("log_error_folder");
Date d = new Date();
String folder =root+"p2p";
if (!new File(folder).isDirectory()) {
new File(folder).mkdirs();
}
String fname=root+"p2p"+File.separator+new SimpleDateFormat("dd_MM_yyyy").format(d)+".log";
File file=new File(fname);
try {
boolean append = true;
FileWriter fw = new FileWriter(file,append);
SimpleDateFormat sd=new SimpleDateFormat("HH:mm:ss SSS");
String logmessage=sd.format(new Date())+" "+msg;
if (!file.exists()) {
file.createNewFile();
}
fw.append(logmessage+nextLineChar);
fw.flush();
fw.close();
//String consoleoutput=Config.getString("log_error_to_console");
//if(consoleoutput!=null && consoleoutput.equalsIgnoreCase("yes")){
//System.out.println(logmessage);
//}
//
}
catch (Exception ex1) {
ex1.printStackTrace();
}
}
}
@@ -0,0 +1,22 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.listener;
public class P2PTransportMessage {
public static String COMMAND_STATUS="STATUS";
public static String COMMAND_STATISTICS="STATISTICS";
public static String COMMAND_R_PACKAGES="R_PACKAGES";
public static String COMMAND_R_PACKAGES_DATA="R_PACKAGES_DATA";
public static String COMMAND_PEER_QUEUE="PEER_QUEUE";
}
@@ -0,0 +1,115 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.p2p.websocket.TomcatWSConsole;
public class CommunicationTrace {
private String peername=null;
public static ArrayList<String> PEERS=new ArrayList<String>();
public static ArrayList<String> IGNORE_CLASS=new ArrayList<String>();
private ArrayList ignoreprops=new ArrayList();
private static final int OUTGOING=1;
private static final int INCOMING=2;
private Logger log = LogManager.getLogger(CommunicationTrace.class.getName());
public CommunicationTrace(String p){
this.peername=p;
if(!IGNORE_CLASS.contains("PeerOnlineStatus")){
IGNORE_CLASS.add("PeerOnlineStatus");
}
ignoreprops.add("class");
ignoreprops.add("RendezVousPropagatejxta-NetGroup");
ignoreprops.add("JxtaWireHeader");
ignoreprops.add("msgCreator");
ignoreprops.add("msgRecipient");
ignoreprops.add(MessageNames.MESSAGE_BEAN_NAME);
ignoreprops.add(MessageNames.MESSAGE_TYPE_CALLBACK);
ignoreprops.add("next_trigger_time");
}
public void outgoing(Map attachment){
if(PEERS.contains(this.peername)){
produceOutPut(OUTGOING,attachment);
}
}
public void incoming(Map attachment){
if(PEERS.contains(this.peername)){
produceOutPut(INCOMING,attachment);
}
}
private void produceOutPut(int direction ,Map attachment){
String handlerclass=(String)attachment.get(MessageNames.MESSAGE_BEAN_NAME);
handlerclass=handlerclass.replaceAll("^(.*\\.)(\\S+)$", "$2");
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss S");
if(!IGNORE_CLASS.contains(handlerclass)){
String d="<span class='time'>"+sdf.format(new Date())+"</span>";
String line="";
String pcname=this.peername;
try{
pcname=pcname.toLowerCase().replaceAll("4ecap(pc|sv|lt|vm)sg(\\d+)" , "$1$2");
}catch(Exception e){log.error("error while parsing computer name:"+e.getMessage()); }
if(direction==OUTGOING) line+=d+" :<span class='hclass'>"+handlerclass+"</span>:<span class='p "+pcname+"'>"+this.peername+"</span><span class='out'>--&gt;</span>";
if(direction==INCOMING) line+=d+" :<span class='hclass'>"+handlerclass+"</span>:<span class='p "+pcname+"'>"+this.peername+"</span><span class='in'>&lt;--</span>";
for(Iterator<String> i=attachment.keySet().iterator();i.hasNext();){
String ky=i.next();
if(!ignoreprops.contains(ky) && attachment.get(ky)!=null){
//log.debug(""+this.peername+" class:"+handlerclass+" data:"+attachment);
if(ky.equals("scheduler_id")){
line+=ky+":<span class='sc_id'>"+attachment.get(ky)+"</span>, ";
}else{
line+=ky+":"+attachment.get(ky)+", ";
}
}
}
//out("\n");
out(line);
}
}
private void out(String s) {
//System.out.print(s);
//WSServer.consoleToAll(s);
TomcatWSConsole.consoleToAll(s);
}
}
@@ -0,0 +1,80 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg;
import java.lang.reflect.Constructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.exception.SchedulerException;
public abstract class ExceptionMessageHandler extends MessageHandler {
private Logger log = LogManager.getLogger(ExceptionMessageHandler.class.getName());
private String exceptionMessage=null;
private String exceptionClass=null;
private SchedulerException ex=null;
public void exception(SchedulerException se){
if(se!=null){
this.exceptionClass=se.getClass().getName();
this.exceptionMessage=se.getMessage();
}
}
public SchedulerException exception(){
//SchedulerException se=null;
if(this.ex==null && this.exceptionClass!=null){
try{
Class c=Class.forName(this.exceptionClass);
Constructor ct = c.getConstructor(String.class);
this.ex=(SchedulerException)ct.newInstance(this.exceptionMessage);
}catch(Exception e){
log.error("Exception class:"+this.exceptionClass+", error message:"+e.getMessage());
}
}
return this.ex;
}
public String getExceptionMessage() {
return exceptionMessage;
}
public void setExceptionMessage(String exceptionMessage) {
this.exceptionMessage = exceptionMessage;
}
public String getExceptionClass() {
return exceptionClass;
}
public void setExceptionClass(String exceptionClass) {
this.exceptionClass = exceptionClass;
}
}
@@ -0,0 +1,95 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.p2p.listener.OutgoingMessageCallBack;
public abstract class MessageHandler {
//private Properties
//public MessageHandlerAbstract
private String msgCreator;
private String msgRecipient;
private String responseJSON;
public String getMsgCreator() {
return msgCreator;
}
public void setMsgCreator(String msgCreator) {
this.msgCreator = msgCreator;
}
public String getMsgRecipient() {
return msgRecipient;
}
public void setMsgRecipient(String msgRecipient) {
this.msgRecipient = msgRecipient;
}
public String getResponseJSON() {
return responseJSON;
}
public void setResponseJSON(String responseJSON) {
this.responseJSON = responseJSON;
}
public void setPriority(int priority) {
this.priority = priority;
}
//private String messageFrom=null;
private int priority=OutgoingMessageCallBack.PRIORITY_NORMAL;
//private Logger log = LogManager.getLogger(MessageHandler.class.getName());
//public abstract MessageHandlerAbstract getThisMessageHandler();
public abstract Map executeAtDestination();
public int getPriority() {
return this.priority ;
}
public abstract void onSendingFailed();
}
@@ -0,0 +1,18 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg;
public class MessageNames {
public static String MESSAGE_BEAN_NAME="__msgBeanName__";
public static String MESSAGE_TYPE_CALLBACK="__msgCallBack__";
public static String RSCRIPT="RScript";
public static String RSCRIPT_ENGINE="RScriptEngine";
}
@@ -0,0 +1,20 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg;
import java.util.Map;
public interface PostCallBack {
public static String IGNORE_CALLBACK="IGNORE_CALLBACK";
public void callBack(Map data);
public void onCallBackSendingFailed();
}
@@ -0,0 +1,160 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.p2p.MessageBean;
import com.fourelementscapital.scheduler.p2p.P2PAdvertisement;
import com.fourelementscapital.scheduler.p2p.P2PService;
import com.fourelementscapital.scheduler.p2p.listener.OutgoingMessage;
import com.fourelementscapital.scheduler.p2p.listener.OutgoingMessageCallBack;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.OutputPipeEvent;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PipeAdvertisement;
public class PostMessage {
//private String sender;
//private String message;
protected MessageHandler mha;
protected String recipient=null;
protected Logger log = LogManager.getLogger(PostMessage.class.getName());
protected boolean callback=false;
protected OutgoingMessageCallBack omc=null;
public PostMessage(MessageHandler mha,String recipient){
this.mha=mha;
this.recipient=recipient;
this.omc=new OutgoingMessageCallBack(this){
@Override
public void onFail(OutputPipeEvent pipe, MessageBean mbean,String destination) {
if(getPostMessage().callback && getPostMessage().mha instanceof PostCallBack){
((PostCallBack)(getPostMessage().mha)).onCallBackSendingFailed();
}else{
getPostMessage().mha.onSendingFailed();
}
}
};
omc.setPriority(this.mha.getPriority());
}
public PostMessage(MessageHandler mha,String recipient,OutgoingMessageCallBack omc1){
this.mha=mha;
this.recipient=recipient;
this.omc=omc1;
}
public void setCallBack(){
this.callback=true;
}
protected boolean getHelperAdvertKey() {
return false;
}
public void send(){
try{
PeerGroup netPeerGroup = P2PService.getPeerGroup();
/*
PipeAdvertisement pipeAdv = null;
if(!getHelperAdvertKey()) {
pipeAdv= new P2PAdvertisement().getPipeAdvertisement(this.recipient,netPeerGroup);
}else{
log.debug("~~~~~~` Posting message: PeerHelper");
pipeAdv=new P2PAdvertisement(getHelperAdvertKey()).getPipeAdvertisement(this.recipient,netPeerGroup);
}
*/
PipeAdvertisement pipeAdv =new P2PAdvertisement().getPipeAdvertisement(this.recipient,netPeerGroup);
MessageBean response=new MessageBean();
response.setReply(MessageBean.REPLYBACK);
response.setType(MessageBean.TYPE_REQUEST);
//response.setCommand("NEWPROTOCOL:myTestId");
OutgoingMessage ogM=new OutgoingMessage(this.omc,response,this.recipient);
HashMap h=new HashMap();
this.mha.setMsgCreator(P2PService.getComputerName());
this.mha.setMsgRecipient(this.recipient);
Map data=BeanUtils.describe(this.mha);
if(this.callback){
data.put(MessageNames.MESSAGE_TYPE_CALLBACK, MessageNames.MESSAGE_TYPE_CALLBACK);
}
data.put(MessageNames.MESSAGE_BEAN_NAME,this.mha.getClass().getName());
//System.out.println("------->IncomingMessageParser:data:"+data);
HashMap debugd=new HashMap();
debugd.putAll(data);
debugd.remove("msgCreator");
debugd.remove("responseJSON");
debugd.remove("priority");
debugd.remove("class");
debugd.remove("__msgBeanName__");
debugd.remove("taskuid");
log.debug("~~~~~~ PostMessage:class:"+this.mha.getClass().getName()+" to:"+this.recipient+":data:"+debugd);
ogM.setAttachment(data);
//ogM.setAttachment(MessageNames.RSCRIPT,"asdf aghjasdfjasdfasd g;adfajsd gasl;sgjasd gjasdg lasdg jasdlgjasdg;l asdjglasd gjsdlagjsdgasdjg;sdagsdgasdg");
PipeService pipeService = P2PService.getPipeService();
try{
//System.out.println("------->IncomingMessageParser: Sending MSG TO:"+mbean.getSender()+":"+command.toString());
pipeService.createOutputPipe(pipeAdv,ogM);
}catch(Exception e){
e.printStackTrace();
PipeService pipeService1 = P2PService.getNewPipeService();
try{
pipeService1.createOutputPipe(pipeAdv,ogM);
}catch(Exception e1){
e1.printStackTrace();
}
pipeService1=null;
}
//cleaning up
netPeerGroup = null;
pipeAdv = null;
response=null;
omc=null;
ogM=null;
data=null;
pipeService = null;
}catch(Exception e){
log.error("Error:"+e.getMessage());
}
}
}
@@ -0,0 +1,127 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
public class ReceiveMessage {
private Map data;
private Logger log = LogManager.getLogger(ReceiveMessage.class.getName());
public ReceiveMessage(Map attachments){
this.data=attachments;
}
public void process(){
String classname=(String)this.data.get(MessageNames.MESSAGE_BEAN_NAME);
String loc="1";
if(classname!=null && !classname.equals("")){
try{
Class c=Class.forName(classname);
Constructor ct = c.getConstructor();
loc+=",2";
MessageHandler mha=(MessageHandler)ct.newInstance();
//this.data.remove("JxtaWireHeader"); //remove jxta msessage
//this.data.remove("RendezVousPropagatejxta-NetGroup");
loc+=",3";
BeanUtils.populate(mha, this.data);
//log.debug("msg recieved,mha.getResponseJSON():"+mha.getResponseJSON()+" mha getMsgCreator:"+mha.getMsgCreator()+ " receipient:"+mha.getMsgRecipient() );
Map rtn=null;
loc+=",4";
//log.debug("~~~~~~ executing at Destination: data:"+data.keySet());
if(mha!=null && mha.getMsgCreator()!=null){
new CommunicationTrace(mha.getMsgCreator()).incoming(this.data);
}
if(this.data!=null && this.data.get(MessageNames.MESSAGE_TYPE_CALLBACK)!=null){
//execute at source
//log.debug("~~~~~~ callback:"+this.data.get(MessageNames.MESSAGE_TYPE_CALLBACK));
loc+=",5";
if(mha instanceof PostCallBack){
HashMap h=new HashMap();
if(mha.getResponseJSON()!=null){
loc+=",6";
JSONObject json1=new JSONObject(mha.getResponseJSON());
loc+=",7";
//log.debug("~~~~~~ json1:"+json1);
for(Iterator it=json1.keys();it.hasNext();){
Object key=(Object)it.next();
//System.out.print("key:"+key+" value:"+json1.get((String)key));
h.put(key, json1.get(key+""));
}
}
//log.debug("callback data:"+h);
((PostCallBack)mha).callBack(h);
}
}else{
//destination
//log.debug("~~~~~~ executing at Destination: mha:"+data+mha);
loc+=",8";
rtn=mha.executeAtDestination();
loc+=",9";
//log.debug("~~~~~~ executed : rtn:"+rtn);
loc+=",10,rtn:"+rtn;
if(rtn!=null){
mha.setResponseJSON(new JSONObject(rtn).toString());
loc+=",10a";
}
loc+="10b";
}
//loc+=",data:"+this.data;
HashMap debugd=new HashMap();
debugd.putAll(this.data);
debugd.remove("RendezVousPropagatejxta-NetGroup");
debugd.remove("JxtaWireHeader");
//if(classname.contains("PeerOnlineStatus") || classname.contains("PeerOnlineStatus") ){
//}else{
log.debug("classname:"+classname+" data:"+debugd);
//}
if(mha instanceof PostCallBack && this.data.get(MessageNames.MESSAGE_TYPE_CALLBACK)==null && (rtn==null || (rtn!=null && rtn.get(PostCallBack.IGNORE_CALLBACK)==null))){
//PostClientTestMessage pctm=new PostClientTestMessage();
//pctm.setName("this name");
//pctm.setScript("this is my script");
//pctm.setTest("this is test");
loc+=",11";
PostMessage pm=new PostMessage(mha,mha.getMsgCreator());
pm.setCallBack();
loc+=",12";
pm.send();
//log.debug("callback message sent");
}
loc+=",mha:"+mha;
}catch(Exception e){
e.printStackTrace();
log.error("process() E:"+e.getMessage()+" classname:"+classname+" loc:"+loc);
}
}
}
}
@@ -0,0 +1,103 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.helper;
import java.util.HashMap;
import java.util.Map;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PipeAdvertisement;
import org.apache.commons.beanutils.BeanUtils;
import com.fourelementscapital.scheduler.p2p.MessageBean;
import com.fourelementscapital.scheduler.p2p.P2PAdvertisement;
import com.fourelementscapital.scheduler.p2p.P2PService;
import com.fourelementscapital.scheduler.p2p.listener.OutgoingMessage;
import com.fourelementscapital.scheduler.p2p.listener.OutgoingMessageCallBack;
import com.fourelementscapital.scheduler.p2p.msg.MessageHandler;
import com.fourelementscapital.scheduler.p2p.msg.MessageNames;
import com.fourelementscapital.scheduler.p2p.msg.PostMessage;
public class HelperPostMessage extends PostMessage {
public static final String helper_prefix="_helper";
public HelperPostMessage(MessageHandler mha, String recipient) {
super(mha, recipient);
}
public HelperPostMessage(MessageHandler mha,String recipient,OutgoingMessageCallBack omc1){
super(mha,recipient,omc1);
}
public void send(){
try{
PeerGroup netPeerGroup = P2PService.getPeerGroup();
//PipeAdvertisement pipeAdv =new P2PAdvertisement().getPipeAdvertisement(super.recipient+P2PServiceServlet.helper_prefix,netPeerGroup);
PipeAdvertisement pipeAdv =new P2PAdvertisement().getPipeAdvertisement(super.recipient+helper_prefix,netPeerGroup);
MessageBean response=new MessageBean();
response.setReply(MessageBean.REPLYBACK);
response.setType(MessageBean.TYPE_REQUEST);
//response.setCommand("NEWPROTOCOL:myTestId");
OutgoingMessage ogM=new OutgoingMessage(super.omc,response,super.recipient);
HashMap h=new HashMap();
super.mha.setMsgCreator(P2PService.getComputerName());
super.mha.setMsgRecipient(super.recipient);
Map data=BeanUtils.describe(super.mha);
if(super.callback){
data.put(MessageNames.MESSAGE_TYPE_CALLBACK, MessageNames.MESSAGE_TYPE_CALLBACK);
}
data.put(MessageNames.MESSAGE_BEAN_NAME,super.mha.getClass().getName());
//System.out.println("------->IncomingMessageParser:data:"+data);
log.debug("~~~~~~` Posting message:to:"+super.recipient+":data:"+data);
ogM.setAttachment(data);
PipeService pipeService = P2PService.getPipeService();
try{
//System.out.println("------->IncomingMessageParser: Sending MSG TO:"+mbean.getSender()+":"+command.toString());
pipeService.createOutputPipe(pipeAdv,ogM);
}catch(Exception e){
e.printStackTrace();
PipeService pipeService1 = P2PService.getNewPipeService();
try{
pipeService1.createOutputPipe(pipeAdv,ogM);
}catch(Exception e1){
e1.printStackTrace();
}
pipeService1=null;
}
//cleaning up
netPeerGroup = null;
pipeAdv = null;
response=null;
omc=null;
ogM=null;
data=null;
pipeService = null;
}catch(Exception e){
log.error("Error:"+e.getMessage());
}
}
}
@@ -0,0 +1,231 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.impl.helper;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.jcs.JCS;
import org.apache.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.fileutils.RandomString;
import com.fourelementscapital.scheduler.p2p.msg.MessageHandler;
import com.fourelementscapital.scheduler.p2p.msg.PostCallBack;
import com.fourelementscapital.scheduler.p2p.msg.helper.HelperPostMessage;
public class SendCommand2Helper extends MessageHandler implements PostCallBack,Serializable {
//implements PostCallBack {
private Logger log = LogManager.getLogger(SendCommand2Helper.class.getName());
//private static final String STOP_CLIENT_QUEUE="yes";
//private String stopqueue=null;
private String command=null;
private String response=null;
public static final boolean WAIT_QUEUE_TO_FINISH=false;
private boolean wait=false;
private String cachekey=null;
public String getCachekey() {
return cachekey;
}
public void setCachekey(String cachekey) {
this.cachekey = cachekey;
}
public String getResponse() {
return response;
}
public void setWait(boolean flag) {
this.wait=flag;
}
public void setResponse(String response) {
this.response = response;
}
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
private static String currentCommand=null;
public Map executeAtDestination() {
try{
System.out.println("RequestPeerRestart: executeAtDestination() command:"+this.command);
StringTokenizer st=new StringTokenizer(command,"\n");
Runtime runtime = Runtime.getRuntime();
this.response="";
while(st.hasMoreTokens()){
String cl=st.nextToken();
Process process = runtime.exec(cl);
BufferedReader br2 = new BufferedReader(new InputStreamReader(process.getInputStream()));
//this.response+="---"+cl+"--->";
String line;
while ((line = br2.readLine()) != null) {
this.response+="\n"+line;
}
this.response+="\n";
//this.wait(2000);
Thread.sleep(2000);
}
System.out.println("RequestPeerRestart: executeAtDestination() response:"+this.response);
}catch(Exception e) {
e.printStackTrace();
log.error("Error while executing on destination:"+ e.getMessage());
}
return null;
}
/**
* this will be executed on client.
* @param data
*/
public void callBack(Map data) {
try{
IElementAttributes att= gCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(30);
gCache().put(getCachekey(), getResponse(),att);
}catch(Exception e){
e.printStackTrace();
log.error("Error while callback e:"+e.getMessage());
}
//System.out.println("RequestPeerRestart: response:"+cachekey);
//System.out.println("RequestPeerRestart: response:"+response);
}
private static JCS cache=null;
private static JCS gCache() throws Exception {
if(cache==null){
cache=JCS.getInstance(SendCommand2Helper.class.getName());
}
return cache;
}
public static String sendCommand(String peername, String command) throws Exception {
String cachekey = RandomString.getString(10);
IElementAttributes att= gCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(30);
gCache().put(cachekey, "", att);
SendCommand2Helper rpr=new SendCommand2Helper();
rpr.setCachekey(cachekey);
//rpr.setCommand("cmd.exe /c sc stop 4EPeer \n cmd.exe /c sc start 4EPeer");
//rpr.setCommand("cmd.exe /c sc "+command+" 4EPeer");
rpr.setCommand(command);
new HelperPostMessage(rpr, peername).send();
while(gCache().get(cachekey)!=null && ((String)gCache().get(cachekey)).equals("") ) {
Thread.sleep(100);
}
String rtn=null;
if(gCache().get(cachekey)!=null && !((String)gCache().get(cachekey)).equals("")){
//System.out.println("get the response::------------------------------------>");
//System.out.println(gCache().get(cachekey));
rtn=(String)gCache().get(cachekey);
}
gCache().remove(cachekey);
//System.out.println("end..");
return rtn;
}
public static String sendCommand(String peername, String command, int waitSecs) throws Exception {
String cachekey = RandomString.getString(10);
IElementAttributes att= gCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(waitSecs);
gCache().put(cachekey, "", att);
SendCommand2Helper rpr=new SendCommand2Helper();
rpr.setCachekey(cachekey);
//rpr.setCommand("cmd.exe /c sc stop 4EPeer \n cmd.exe /c sc start 4EPeer");
//rpr.setCommand("cmd.exe /c sc "+command+" 4EPeer");
rpr.setCommand(command);
new HelperPostMessage(rpr, peername).send();
while(gCache().get(cachekey)!=null && ((String)gCache().get(cachekey)).equals("") ) {
Thread.sleep(100);
}
String rtn=null;
if(gCache().get(cachekey)!=null && !((String)gCache().get(cachekey)).equals("")){
//System.out.println("get the response::------------------------------------>");
//System.out.println(gCache().get(cachekey));
rtn=(String)gCache().get(cachekey);
}
gCache().remove(cachekey);
//System.out.println("end..");
return rtn;
}
@Override
public void onCallBackSendingFailed() {
// TODO Auto-generated method stub
}
@Override
public void onSendingFailed() {
// TODO Auto-generated method stub
}
}
@@ -0,0 +1,63 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.scheduler;
import org.apache.jcs.JCS;
import org.apache.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class PeerCacheLock {
private static synchronized void lockPeer(String peer, String taskuid) throws Exception{
try{
IElementAttributes att= getCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(1);
getCache().put("peer_locked_"+peer+taskuid, "locked",att);
}catch(Exception e){}
}
public static synchronized boolean lockPeerIfFree(String peer, String taskuid) {
Logger log = LogManager.getLogger(PeerCacheLock.class.getName());
boolean rtn=true;
try{
if(getCache()!=null && getCache().get("peer_locked_"+peer+taskuid)!=null ){
rtn=false;
}else{
lockPeer(peer,taskuid);
}
}catch(Exception e){}
log.debug("locking the peer "+peer+" taskuid:"+taskuid);
return rtn;
}
public static synchronized void releasePeer(String peer,String taskuid) {
try{
if(getCache()!=null){
getCache().remove(peer);
}
}catch(Exception e){}
}
private static JCS cache=null;
private static JCS getCache() throws Exception {
if(cache==null){
cache=JCS.getInstance("PeerCacheLock");
}
return cache;
}
}
@@ -0,0 +1,59 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.scheduler;
import com.fourelementscapital.scheduler.p2p.msg.ExceptionMessageHandler;
public abstract class TaskMessage extends ExceptionMessageHandler {
private String scheduler_id="";
private String trigger_time="";
private String next_trigger_time="";
private String taskuid="";
public String getScheduler_id() {
return scheduler_id;
}
public void setScheduler_id(String scheduelr_id) {
this.scheduler_id = scheduelr_id;
}
public String getTrigger_time() {
return trigger_time;
}
public void setTrigger_time(String trigger_time) {
this.trigger_time = trigger_time;
}
public String getNext_trigger_time() {
return next_trigger_time;
}
public void setNext_trigger_time(String next_trigger_time) {
this.next_trigger_time = next_trigger_time;
}
public String getTaskuid() {
return taskuid;
}
public void setTaskuid(String taskuid) {
this.taskuid = taskuid;
}
}
@@ -0,0 +1,83 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.scheduler.rserve;
import java.util.Map;
import java.util.Properties;
import org.apache.jcs.JCS;
import org.apache.jcs.engine.behavior.IElementAttributes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.p2p.msg.MessageHandler;
import com.fourelementscapital.scheduler.p2p.msg.PostCallBack;
import com.fourelementscapital.scheduler.p2p.peer.PeerSpecificConfigurations;
public class PeerPropertiesGet extends MessageHandler implements PostCallBack {
private Logger log = LogManager.getLogger(PeerPropertiesGet.class.getName());
public Map executeAtDestination() {
Properties p=null;
try{
p=PeerSpecificConfigurations.getProperties();
}catch(Exception e){
log.error("Error:"+e.getMessage());
}
return p;
}
public void callBack(Map data) {
try{
IElementAttributes att= getCache().getDefaultElementAttributes();
att.setMaxLifeSeconds(20);
getCache().put("peer_"+this.getMsgCreator(),data,att);
}catch(Exception e){
log.error("Error:"+e.getMessage());
}
}
public static Map getPeerCachedProp(String peername) throws Exception {
Map data=(Map)getCache().get("peer_"+peername);
return data;
}
private static JCS cache=null;
private static JCS getCache() throws Exception {
if(cache==null){
cache=JCS.getInstance(PeerPropertiesGet.class.getName());
}
return cache;
}
@Override
public void onCallBackSendingFailed() {
// TODO Auto-generated method stub
}
@Override
public void onSendingFailed() {
// TODO Auto-generated method stub
}
}
@@ -0,0 +1,81 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.msg.scheduler.rserve;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fourelementscapital.scheduler.p2p.msg.MessageHandler;
import com.fourelementscapital.scheduler.p2p.peer.PeerSpecificConfigurations;
public class PeerPropertiesSet extends MessageHandler {
private String peerConcurrentThread=null;
private String peerSessionExecutionMax=null;
private Logger log = LogManager.getLogger(PeerPropertiesSet.class.getName());
public Map executeAtDestination() {
// this log error is for production debugging purpose. remove when it's done. itask 8257.
log.error(">>>>> debug itask 8257 >>>>> PeerPropertiesSet.executeAtDestination() - start");
log.error(">>>>> debug itask 8257 >>>>> PeerPropertiesSet.getPeerConcurrentThread() : " + getPeerConcurrentThread());
log.error(">>>>> debug itask 8257 >>>>> PeerPropertiesSet.getPeerSessionExecutionMax() : " + getPeerSessionExecutionMax());
try{
Properties p=new Properties();
p.setProperty(PeerSpecificConfigurations.KEY_CONCURRENT_SESSION, getPeerConcurrentThread());
p.setProperty(PeerSpecificConfigurations.KEY_MAX_EXEC_SESSION, getPeerSessionExecutionMax());
PeerSpecificConfigurations.syncPartial(p);
}catch(Exception e){
log.error("Error:"+e.getMessage());
}
// this log error is for production debugging purpose. remove when it's done. itask 8257.
log.error(">>>>> debug itask 8257 >>>>> PeerPropertiesSet.executeAtDestination() - end");
return new HashMap();
///return null;
}
public String getPeerConcurrentThread() {
return peerConcurrentThread;
}
public void setPeerConcurrentThread(String peerConcurrentThread) {
this.peerConcurrentThread = peerConcurrentThread;
}
public String getPeerSessionExecutionMax() {
return peerSessionExecutionMax;
}
public void setPeerSessionExecutionMax(String peerSessionExecutionMax) {
this.peerSessionExecutionMax = peerSessionExecutionMax;
}
@Override
public void onSendingFailed() {
// TODO Auto-generated method stub
}
}
@@ -0,0 +1,113 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.peer;
import java.util.ArrayList;
import java.util.Date;
import java.util.StringTokenizer;
public class PeerMachine {
public PeerMachine(){}
public PeerMachine(String peer){this.peername=peer;}
private String executing=null;
private Date lastresponse=null;
private int num_tasks_running=0;
private String peername=null;
private String version="";
private String peerOS=""; // added by andy 2014-02-20
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getExecuting() {
return executing;
}
public void setExecuting(String executing) {
this.executing = executing;
}
public Date getLastresponse() {
return lastresponse;
}
public void setLastresponse(Date lastresponse) {
this.lastresponse = lastresponse;
}
public int getNum_tasks_running() {
return num_tasks_running;
}
public void setNum_tasks_running(int num_tasks_running) {
this.num_tasks_running = num_tasks_running;
}
public String getPeername() {
return peername;
}
public void setPeername(String peername) {
this.peername = peername;
}
public void setPeerOS(String peeros){ // added by andy 2014-02-20
this.peerOS = peeros;
}
public String getPeerOS(){ // added by andy 2014-02-20
return peerOS;
}
public String getBusyStatus(){
if(this.executing!=null && !this.executing.trim().equals("")){
return "BUSY";
}else{
return "NOBUSY";
}
}
public ArrayList getRunning(){
ArrayList rtn=new ArrayList();
if(this.executing!=null && !this.executing.trim().equals("")){
StringTokenizer st=new StringTokenizer(this.executing,",");
while(st.hasMoreTokens()){
String token=st.nextToken();
StringTokenizer st2=new StringTokenizer(token,"=");
if(st2.countTokens()>=2){
String scid=st2.nextToken();
String trigtime=st2.nextToken();
rtn.add(scid+"_"+trigtime);
}
}
}
return rtn;
}
public boolean equals(Object other){
boolean rtn=false;
if(other!=null && other instanceof PeerMachine){
PeerMachine other1=(PeerMachine)other;
if(other1.getPeername()!=null && this.peername!=null && other1.getPeername().equals(this.peername)){
rtn=true;
}
}
return rtn;
}
private static ArrayList<String> ver=new ArrayList();
public static String getLastVersion(){
ver.add("2.1"); //added on 13-june-2013
return ver.get(ver.size()-1);
}
}
@@ -0,0 +1,241 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.peer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLSyntaxErrorException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class PeerManagerHSQL {
private Logger log = LogManager.getLogger(PeerManagerHSQL.class.getName());
private static Connection connection=null;
private static final Semaphore dblock=new Semaphore(1,true);
private static long TIMEOUT_MS=2000;
protected void acquireLock(){
try{
Date start=new Date();
PeerManagerHSQL.dblock.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS);
Date end=new Date();
long diff=end.getTime()-start.getTime();
if(diff>0){
String caller=collectStack(Thread.getAllStackTraces().get(Thread.currentThread()));
//log.debug("!!!!!!!!!!!!!!!!!acquiring lock: thread:"+Thread.currentThread().getId()+" time took:"+diff+" ms");
log.debug(" Caller time:"+diff+" id:"+Thread.currentThread().getId() +"\n"+caller);
}
//LoadBalancingHSQLLayerDB.dblock.acquire();
}catch(Exception e){
log.error("Error:"+e.getMessage());
}
}
protected void releaseLock(){
try{
PeerManagerHSQL.dblock.release();
//log.debug("....releasing lock: thread:"+Thread.currentThread().getId());
}catch(Exception e){
log.error("Error:"+e.getMessage());
}
}
private static String nextLineChar="\r\n";
private static String collectStack(StackTraceElement[] stacks) throws Exception {
//StackTraceElement[] stacks=ex.getStackTrace();
String rtn="";
//if(msg!=null) rtn+=msg.trim()+nextLineChar;
//rtn+="ERROR MSG:"+ex.getMessage()+nextLineChar;
int startat=0;
for(int i=0;i<stacks.length;i++){
if(stacks[i].getClassName().startsWith("com.fe.")){
startat=i;
}
}
int numbefore=2;
startat=(startat>numbefore)?startat-numbefore:0;
int counter=1;
for(int i=startat;i<stacks.length;i++){
if((i>(startat+numbefore)) && !stacks[i].getClassName().startsWith("com.fe.")){
}else{
String space="";
for(int ab=0;ab<counter;ab++) space+=" "; counter++;
if(stacks[i].getClassName().startsWith("com.fe.")){
space+="->";
}
rtn+=space+""+stacks[i].getClassName()+"."+stacks[i].getMethodName()+"()"+nextLineChar;
}
}
return rtn;
}
private synchronized Connection getConnection(){
if(PeerManagerHSQL.connection==null){
//synchronized(PeerManagerHSQL.connection){
if(PeerManagerHSQL.connection==null){
//String driver="jdbc:hsqldb:mem:peers";
acquireLock();
try{
if(PeerManagerHSQL.connection==null){
log.debug("~~~~~~~~~getConnection(-------)~~~~~~~~~~~~~~");
Class.forName("org.hsqldb.jdbc.JDBCDriver" );
//String driver="jdbc:hsqldb:file:C:\\rnd\\hsqldb-2.2.9\\scheduler_peers\\peers";
String driver="jdbc:hsqldb:mem:peers";
this.connection = DriverManager.getConnection(driver, "SA", "SA");
String q=createSchemaQuery();
Statement st=this.connection.createStatement();
st.execute(q);
st.close();
}
}catch(SQLSyntaxErrorException sqle){
log.debug("Table already existing, e:"+sqle.getMessage());
}catch(Exception e){
e.printStackTrace();
}finally{
releaseLock();
}
}
//}
}
return PeerManagerHSQL.connection;
}
private String createSchemaQuery(){
String query="CREATE TABLE peers(";
query+=" id IDENTITY,";
query+=" peername VARCHAR(50),";
query+=" executing VARCHAR(250),";
query+=" lastresponse DATETIME,";
query+=" version VARCHAR(50),";
query+=" num_tasks_running INT ";
query+=")";
return query;
}
public List<PeerMachine> getOnlinePeers(int last_milli_sec){
//i guess no need to lock
//acquireLock();
ArrayList rtn=new ArrayList();
try{
String query="Select * from peers WHERE lastresponse>dateadd('ms',-"+last_milli_sec+",LOCALTIMESTAMP)";
PreparedStatement ps=getConnection().prepareStatement(query);
ResultSet rs=ps.executeQuery();
while(rs.next()){
PeerMachine pm=new PeerMachine();
pm.setExecuting(rs.getString("executing"));
pm.setPeername(rs.getString("peername"));
pm.setVersion(rs.getString("version"));
pm.setNum_tasks_running(rs.getInt("num_tasks_running"));
if(rs.getTimestamp("lastresponse")!=null){
Date d=new Date(rs.getTimestamp("lastresponse").getTime());
pm.setLastresponse(d);
}
rtn.add(pm);
}
rs.close();
ps.close();
}catch(Exception e){
log.error("getOnlinePeers:"+last_milli_sec);
e.printStackTrace();
}finally{
//releaseLock();
}
return rtn;
}
public void updatePeerResponse(String peername, Map executing,String version) {
acquireLock();
try{
String selq="Select * from peers where peername=?";
String strfy=null;
int num_running=0;
if(executing!=null){
num_running=executing.size();
for(Object sc_id: executing.keySet()){
strfy=(strfy==null)
?sc_id+"="+executing.get(sc_id)
:strfy+","+sc_id+"="+executing.get(sc_id);
}
}
PreparedStatement ps=this.getConnection().prepareStatement(selq);
ps.setString(1, peername);
ResultSet rs=ps.executeQuery();
String query="";
if(rs.next()){
query="UPDATE peers SET executing=?, lastresponse=?,num_tasks_running=?, version=? WHERE peername=?";
}else{
query="INSERT INTO peers(executing,lastresponse,num_tasks_running,version,peername) VALUES(?,?,?,?,?)";
}
rs.close();
ps.close();
PreparedStatement ps1=getConnection().prepareStatement(query);
ps1.setString(1,strfy);
//try catch block added because occasionally get the following error message.
//java.lang.IllegalArgumentException: YEAR
//at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2316)
try{
ps1.setTimestamp(2,new Timestamp(new Date().getTime()));
}catch(Exception e){;
log.error("error, e:"+e.getMessage());
ps1.setTimestamp(2,null);
}
ps1.setInt(3,num_running);
ps1.setString(4,version);
ps1.setString(5,peername);
ps1.executeUpdate();
ps1.close();
}catch(Exception e){
log.error("Executing:"+executing+" Error:"+e.getMessage());
//e.printStackTrace();
}finally{
releaseLock();
}
}
}
@@ -0,0 +1,72 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.peer;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import org.jfree.util.Log;
public class PeerSpecificConfigurations {
private static String propertyfile="peer_specific.properties";
private static Properties defa=new Properties();
public static final String KEY_CONCURRENT_SESSION="rserve.concurrent.sessions";
public static final String KEY_MAX_EXEC_SESSION="rserve.max.executions.in.session";
public static Properties getDefaultProperties(){
defa.put(KEY_CONCURRENT_SESSION,"15");
defa.put(KEY_MAX_EXEC_SESSION,"10000");
return defa;
}
public static Properties getProperties() throws Exception {
Properties p=new Properties();
try{
FileInputStream fin=new FileInputStream(PeerSpecificConfigurations.propertyfile);
p.load(fin);
}catch(Exception e){
Log.error("No property file found, or error:"+e.getMessage());
}
Properties d=getDefaultProperties();
for(Object key:d.keySet()){
if(!p.containsKey(key)){
p.put(key, d.getProperty((String)key));
}
}
return p;
}
public static void syncPartial(Properties newp) throws Exception {
Properties oldp=getProperties();
for(Object key:newp.keySet()){
oldp.put(key, newp.getProperty((String)key));
}
FileOutputStream fos=new FileOutputStream(PeerSpecificConfigurations.propertyfile);
oldp.store(fos, null);
}
}
@@ -0,0 +1,94 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.websocket;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public abstract class CommandAbstract {
private Logger log = LogManager.getLogger(CommandAbstract.class.getName());
public Options getOptionsWithHelp(){
Options opt=getOptions();
opt.addOption("h", "help", false, "Show help of this command");
return opt;
}
public String showHelp() {
HelpFormatter h = new HelpFormatter();
StringWriter sw=new StringWriter();
String firstcommand=this.getClass().getSimpleName();
firstcommand=CommandMain.classToCommand(firstcommand);
//firstcommand=firstcommand.replaceFirst("Command", "");
h.printHelp(new PrintWriter(sw),200,firstcommand, getHeader(), getOptionsWithHelp(),0,5,getFooter(),true);
return sw.toString();
}
public String getCommand() {
String firstcommand=this.getClass().getSimpleName();
firstcommand=CommandMain.classToCommand(firstcommand);
return firstcommand;
}
public String executeCommand (String command) throws Exception {
String msg="";
try{
CommandLineParser parser = new PosixParser();
CommandLine cmd = parser.parse(getOptionsWithHelp(), command.split(" "));
if(cmd.hasOption('h')){
msg=showHelp();
}else{
log.debug("executeValidCommand()");
msg=executeValidCommand(cmd, command);
}
}catch(ParseException pe){
msg=showHelp();
}catch(Exception e){
msg=e.getMessage();
}
return msg;
}
public abstract String executeValidCommand(CommandLine cmd, String command);
public abstract Options getOptions();
public abstract String getHeader();
public abstract String getFooter();
}
@@ -0,0 +1,120 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.websocket;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class CommandMain {
private Logger log = LogManager.getLogger(CommandMain.class.getName());
private String command;
public CommandMain(String cmd){
this.command=cmd;
}
public String validate() {
String cmds[]=this.command.split(" ");
String msg="";
if(cmds.length>0){
//char[] stringArray = cmds[0].toCharArray();
//stringArray[0] = Character.toUpperCase(stringArray[0]);
//String commandclass = this.getClass().getPackage().getName()+".cmd."+ new String(stringArray);
String clss=command2Class(cmds[0]);
String commandclass = this.getClass().getPackage().getName()+".cmd."+ clss;
//System.out.println("CommandMain: his.getClass().getPackage():"+commandclass);
//ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
ClassLoader myClassLoader =this.getClass().getClassLoader();
log.debug("commandclass:"+commandclass);
try {
Class myClass = myClassLoader.loadClass(commandclass);
CommandAbstract cmdobj=(CommandAbstract)myClass.newInstance();
msg=cmdobj.executeCommand(this.command);
} catch (ClassNotFoundException e) {
msg="Invalid command";
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return msg;
}else{
msg="Invalid command";
}
return msg;
}
public static String command2Class(String command) {
try {
Pattern p1 = Pattern.compile("(\\_)([a-z]+?)",Pattern.DOTALL);
final Matcher matcher = p1.matcher(command);
final StringBuffer sb = new StringBuffer();
String grp="";
while(matcher.find()){
if( matcher.groupCount()>1){
matcher.appendReplacement(sb, matcher.group(2).toUpperCase());
}
}
matcher.appendTail(sb);
String cls=sb.toString();
char[] stringArray = cls.toCharArray();
stringArray[0] = Character.toUpperCase(stringArray[0]);
return new String(stringArray);
}catch(Exception e){
return "";
}
}
public static String classToCommand(String classname) {
try {
char[] stringArray = classname.toCharArray();
stringArray[0] = Character.toLowerCase(stringArray[0]);
classname= new String(stringArray);
Pattern p1 = Pattern.compile("([A-Z]+?)",Pattern.DOTALL);
final Matcher matcher = p1.matcher(classname);
final StringBuffer sb = new StringBuffer();
String grp="";
while(matcher.find()){
if( matcher.groupCount()>0){
matcher.appendReplacement(sb, "_"+matcher.group(1).toLowerCase());
}
}
matcher.appendTail(sb);
return sb.toString();
}catch(Exception e){
return "";
}
}
}
@@ -0,0 +1,112 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.websocket;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
import org.apache.catalina.websocket.WsOutbound;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
public class TomcatWSConsole extends WebSocketServlet {
private Logger log = LogManager.getLogger(TomcatWSConsole.class.getName());
private final static Set<ChatMessageInbound> connections = new CopyOnWriteArraySet<ChatMessageInbound>();
@Override
protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) {
//System.out.println("TomcatWSServer.createWebSocketInbound()");
return new ChatMessageInbound();
}
private final class ChatMessageInbound extends MessageInbound {
protected void onOpen(WsOutbound outbound) {
connections.add(this);
// System.out.println("TomcatWSServer.onOpen()");
log.debug("connected...");
}
@Override
protected void onClose(int status) {
connections.remove(this);
log.debug("closed...");
// System.out.println("TomcatWSServer.onClose()");
}
@Override
protected void onBinaryMessage(ByteBuffer message) throws IOException {
throw new UnsupportedOperationException(
"Binary message not supported.");
}
@Override
protected void onTextMessage(CharBuffer message) throws IOException {
// Never trust the client
//String filteredMessage = String.format("%s: %s", nickname, HTMLFilter.filter(message.toString()));
//broadcast(message.toString());
log.debug("msg received:"+message);
//this.getWsOutbound().writeTextMessage(message);
CommandMain cm=new CommandMain(message.toString());
String val=cm.validate();
JSONObject json=new JSONObject();
try{
json.put("rs", val);
this.getWsOutbound().writeTextMessage(CharBuffer.wrap(json.toString()));
//conn.send(json.toString());
}catch(Exception e){
log.error("Error onMessage:"+e.getMessage());
}
}
}
public static void consoleToAll(String msg){
for (ChatMessageInbound connection : connections) {
try {
JSONObject obj=new JSONObject();
try {
obj.put("c", msg);
} catch (JSONException e) { }
CharBuffer buffer = CharBuffer.wrap(obj.toString());
connection.getWsOutbound().writeTextMessage(buffer);
} catch (IOException ignore) {
// Ignore
}
}
}
}
@@ -0,0 +1,99 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p.websocket;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
import org.apache.catalina.websocket.WsOutbound;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Example web socket servlet for chat.
*/
public class TomcatWSServer extends WebSocketServlet {
private Logger log = LogManager.getLogger(TomcatWSServer.class.getName());
private final static Set<ChatMessageInbound> connections = new CopyOnWriteArraySet<ChatMessageInbound>();
@Override
protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) {
//System.out.println("TomcatWSServer.createWebSocketInbound()");
return new ChatMessageInbound();
}
private final class ChatMessageInbound extends MessageInbound {
protected void onOpen(WsOutbound outbound) {
connections.add(this);
// System.out.println("TomcatWSServer.onOpen()");
log.debug("connected...");
}
@Override
protected void onClose(int status) {
connections.remove(this);
log.debug("closed...");
// System.out.println("TomcatWSServer.onClose()");
}
@Override
protected void onBinaryMessage(ByteBuffer message) throws IOException {
throw new UnsupportedOperationException(
"Binary message not supported.");
}
@Override
protected void onTextMessage(CharBuffer message) throws IOException {
// Never trust the client
//String filteredMessage = String.format("%s: %s", nickname, HTMLFilter.filter(message.toString()));
//broadcast(message.toString());
log.debug("msg received:"+message);
this.getWsOutbound().writeTextMessage(message);
}
}
public static void broadcast(String message) {
for (ChatMessageInbound connection : connections) {
//Logger log=Logger.getLogger(TomcatWSServer.class);
//log.debug("# connections:"+connections.size());
//log.debug("# Msg:"+message);
try {
CharBuffer buffer = CharBuffer.wrap(message);
connection.getWsOutbound().writeTextMessage(buffer);
} catch (IOException ignore) {
// Ignore
}
}
}
}
@@ -0,0 +1,46 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.scheduler.p2p;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for App
*/
public class AppTest extends TestCase {
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Empty Test
*/
public void testApp()
{
assertTrue( true );
}
}