Changeset 4695

Show
Ignore:
Timestamp:
06/11/08 21:33:41 (6 months ago)
Author:
xmlhacker
Message:

adds basic async functionality to the mix, though at present time there is a problem with exception handling that I
'm trying to decided how to best handle

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/nuxleus/Source/Nuxleus.Extension.Aws/Nuxleus.Extension.Aws.csproj

    r4693 r4695  
    459459  </ItemGroup> 
    460460  <ItemGroup> 
     461    <ProjectReference Include="..\Nuxleus.Extension.Linq\Nuxleus.Extension.Linq.csproj"> 
     462      <Project>{D937BA9E-1E6C-4757-98CC-7D932F4697B0}</Project> 
     463      <Name>Nuxleus.Extension.Linq</Name> 
     464    </ProjectReference> 
    461465    <ProjectReference Include="..\Nuxleus.Extension\Nuxleus.Extension.csproj"> 
    462466      <Project>{3EC9EEB0-71CA-4F71-BF8E-AAF0365D4234}</Project> 
    463467      <Name>Nuxleus.Extension</Name> 
     468    </ProjectReference> 
     469    <ProjectReference Include="..\Nuxleus.MetaData\Nuxleus.MetaData.csproj"> 
     470      <Project>{82ACAECC-C5E3-470D-91C4-9F6C43CCC1CC}</Project> 
     471      <Name>Nuxleus.MetaData</Name> 
    464472    </ProjectReference> 
    465473  </ItemGroup> 
  • trunk/nuxleus/Source/Nuxleus.Extension.Aws/SimpleDB/SdbAction.cs

    r4693 r4695  
    66using System.Security.Cryptography; 
    77using System.Globalization; 
     8using Nuxleus.MetaData; 
    89 
    910namespace Nuxleus.Extension.AWS.SimpleDB { 
    1011 
    11     public enum RequestType { Query, CreateDomain, DeleteDomain, ListDomains, PutAttributes, DeleteAttributes, GetAttributes } 
     12    public enum RequestType { 
     13        [Label("Query")] 
     14        Query, 
     15        [Label("CreateDomain")] 
     16        CreateDomain, 
     17        [Label("DeleteDomain")] 
     18        DeleteDomain, 
     19        [Label("ListDomains")] 
     20        ListDomains, 
     21        [Label("PutAttributes")] 
     22        PutAttributes, 
     23        [Label("DeleteAttributes")] 
     24        DeleteAttributes, 
     25        [Label("GetAttributes")]  
     26        GetAttributes  
     27    } 
    1228 
    1329    public struct SdbAction { 
  • trunk/nuxleus/Source/Nuxleus.Extension.Aws/SimpleDB/SimpleDBService.cs

    r4693 r4695  
    1111using System.Globalization; 
    1212using Nuxleus.Extension; 
     13using EeekSoft.Asynchronous; 
     14using System.Collections.Generic; 
     15using System.Threading; 
     16using Nuxleus.MetaData; 
     17using System.Collections; 
    1318 
    1419namespace Nuxleus.Extension.AWS.SimpleDB { 
    1520 
    16     public class SimpleDBService { 
    17  
    18         public SimpleDBService() { } 
     21    public struct SimpleDBService { 
    1922 
    2023        public XContainer GetMessage(RequestType requestType, params string[] paramArray) { 
     
    8588        public StreamReader MakeRequest(RequestType requestType, XContainer message) { 
    8689 
    87              
    88             string rType = String.Empty; 
    89             switch (requestType) { 
    90                 case RequestType.PutAttributes: 
    91                     rType = "PutAttributes"; 
    92                     break; 
    93                 case RequestType.ListDomains: 
    94                     rType = "ListDomains"; 
    95                     break; 
    96                 case RequestType.GetAttributes: 
    97                     rType = "GetAttributes"; 
    98                     break; 
    99                 case RequestType.DeleteDomain: 
    100                     rType = "DeleteDomain"; 
    101                     break; 
    102                 case RequestType.DeleteAttributes: 
    103                     rType = "DeleteAttributes"; 
    104                     break; 
    105                 case RequestType.CreateDomain: 
    106                     rType = "CreateDomain"; 
    107                     break; 
    108                 case RequestType.Query: 
    109                 default: 
    110                     rType = "Query"; 
    111                     break; 
    112             } 
     90            string rType = LabelAttribute.FromMember(requestType); 
    11391 
    11492            Encoding encoding = new UTF8Encoding(); 
     
    149127            } 
    150128        } 
     129 
     130        public static IEnumerable<IAsync> MakeSoapRequestAsync<T>(RequestType requestType, XContainer message, List<T> responseList) { 
     131 
     132            string rType = LabelAttribute.FromMember(requestType); 
     133 
     134            Encoding encoding = new UTF8Encoding(); 
     135 
     136            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://sdb.amazonaws.com/"); 
     137 
     138            request.Timeout = 10000 /*TODO: This should be set dynamically*/; 
     139            request.KeepAlive = true; 
     140            request.Pipelined = true; 
     141 
     142            XmlReader xreader = message.CreateReader(); 
     143            StringBuilder output = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
     144 
     145            byte[] buffer = null; 
     146 
     147            do { 
     148                if (xreader.IsStartElement()) { 
     149                    output.Append(xreader.ReadOuterXml()); 
     150                    Console.WriteLine("Output: {0} :/Output", output.ToString()); 
     151                    buffer = encoding.GetBytes(output.ToString()); 
     152                } 
     153            } while (xreader.Read()); 
     154 
     155            int contentLength = buffer.Length; 
     156            request.ContentLength = contentLength; 
     157            request.Method = "POST"; 
     158            request.ContentType = "application/soap+xml"; 
     159            request.Headers.Add("SOAPAction", rType); 
     160 
     161            request.Timeout = 10000 /*TODO: This should be set dynamically*/; 
     162            request.KeepAlive = true; 
     163            request.Pipelined = true; 
     164 
     165            Console.WriteLine("[] starting on thread: {0}", Thread.CurrentThread.ManagedThreadId); 
     166 
     167            using (Stream newStream = request.GetRequestStream()) { 
     168                try { 
     169                    newStream.Write(buffer, 0, contentLength); 
     170                    Async<WebResponse> response = request.GetResponseAsync(); 
     171                    yield return response; 
     172                    Console.WriteLine("[] got response on thread: {0}", Thread.CurrentThread.ManagedThreadId); 
     173                    Stream stream = response.Result.GetResponseStream(); 
     174                    Async<T> responseObject = stream.ReadToEndAsync(typeof(T)).ExecuteAsync<T>(); 
     175                    yield return responseObject; 
     176                    responseList.Add(responseObject.Result); 
     177                    //} finally (System.Net.WebException e) { 
     178                    //    Console.WriteLine("Failed! Reason: {0}, Message: {1}", e.Response, e.Message); 
     179                    //    return new StreamReader(e.Response.GetResponseStream()); 
     180                    //} 
     181                } finally { 
     182                     
     183                } 
     184            } 
     185 
     186            Console.WriteLine("Current thread id: {0}", Thread.CurrentThread.ManagedThreadId); 
     187             
     188        } 
    151189    } 
    152190} 
  • trunk/nuxleus/Source/Nuxleus.Extension.Linq/Async.cs

    r4592 r4695  
    1717    /// </summary> 
    1818    public class Unit { 
    19         private Unit () { } 
    20         static Unit () { 
     19        private Unit() { } 
     20        static Unit() { 
    2121            Value = new Unit(); 
    2222        } 
     
    3636    public class Result<T> : IAsync { 
    3737        public T ReturnValue { get; private set; } 
    38         public Result ( T value ) { 
     38        public Result(T value) { 
    3939            ReturnValue = value; 
    4040        } 
    4141 
    42         public void ExecuteStep ( Action cont ) { 
     42        public void ExecuteStep(Action cont) { 
    4343            throw new InvalidOperationException 
    4444                ("Cannot call ExecuteStep on IAsync created as a 'Result'."); 
     
    5656        /// Asynchronously gets response from the internet using BeginGetResponse method. 
    5757        /// </summary> 
    58         public static Async<WebResponse> GetResponseAsync ( this WebRequest req ) { 
     58        public static Async<WebResponse> GetResponseAsync(this WebRequest req) { 
    5959            return new AsyncPrimitive<WebResponse>(req.BeginGetResponse, req.EndGetResponse); 
    6060        } 
     
    6868        /// <param name="count">Maximum number of bytes to read</param> 
    6969        /// <returns>Returns non-zero if there are still some data to read</returns> 
    70         public static Async<int> ReadAsync ( this Stream stream, byte[] buffer, int offset, int count ) { 
     70        public static Async<int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count) { 
    7171            return new AsyncPrimitive<int>( 
    72                 ( callback, st ) => stream.BeginRead(buffer, offset, count, callback, st), 
     72                (callback, st) => stream.BeginRead(buffer, offset, count, callback, st), 
    7373                stream.EndRead); 
    7474        } 
     
    7979        /// </summary> 
    8080        /// <returns>Returns string using the 'Result' class.</returns> 
    81         public static IEnumerable<IAsync> ReadToEndAsync ( this Stream stream ) { 
    82             return stream.ReadToEndAsync(ReturnType.String); 
     81        public static IEnumerable<IAsync> ReadToEndAsync(this Stream stream) { 
     82            return stream.ReadToEndAsync(typeof(String)); 
    8383        } 
    8484 
     
    8888        /// </summary> 
    8989        /// <param name="returnType">Specifies the desired return type.  The default is System.String.</param> 
    90         public static IEnumerable<IAsync> ReadToEndAsync ( this Stream stream, ReturnType returnType ) { 
     90        public static IEnumerable<IAsync> ReadToEndAsync(this Stream stream, Type returnType) { 
    9191            MemoryStream ms = new MemoryStream(); 
    9292            int read = -1; 
     
    103103            ms.Seek(0, SeekOrigin.Begin); 
    104104 
    105             switch (returnType) { 
    106                 case ReturnType.XmlReader
     105            switch (returnType.FullName) { 
     106                case "System.Xml.XmlReader"
    107107                    yield return new Result<XmlReader>(XmlReader.Create(ms)); 
    108108                    break; 
    109                 case ReturnType.XDocument
     109                case "System.Xml.Linq.XDocument"
    110110                    yield return new Result<XDocument>(XDocument.Parse(new StreamReader(ms).ReadToEnd())); 
    111111                    break; 
    112                 case ReturnType.XElement
     112                case "System.Xml.Linq.XElement"
    113113                    yield return new Result<XElement>(XElement.Parse(new StreamReader(ms).ReadToEnd())); 
    114114                    break; 
    115                 case ReturnType.XStreamingElement
     115                case "System.Xml.Linq.XStreamingElement"
    116116                    yield return new Result<XStreamingElement>(new XStreamingElement("Result", XElement.Parse(new StreamReader(ms).ReadToEnd()))); 
    117117                    break; 
    118                 case ReturnType.XNode
     118                case "System.Xml.Linq.XNode"
    119119                    yield return new Result<XNode>(XNode.ReadFrom(XmlReader.Create(ms))); 
    120120                    break; 
    121                 case ReturnType.String
     121                case "System.String"
    122122                default: 
    123123                    string s = new StreamReader(ms).ReadToEnd(); 
     
    135135        /// </summary> 
    136136        /// <param name="async"></param> 
    137         public static void ExecuteAndWait ( this IEnumerable<IAsync> async ) { 
     137        public static void ExecuteAndWait(this IEnumerable<IAsync> async) { 
    138138            ManualResetEvent wh = new ManualResetEvent(false); 
    139139            AsyncExtensions.Run(async.GetEnumerator(), 
     
    147147        /// </summary> 
    148148        /// <param name="async"></param> 
    149         public static void Execute ( this IEnumerable<IAsync> async ) { 
     149        public static void Execute(this IEnumerable<IAsync> async) { 
    150150            AsyncExtensions.Run(async.GetEnumerator()); 
    151151        } 
     
    155155        /// and assumes that the method returns result of type T. 
    156156        /// </summary> 
    157         public static Async<T> ExecuteAsync<T> ( this IEnumerable<IAsync> async ) { 
     157        public static Async<T> ExecuteAsync<T>(this IEnumerable<IAsync> async) { 
    158158            return new AsyncWithResult<T>(async); 
    159159        } 
     
    163163        /// and assumes that the method doesn't return any result. 
    164164        /// </summary> 
    165         public static Async<Unit> ExecuteAsync ( this IEnumerable<IAsync> async ) { 
     165        public static Async<Unit> ExecuteAsync(this IEnumerable<IAsync> async) { 
    166166            return new AsyncWithUnitResult(async); 
    167167        } 
     
    171171        #region Implementation 
    172172 
    173         internal static void Run<T> ( IEnumerator<IAsync> en, Action<T> cont ) { 
     173        internal static void Run<T>(IEnumerator<IAsync> en, Action<T> cont) { 
    174174            if (!en.MoveNext()) 
    175175                throw new InvalidOperationException("Asynchronous workflow executed using" 
    176                                        + "'AsyncWithResult' didn't return result using 'Result'!"); 
     176                    + "'AsyncWithResult' didn't return result using 'Result'!"); 
    177177 
    178178            var res = (en.Current as Result<T>); 
     
    183183        } 
    184184 
    185         internal static void Run ( IEnumerator<IAsync> en, Action cont ) { 
     185        internal static void Run(IEnumerator<IAsync> en, Action cont) { 
    186186            if (!en.MoveNext()) { cont(); return; } 
    187187            en.Current.ExecuteStep 
     
    189189        } 
    190190 
    191         internal static void Run ( IEnumerator<IAsync> en ) { 
     191        internal static void Run(IEnumerator<IAsync> en) { 
    192192            if (!en.MoveNext()) 
    193193                return; 
     
    208208        /// when executed - executes the methods in parallel. 
    209209        /// </summary> 
    210         public static Async<Unit> Parallel ( params IEnumerable<IAsync>[] operations ) { 
    211             return new AsyncPrimitive<Unit>(( cont ) => { 
     210        public static Async<Unit> Parallel(params IEnumerable<IAsync>[] operations) { 
     211            return new AsyncPrimitive<Unit>((cont) => { 
    212212                bool[] completed = new bool[operations.Length]; 
    213213                for (int i = 0; i < operations.Length; i++) 
     
    218218        #region Implementation 
    219219 
    220         private static IEnumerable<IAsync> ExecuteAndSet ( IEnumerable<IAsync> op, bool[] flags, int index, Action<Unit> cont ) { 
     220        private static IEnumerable<IAsync> ExecuteAndSet(IEnumerable<IAsync> op, bool[] flags, int index, Action<Unit> cont) { 
    221221            foreach (IAsync async in op) 
    222222                yield return async; 
     
    239239    /// </summary> 
    240240    public interface IAsync { 
    241         void ExecuteStep ( Action cont ); 
     241        void ExecuteStep(Action cont); 
    242242    } 
    243243 
     
    257257        } 
    258258 
    259         abstract public void ExecuteStep ( Action cont ); 
     259        abstract public void ExecuteStep(Action cont); 
    260260    } 
    261261 
     
    263263        Action<Action<T>> func; 
    264264 
    265         public AsyncPrimitive ( Action<Action<T>> function ) { 
     265        public AsyncPrimitive(Action<Action<T>> function) { 
    266266            this.func = function; 
    267267        } 
    268268 
    269         public AsyncPrimitive ( Func<AsyncCallback, object, IAsyncResult> begin, Func<IAsyncResult, T> end ) { 
    270             this.func = ( cont ) => begin(delegate( IAsyncResult res ) { cont(end(res)); }, null); 
    271         } 
    272  
    273         public override void ExecuteStep ( Action cont ) { 
    274             func(( res ) => { 
     269        public AsyncPrimitive(Func<AsyncCallback, object, IAsyncResult> begin, Func<IAsyncResult, T> end) { 
     270            this.func = (cont) => begin(delegate(IAsyncResult res) { cont(end(res)); }, null); 
     271        } 
     272 
     273        public override void ExecuteStep(Action cont) { 
     274            func((res) => { 
    275275                result = res; 
    276276                completed = true; 
     
    283283        IEnumerable<IAsync> en; 
    284284 
    285         public AsyncWithResult ( IEnumerable<IAsync> async ) { 
     285        public AsyncWithResult(IEnumerable<IAsync> async) { 
    286286            en = async; 
    287287        } 
    288288 
    289         public override void ExecuteStep ( Action cont ) { 
    290             AsyncExtensions.Run<T>(en.GetEnumerator(), ( res ) => { 
     289        public override void ExecuteStep(Action cont) { 
     290            AsyncExtensions.Run<T>(en.GetEnumerator(), (res) => { 
    291291                completed = true; 
    292292                result = res; 
     
    299299        IEnumerable<IAsync> en; 
    300300 
    301         public AsyncWithUnitResult ( IEnumerable<IAsync> async ) { 
     301        public AsyncWithUnitResult(IEnumerable<IAsync> async) { 
    302302            en = async; 
    303303            result = Unit.Value; 
    304304        } 
    305305 
    306         public override void ExecuteStep ( Action cont ) { 
     306        public override void ExecuteStep(Action cont) { 
    307307            AsyncExtensions.Run(en.GetEnumerator(), () => { 
    308308                completed = true; 
  • trunk/nuxleus/Source/Nuxleus.MetaData/Nuxleus.MetaData.csproj

    r4649 r4695  
    33    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> 
    44    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> 
    5     <ProductVersion>9.0.20706</ProductVersion> 
     5    <ProductVersion>9.0.30428</ProductVersion> 
    66    <SchemaVersion>2.0</SchemaVersion> 
    77    <ProjectGuid>{82ACAECC-C5E3-470D-91C4-9F6C43CCC1CC}</ProjectGuid> 
     
    4646  <ItemGroup> 
    4747    <Compile Include="Agent.cs" /> 
     48    <Compile Include="Attributes\Label.cs" /> 
    4849    <Compile Include="Attributes\Message.cs" /> 
    4950    <Compile Include="Properties\AssemblyInfo.cs" /> 
  • trunk/nuxleus/Source/nux.build

    r4693 r4695  
    308308    </csc> 
    309309  </target> 
    310   <target name="Nuxleus.Extension.Aws" depends="init Nuxleus.Extension"> 
     310  <target name="Nuxleus.Extension.Aws" depends="init Nuxleus.Extension Nuxleus.Extension.Linq Nuxleus.MetaData"> 
    311311    <csc target="library" output="${build.dir}/Nuxleus.Extension.Aws.dll" keyfile="./Dependencies/Extf.Net.snk"> 
    312312      <arg if="${platform::is-unix()}" value="-langversion:linq"/> 
     
    316316      <references> 
    317317        <include name="${build.dir}/Nuxleus.Extension.dll"/> 
     318        <include name="${build.dir}/Nuxleus.Extension.Linq.dll"/> 
     319        <include name="${build.dir}/Nuxleus.MetaData.dll"/> 
    318320        <include name="System.dll"/> 
    319321        <include name="System.Collections.dll"/> 
     
    893895      <references> 
    894896        <include name="${build.dir}/Nuxleus.Extension.Aws.dll"/> 
    895         <include name="System.dll"/> 
    896         <include name="/usr/lib/mono/3.0/System.ServiceModel.dll"/> 
     897        <include name="${build.dir}/Nuxleus.Extension.Linq.dll"/> 
     898        <include name="System.dll"/> 
     899        <include name="System.Xml.Linq.dll"/> 
    897900        <include name="System.Xml.dll"/> 
    898901        <include name="System.Web.dll"/>