Пример #1
0
        /// <summary>
        /// Serialisiert ein Objekt in ein byte Array
        /// </summary>
        /// <param name="input">Objekt das Serialisiert werden soll</param>
        /// <returns>Serialisiertes Objekt</returns>
        public byte[] writeCommand(Command input)
        {
            MemoryStream stream = new MemoryStream();

            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, input);

            byte[] result = stream.GetBuffer();

            stream.Close();

            return result;
        }
        string IDarPoolingMobile.HandleDarPoolingMobileRequest(Command c)
        {
            Console.WriteLine("{0} Received Mobile Request", LogTimestamp);
            string requestID = mainServiceImpl.generateGUID();
            c.CommandID = requestID;

            // Handle the command using the main service.
            mainServiceImpl.HandleMobileDarPoolingRequest(c);

            Console.WriteLine("Returning Request ID to client");
            //string requestIDTest = DateTime.Now.ToString();
            //return requestIDTest;
            return requestID;
        }
        /// <summary>
        /// Forward aCommand to remote service.
        /// </summary>
        /// <param name="command">The Command to be forwarded</param>
        /// <param name="destination">String that represent the address of the target service.</param>
        private void ForwardCommand(Command command, string destination, string rootSender)
        {
            if (debug)
                Console.WriteLine("{0} Forwarding a {1} to {2}",LogTimestamp, command.GetType().Name, destination.Split('/').Last().ToUpper());

            /** Invoke the remote node service via the dedicated forward endpoint address. */
            BasicHttpBinding fwdBinding = new BasicHttpBinding();
            EndpointAddress fwdEndpoint = new EndpointAddress(destination);
            ChannelFactory<IDarPoolingForwarding> fwdChannelFactory = new ChannelFactory<IDarPoolingForwarding>(fwdBinding, fwdEndpoint);
            IDarPoolingForwarding destinationService = fwdChannelFactory.CreateChannel();

            //string senderAddress = receiver.BaseForwardAddress + receiver.NodeName;

            destinationService.HandleForwardedDarPoolingRequest(command, rootSender);

            /** Close the channel: the communication is fire-and-forget (one-way) */
            ((IClientChannel)destinationService).Close();
            fwdChannelFactory.Close();
        }
        /// <summary>
        /// IDarPoolingForwarding method. The service node obtain the result of the forwarded command.
        /// </summary>
        /// <param name="forwardedCommand"></param>
        /// <param name="finalResult"></param>
        public void ReturnFinalResult(Result result, Command originalCommand)
        {
            if (IsFwdCommand(originalCommand.CommandID))
            {
                string senderAddress = ExtractService(originalCommand.CommandID);
                // Get ready to contact the sender Service node.
                BasicHttpBinding myBinding = new BasicHttpBinding();
                EndpointAddress myEndpoint = new EndpointAddress(senderAddress);
                ChannelFactory<IDarPoolingForwarding> myChannelFactory = new ChannelFactory<IDarPoolingForwarding>(myBinding, myEndpoint);
                IDarPoolingForwarding service = myChannelFactory.CreateChannel();

                // Give the result back to the the sender Service node.
                service.ReturnFinalResult(result, originalCommand);
                // Close channel.
                ((IClientChannel)service).Close();
                myChannelFactory.Close();
            }
            else
            {
                if (debug)
                {
                    TimeSpan totalTime = DateTime.Now.Subtract(originalCommand.Timestamp);
                    Console.WriteLine("{0} Total time for {1}: {2}", LogTimestamp, originalCommand.GetType().Name, totalTime.TotalMilliseconds);
                }
                if (IsMobileCommand(originalCommand.CommandID))
                {
                   Console.WriteLine("\n{0} Ready to send the result back to Mobile", LogTimestamp);
                   mobile.AddMobileResult(originalCommand.CommandID, result);
                   //mobile.

                }
                else
                {
                    IDarPoolingCallback client = ExtractClient(originalCommand.CommandID);
                    ReturnResultToClient(result, client);
                }
            }
        }
        public void HandleMobileDarPoolingRequest(Command command)
        {
            if (debug)
                Console.WriteLine("\n{0} {1} node received {2}", LogTimestamp, receiver.NodeName.ToUpper(), command.GetType());

            /** Assign a GUID to the command. */
            pendingMobileCommands.Add(command.CommandID, command);

            /** Set a ServiceNodeCore as the receiver of the command and execute the command. */
            command.Timestamp = DateTime.Now;
            command.Receiver = receiver;
            command.Callback = new AsyncCallback(ProcessResult);
            command.Execute();
        }
        public void HandleForwardedRangeSearch(Command command, string senderAddress, QueryBuilder query)
        {
            if (debug)
                Console.WriteLine("{0} {1} node received FWD_RangeSearch", LogTimestamp, receiver.NodeName.ToUpper());

            //AddFwdCommandService(fwdCommand.CommandID, rootSenderAddress);
        }
        /// <summary>
        /// IDarPoolingForwarding method. The user-related command uses only one hop,
        /// i.e. they always reach the correct and final destination node with only
        /// one connession. For these reasons, we only have to execute the command,
        /// and the return the result to the rootSender service node.
        /// </summary>
        /// <param name="forwardedCommand"></param>
        public void HandleForwardedDarPoolingRequest(Command fwdCommand, string rootSenderAddress)
        {
            if (debug)
                Console.WriteLine("{0} {1} node received FWD_{2}",LogTimestamp, receiver.NodeName.ToUpper(), fwdCommand.GetType().Name);

            AddFwdCommandService(fwdCommand.CommandID, rootSenderAddress);
            fwdCommand.Receiver = receiver;
            fwdCommand.Callback = new AsyncCallback(ProcessResultOfForwardedCommand);
            fwdCommand.Execute();
        }
        /// <summary>
        /// Execute request of clients.
        /// </summary>
        /// <param name="command">The Command object, sent by a client</param>
        public void HandleDarPoolingRequest(Command command)
        {
            if (debug)
                Console.WriteLine("\n{0} {1} node received {2}",LogTimestamp, receiver.NodeName.ToUpper(),command.GetType());

            /** Assign a GUID to the command. */
            command.CommandID = generateGUID();
            /** Save information about the client that has sent the command. */
            IDarPoolingCallback client = OperationContext.Current.GetCallbackChannel<IDarPoolingCallback>();
            commandClient.Add(command.CommandID, client);

            /** Set a ServiceNodeCore as the receiver of the command and execute the command. */
            command.Timestamp = DateTime.Now;
            command.Receiver = receiver;
            command.Callback = new AsyncCallback(ProcessResult);
            command.Execute();
        }
Пример #9
0
        public static void TestCommands(Command c)
        {
            string serviceNodeAddress = "http://localhost:1111/Catania";
            string callbackAddress = "http://localhost:2222/prova";

            ClientCallback callback = new ClientCallback();

            // First of all, set up the connection
            EndpointAddress endPointAddress = new EndpointAddress(serviceNodeAddress);
            WSDualHttpBinding binding = new WSDualHttpBinding();
            binding.ClientBaseAddress = new Uri(callbackAddress);
            DuplexChannelFactory<IDarPooling> factory = new DuplexChannelFactory<IDarPooling>(
                    callback, binding, endPointAddress);

            IDarPooling serviceProxy = factory.CreateChannel();

            serviceProxy.HandleDarPoolingRequest(c);
        }