public string GetDocument(string serverIP, string documentName) { // retrieve requested document from the specified server // manage the session with the SD Server // opening or resuming as needed // connect to and disconnect from the server w/in this method // make sure we have valid parameters // serverIP is the SD Server's IP address // documentName is the name of a docoument on the SD Server // both should not be empty if (String.IsNullOrWhiteSpace(serverIP) || String.IsNullOrWhiteSpace(documentName)) { throw new Exception("EMpty server IP or document name!"); } // contact the PRS and lookup port for "SD Server" PRSClient prs = new PRSClient(prsIP, prsPort, "SD Server"); ushort sdPort = prs.LookupPort(); // connect to SD server by ipAddr and port // use OpenOrResumeSession() to ensure session is handled correctly SDClient client = OpenOrResumeSession(serverIP, sdPort); // send get message to server for requested document string content = client.GetDocument(documentName); // disconnect from server client.Disconnect(); // return the content return(content); }
private SDClient OpenOrResumeSession(string ipAddr, ushort port) { // create and connect an SDClient instance to the given SD Server // open or resume a session // leave the SDClient connected and return it for communication to the server // Create SDClient and connect to the server // connect to the SD Server's IP address and port SDClient client = new SDClient(ipAddr, port); client.Connect(); // do we already have a session for this server? if (sessions.ContainsKey(ipAddr)) { // yes, session already open // retrieve the sessionId and send resume message to server ulong sessionId = sessions[ipAddr].sessionId; // Resume the session on the server client.ResumeSession(sessionId); } else { // no, session not open for this server // open a new session and save the sessionId client.OpenSession(); // save this open session in the sessions dictionary for later sessions[ipAddr] = new SDSession(ipAddr, port, client.SessionID); } // keep the client open and return it return(client); }
private void Form1_Closing(object sender, EventArgs e)//Form closing { if (SDSessions.Count > 0) { foreach (KeyValuePair <string, SDClient> pair in SDSessions) { string serverIP = pair.Key; SDClient client = pair.Value; try { client.CloseSession(); } catch (Exception ex) { MessageBox.Show("Failed to close session to server:" + serverIP + ", error: " + ex.Message); } } } }
public void Close() { // close each open session with the various servers // for each session... foreach (SDSession session in sessions.Values) { // connect to the SD Server's IP address and port SDClient client = new SDClient(session.ipAddr, session.port); client.Connect(); // send the close for this sessionId client.SessionID = session.sessionId; client.CloseSession(); // disconnect from server client.Disconnect(); } sessions.Clear(); }
private void SDGet(string serverIP, string directoryName) { contentTextBox.Clear();//Clear the text box try { SDClient client = null; if (SDSessions.ContainsKey(serverIP)) //Session is already started { client = SDSessions[serverIP]; //Get the session from the session dict } else//if we dont have a session we need to make a new session { client = new SDClient(PRS_ADDRESS, PRS_PORT, serverIP); SDSessions[serverIP] = client; } string documentcontents = client.Get(directoryName); //Get command on the SDClient contentTextBox.AppendText(documentcontents); //Put the text in the textbox } catch (Exception e) { MessageBox.Show(e.ToString());//Display error message } }