Ejemplo n.º 1
0
        public Task <RPResult> SendAndReceiveAsync(RPCall call)
        {
            RPResult result = new RPResult();

            switch (call.procedureName)
            {
            case "CommandContact":
                result.dt = th.GetTestPersonDataTable();
                break;

            case "CommandUpsert":
                result.success = 1;
                break;

            case "CommandGetCompanies":
                result.dt = th.GetTestCompanyDataTable();
                break;

            default:
                break;
            }
            var tcs = new TaskCompletionSource <RPResult>();

            tcs.SetResult(result);
            return(tcs.Task);
        }
Ejemplo n.º 2
0
        public RPResult Execute(RPCall call)
        {
            RPResult result = new RPResult();

            result.success = DatabaseFactory.Factory().DeleteCompany(Int32.Parse(call.procedureArgs[0]));
            return(result);
        }
Ejemplo n.º 3
0
        public RPResult Execute(RPCall call)
        {
            RPResult test = new RPResult();

            test.count = 12345;
            return(test);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Handles incoming requests.
        /// </summary>
        /// <param name="ctx">The HttpListenerContext returned by the HttpListener</param>
        public void HandleConnection(IHttpConnection con)
        {
            try {
                StreamReader msg = con.GetClientData();

                try{
                    RPCall   clientCall = Deserialize(msg);
                    ICommand requestedCommand;
                    if (!CommandDictionary.Instance.GetCommand(clientCall.procedureName, out requestedCommand))
                    {
                        // Wahrscheinlich besser eine eigene Exception zu schreiben und 404 zurückgeben.
                        throw new InvalidOperationException();
                    }
                    RPResult result = requestedCommand.Execute(clientCall);
                    byte[]   buffer = Encoding.UTF8.GetBytes(Serialize(result));
                    con.SendReponseData(buffer);
                } catch (Exception ex) {
                    // Deserialization did not work aborting.
                    if (ex is InvalidOperationException || ex is ArgumentNullException)
                    {
                        Console.WriteLine(ex.Message);
                        con.SendErrorCode(500);
                        return;
                    }
                    throw;
                }
            } catch (HttpListenerException ex) {
                // Can't really do anything at this point.
                Console.WriteLine(ex.Message);
                return;
            }
        }
Ejemplo n.º 5
0
        string Serialize(RPCall call)
        {
            StringWriter writer = new StringWriter();

            _RPCallSerializer.Serialize(writer, call);
            return(writer.ToString());
        }
Ejemplo n.º 6
0
        public RPResult Execute(RPCall call)
        {
            RPResult result = new RPResult();

            result.success = DatabaseFactory.Factory().UpsertInvoice(call.dt);
            return(result);
        }
Ejemplo n.º 7
0
        public RPResult Execute(RPCall call)
        {
            RPResult result = new RPResult();

            result.dt           = DatabaseFactory.Factory().GetCompany(Int32.Parse(call.procedureArgs[0]));
            result.dt.TableName = "Company";
            return(result);
        }
Ejemplo n.º 8
0
        internal async Task <RPResult> UpsertInvoice(Invoice invoice)
        {
            RPCall call = new RPCall("CommandUpsertInvoice");

            call.dt = Invoice.CreateTable();
            call.dt.Rows.Add(invoice.ToDataRow(call.dt));
            return(await _client.SendAndReceiveAsync(call));
        }
Ejemplo n.º 9
0
        public void CommandGetCompaniesReturnsCorrectTable()
        {
            RPCall call = new RPCall();

            CommandGetCompanies com = new CommandGetCompanies();
            RPResult            ret = com.Execute(call);

            Assert.AreEqual(ret.dt.TableName, "Companies");
            Assert.IsTrue(TestHelper.CompareDataTables(ret.dt, _th.GetTestCompanyDataTable()));
        }
Ejemplo n.º 10
0
        public void CommandUpsertIsSuccessful()
        {
            RPCall call = new RPCall();

            call.dt = _th.GetTestPersonDataTable();
            CommandUpsert com = new CommandUpsert();
            RPResult      ret = com.Execute(call);

            Assert.AreEqual(1, ret.success);
        }
Ejemplo n.º 11
0
        public void CommandGetCompanyReturnsCorrectTable()
        {
            RPCall call = new RPCall();

            call.procedureArgs = new string[] { "1" };
            CommandGetCompany com = new CommandGetCompany();
            RPResult          ret = com.Execute(call);

            Assert.IsTrue(TestHelper.CompareDataTables(ret.dt, _th.GetTestCompanyDataTable()));
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Search for a specific contact.
        /// </summary>
        /// <param name="name">The contacts name</param>
        /// <returns></returns>
        public async Task <RPResult> SearchContactsAsync(string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException();
            }
            RPCall call = new RPCall("CommandContact");

            call.procedureArgs = new string[] { name };
            return(await _client.SendAndReceiveAsync(call));
        }
Ejemplo n.º 13
0
        public void CommandDeleteCompaniesDeletesCorrectAmount()
        {
            RPCall call = new RPCall();

            call.procedureArgs = new string[] { "1" };

            CommandDeleteCompany com = new CommandDeleteCompany();
            RPResult             ret = com.Execute(call);

            Assert.AreEqual(1, ret.success);
        }
Ejemplo n.º 14
0
        public RPResult Execute(RPCall call)
        {
            List <Contact>       companies = DatabaseFactory.Factory().GetCompanies();
            ContactListConverter conv      = new ContactListConverter();

            RPResult ret = new RPResult();

            ret.dt           = (DataTable)conv.ConvertTo(null, null, companies, typeof(DataTable));
            ret.dt.TableName = "Companies";
            return(ret);
        }
Ejemplo n.º 15
0
        public async Task <RPResult> SearchInvoicesAsync(InvoiceSearchData data)
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            MemoryStream    memoryStream    = new MemoryStream();

            binaryFormatter.Serialize(memoryStream, data);

            RPCall call = new RPCall("CommandInvoice", memoryStream.GetBuffer());

            return(await _client.SendAndReceiveAsync(call));
        }
Ejemplo n.º 16
0
        public async Task <RPResult> SendAndReceiveAsync(RPCall call)
        {
            StringContent       sendCall = new StringContent(Serialize(call).ToString());
            HttpResponseMessage response = await _client.PostAsync("http://localhost:12345/", sendCall);

            response.EnsureSuccessStatusCode();
            string resultString = await response.Content.ReadAsStringAsync();

            RPResult result = Deserialize(resultString);

            return(result);
        }
Ejemplo n.º 17
0
        public void CommandContactReturnsCorrectTable()
        {
            RPCall call = new RPCall();

            call.procedureArgs = new string[] { "Max" };

            CommandContact com = new CommandContact();
            RPResult       ret = com.Execute(call);

            Assert.AreEqual(ret.dt.TableName, "Contacts");
            Assert.IsTrue(TestHelper.CompareDataTables(ret.dt, _th.GetTestPersonDataTable()));
        }
Ejemplo n.º 18
0
        public RPResult Execute(RPCall call)
        {
            if (call.procedureArgs == null || call.procedureArgs.Length < 1)
            {
                throw new InvalidOperationException();
            }
            List <Contact>       contacts = DatabaseFactory.Factory().SearchContacts(call.procedureArgs[0]);
            ContactListConverter conv     = new ContactListConverter();

            RPResult retVal = new RPResult();

            retVal.dt           = (DataTable)conv.ConvertTo(null, null, contacts, typeof(DataTable));
            retVal.dt.TableName = "Contacts";
            return(retVal);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Send a Contact object to be updated or Inserted
        /// into the database if it does not already exist.
        /// </summary>
        /// <param name="contact">The contact to be inserted or updated</param>
        /// <returns>
        ///		A RPResult object its success field determines
        ///		wether the query was successful(1) or not(0).
        /// </returns>
        public async Task <RPResult> SendContactAsync(Contact contact)
        {
            if (contact == null)
            {
                throw new ArgumentNullException();
            }
            RPCall         call = new RPCall("CommandUpsert");
            List <Contact> temp = new List <Contact>()
            {
                contact
            };
            ContactListConverter conv = new ContactListConverter();

            call.dt = (DataTable)conv.ConvertTo(null, null, temp, typeof(DataTable));

            return(await _client.SendAndReceiveAsync(call));
        }
Ejemplo n.º 20
0
 public RPResult Execute(RPCall call)
 {
     try {
         List <Contact> contact = CreateContactList(call.dt);
         DatabaseFactory.Factory().UpsertContact(contact[0]);
         RPResult retVal = new RPResult();
         retVal.success = 1;
         return(retVal);
     } catch (Exception ex) {
         if (ex is InvalidOperationException || ex is ArgumentNullException)
         {
             RPResult retval = new RPResult();
             retval.success = 0;
             return(retval);
         }
         throw;
     }
 }
Ejemplo n.º 21
0
        public RPResult Execute(RPCall call)
        {
            List <Invoice> invoices = null;
            DataTable      table    = null;

            if ((call.Buffer == null || call.Buffer.Length < 1) && (call.procedureArgs == null || call.procedureArgs.Length < 1))
            {
                throw new InvalidOperationException();
            }
            else if (call.Buffer != null)
            {
                BinaryFormatter   binaryFormatter = new BinaryFormatter();
                MemoryStream      memoryStream    = new MemoryStream(call.Buffer);
                InvoiceSearchData data            = (InvoiceSearchData)binaryFormatter.Deserialize(memoryStream);

                invoices = DatabaseFactory.Factory().SearchInvoices(data);
            }
            else
            {
                invoices = DatabaseFactory.Factory().SearchInvoices(Int32.Parse(call.procedureArgs[0]));
            }

            table = Invoice.CreateTable();

            foreach (Invoice invoice in invoices)
            {
                table.Rows.Add(invoice.ToDataRow(table));
            }

            table.AcceptChanges();

            RPResult retVal = new RPResult();

            retVal.dt           = table;
            retVal.dt.TableName = "Invoices";
            return(retVal);
        }
Ejemplo n.º 22
0
        internal async Task <RPResult> DeleteCompany(int p)
        {
            RPCall call = new RPCall("CommandDeleteCompany", new string[] { p.ToString() });

            return(await _client.SendAndReceiveAsync(call));
        }
Ejemplo n.º 23
0
        RPCall Deserialize(StreamReader reader)
        {
            RPCall call = (RPCall)_RPCallSerializer.Deserialize(reader);

            return(call);
        }
Ejemplo n.º 24
0
        internal async Task <RPResult> SearchCompany(int p, string company)
        {
            RPCall call = new RPCall("CommandSearchCompany", new string[] { p.ToString(), company });

            return(await _client.SendAndReceiveAsync(call));
        }
Ejemplo n.º 25
0
        public async Task <RPResult> SearchContactInvoicesAsync(int id)
        {
            RPCall call = new RPCall("CommandInvoice", new string[] { id.ToString() });

            return(await _client.SendAndReceiveAsync(call));
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Returns all available companies
        /// </summary>
        /// <returns>A RPResult with the companies in its "dt" field</returns>
        public async Task <RPResult> GetCompaniesAsync()
        {
            RPCall call = new RPCall("CommandGetCompanies");

            return(await _client.SendAndReceiveAsync(call));
        }
Ejemplo n.º 27
0
        internal async Task <RPResult> SetCompany(int?id)
        {
            RPCall call = new RPCall("CommandSetCompany", new string[] { id.Value.ToString() });

            return(await _client.SendAndReceiveAsync(call));
        }