using System; using System.Collections.Specialized; using System.Collections.Generic; using System.Text; using System.Data; using System.Xml; using System.IO; using System.Diagnostics; using System.Threading; using System.Globalization; namespace WmData { public class WmDataLinkServer { protected static CultureInfo cultureInfoUs = new CultureInfo("en-US"); protected int timeout; protected string serverUrl; protected bool isLocalServer; protected WmGenericData localData; public WmDataLinkServer(string serverUrl, bool isLocalServer, WmGenericData localData) { this.serverUrl = serverUrl; this.isLocalServer = isLocalServer; this.localData = localData; } public WmDataLinkRole getCurrentRole() { string commandUrl = serverUrl + "?command=CURRENT_ROLE"; WmDataLinkRole returnRole = WmDataLinkRole.UNAVAILABLE; try { string responseValue = WmDataTools.getInternetResponse(commandUrl, 2000); returnRole = (WmDataLinkRole)Enum.Parse(typeof(WmDataLinkRole),responseValue); }catch (Exception error) { Trace.WriteLine("WmDataLinkServer: Error while getting the current role from "+serverUrl+" " + error.Message); } return returnRole; } public WmDataTransaction[] getTransactionsSinceLastSync(long lastSyncMomentUtcTicks) { List transactions = new List(); string commandUrl = serverUrl + "?command=GET_TRANSACTIONS&since=" + lastSyncMomentUtcTicks; try { string responseValue = WmDataTools.getInternetResponse(commandUrl, 60000); responseValue = WmDataTools.decompress(responseValue); StringReader responseReader = new StringReader(responseValue); DataTable remoteTransactionTable = new DataTable(); remoteTransactionTable.ReadXml(responseReader); DataRow[] remoteTransactionRows = remoteTransactionTable.Select(); foreach (DataRow remoteTransactionRow in remoteTransactionRows) { WmDataTransaction remoteTransaction = (WmDataTransaction)localData.getObjectInstanceFromRow(typeof(WmDataTransaction), remoteTransactionRow); transactions.Add(remoteTransaction); } // System.Diagnostics.Trace.WriteLine("WmDataLinkServer: " + serverUrl + " has " + remoteTransactionTable.Rows.Count + " transactions since " + lastSyncMomentUtcTicks); } catch (Exception error) { Trace.WriteLine("WmDataLinkServer: Error while getting transactions from "+serverUrl+" " + error.Message); } return transactions.ToArray(); } public DataTable getRemoteDataTable(string tableName) { string commandUrl = serverUrl + "?command=GET_DATATABLE&table=" + tableName; bool didChangeCulture = false; CultureInfo originalCultureInfo = Thread.CurrentThread.CurrentCulture; if (!Thread.CurrentThread.CurrentCulture.Name.Equals("en-US")) { Thread.CurrentThread.CurrentCulture = cultureInfoUs; } DataTable returnTable = null; try { string responseValue = WmDataTools.getInternetResponse(commandUrl, 60000); responseValue = WmDataTools.decompress(responseValue); WmIntermediateObject intermediateObject = localData.IntermediateObjectManager.getExistingIntermediateObjectByTableName(tableName); returnTable = intermediateObject.DataTable; StringReader responseReader = new StringReader(responseValue); while (true) { string line = responseReader.ReadLine(); if (line == null) { break; } NameValueCollection nameValueCollection = WmDataTools.getNameValueCollectionFromQueryString(line); DataRow importedRow = returnTable.NewRow(); foreach (WmIntermediateObjectProperty objectProperty in intermediateObject.Properties) { importedRow[objectProperty.Name] = objectProperty.parseValueFromString(nameValueCollection[objectProperty.DataName]); } returnTable.Rows.Add(importedRow); } } catch (Exception error) { Trace.WriteLine("WmDataLinkServer: Error while getting remote data table. " + error.Message); } if (didChangeCulture) { Thread.CurrentThread.CurrentCulture = originalCultureInfo; } return returnTable; } public long getChecksum(string tableName) { string commandUrl = serverUrl + "?command=GET_CHECKSUM&table=" + tableName; long checksum = 0; try { string responseValue = WmDataTools.getInternetResponse(commandUrl, 10000); checksum = long.Parse(responseValue); } catch (Exception error) { Trace.WriteLine("WmDataLinkServer: Error while getting remote checksum. " + error.Message); } return checksum; } public int getCurrentMaximumId(string tableName) { string commandUrl = serverUrl + "?command=GET_CURRENT_MAXIMUM_ID&table=" + tableName; int checksum = 0; try { string responseValue = WmDataTools.getInternetResponse(commandUrl, 10000); checksum = int.Parse(responseValue); } catch (Exception error) { Trace.WriteLine("WmDataLinkServer: Error while getting remote current maximum id. " + error.Message); } return checksum; } public int getRowCount(string tableName) { string commandUrl = serverUrl + "?command=GET_ROW_COUNT&table=" + tableName; int rowCount = 0; try { string responseValue = WmDataTools.getInternetResponse(commandUrl, 10000); rowCount = int.Parse(responseValue); } catch (Exception error) { Trace.WriteLine("WmDataLinkServer: Error while getting remote row count. " + error.Message); } return rowCount; } public string ServerUrl { get { return serverUrl; } set { serverUrl = value; } } public int Timeout { get { return timeout; } set { timeout = value; } } public bool IsLocalServer { get { return isLocalServer; } set { isLocalServer = value; } } } }