Exemplo n.º 1
0
        /// <summary>
        /// Converts stringified ComData to comdata and moves it over
        /// </summary>
        /// <param name="rawcommand">A stringified ComData</param>
        /// <returns></returns>
        public bool Send(string rawcommand)
        {
            DataInterpretor reader = new DataInterpretor();

            reader.addString(rawcommand);

            if (reader.usableInput)
            {
                ComData comdata    = reader.getComData();
                bool    succession = Send(comdata);

                if (succession == true)
                {
                    UpdateLifetime();
                }

                return(succession);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 2
0
 public void IncomingCommand_Handle(ComData properCommand)
 {
     unhandledcommands.Add(properCommand, this);
 }
Exemplo n.º 3
0
 public DataInterpretor()
 {
     comdata = new ComData();
 }
Exemplo n.º 4
0
        // <summary>
        // The following commands are internally used in the server
        // </summary>

        public dynamic debug(int commandID, int myserverID, int senderID, int receiverID, ComData comdata, DeviceRouting devices)
        {
            string CurrentTime = DateTime.Now.ToString("HH:mm:ss");

            DeviceRegistry sender = devices.FindByID(receiverID);

            if (sender != null)
            {//has sender that is acknowledged by the server
                {
                    string message1 = "";
                    comdata.Get(3, ref message1);

                    string message2 = "";
                    comdata.Get(4, ref message2);


                    Console.WriteLine("'" + sender.Name + "' at " + CurrentTime + ": " + message1 + '\n' + message2);
                }
            }
            else
            {
                Console.WriteLine("Incoming command doesn't have proper sender at " + CurrentTime);
            }
            return(null);
        }
Exemplo n.º 5
0
        public dynamic ExecuteCommand(int commandID, int myserverID, int senderID, int receiverID, ComData comdata, DeviceRouting devices)
        {
            if (commandID < 1)
            {
                return(null);
            }

            Func <int, int, int, int, ComData, DeviceRouting, dynamic> f = null;

            try
            {
                f = commandlist.ElementAt(commandID);
            }
            catch
            {
                return(null);
            }

            if (f == null)
            {
                return(null);
            }

            return(f(commandID, myserverID, senderID, receiverID, comdata, devices));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Incoming messages from clientss are processed here.
        /// </summary>
        /// <param name="message">Stringified version of ComData</param>
        public void SendMessage(string message)
        {
            //Process Stringified ComData
            data.addString(message);

            //Is it really usable (as ComData)
            if (data.usableInput)
            {
                //Get the comdata
                ComData comdata = data.getComData();

                int senderID   = 0;
                int receiverID = 0;
                int commandID  = 0;

                string CurrentTime = DateTime.Now.ToString("HH:mm:ss");

                comdata.Get(0, ref senderID);
                comdata.Get(1, ref receiverID);
                comdata.Get(2, ref commandID);

                //Is the command valid?
                if (commandID < 1)
                {
                    MessagesList.Add(Commandslist.GenerateErrorMessage(senderID, 1, "Invalid command"));
                    return;
                }

                //A message has been received by something, find device
                DeviceRegistry receiver = devices.FindByID(receiverID);

                //The receiver has not been registered in this system
                if (receiver == null)
                {
                    //And the receiver is not supposed to be 'everyone'
                    if (receiverID != -1)
                    {
                        //then stop the code..
                        //Add the message to return error to sender
                        MessagesList.Add(Commandslist.GenerateErrorMessage(senderID, 1, "Invalid receiver"));
                        return;
                    }
                }

                //If this message is meant for this server
                if (receiverID == myserverID || receiverID == -1)
                {
                    //Does not require a valid senderID
                    //Switch base handshake commands

                    dynamic result = Commandslist.ExecuteCommand(commandID, myserverID, senderID, receiverID, comdata, devices);

                    //if result is a command
                    if (result is ComData)
                    {
                        int param1 = 0;
                        result.Get(0, ref param1);

                        Console.WriteLine(param1);
                        Console.WriteLine(message);
                        MessagesList.Add(result);
                    }
                }
                else//wasn't meant  for this server
                {
                    //TODO make code to transmit command to target
                }
            }
            else
            {
                string CurrentTime = DateTime.Now.ToString("HH:mm:ss");
                Console.WriteLine("corrupted message received at " + CurrentTime);
            }
        }
Exemplo n.º 7
0
        public void Add(ComData data, Communication handler)
        {
            if (!Program.MasterServer.Connected)//Then if connection with master server is there
            {
                return;
            }

            int target = 0;

            data.Get(0, ref target);

            int sender = 0;

            data.Get(1, ref sender);

            int commandid = 0;

            data.Get(2, ref commandid);

            //Reporter.AddReport(4, handler.DeviceName+ ": "+ data.ToString());

            switch (handler.devicetype.ToLower())
            {
            case "arduino":
            case "i/o":
            {
                if (target == -1 || target == Program.MasterServer.serverID)
                {
                    Program.MasterServer.Send(data);
                    return;
                }
                else if (target == handler.comID || sender == handler.comID)        //coming or going from device
                {
                    executable(data, handler);
                }

                if (target == handler.comID)
                {
                    if (commandid != 3 || target != sender)
                    {
                        handler.Send(data);
                    }
                }
                break;
            }

            case "server":
            case "pc":
            {
                if (target == -1)        //is -1 when
                {
                    foreach (Communication device in Program.LocalDevices)
                    {
                        if (device.Connected)
                        {
                            data.Set(0, device.comID);
                            device.unhandledcommands.executable(data, device);
                        }
                    }
                }
                else if (target == handler.comID)
                {
                    executable(data, handler);
                }
                else
                {
                    foreach (Communication device in Program.LocalDevices)
                    {
                        if (device.Connected && target == device.comID)
                        {
                            device.unhandledcommands.executable(data, device);
                        }
                    }
                }

                break;
            }
            }
        }