/** * <summary> * Converts byte array into Guid.</summary> * * <param name="id">Guid to convert.</param> * <returns>Converted Guid.</returns> */ private static Guid WrapGuid(ByteString id) { Dbg.Assert(id != null, "id != null"); Dbg.Assert(id.Length == 16, "id.Length == " + id.Length); return(U.BytesToGuid(id.ToByteArray(), 0)); }
/** * <summary> * Tries to send packet over network.</summary> * * <param name="msg">Message being sent.</param> * <exception cref="IOException">If client was closed before message was sent over network.</exception> */ private void sendPacket(GridClientRequest msg) { try { byte[] data = marshaller.Marshal(msg); byte[] size = U.ToBytes(40 + data.Length); byte[] head = new byte[1 + size.Length + 40 + data.Length]; // Prepare packet. head[0] = (byte)0x90; Array.Copy(size, 0, head, 1, 4); Array.Copy(GridClientUtils.ToBytes(msg.RequestId), 0, head, 5, 8); Array.Copy(GridClientUtils.ToBytes(msg.ClientId), 0, head, 13, 16); Array.Copy(GridClientUtils.ToBytes(msg.DestNodeId), 0, head, 29, 16); Array.Copy(data, 0, head, 45, data.Length); // Enqueue packet to send. lock (stream) { stream.Write(head, 0, head.Length); stream.Flush(); } lastPacketSndTime = U.Now; } catch (IOException e) { // In case of IOException we should shutdown the whole client since connection is broken. Close(false); throw new GridClientConnectionResetException("Failed to send message over network (will try to " + "reconnect): " + ServerAddress, e); } }
/** <summary>Closes all resources and fails all pending requests.</summary> */ private void shutdown() { Dbg.Assert(closed); Dbg.Assert(!Thread.CurrentThread.Equals(rdr)); rdr.Interrupt(); writer.Interrupt(); rdr.Join(); writer.Join(); lock (outStream) { // It's unclear from documentation why this call is needed, // but without it we get IOException on the server. U.DoSilent <Exception>(() => tcp.Client.Disconnect(false), null); U.DoSilent <Exception>(() => outStream.Close(), null); U.DoSilent <Exception>(() => tcp.Close(), null); } foreach (GridClientFuture fut in pendingReqs.Values) { fut.Fail(() => { throw new GridClientClosedException("Failed to perform request" + " (connection was closed before response was received): " + ServerAddress); }); } Dbg.WriteLine("Connection closed: " + ServerAddress); }
/** <summary>Writer thread.</summary> */ private void writePackets() { try { bool take = true; while (true) { MemoryStream msg; if (take) { msg = q.Take(); take = false; } else { q.TryTake(out msg); } if (msg == null) { take = true; lock (outStream) { outStream.Flush(); } continue; } lock (outStream) { msg.WriteTo(outStream); } } } catch (IOException e) { if (!closed) { Dbg.WriteLine("Failed to write data to socket (will close connection)" + " [addr=" + ServerAddress + ", e=" + e.Message + "]"); } } catch (Exception e) { if (!closed) { Dbg.WriteLine("Unexpected throwable in connection writer thread (will close connection)" + " [addr={0}, e={1}]", ServerAddress, e); } } finally { U.Async(() => Close(false)); } }
/** * <summary> * Thread that checks opened client connections for idle * and closes connections that idle for a long time.</summary> */ private void checkIdle() { while (!closed && U.Sleep(IDLE_CHECK_DELAY)) { try { connMgr.closeIdle(cfg.ConnectionIdleTimeout); } catch (ThreadInterruptedException) { break; } } }
/** * <summary> * Finishes user request to the grid.</summary> * * <param name="fut">Future used to pass result to the user.</param> * <param name="resp">Incoming response message.</param> */ private void finishRequest(GridClientFuture fut, GridClientResponse resp) { switch (resp.Status) { case GridClientResponseStatus.Success: fut.Done(resp.Result); break; case GridClientResponseStatus.Failed: var error = resp.ErrorMessage; if (error == null) { error = "Unknown server error"; } fut.Fail(() => { throw new GridClientException(error); }); break; case GridClientResponseStatus.AuthFailure: fut.Fail(() => { throw new GridClientAuthenticationException("Authentication failed (session expired?)" + " errMsg=" + resp.ErrorMessage + ", [srv=" + ServerAddress + "]"); }); U.Async(() => Close(false)); break; case GridClientResponseStatus.AuthorizationFailure: fut.Fail(() => { throw new GridClientException("Client authorization failed: " + resp.ErrorMessage); }); break; default: fut.Fail(() => { throw new GridClientException("Unknown server response status code: " + resp.Status); }); break; } pendingReqs.Remove(resp.RequestId); }
/** * <summary> * Convert the map representation of the cache metrics to an equivalent objects.</summary> * * <param name="data">Map of raw cache metrics.</param> * <returns>Converted cache metrics.</returns> */ protected IGridClientDataMetrics parseCacheMetrics(IDictionary <String, Object> data) { var m = new GridClientDataMetrics(); m.CreateTime = U.Timestamp(asLong(data["createTime"])); m.WriteTime = U.Timestamp(asLong(data["writeTime"])); m.ReadTime = U.Timestamp(asLong(data["readTime"])); m.Reads = asLong(data["reads"]); m.Writes = asLong(data["writes"]); m.Hits = asLong(data["hits"]); m.Misses = asLong(data["misses"]); return(m); }
/** * <summary> * Thread that updates topology according to refresh interval specified * in configuration.</summary> */ private void updateTopology() { IGridClientCompute topPrj = new GridClientComputeImpl(this, null, null, topUpdateBalancer); while (!closed && U.Sleep(cfg.TopologyRefreshFrequency)) { try { topPrj.RefreshTopology(false, false); } catch (GridClientException e) { Dbg.WriteLine("Failed to update topology: " + e.Message); } catch (ThreadInterruptedException) { break; } } }
/** * <summary> * Creates new future and passes it to the MakeJettyRequest.</summary> * * <param name="args">Request parameters.</param> * <param name="destNodeId">Node ID to route request to.</param> * <param name="converter">Response converter to pass into generated future.</param> * <returns>Future.</returns> * <exception cref="GridClientClosedException">If client was manually closed.</exception> */ private IGridClientFuture <T> MakeJettyRequest <T>(Guid destNodeId, IDictionary <String, Object> args, Func <Object, T> converter) { Dbg.Assert(args != null); Dbg.Assert(args.ContainsKey("cmd")); if (destNodeId != null && destNodeId != Guid.Empty) { args.Add("destId", destNodeId.ToString()); } var fut = new GridClientFuture <T>(); fut.DoneConverter = converter; busyLock.AcquireReaderLock(Timeout.Infinite); try { if (closed) { throw new GridClientConnectionResetException("Failed to perform request (connection was closed" + " before request is sent): " + ServerAddress); } lock (pendingReqs) { pendingReqs.Add(fut); } U.Async(() => { try { OnResponse(args, fut, LoadString(args, sessionToken)); } catch (Exception e) { fut.Fail(() => { throw new GridClientException(e.Message, e); }); } }); return(fut); } finally { busyLock.ReleaseReaderLock(); } }
/** * <summary> * Creates a sub-projection for current projection.</summary> * * <param name="nodes">Collection of nodes that sub-projection will be restricted to. If <c>null</c>,</param> * created projection is dynamic and will take nodes from topology. * <param name="filter">Filter to be applied to nodes in projection.</param> * <param name="balancer">Balancer to use in projection.</param> * <returns>Created projection.</returns> * <exception cref="GridClientException"> * If resulting projection is empty. Note that this exception may only be thrown on * case of static projections, i.e. where collection of nodes is not null.</exception> */ protected T CreateProjection(ICollection <N> nodes, Predicate <N> filter, IGridClientLoadBalancer balancer) { if (nodes != null && nodes.Count == 0) { throw new GridClientException("Failed to create projection: given nodes collection is empty."); } if (filter != null && this._filter != null) { filter = U.And <N>(this._filter, filter); } else if (filter == null) { filter = this._filter; } ICollection <N> subset = Intersection(this._nodes, nodes); if (subset != null && subset.Count == 0) { throw new GridClientException("Failed to create projection (given node set does not overlap with " + "existing node set) [prjNodes=" + this._nodes + ", nodes=" + nodes); } if (filter != null && subset != null) { subset = U.ApplyFilter(subset, filter); if (subset != null && subset.Count == 0) { throw new GridClientException("Failed to create projection (none of the nodes in projection node " + "set passed the filter) [prjNodes=" + subset + ", filter=" + filter + ']'); } } if (balancer == null) { balancer = this._balancer; } return(CreateProjectionImpl(nodes, filter, balancer)); }
/** * <summary> * Gets most recently refreshed topology. If this compute instance is a projection, * then only nodes that satisfy projection criteria will be returned.</summary> * * <returns>Most recently refreshed topology.</returns> */ protected IList <N> ProjectionNodes() { IList <N> prjNodes; if (_nodes == null) { // Dynamic projection, ask topology for current snapshot. prjNodes = cfg.Topology.Nodes(); if (_filter != null) { prjNodes = U.ApplyFilter(prjNodes, _filter); } } else { prjNodes = _nodes; } return(prjNodes); }
/** * <summary> * Tries to send packet over network.</summary> * * <param name="msg">Message being sent.</param> * <exception cref="IOException">If client was closed before message was sent over network.</exception> */ private void sendPacket(GridClientRequest msg) { try { MemoryStream buf = new MemoryStream(1024); // Header. buf.WriteByte((byte)0x90); // Reserve place for size. buf.WriteByte((byte)0); buf.WriteByte((byte)0); buf.WriteByte((byte)0); buf.WriteByte((byte)0); buf.Write(GridClientUtils.ToBytes(msg.RequestId), 0, 8); buf.Write(GridClientUtils.ToBytes(msg.ClientId), 0, 16); buf.Write(GridClientUtils.ToBytes(msg.DestNodeId), 0, 16); marshaller.Marshal(msg, buf); int len = (int)buf.Length; buf.Seek(1, SeekOrigin.Begin); buf.Write(U.ToBytes(len - 1 - 4), 0, 4); buf.Position = len; q.Add(buf); } catch (Exception e) { // In case of Exception we should shutdown the whole client since connection is broken // and future for this reques will never be completed. Close(false); throw new GridClientConnectionResetException("Failed to send message over network (will try to " + "reconnect): " + ServerAddress, e); } }
/** <summary>Reader thread.</summary> */ private void readPackets() { try { bool running = true; while (running) { // Note that only this thread removes futures from map. // So if we see closed condition, it is safe to check map size since no more futures // will be added to the map. if (closed) { // Exit if either all requests processed or we do not wait for completion. if (!waitCompletion) { break; } lock (pendingReqs) { if (pendingReqs.Count == 0) { break; } } } // Header. int symbol; try { symbol = readByte(); } catch (TimeoutException) { checkPing(); continue; } // Connection closed. if (symbol == -1) { Dbg.WriteLine("Connection closed by remote host " + "[srvAddr=" + ServerAddress + ", symbol=" + symbol + "]"); break; } // Check for correct header. if ((byte)symbol != (byte)0x90) { Dbg.WriteLine("Failed to parse incoming message (unexpected header received, will close) " + "[srvAddr=" + ServerAddress + ", symbol=" + symbol + "]"); break; } // Packet. MemoryStream buf = new MemoryStream(); int len = 0; while (true) { try { symbol = readByte(); } catch (TimeoutException) { checkPing(); continue; } if (symbol == -1) { running = false; break; } byte b = (byte)symbol; buf.WriteByte(b); if (len == 0) { if (buf.Length == 4) { len = U.BytesToInt32(buf.ToArray(), 0); // Reset buffer. buf.SetLength(0); if (len == 0) { // Ping received. lastPingRcvTime = U.Now; break; } } } else { if (buf.Length == len) { if (len < 40) { Dbg.WriteLine("Invalid packet received [len=" + len + ", buf=" + buf + "]"); break; } buf.Position = 0; // Rewind buffer. byte[] head = new byte[40]; byte[] packet = new byte[len - head.Length]; buf.Read(head, 0, head.Length); buf.Read(packet, 0, packet.Length); GridClientResponse msg = marshaller.Unmarshal <GridClientResponse>(packet); msg.RequestId = U.BytesToInt64(head, 0); msg.ClientId = U.BytesToGuid(head, 8); msg.DestNodeId = U.BytesToGuid(head, 24); // Reset buffer. buf.SetLength(0); len = 0; lastPacketRcvTime = U.Now; handleResponse(msg); break; } } } } } catch (IOException e) { if (!closed) { Dbg.WriteLine("Failed to read data from remote host (will close connection)" + " [addr=" + ServerAddress + ", e=" + e.Message + "]"); } } catch (Exception e) { Dbg.WriteLine("Unexpected throwable in connection reader thread (will close connection)" + " [addr={0}, e={1}]", ServerAddress, e); } finally { U.Async(() => Close(false)); } }
/** * <summary> * Converts java object to a protocol-understandable format.</summary> * * <param name="val">Value to convert.</param> * <returns>Wrapped protocol message object.</returns> */ public static ObjectWrapper WrapObject(Object val) { ObjectWrapper.Builder builder = ObjectWrapper.CreateBuilder(); // Primitives. if (val == null) { builder .SetType(ObjectWrapperType.NONE) .SetBinary(ByteString.Empty); } else if (val is bool) { builder .SetType(ObjectWrapperType.BOOL) .SetBinary(ByteString.CopyFrom(new byte[] { (byte)((bool)val == true ? 1 : 0) })); } else if (val is byte) { builder .SetType(ObjectWrapperType.BYTE) .SetBinary(ByteString.CopyFrom(new byte[] { (byte)val })); } else if (val is short) { builder .SetType(ObjectWrapperType.SHORT) .SetBinary(ByteString.CopyFrom(U.ToBytes((short)val))); } else if (val is int) { builder .SetType(ObjectWrapperType.INT32) .SetBinary(ByteString.CopyFrom(U.ToBytes((int)val))); } else if (val is long) { builder .SetType(ObjectWrapperType.INT64) .SetBinary(ByteString.CopyFrom(U.ToBytes((long)val))); } else if (val is float) { builder .SetType(ObjectWrapperType.FLOAT) .SetBinary(ByteString.CopyFrom(U.ToBytes((float)val))); } else if (val is double) { builder .SetType(ObjectWrapperType.DOUBLE) .SetBinary(ByteString.CopyFrom(U.ToBytes((double)val))); } else if (val is string) { builder .SetType(ObjectWrapperType.STRING) .SetBinary(ByteString.CopyFromUtf8((string)val)); } else if (val is Guid) { builder .SetType(ObjectWrapperType.UUID) .SetBinary(WrapGuid((Guid)val)); } else if (val is byte[]) { builder .SetType(ObjectWrapperType.BYTES) .SetBinary(ByteString.CopyFrom((byte[])val)); } // Common objects. else if (val is sc::IDictionary) { // Note! Map's check should goes BEFORE collections check due to IDictionary is inherited from ICollection. builder .SetType(ObjectWrapperType.MAP) .SetBinary(WrapMap((sc::IDictionary)val).ToByteString()); } else if (val is sc::ICollection) { builder .SetType(ObjectWrapperType.COLLECTION) .SetBinary(WrapCollection((sc::ICollection)val).ToByteString()); } // Requests. else if (val is GridClientAuthenticationRequest) { builder .SetType(ObjectWrapperType.AUTH_REQUEST) .SetBinary(WrapAuthRequest((GridClientAuthenticationRequest)val).ToByteString()); } else if (val is GridClientCacheRequest) { builder .SetType(ObjectWrapperType.CACHE_REQUEST) .SetBinary(WrapCacheRequest((GridClientCacheRequest)val).ToByteString()); } else if (val is GridClientTaskRequest) { builder .SetType(ObjectWrapperType.TASK_REQUEST) .SetBinary(WrapTaskRequest((GridClientTaskRequest)val).ToByteString()); } else if (val is GridClientLogRequest) { builder .SetType(ObjectWrapperType.LOG_REQUEST) .SetBinary(WrapLogRequest((GridClientLogRequest)val).ToByteString()); } else if (val is GridClientTopologyRequest) { builder .SetType(ObjectWrapperType.TOPOLOGY_REQUEST) .SetBinary(WrapTopologyRequest((GridClientTopologyRequest)val).ToByteString()); } // Responses. else if (val is GridClientResponse) { builder .SetType(ObjectWrapperType.RESPONSE) .SetBinary(WrapResultBean((GridClientResponse)val).ToByteString()); } else if (val is GridClientNodeBean) { builder .SetType(ObjectWrapperType.NODE_BEAN) .SetBinary(WrapNode((GridClientNodeBean)val).ToByteString()); } else if (val is GridClientTaskResultBean) { builder .SetType(ObjectWrapperType.TASK_BEAN) .SetBinary(WrapTaskResult((GridClientTaskResultBean)val).ToByteString()); } // To-string conversion for special cases. else if (val is Enum) { builder .SetType(ObjectWrapperType.STRING) .SetBinary(ByteString.CopyFromUtf8(Enum.GetName(val.GetType(), val))); } else if (val is System.Net.IPAddress) { builder .SetType(ObjectWrapperType.STRING) .SetBinary(ByteString.CopyFromUtf8(val.ToString())); } // In all other cases. else { throw new ArgumentException("Failed to serialize object (object serialization" + " of given type is not supported): " + val.GetType()); } return(builder.Build()); }
/** * <summary> * Converts Guid to a byte array.</summary> * * <param name="id">Guid to convert.</param> * <returns>Converted bytes.</returns> */ private static ByteString WrapGuid(Guid id) { return(ByteString.CopyFrom(U.ToBytes(id))); }
/** * <summary> * Convert the map representation of the node metrics to an equivalent objects.</summary> * * <param name="data">Map of raw node metrics.</param> * <returns>Converted node metrics.</returns> */ protected IGridClientNodeMetrics parseNodeMetrics(IDictionary <String, Object> data) { GridClientNodeMetrics m = new GridClientNodeMetrics(); m.StartTime = U.Timestamp(safeLong(data, "startTime")); m.WaitingJobs.Average = safeDouble(data, "averageWaitingJobs"); m.WaitingJobs.Current = safeLong(data, "currentWaitingJobs"); m.WaitingJobs.Maximum = safeLong(data, "maximumWaitingJobs"); m.WaitingJobs.Total = m.WaitingJobs.Maximum; m.ExecutedJobs.Average = safeDouble(data, "averageActiveJobs"); m.ExecutedJobs.Current = safeLong(data, "currentActiveJobs"); m.ExecutedJobs.Maximum = safeLong(data, "maximumActiveJobs"); m.ExecutedJobs.Total = safeLong(data, "totalExecutedJobs"); m.RejectedJobs.Average = safeDouble(data, "averageRejectedJobs"); m.RejectedJobs.Current = safeLong(data, "currentRejectedJobs"); m.RejectedJobs.Maximum = safeLong(data, "maximumRejectedJobs"); m.RejectedJobs.Total = safeLong(data, "totalRejectedJobs"); m.CancelledJobs.Average = safeDouble(data, "averageCancelledJobs"); m.CancelledJobs.Current = safeLong(data, "currentCancelledJobs"); m.CancelledJobs.Maximum = safeLong(data, "maximumCancelledJobs"); m.CancelledJobs.Total = safeLong(data, "totalCancelledJobs"); m.JobWaitTime.Average = TimeSpan.FromMilliseconds(safeDouble(data, "averageJobWaitTime")); m.JobWaitTime.Current = TimeSpan.FromMilliseconds(safeDouble(data, "currentJobWaitTime")); m.JobWaitTime.Maximum = TimeSpan.FromMilliseconds(safeDouble(data, "maximumJobWaitTime")); m.JobWaitTime.Total = TimeSpan.FromMilliseconds(m.JobWaitTime.Average.TotalMilliseconds * m.ExecutedJobs.Total); m.JobExecuteTime.Average = TimeSpan.FromMilliseconds(safeDouble(data, "averageJobExecuteTime")); m.JobExecuteTime.Current = TimeSpan.FromMilliseconds(safeDouble(data, "currentJobExecuteTime")); m.JobExecuteTime.Maximum = TimeSpan.FromMilliseconds(safeDouble(data, "maximumJobExecuteTime")); m.JobExecuteTime.Total = TimeSpan.FromMilliseconds(m.JobExecuteTime.Average.TotalMilliseconds * m.ExecutedJobs.Total); m.StartTime = U.Timestamp(safeLong(data, "startTime")); m.NodeStartTime = U.Timestamp(safeLong(data, "nodeStartTime")); m.UpTime = TimeSpan.FromMilliseconds(safeLong(data, "upTime")); m.LastUpdateTime = U.Timestamp(safeLong(data, "lastUpdateTime")); m.IdleTimeTotal = TimeSpan.FromMilliseconds(safeLong(data, "totalIdleTime")); //m.IdleTimeCurrent = (safeLong(data, "currentIdleTime")); m.CpuCount = (int)safeLong(data, "totalCpus"); m.CpuAverageLoad = safeDouble(data, "averageCpuLoad"); m.CpuCurrentLoad = safeLong(data, "currentCpuLoad"); m.FileSystemFreeSpace = safeLong(data, "fileSystemFreeSpace"); m.FileSystemTotalSpace = safeLong(data, "fileSystemTotalSpace"); m.FileSystemUsableSpace = safeLong(data, "fileSystemUsableSpace"); m.HeapMemoryInitialized = safeLong(data, "heapMemoryInitialized"); m.HeapMemoryUsed = safeLong(data, "heapMemoryUsed"); m.HeapMemoryCommitted = safeLong(data, "heapMemoryCommitted"); m.HeapMemoryMaximum = safeLong(data, "heapMemoryMaximum"); m.NonHeapMemoryInitialized = safeLong(data, "nonHeapMemoryInitialized"); m.NonHeapMemoryUsed = safeLong(data, "nonHeapMemoryUsed"); m.NonHeapMemoryCommitted = safeLong(data, "nonHeapMemoryCommitted"); m.NonHeapMemoryMaximum = safeLong(data, "nonHeapMemoryMaximum"); m.ThreadCount.Current = safeLong(data, "currentThreadCount"); m.ThreadCount.Maximum = safeLong(data, "maximumThreadCount"); m.ThreadCount.Total = safeLong(data, "totalStartedThreadCount"); m.DaemonThreadCount = safeLong(data, "currentDaemonThreadCount"); m.LastDataVersion = safeLong(data, "lastDataVersion"); return(m); }
/** * <summary> * Converts protocol object into object.</summary> * * <param name="val">Protocol message object to convert into value.</param> * <returns>Recovered object.</returns> */ public static Object WrapObject(ObjectWrapper val) { byte[] bin = val.Binary.ToByteArray(); // Primitives. switch (val.Type) { case ObjectWrapperType.NONE: return(null); case ObjectWrapperType.BOOL: Dbg.Assert(bin.Length == 1, "bin.Length == 1"); return(bin[0] != 0); case ObjectWrapperType.BYTE: Dbg.Assert(bin.Length == 1, "bin.Length == 1"); return(bin[0]); case ObjectWrapperType.SHORT: Dbg.Assert(bin.Length == 2, "bin.Length == 2"); return(U.BytesToInt16(bin, 0)); case ObjectWrapperType.INT32: Dbg.Assert(bin.Length == 4, "bin.Length == 4"); return(U.BytesToInt32(bin, 0)); case ObjectWrapperType.INT64: Dbg.Assert(bin.Length == 8, "bin.Length == 8"); return(U.BytesToInt64(bin, 0)); case ObjectWrapperType.FLOAT: Dbg.Assert(bin.Length == 4, "bin.Length == 4"); return(U.BytesToSingle(bin, 0)); case ObjectWrapperType.DOUBLE: Dbg.Assert(bin.Length == 8, "bin.Length == 8"); return(U.BytesToDouble(bin, 0)); case ObjectWrapperType.BYTES: return(bin); case ObjectWrapperType.UUID: return(WrapGuid(val.Binary)); case ObjectWrapperType.STRING: return(val.Binary.ToStringUtf8()); case ObjectWrapperType.COLLECTION: return(WrapCollection(Collection.ParseFrom(bin))); case ObjectWrapperType.MAP: return(WrapMap(Map.ParseFrom(bin))); case ObjectWrapperType.AUTH_REQUEST: return(WrapAuthRequest(ProtoRequest.ParseFrom(bin))); case ObjectWrapperType.CACHE_REQUEST: return(WrapCacheRequest(ProtoRequest.ParseFrom(bin))); case ObjectWrapperType.TASK_REQUEST: return(WrapTaskRequest(ProtoRequest.ParseFrom(bin))); case ObjectWrapperType.LOG_REQUEST: return(WrapLogRequest(ProtoRequest.ParseFrom(bin))); case ObjectWrapperType.TOPOLOGY_REQUEST: return(WrapTopologyRequest(ProtoRequest.ParseFrom(bin))); case ObjectWrapperType.RESPONSE: return(WrapResponse(ProtoResponse.ParseFrom(bin))); case ObjectWrapperType.NODE_BEAN: return(WrapNode(ProtoNodeBean.ParseFrom(bin))); case ObjectWrapperType.TASK_BEAN: return(WrapTaskResult(ProtoTaskBean.ParseFrom(bin))); default: throw new ArgumentException("Failed to deserialize object (object deserialization" + " of given type is not supported): " + val.Type); } }
/** <inheritdoc /> */ public IList <N> Nodes(Predicate <N> filter) { return(U.ApplyFilter(ProjectionNodes(), filter)); }
/** <inheritdoc /> */ public IGridClientCompute Projection(N node) { return(CreateProjection(U.List(node), null, null)); }
/** <summary>Reader thread.</summary> */ private void readPackets() { try { bool running = true; byte[] lenByte = new byte[4]; byte[] head = new byte[40]; while (running) { // Note that only this thread removes futures from map. // So if we see closed condition, it is safe to check map size since no more futures // will be added to the map. if (closed) { // Exit if either all requests processed or we do not wait for completion. if (!waitCompletion) { break; } if (pendingReqs.Count == 0) { break; } } // Header. int symbol; try { if (lastReadTimedOut) { lastReadTimedOut = false; if (isSslStream) { // Recover SSL stream state after socket exception. skipSslDataRecordHeader(); } } symbol = inStream.ReadByte(); } catch (Exception e) { if (e.InnerException is SocketException) { e = e.InnerException; } var sockEx = e as SocketException; if (sockEx != null && sockEx.ErrorCode == 10060) { checkPing(); lastReadTimedOut = true; continue; } // All other exceptions are interpreted as stream ends. throw; } // Connection closed. if (symbol == -1) { Dbg.WriteLine("Connection closed by remote host " + "[srvAddr=" + ServerAddress + ", symbol=" + symbol + "]"); break; } // Check for correct header. if ((byte)symbol != (byte)0x90) { Dbg.WriteLine("Failed to parse incoming message (unexpected header received, will close) " + "[srvAddr=" + ServerAddress + ", symbol=" + symbol + "]"); break; } U.ReadFully(inStream, lenByte, 0, 4); int len = U.BytesToInt32(lenByte, 0); if (len == 0) { // Ping received. lastPingRcvTime = U.Now; continue; } if (len < 40) { Dbg.WriteLine("Invalid packet received [len=" + len + "]"); break; } U.ReadFully(inStream, head, 0, 40); long reqId = U.BytesToInt64(head, 0); Guid clientId = U.BytesToGuid(head, 8); Guid destNodeId = U.BytesToGuid(head, 24); byte[] msgBytes = new byte[len - 40]; U.ReadFully(inStream, msgBytes, 0, msgBytes.Length); GridClientResponse msg = marshaller.Unmarshal <GridClientResponse>(msgBytes); msg.RequestId = reqId; msg.ClientId = clientId; msg.DestNodeId = destNodeId; lastPacketRcvTime = U.Now; handleResponse(msg); } } catch (IOException e) { if (!closed) { Dbg.WriteLine("Failed to read data from remote host (will close connection)" + " [addr=" + ServerAddress + ", e=" + e.Message + "]"); } } catch (Exception e) { Dbg.WriteLine("Unexpected throwable in connection reader thread (will close connection)" + " [addr={0}, e={1}]", ServerAddress, e); } finally { U.Async(() => Close(false)); } }