コード例 #1
0
        private void parseTransaction(GenericMessage M)
        {
            //TRANSACTION <ID> <ACTION>
            //ACTION: GET,CONFIRM,REJECT,COMMENT
            string[] Parts = M.Command.Split(' ');
            if (Parts.Length == 3)
            {
                int index = -1;
                if (int.TryParse(Parts[1], out index))
                {
                    BitTransaction BT = null;
                    try
                    {
                        //check if transaction exists and if user is member of it.
                        BT = new BitTransaction(index);
                        if (BT.AddressBuyer != M.Sender && BT.AddressSeller != M.Sender)
                        {
                            throw new Exception("CRAP!");
                        }
                    }
                    catch
                    {
                        sendErr(new GenericMessage()
                        {
                            Receiver = M.Sender, RawContent = "invalid transaction ID", Server = M.Server
                        });
                        return;
                    }
                    switch (Parts[2].ToUpper())
                    {
                    case "GET":
                        sendMsg(new GenericMessage()
                        {
                            Receiver   = M.Sender,
                            Command    = M.Command,
                            RawContent = string.Format(@"AddressBuyer={0}
AddressSeller={1}
Amount={2}
Offer={3}
State={4}
BuyerComment={5}
BuyerRating={6}
SellerComment={7}
SellerRating={8}
TransactionTime={9}",
                                                       BT.AddressBuyer, BT.AddressSeller,
                                                       BT.Offer, (int)BT.State, BT.Amount,
                                                       Base.B64enc(BT.BuyerComment),
                                                       (int)BT.BuyerRating,
                                                       Base.B64enc(BT.SellerComment),
                                                       (int)BT.SellerRating,
                                                       BT.TransactionTime.ToString(Base.DT_FORMAT)),
                            Server = M.Server
                        });
                        break;

                    case "CONFIRM":
                        try
                        {
                            BT.Confirm(M.Sender);
                        }
                        catch (Exception ex)
                        {
                            sendErr(new GenericMessage()
                            {
                                Receiver = M.Sender, RawContent = "cannot confirm transaction: " + ex.Message, Server = M.Server
                            });
                            break;
                        }
                        GenericMessage Clone1 = M.Clone();
                        Clone1.Command = "TRANSACTION " + BT.Index.ToString() + " GET";
                        Clone1.Sender  = BT.AddressSeller;
                        parseTransaction(Clone1);
                        Clone1.Sender = BT.AddressBuyer;
                        parseTransaction(Clone1);
                        break;

                    case "REJECT":
                        try
                        {
                            BT.Reject(M.Sender);
                        }
                        catch (Exception ex)
                        {
                            sendErr(new GenericMessage()
                            {
                                Receiver = M.Sender, RawContent = "cannot reject transaction: " + ex.Message, Server = M.Server
                            });
                            break;
                        }
                        GenericMessage Clone2 = M.Clone();
                        Clone2.Command = "TRANSACTION " + BT.Index.ToString() + " GET";
                        Clone2.Sender  = BT.AddressSeller;
                        parseTransaction(Clone2);
                        Clone2.Sender = BT.AddressBuyer;
                        parseTransaction(Clone2);
                        break;

                    case "COMMENT":
                        var NVC = Base.ParseContent(M.RawContent);
                        if (BT.canRate(M.Sender))
                        {
                            try
                            {
                                if (!string.IsNullOrEmpty(NVC["Comment"]))
                                {
                                    BT.Comment(M.Sender, Base.B64dec(NVC["Comment"]));
                                }
                                if (!string.IsNullOrEmpty(NVC["Rating"]))
                                {
                                    BT.Rate(M.Sender, (Rating)int.Parse(NVC["Rating"]));
                                }
                            }
                            catch
                            {
                                sendErr(new GenericMessage()
                                {
                                    Receiver = M.Sender, RawContent = "The value for 'Comment' or 'Rating' is invalid", Server = M.Server
                                });
                            }
                        }
                        else
                        {
                            sendErr(new GenericMessage()
                            {
                                Receiver = M.Sender, RawContent = "You cannot rate the given transaction", Server = M.Server
                            });
                        }
                        break;

                    default:
                        sendErr(new GenericMessage()
                        {
                            Receiver = M.Sender, RawContent = "invalid TRANSACTION command", Server = M.Server
                        });
                        break;
                    }
                }
            }
        }