public DSDPSClient(Action<string, int> ServerClientJobSubmitCallback, Action<string, int> ServerClientJobCancelCallback
     , Action<string, List<JobStatus>> ServerClientJobStatusCallback)
 {
     GlobalRabbitMQConnection.Instance.GetConnection().ConnectionShutdown += DSDPSClient_ConnectionShutdown;
     jobListDictionary = new Dictionary<int, List<Job>>();
     //serverMessageSender = new MessageSender("DPSClientToServer");
     syncServerComm = new SyncMessageSender("DSDPS_SYNC", "CLIENT", "SERVER");
     asyncServerCommSender = null;
     this.ServerClientJobSubmitCallback = ServerClientJobSubmitCallback;
     this.ServerClientJobCancelCallback = ServerClientJobCancelCallback;
     this.ServerClientJobStatusCallback = ServerClientJobStatusCallback;
     
 }
        public DSDPSNode()
        {
            GlobalRabbitMQConnection.Instance.GetConnection().ConnectionShutdown += DSDPSNode_ConnectionShutdown;
            jobListDictionary = new Dictionary<int, List<Job>>();
            jobProcessorManager = new JobProcessorkManager();
            jobProcessorManager.OnJobCompleted += JobProcessorManager_OnJobCompleted;
            jobProcessorManager.OnJobProgress += JobProcessorManager_OnJobProgress;
            updateManager = new UpdateManager();

            syncServerComm = new SyncMessageSender("DSDPS_SYNC", "NODE", "SERVER");
            asyncServerCommSender = null;

        }
 public AsyncMessageSender CreateCommunicator(string commName, string mainName, string subName, string subKey, bool isMain, Action<string, object> receiveMessageProcessor)
 {
     AsyncMessageReceiver receiver = null;
     
     string mainToSubQueueName = string.Format("{0}_{1}_TO_{2}_{3}", commName, mainName, subName, subKey);
     string subToMainQueueName = string.Format("{0}_{1}_TO_{2}", commName, subName, mainName);
     
     if (isMain)
     {
         //main's receiver(DSDPS_CLIENT_SERVER) is shared on same system.
         if (subToMainReceiverDic.ContainsKey(subToMainQueueName))
         {
             receiver = subToMainReceiverDic[subToMainQueueName];
         }
         else
         {
             receiver = new AsyncMessageReceiver(commName, mainName, subName, subKey, isMain, receiveMessageProcessor);
             receiver.Init();
             subToMainReceiverDic.Add(subToMainQueueName, receiver);
         }
         //main's sender(DSDPS_SERVER_CLIENT_XXXX) is not shared.
         return new AsyncMessageSender(commName, mainName, subName, subKey, isMain, receiver);
     }
     else
     {
         //sub's receiver is not shared.
         receiver = new AsyncMessageReceiver(commName, mainName, subName, subKey, isMain, receiveMessageProcessor);
         receiver.Init();
         //sub's sender(DSDPS_CLIENT_SERVER) is shared, eg only one sender exists on system.
         if (subToMainSenderDic.ContainsKey(subToMainQueueName))
         {
             return subToMainSenderDic[subToMainQueueName];
         }
         else
         {
             AsyncMessageSender sender = new AsyncMessageSender(commName, mainName, subName, subKey, isMain, receiver);
             subToMainSenderDic.Add(subToMainQueueName, sender);
             return sender;
         }
     }
 }
        public bool Login(string userName, string jobClassPriority)
        {
            lock(lockObj)
            {
                this.userName = userName;
                this.jobClassPriority = jobClassPriority;
                try
                {
                    SyncNodeServerLoginRequestMessage msg = new SyncNodeServerLoginRequestMessage();

                    System.Net.IPAddress[] ipaddr = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());
                    for (int i = 0; i < ipaddr.Length; ++i)
                    {
                        msg.nodeIP = ipaddr[i].ToString();
                        if (System.Net.IPAddress.Parse(ipaddr[i].ToString()).AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            break;
                        }
                    }
                    msg.nodePCName = System.Environment.MachineName;
                    msg.nodeName = this.userName;
                    msg.priorityKeyData = this.jobClassPriority;// "EQT=FICC>TEST";
                    msg.totalWorkerCount = this.totalWorkerCount;// Environment.ProcessorCount;
                    msg.benchmarkRunningTimeMS = this.benchmarkRunningTimeMS;
                    msg.lastProcBinaryVersion = DPSNodeSetting.Instance.LastProcBinaryVersion;
                    msg.lastNodeBinaryVersion = DPSNodeSetting.Instance.NodeVersion;
                    if (verbose)
                        Console.WriteLine(DateTime.Now.ToString("[HH:mm:ss.fff]")+"[N->S][Login] called.");
                    string ret = syncServerComm.Call(msg.GetJson());
                    if (verbose)
                        Console.WriteLine(DateTime.Now.ToString("[HH:mm:ss.fff]")+"[N->S][Login] finished.");
                    if (ret == null)
                        return false;
                    SyncNodeServerLoginResponseMessage retMsg = JsonConvert.DeserializeObject<SyncNodeServerLoginResponseMessage>(ret);

                    if (retMsg.returnCode == 0)
                    {
                        loggedin = true;
                        nodeKey = retMsg.nodeKey;
                        serverKey = retMsg.serverKey;
                        
                        
                        //login succeed. Open async queue between server and current node
                        asyncServerCommSender = AsyncMessageCommunicator.Instance.CreateCommunicator("DSDPS_ASYNC", "SERVER", "NODE", nodeKey, false, AsyncMessageProcessor);
                        asyncServerCommSender.Init();

                        //hb will be sent to server in 3 sec from hbmanager creation. 
                        hbManager = new NodeHeartbeatManager(nodeKey, OnServerUnreachable);

                        return true;
                    }
                    else
                        return false;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public bool Login(){
            try
            {
                SyncClientServerLoginRequestMessage msg = new SyncClientServerLoginRequestMessage();

                System.Net.IPAddress[] ipaddr = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());
                for (int i = 0; i < ipaddr.Length; ++i)
                {
                    msg.clientIP = ipaddr[i].ToString();
                    if (System.Net.IPAddress.Parse(ipaddr[i].ToString()).AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        break;
                    }
                }
                msg.clientPCName = System.Environment.MachineName;
                msg.clientName = "User";
                string ret = syncServerComm.Call(msg.GetJson());

                SyncClientServerLoginResponseMessage retMsg = JsonConvert.DeserializeObject<SyncClientServerLoginResponseMessage>(ret);

                if (retMsg.returnCode == 0)
                {
                    loggedIn = true;
                    clientKey = retMsg.clientKey;
                    asyncServerCommSender = AsyncMessageCommunicator.Instance.CreateCommunicator("DSDPS_ASYNC", "SERVER", "CLIENT", clientKey, false, ProcessAsyncServerMessage);
                    asyncServerCommSender.Init();
                    //hb will be sent to server in 3 sec from hbmanager creation. 
                    hbManager = new ClientHeartbeatManager(clientKey, OnServerUnreachable);
                    NotifyReady();
                    return true;
                }
                else
                    return false;
            }
            catch(Exception e)
            {
                return false;
            }
        }
 public void AddClientHB(string key, AsyncMessageSender comm)
 {
     lock (lockObj)
     {
         hbClientCommSenderDic.Add(key, comm);
         
     }
 }
 public void AddNodeHB(string key, AsyncMessageSender comm)
 {
     lock (lockObj)
     {
         hbNodeCommSenderDic.Add(key, comm);
         
     }
 }
 public void SetAsyncMessageCommunicator(AsyncMessageSender asyncNodeComm)
 {
     this.asyncNodeCommSender = asyncNodeComm;
 }
 public void SetAsyncMessageCommunicator(AsyncMessageSender asyncClientComm)
 {
     this.asyncClientComm = asyncClientComm;
 }