/// <summary> /// Unload the assembly according to the assembly id; /// </summary> /// <param id="state">Server state object for request.</param> private static void unloadDomain(ServerState state) { //get the Key of the domain string domainKey = state.GetDataASCIIString(); //tell the domain manager to delete the domain DomainManager.DestroyDomain(domainKey); }
/// <summary> /// The dispatch action called when a status request is signaled. /// </summary> /// <param name="state">The server state object of the network connection.</param> public static void status(ServerState state) { ServerStatus status = new ServerStatus(); status.NodeCount = Nodes.GetTotalAvailableNodeCount(); status.CPUCount = Nodes.GetCPUCount(); status.CDPI = Nodes.GetCDPI(); status.TotalMemory = Nodes.GetTotalMemory(); SerializationEngine serializer = new SerializationEngine(); state.WriteAndClose(MessageType.STATUS_TERMINAL, serializer.Serialize(status)); }
/// <summary> /// Internal execute function to be called asynchronously. /// </summary> /// <param id="state">The server state object associated with the execution.</param> private static void execute(ServerState state) { Interlocked.Increment(ref runningJobs); //get the execution context SerializationEngine serializer = new SerializationEngine (); Transfers.ExecutionContext context = (Transfers.ExecutionContext)serializer.Deserialize(state.GetDataArray()); byte[] res = DomainManager.ExecuteIncoming(context); Transfers.ExecutionResult result = new Transfers.ExecutionResult(res, context.ContextID, context.DomainKey, ClusterManager.NodeID); state.Write(MessageType.EXECUTION_COMPLETE, serializer.Serialize(result)); Interlocked.Decrement(ref runningJobs); }
/// <summary> /// A dispatch event to be rasied upon reception of an assembly from the Presto client application. /// </summary> /// <param id="state">The server state object recieved along with this event.</param> private static void recieveAssemblyMaster(ServerState state) { //close the socket state.CloseSocket(); //create a new domain and add the assembly to it string domainkey = Generator.RandomAlphaNumeric(Config.UIDLength); DomainManager.CreateDomain(domainkey); DomainManager.LoadAssemblyIntoDomain(domainkey, state.GetDataArray()); //push assembly onto executor to be executed Executor.ExecuteModule(domainkey); }
/// <summary> /// A dispatch event to be rasied upon reception of an assembly from another Presto server.. /// </summary> /// <param id="state">The server state object recieved along with this event.</param> private static void recieveAssemblySlave(ServerState state) { //get the slave assembly struct SerializationEngine serializer = new SerializationEngine (); SlaveAssembly slaveAssembly = (SlaveAssembly)serializer.Deserialize(state.GetDataArray()); //create the domain and add the assembly to it DomainManager.CreateDomain(slaveAssembly.DomainKey); if(!DomainManager.DomainHasAssembly(slaveAssembly.DomainKey, slaveAssembly.AssemblyName)){ DomainManager.LoadAssemblyIntoDomain(slaveAssembly.DomainKey, slaveAssembly.AssemblyImage); } Presto.Remote.Node from = Nodes.GetNodeByID(slaveAssembly.NodeID); if (from != null) { from.SetLoadedAssembly(slaveAssembly.AssemblyName); from.SetLoadedDomain(slaveAssembly.DomainKey); } //send back assembly transfer complete message state.Write(MessageType.ASSEMBLY_TRANSFER_COMPLETE); }
/// <summary> /// Dispatches the recieved message to the appropriate object /// </summary> /// <param name="state">The state object to be dispatched.</param> private void dispatch(ServerState state) { //first extract the message type from the message string messageType = state.GetMessageType(); //if messageType is null we return the Unknowm message response if (messageType == null) { state.Write(MessageType.UNKOWN); } //find the corresponding message type in the listing and dispatch accordingly, or return Unknown message response if (dispatchList.ContainsKey(messageType)) { dispatchList[messageType].BeginInvoke(state, null, null); } else { state.Write(MessageType.UNKOWN); } }
/// <summary> /// Executes a particular function in a module, according to the passed in parameters, acting as a slave server /// </summary> /// <param id="state">The server state object of the request</param> public static void ExecutionBegin(ServerState state) { //finally execute the function defined in the transfer AsyncMethodCaller execution = new AsyncMethodCaller(execute); execution.BeginInvoke(state, null, null); }
/// <summary> /// This server needs to verify itself and send back a Verfication object. /// </summary> /// <param id="state">The server state object.</param> private static void verifyResponse(ServerState state) { Transfers.Verification verification = new Transfers.Verification(NodeID, ClusterManager.HostName, DPI.GetDPI(), Memory.GetTotalSize(), CPU.GetCount(), Executor.RunningJobs(), DomainManager.GetAllDomainKeys(), DomainManager.GetAllAssemblyNames()); SerializationEngine serializer = new SerializationEngine (); state.Write(MessageType.VERIFICATION_RESPONSE, serializer.Serialize(verification).ToArray()); }
/// <summary> /// Recieve a user message from a remote node. /// </summary> /// <param name="state">The server state object of this transfer.</param> private static void receiveMessage(ServerState state) { SerializationEngine serializer = new SerializationEngine (); UserMessage message = (UserMessage)serializer.Deserialize(state.GetDataArray()); DomainManager.DeliverMessage(message); }
/// <summary> /// Internal: Connect to the Client and spin off a recieve callback /// </summary> /// <param name="ar"></param> private void accept(IAsyncResult ar) { // Signal the main thread to continue. allDone.Set(); // Get the socket that handles the Client request. Socket asyncListener = (Socket)ar.AsyncState; Socket handler = asyncListener.EndAccept(ar); //Create the state object ServerState state = new ServerState(handler); //begin recieving the data state.socket.BeginReceive(state.Buffer, 0, ServerState.BufferSize, 0, new AsyncCallback(read), state); }
/// <summary> /// read data sent from the Client, called recursively until all data is receieved /// </summary> /// <param name="ar"></param> private void read(IAsyncResult ar) { //I absolutely hate the implementation of this. I hate it so much. Need to rewrite fully. try { //Get the Server State Object ServerState state = (ServerState)ar.AsyncState; //read from the socket int readCount = state.socket.EndReceive(ar); //check if reading is done, move on if so. then trigger another read if (readCount > 0) { //purge the Buffer and Start another read state.PurgeBuffer(readCount); //check if the message is fully recieved, if it is, create a new state object and pass that to the read, // if not, continue the read with the same state object if (state.IsFullyRecieved()) { //Need to redo all this below, not as efficient as id like ServerState newState = new ServerState(state.socket); byte[] excessData = state.CompleteAndTrim(); dispatch(state); newState.PreSetData(excessData); bool moreToProcess = newState.IsFullyRecieved(); if (moreToProcess) { do { ServerState newStateRepeat = new ServerState(state.socket); newStateRepeat.PreSetData(excessData); excessData = newStateRepeat.CompleteAndTrim(); dispatch(newStateRepeat); newState.PreSetData(excessData); } while (newState.IsFullyRecieved()); newState.socket.BeginReceive(newState.Buffer, 0, ServerState.BufferSize, 0, new AsyncCallback(read), newState); } else { newState.socket.BeginReceive(newState.Buffer, 0, ServerState.BufferSize, 0, new AsyncCallback(read), newState); } } else { state.socket.BeginReceive(state.Buffer, 0, ServerState.BufferSize, 0, new AsyncCallback(read), state); } } else { //socket has been closed... handle it //TODO: handle socket close } } catch (Exception e) { //The server was closed. Log.Error(e.ToString()); } }