//This class is preserved almost unscathed from the tutorial // of teletrader's API instructions. // CommonHelper is repsonsible for validation of the // the status of the network using System; using System.Collections.Generic; using System.Text; using MdsApi; using MdsApiHelper; namespace TeletraderProxy { class CommonHelper { public static bool CheckForError(OperationStatus cs, IMDSClient mds) { if (cs != OperationStatus.OK) { Console.WriteLine("Connection error! "); Console.WriteLine("Network error code: {0}", mds.GetLastNetworkError()); //Console.WriteLine("Error message: {0}", mds.GetLastErrorDescription()); Console.WriteLine("Returned error code: {0}", cs); Form1.stopProgram = 1; Environment.Exit(Form1.stopProgram); return false; } else { return true; } } // Function tries to login to server, returns userID if successful, 0 otherwise public static int LoginToServer(IMDSClient mds, string username, string password, int requestID) { IMessage loginMessage = RequestFactory.MakeLoginRequest(requestID, username, password); OperationStatus cs = mds.Send(loginMessage); if (!CheckForError(cs, mds)) { return 0; } // wait 5 sec for response... IMessage answer = null; cs = mds.Receive(ref answer, 5000); if (!CheckForError(cs, mds) || answer == null) return 0; SafeMessage response = new SafeMessage(answer); if (response[MessageFolderName.Header][FID.MessageType].AsInteger() != (int)MessageType.LoginResponse) return 0; // something other than LoginResponse came from server, return // all usefull data are in folder Response - see the documentation SafeMessageFolder loginFolder = response[MessageFolderName.Response]; if (loginFolder[FID.LoginSucceed].AsBoolean()) return loginFolder[FID.LoginID].AsInteger(); else return 0; } public static bool IsErrorMessage(IMessage message) { if (message == null) return false; IMessageFolder header = message.GetFolder(MessageFolderName.Header); if (header == null) return false; IMessageItem messageType = header.GetItem(FID.MessageType); if (messageType == null) return false; return (messageType.AsInteger() == (int)MessageType.Error); } public static void PrintErrorMessage(IMessage message) { if (message == null) return; SafeMessage msg = new SafeMessage(message); Console.WriteLine("Error received!"); Console.WriteLine("Error message: {0}", msg[MessageFolderName.Error][FID.ErrorMessage].AsString()); Console.WriteLine("Error code: {0}", msg[MessageFolderName.Error][FID.ErrorCode].AsString()); Console.WriteLine("Extended error code: {0}", msg[MessageFolderName.Error][FID.ExtendedErrorCode].AsString()); } public static void PrintItems(IMessageItemIterator items, string preffix) { while (items.Fetch()) { Console.WriteLine("{0}{1} = {2}", preffix, items.Value.Id.ToString(), items.Value.AsString()); } } public static void PrintFolder(IMessageFolder folder, string preffix) { Console.WriteLine("{0}{1} [", preffix, folder.Name); string preff = " "; preff += preffix; PrintItems(folder.GetItems(), preff); IMessageFolderIterator children = folder.GetFolders(MessageFolderName.All); while (children.Fetch()) PrintFolder(children.Value, preff); Console.WriteLine("{0}]", preffix); } } }