Ejemplo n.º 1
0
        public void OnResponse(Driver driver, DataRequestBatch request, StatusResponse response)
        {
            if (driver.Heartbeat.Value > Params.MAX_RETRIES)
            {
                CurrentState.Instance.SetHIEVar(false);
                request.Batch.ForEach((r0) => {
                    driver.CurrentlySending.Remove(r0.ID);
                    driver.RemoveRequestBehind(r0.ID);
                    if (CurrentState.Instance.IsLeader)
                    {
                        StateLog.Instance.Leader_AddActionCompleted(r0.ID, Params.ID);
                    }
                    else
                    {
                        StateLog.Instance.Follower_MarkActionCompleted(r0.ID);
                    }
                    StateLog.Instance.RemoveCurrentTask(r0.ID);

                    Logger.Write(Logger.Tag.WARN, "Failed to execute data-request [entry:" + r0.ID.Substring(0, 10) + "]");

                    if (Params.TEST_RECEIVER_HOST != string.Empty)
                    {
                        try
                        {
                            TestReceiverClient.Instance.AddEntryError("Failed to deploy driver.");
                        } catch (Exception e)
                        {
                            Logger.Write(Logger.Tag.ERROR, "ADD_ENTRY: " + e.ToString());
                        }
                    }
                });
            }
            else if (response != null && response.Status != null)
            {
                request.Batch.ForEach((r0) => {
                    driver.CurrentlySending.Remove(r0.ID);

                    if (response.Status.ContainsKey(r0.ID) && response.Status[r0.ID])
                    {
                        driver.RemoveRequestBehind(r0.ID);
                        driver.Heartbeat.Reset();
                        CurrentState.Instance.SetHIEVar(true);
                    }
                    else
                    {
                        driver.AddRequestBehind(r0);
                    }
                });
            }
            else
            {
                request.Batch.ForEach((r0) => {
                    driver.CurrentlySending.Remove(r0.ID);
                    driver.AddRequestBehind(r0);
                });
            }
            // driver.SetIsSending(false);
        }
Ejemplo n.º 2
0
        private void HandleFailure(Driver driver, DataRequestBatch request)
        {
            // Activate the responsehandler
            Dictionary <string, bool> r0 = new Dictionary <string, bool>();

            request.Batch.ForEach((r) => {
                r0.Add(r.ID, false);
            }
                                  );
            new DriverResponseHandler().OnResponse(driver, request, new StatusResponse()
            {
                Status = r0
            });

            Logger.Write(Logger.Tag.WARN, "Unable to connect to [driver:" + driver.Config.ID.Substring(0, 10) + "..., retries:" + driver.Heartbeat.Value.ToString() + "]");
            driver.Heartbeat.Increment();
            _notified = true;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Establishes connection to the remote host, sends the request an activates OnResponse on the requesthandler
        /// </summary>
        public void StartClient(Driver driver, DataRequestBatch request)
        {
            _received.Clear();

            remoteEP = new IPEndPoint(IPAddress.Parse(Host), Port);
            sender   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try {
                _notified          = false;
                sender.LingerState = _linger;
                // Establish the remote endpoint for the socket.
                try {
                    IAsyncResult result = sender.BeginConnect(remoteEP, null, null);

                    bool success = result.AsyncWaitHandle.WaitOne(Params.TIMEOUT, true);

                    if (success && sender.Connected)
                    {
                        sender.EndConnect(result);

                        // Encode the data string into a byte array.
                        byte[] msg = Encoder.EncodeDataRequestBatch(request);

                        // Send the data through the socket.
                        bool sendSuccess = Task.Run(() => {
                            int bytesSent = sender.Send(msg);
                            // Data buffer for incoming data.
                            // TODO: implement proper parsing of incoming response data
                            byte[] bytes = new byte[2048 * Params.BATCH_SIZE];  // hope thats enough
                            // byte[] bytes = new byte[10240];  // hope thats enough
                            // Receive the response from the remote device.
                            int bytesRec = sender.Receive(bytes);

                            if (bytesRec < 1)
                            {
                                HandleFailure(driver, request);
                            }

                            StatusResponse response = Encoder.DecodePointResponse(bytes);
                            if (response == null)
                            {
                                HandleFailure(driver, request);
                            }
                            else
                            {
                                new DriverResponseHandler().OnResponse(driver, request, response);
                            }

                            _notified = true;
                        }).Wait(Params.TIMEOUT);

                        if (!sendSuccess && !_notified)
                        {
                            HandleFailure(driver, request);
                        }
                        if (!_notified)
                        {
                            HandleFailure(driver, request);
                        }
                    }
                } catch (ArgumentNullException ane) {
                    Logger.Write(Logger.Tag.ERROR, string.Format("ArgumentNullException : {0}", ane.ToString()));
                } catch (SocketException se) {
                    Logger.Write(Logger.Tag.ERROR, string.Format("ArgumentNullException : {0}", se.ToString()));
                } catch (Exception e) {
                    Logger.Write(Logger.Tag.ERROR, string.Format("ArgumentNullException : {0}", e.ToString()));
                }
            } catch (Exception e) {
                Logger.Write(Logger.Tag.ERROR, e.ToString());
            } finally {
                try
                {
                    // Release the socket.
                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close(0);
                } catch {}
            }

            if (!_notified)
            {
                HandleFailure(driver, request);
            }
        }
Ejemplo n.º 4
0
 public static byte[] EncodeDataRequestBatch(DataRequestBatch msg)
 {
     return(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(msg, Formatting.None) + "<EOF>"));
 }