public virtual IEnumerator <ITask> ConfigureDeviceHandler(ConfigureDevice configureDevice)
        {
            int ix = _state.SensorConfiguration.FindIndex(
                delegate(SensorConfiguration c)
            {
                return(c.DeviceModel == configureDevice.Body.DeviceModel &&
                       (c.DeviceName ?? string.Empty) == (configureDevice.Body.DeviceName ?? string.Empty));
            });

            if (ix < 0)
            {
                _state.SensorConfiguration.Add(configureDevice.Body);
            }
            else
            {
                _state.SensorConfiguration[ix].SuccessRangeMin = configureDevice.Body.SuccessRangeMin;
                _state.SensorConfiguration[ix].SuccessRangeMax = configureDevice.Body.SuccessRangeMax;
            }

            ValidateState();

            yield return(Arbiter.ExecuteToCompletion(Environment.TaskQueue,
                                                     new IterativeTask(FindSensors)));

            configureDevice.ResponsePort.Post(DefaultUpsertResponseType.Instance);
            yield break;
        }
Example #2
0
 /// <summary>
 /// Calls DirectorySearch function for every generic contract known.
 /// </summary>
 protected virtual void Bind()
 {
     AdaptersDictionary = new Dictionary <AdapterTypes, ArrayList>();
     foreach (string contract in genericContractList)
     {
         Arbiter.ExecuteToCompletion(DssEnvironment.TaskQueue, new IterativeTask <string>(contract, DirectorySearch));
     }
 }
Example #3
0
        protected void updateAdapters()
        {
            var dirPort = DssEnvironment.ServiceForwarder <dirProxy.DirectoryPort>(new Uri("http://localhost:50000/directory"));

            //Console.WriteLine("Querying directory");
            //dirProxy.QueryRequest request = new dirProxy.QueryRequest();
            //request.QueryRecord = new ServiceInfoType();
            Arbiter.ExecuteToCompletion(DssEnvironment.TaskQueue,
                                        Arbiter.Choice <dirProxy.GetResponse, Fault>(
                                            dirPort.Get(),
                                            delegate(dirProxy.GetResponse success)
            {
                // See if each service matches a known one from the config file, if so, make an adapter
                //Console.WriteLine("Checking " + success.RecordList.Length + " services");
                foreach (var rec in success.RecordList)
                {
                    //Uri foundUri = new Uri(rec.Service);
                    AdapterSpec adapterSpec = null;
                    //Console.WriteLine("Checking " + rec.Service);
                    foreach (var adapter in adapterNames.Values)
                    {
                        //Console.WriteLine("Comparing " + rec.Service + " to " + adapter.ServiceInfo.Service);
                        //Uri testUri = new Uri(adapter.ServiceInfo.Service);
                        //if (Uri.Compare(foundUri, testUri, UriComponents.Path, UriFormat.UriEscaped, StringComparison.InvariantCultureIgnoreCase) == 0)
                        if (rec.Service.EndsWith(adapter.ServiceConfig.Service))
                        {
                            //Console.WriteLine("* Assigned * " + rec.Service + " to " + adapter.ServiceInfo.Service);
                            if (adapterSpec == null)
                            {
                                adapterSpec = adapter;
                            }
                            else
                            {
                                Console.WriteLine("WARNING: duplicate service: " + rec.Service + " (already had one for " + adapter.ServiceConfig.Service + ")");
                            }
                        }
                    }
                    if (adapterSpec != null)
                    {
                        AdapterFactory.CreateAdapterIfNeeded(adapterSpec, rec);
                    }
                }
            },
                                            delegate(Fault fault)
            {
                Console.WriteLine("AdapterBank: Fault querying directory: " + fault.Reason);
            }));
        }
Example #4
0
 public drive.DriveDifferentialTwoWheelState get()
 {
     drive.DriveDifferentialTwoWheelState ret = null;
     Arbiter.ExecuteToCompletion(DssEnvironment.TaskQueue,
                                 Arbiter.Choice <drive.DriveDifferentialTwoWheelState, Fault>(
                                     drivePort.Get(),
                                     delegate(drive.DriveDifferentialTwoWheelState state)
     {
         ret = state;
     },
                                     delegate(Fault failure)
     {
         throw new AdapterOperationException(failure);
     }));
     return(ret);
 }
Example #5
0
 public vector.VectorState get()
 {
     vector.VectorState ret = null;
     Arbiter.ExecuteToCompletion(DssEnvironment.TaskQueue,
                                 Arbiter.Choice <vector.VectorState, Fault>(
                                     opPort.Get(),
                                     delegate(vector.VectorState state)
     {
         ret = state;
     },
                                     delegate(Fault failure)
     {
         throw new AdapterOperationException(failure);
     }));
     return(ret);
 }
Example #6
0
        public IEnumerator <ITask> OnHttpGet(HttpGet httpGet)
        {
            HttpListenerContext  context  = httpGet.Body.Context;
            HttpListenerRequest  request  = context.Request;
            HttpListenerResponse response = context.Response;

            string path = request.RawUrl;
            string file = Path.GetFileName(path);

            if (file != "SegmentedImage")
            {
                httpGet.ResponsePort.Post(new HttpResponseType(HttpStatusCode.OK, _state.SmallCopy, _transform));
                yield break;
            }

            Bitmap bitmap  = null;
            bool   dispose = false;

            switch (file)
            {
            case "SegmentedImage":
                if (_state.SegmentedImage != null)
                {
                    bitmap  = _state.SegmentedImage.Bitmap;
                    dispose = true;
                }
                else
                {
                    bitmap = Resources.SegNotAvailable;
                }
                break;

            default:
                break;
            }

            yield return(Arbiter.ExecuteToCompletion(TaskQueue,
                                                     new IterativeTask <HttpListenerContext, Bitmap>(context, bitmap, StreamImage)));

            if (dispose)
            {
                bitmap.Dispose();
            }
        }
Example #7
0
        protected void subscribeDirectory()
        {
            var dirPort = DssEnvironment.ServiceForwarder <dirProxy.DirectoryPort>(new Uri("http://localhost:50000/directory"));
            var resPort = new dirProxy.DirectoryPort();

            Arbiter.ExecuteToCompletion(DssEnvironment.TaskQueue,
                                        Arbiter.Choice <SubscribeResponseType, Fault>(
                                            dirPort.Subscribe(resPort, null),
                                            delegate(SubscribeResponseType success)
            {
                Console.WriteLine("AdapterBank subscribed to directory service");
            },
                                            delegate(Fault failure)
            {
                throw new Exception("Could not subscribe to directory service: " + failure.Reason);
            }));
            Arbiter.Activate(DssEnvironment.TaskQueue,
                             Arbiter.Receive <dirProxy.Insert>(true, resPort, directoryInsertHandler));
        }
Example #8
0
        public IEnumerator <ITask> OnHttpQuery(HttpQuery httpQuery)
        {
            HttpListenerContext context = httpQuery.Body.Context;
            HttpListenerRequest request = context.Request;
            NameValueCollection query   = httpQuery.Body.Query;

            string[] segments = request.Url.Segments;
            string   file     = segments[segments.Length - 1];

            if (file != "SegmentedImage")
            {
                httpQuery.ResponsePort.Post(new HttpResponseType(HttpStatusCode.OK, _state.SmallCopy, _transform));
                yield break;
            }

            Bitmap bitmap = null;

            switch (file)
            {
            case "SegmentedImage":
                if (_state.SegmentedImage != null)
                {
                    bitmap = _state.SegmentedImage.Bitmap;
                }
                else
                {
                    bitmap = Resources.SegNotAvailable;
                }
                break;

            default:
                break;
            }

            yield return(Arbiter.ExecuteToCompletion(TaskQueue,
                                                     new IterativeTask <HttpListenerContext, Bitmap>(context, bitmap, StreamImage)));
        }
        /// <summary>
        /// Drains the pending requests.
        /// </summary>
        /// <param name="timestamp">The timestamp.</param>
        /// <returns>Standard ccr iterator</returns>
        private IEnumerator <ITask> DrainPendingRequests(DateTime timestamp)
        {
            var stopwatch = Stopwatch.StartNew();

            try
            {
                // No pending request - exit early
                if (this.pendingFrameRequests.Count == 0)
                {
                    yield break;
                }

                // Fetch the depth and video in parallel as required. Do once each for any pending requests.
                var taskCount = 0;
                var donePort  = new Port <EmptyValue>();
                var rawFrames = new RawKinectFrames();

                if (this.pendingFrameRequests.Any(p => p.Body.IncludeDepth))
                {
                    Arbiter.ExecuteToCompletion(
                        TaskQueue,
                        new IterativeTask <DepthImageFormat, RawKinectFrames>(
                            this.state.DepthImageFormat,
                            rawFrames,
                            this.BuildRawDepthFrame),
                        donePort);

                    ++taskCount;
                }

                if (this.pendingFrameRequests.Any(p => p.Body.IncludeVideo))
                {
                    Arbiter.ExecuteToCompletion(
                        TaskQueue,
                        new IterativeTask <ColorImageFormat, RawKinectFrames>(
                            this.state.ColorImageFormat,
                            rawFrames,
                            this.BuildRawVideoFrame),
                        donePort);

                    ++taskCount;
                }

                if (taskCount > 0)
                {
                    // Wait for the operations to complete
                    yield return(Arbiter.MultipleItemReceive(false, donePort, taskCount, EmptyHandler));
                }

                // Stamp the returned frames
                var currentFrame = ++this.frameNumber;
                if (rawFrames.RawDepthFrameData != null)
                {
                    rawFrames.RawDepthFrameInfo.FrameNumber = currentFrame;
                    rawFrames.RawDepthFrameInfo.Timestamp   = timestamp.Ticks;
                }

                if (rawFrames.RawColorFrameData != null)
                {
                    rawFrames.RawColorFrameInfo.FrameNumber = currentFrame;
                    rawFrames.RawColorFrameInfo.Timestamp   = timestamp.Ticks;
                }

                // Return the requested frames to each pending request.
                foreach (var request in this.pendingFrameRequests)
                {
                    var response = new RawKinectFrames
                    {
                        RawDepthFrameInfo = request.Body.IncludeDepth ? rawFrames.RawDepthFrameInfo : null,
                        RawDepthFrameData = request.Body.IncludeDepth ? rawFrames.RawDepthFrameData : null,
                        RawColorFrameInfo = request.Body.IncludeVideo ? rawFrames.RawColorFrameInfo : null,
                        RawColorFrameData = request.Body.IncludeVideo ? rawFrames.RawColorFrameData : null,
                    };
                    request.ResponsePort.Post(new kinect.GetRawFrameResponse {
                        RawFrames = response
                    });
                }
            }
            finally
            {
                this.pendingFrameRequests.Clear();

                var timeToNextFrame = this.pollInterval - stopwatch.Elapsed;
                if (timeToNextFrame <= TimeSpan.Zero)
                {
                    this.pollPort.Post(DateTime.Now);
                }
                else
                {
                    TaskQueue.EnqueueTimer(timeToNextFrame, this.pollPort);
                }
            }
        }
Example #10
0
        public IEnumerator <ITask> OnHttpPost(HttpPost post)
        {
            Fault fault = null;

            NameValueCollection parameters = null;

            ReadFormData readForm = new ReadFormData(post);

            _utilities.Post(readForm);
            yield return(Arbiter.Choice(
                             readForm.ResultPort,
                             delegate(NameValueCollection success)
            {
                parameters = success;
            },
                             delegate(Exception e)
            {
                fault = Fault.FromException(e);
            }
                             ));

            string operation = parameters["Operation"];

            if (operation == "UpdateControl")
            {
                UpdateControl message = new UpdateControl();
                FlexControl   update  = message.Body;
                update.Id    = parameters["ID"];
                update.Value = parameters["Value"];
                update.Text  = parameters["Text"];

                FlexControl control = _state.Controls.Find(update.CompareId);

                if (control == null)
                {
                    post.ResponsePort.Post(Fault.FromCodeSubcodeReason(
                                               FaultCodes.Receiver,
                                               DsspFaultCodes.UnknownEntry,
                                               "Cannot find control with ID: " + update.Id
                                               ));
                    yield break;
                }
                update.ControlType = control.ControlType;

                yield return(Arbiter.ExecuteToCompletion(
                                 TaskQueue,
                                 new IterativeTask <UpdateControl>(message, OnUpdateControl)
                                 ));

                post.ResponsePort.Post(new HttpResponseType(DefaultUpdateResponseType.Instance));
            }
            else if (operation == "ButtonPress")
            {
                ButtonPress        message = new ButtonPress();
                ButtonPressRequest request = message.Body;
                request.Id      = parameters["ID"];
                request.Pressed = bool.Parse(parameters["Pressed"]);

                OnButtonPress(message);

                post.ResponsePort.Post(new HttpResponseType(DefaultUpdateResponseType.Instance));
            }
            else
            {
                fault = Fault.FromCodeSubcodeReason(
                    FaultCodes.Receiver,
                    DsspFaultCodes.MessageNotSupported,
                    "Unknown operation: " + operation
                    );
            }

            if (fault != null)
            {
                post.ResponsePort.Post(fault);
                yield break;
            }
        }