/// <summary> /// IServerAdmin.LoadScriptFile impl /// load script file. File needs to be on the server and in an accessible directory /// </summary> /// <param name="clientId"></param> /// <param name="scriptFilePath">path to file to load. If not an xml file will assume that the file contains a list of xml files</param> /// <param name="streamFileContentsBackToClient">should the server stream the files back to the client</param> public AsyncMethodResponse LoadScriptFile(Guid clientId, string scriptFilePath, bool streamFileContentsBackToClient) { AsyncMethodResponse response = new AsyncMethodResponse(clientId, null); //check for empty path if (string.IsNullOrEmpty(scriptFilePath)) { response.AddError("Missing filePath", ErrorCode.Fatal); return(response); } //this call will return right away and //pass back an async ticket. the processing //will be done in a worker //and updates/information about the progress will //be sent back to the caller via a callback. response.AsyncTicket = Guid.NewGuid().ToString(); ClientProxy clientProxy = CallbackManagerImpl.Instance.LookupClientProxy(clientId); //contructing the Impl will start a thread to do the work new LoadSciptFileMethodImpl(this, clientProxy, scriptFilePath, response.AsyncTicket, streamFileContentsBackToClient); return(response); }
/// <summary> /// called by client to get the async call response /// </summary> /// <param name="clientId"></param> /// <param name="payloadTicket"></param> /// <returns></returns> public AsyncMethodResponse GetAsyncResponseData(Guid clientId, Guid payloadTicket) { QueuedResponsePayload payload = null; lock (payloadTicketToResponseMap) { payloadTicketToResponseMap.TryGetValue(payloadTicket, out payload); } if (payload != null && payload.ClientId == clientId) { AsyncMethodResponse response = new AsyncMethodResponse(clientId, payload.AsyncTicket); response.AsyncResponseData = payload.PayloadData; lock (payloadTicketToResponseMap) payloadTicketToResponseMap.Remove(payloadTicket); return(response); } AsyncMethodResponse error = new AsyncMethodResponse(clientId, null); error.AddError("Not found", ErrorCode.RecordNotFound); return(error); }
/// <summary> /// Add a client to subscription list /// </summary> /// <param name="clientId"></param> /// <returns></returns> public AsyncMethodResponse Subscribe(Guid clientId) { try { Guid newClientId = clientId; if (newClientId == Guid.Empty) { newClientId = Guid.NewGuid(); } IServerAdminCallback callback = OperationContext.Current.GetCallbackChannel <IServerAdminCallback>(); ClientProxy clientProxy = new ClientProxy(newClientId, callback); lock (subscribers) { ClientProxy existingClientProxy; if (clientId != Guid.Empty) { subscribers.TryGetValue(clientId, out existingClientProxy); } else { existingClientProxy = null; } if (existingClientProxy == null) { subscribers[newClientId] = clientProxy; } else { existingClientProxy.AssignNewCallback(callback); List <KeyValuePair <PublishEventArgs, PublishEventHandler> > pendingList = existingClientProxy.PopPendingMessageList(); if (pendingList != null) { Dictionary <Guid, ClientProxy> targetClientProxyMap = new Dictionary <Guid, ClientProxy>(); targetClientProxyMap[existingClientProxy.ClientId] = existingClientProxy; foreach (KeyValuePair <PublishEventArgs, PublishEventHandler> pair in pendingList) { Publish(targetClientProxyMap, true, pair.Key, pair.Value); } } } } AsyncMethodResponse response = new AsyncMethodResponse(newClientId, null); return(response); } catch (Exception ex) { AsyncMethodResponse errorResponse = new AsyncMethodResponse(clientId, null); errorResponse.AddError(ex.Message, ErrorCode.Fatal); return(errorResponse); } }
/// <summary> /// remove client from subscription list /// </summary> /// <param name="clientId"></param> /// <returns></returns> public AsyncMethodResponse Unsubscribe(Guid clientId) { try { ClientProxy clientProxy = null; lock (this.subscribers) { if (subscribers.TryGetValue(clientId, out clientProxy)) { clientProxy.Unsubscribe(); subscribers.Remove(clientId); } } return(null); } catch (Exception ex) { AsyncMethodResponse response = new AsyncMethodResponse(clientId, null); response.AddError(ex.Message, ErrorCode.Fatal); return(response); } }
/// <summary> /// Thread Method to do the work of Loading the file in a worker thread /// </summary> /// <param name="state">not used, but needed to match the WaitCallback signature</param> private void LoadScriptFileProc(object state) { AsyncMethodResponse response = new AsyncMethodResponse(this.clientProxy.ClientId, this.asyncTicket); FileStream responseFileStream = null; try { string directoryPath = System.IO.Path.GetDirectoryName(this.scriptFilePath); //figure out what file(s) need to be loaded. if xml then assume it is just one file //if not xml assume that the files contains a list of paths to xml files List <string> filePathList = new List <string>(); if (this.scriptFilePath.EndsWith(".xml") == false) { using (System.IO.FileStream fileStream = new System.IO.FileStream(this.scriptFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { using (StreamReader sr = new StreamReader(fileStream)) { string curFileLine = null; while ((curFileLine = sr.ReadLine()) != null) { curFileLine = curFileLine.Trim(); if (string.IsNullOrEmpty(curFileLine)) { continue; } curFileLine = Path.IsPathRooted(curFileLine) ? curFileLine : Path.Combine(directoryPath, curFileLine); //make sure to use the absolute path filePathList.Add(curFileLine); } } } } else { filePathList.Add(this.scriptFilePath); } try { foreach (string scriptFile in filePathList) { if (scriptFile == null) { continue; } //create a scriptloader to load the file ScriptLoader scriptLoader = new ScriptLoader(); //subscribe to scriptloader messages so can publish them back to the client scriptLoader.NotifyMessage += new ScriptLoader.MessageEventHander(scriptLoader_NotifyMessage); //set scriptloader in local mode (meaning it is running inside the server) scriptLoader.LocalMode = true; scriptLoader.FileName = scriptFile; //load the file into the MT/DB scriptLoader.Load(); } if (this.streamConents == true) { responseFileStream = File.OpenRead(this.scriptFilePath); } //it all worked response.Result = ErrorCode.Success; } catch (Exception ex) { //response.AddError(ex.Message); //catch exception and add it to the return EventLog.Warning(String.Format("{0}: {1}", ex.GetType(), ex.Message)); response.AddError(ex.Message, ErrorCode.Fatal); } } catch (Exception ex) { //response.AddError(ex.Message); //catch exception and add it to the return //response.AddError(ex); EventLog.Warning(String.Format("{0}: {1}", ex.GetType(), ex.Message)); response.AddError(ex.Message, ErrorCode.Fatal); } if (responseFileStream != null) { CallbackManagerImpl.Instance.PublishSendResponseTicket(this.clientProxy, null, null, responseFileStream, response); } else { //notify client that we are all done CallbackManagerImpl.Instance.PublishSendResponseTicket(this.clientProxy, "PublishSendResponseTicketResp", string.Format("Completed: {0}\r\n", this.scriptFilePath), null, response); } }