Пример #1
0
                public override void HandleGetCmd(SDSession session)
                {
                    // read the document name from the client
                    string documentName = socketReader.ReadLine();

                    Console.WriteLine("Getting document " + documentName);

                    // lookup the document in the session
                    try
                    {
                        string documentContents = session.GetValue(documentName);
                        Console.WriteLine("Found value " + documentName);

                        // send the document name and length to the client
                        socketWriter.WriteLine("success");
                        socketWriter.WriteLine(documentName);
                        socketWriter.WriteLine(documentContents.Length.ToString());
                        socketWriter.Flush();

                        // send the document contents to the client
                        socketWriter.Write(documentContents);
                        socketWriter.Flush();

                        Console.WriteLine("Sent contents of " + documentName);
                    }
                    catch (Exception ex)
                    {
                        SendError(ex.Message);
                    }
                }
Пример #2
0
 public ClientThread(Socket clientSocket, SessionTable sessionTable)
 {
     this.clientSocket = clientSocket;
     this.sessionTable = sessionTable;
     session           = null;
     currentState      = null;
     theThread         = new Thread(new ParameterizedThreadStart(ClientThreadFunc));
 }
Пример #3
0
        public SDSession NewSession()
        {
            // allocate a new session, with a unique ID and save it for later in the session table
            ulong     sessionId = nextSessionId++;
            SDSession session   = new SDSession(sessionId);

            sessionTable[sessionId] = session;
            return(session);
        }
Пример #4
0
 public SDSession Lookup(ulong Sessionid)
 {
     if (sessionTable.ContainsKey(Sessionid))
     {
         SDSession session = sessionTable[Sessionid];
         return(session);
     }
     return(null);//no match found
 }
Пример #5
0
 public ClientThread(Socket clientSocket, SessionTable sessionTable)
 {
     socketNetworkStream = new NetworkStream(clientSocket);
     socketReader        = new StreamReader(socketNetworkStream);
     socketWriter        = new StreamWriter(socketNetworkStream);
     this.clientSocket   = clientSocket;
     this.sessionTable   = sessionTable;
     session             = null;
     currentState        = null;
     theThread           = new Thread(new ParameterizedThreadStart(ClientThreadFunc));
 }
Пример #6
0
                public override SDSession HandleResumeCmd()
                {
                    ulong     sessionId = System.Convert.ToUInt64(client.socketReader.ReadLine());
                    SDSession session   = client.sessionTable.Lookup(sessionId); //lookup the session ID

                    if (session == null)                                         //The lookup was not valid
                    {
                        client.socketWriter.WriteLine("rejected");
                        client.socketWriter.WriteLine("Session does not exist");
                    }
                    return(null);
                }
Пример #7
0
                override public SDSession HandleOpenCmd()
                {
                    // create a new session for the client
                    SDSession session = sessionTable.NewSession();

                    Console.WriteLine("Creating new session with session ID" + session.ID);
                    // send Accepted(sessionId)
                    socketWriter.WriteLine("accepted");
                    socketWriter.WriteLine(session.ID.ToString());
                    socketWriter.Flush();

                    return(session);
                }
Пример #8
0
            private void Run()
            {
                currentState = new ReadyForSessionCmd(this);

                bool done = false;

                while (!done && clientSocket.Connected)
                {
                    // read the next command from the client
                    string cmd = socketReader.ReadLine();
                    if (cmd == null)
                    {
                        // client disconnected
                        done = true;
                        break;
                    }
                    Console.WriteLine("Received cmd " + cmd);

                    switch (cmd)
                    {
                    case "close":
                        Console.WriteLine("Recieved Close command");
                        currentState.HandleCloseCmd();    //handle the command
                        currentState = new ReadyForSessionCmd(this);
                        break;

                    case "open":
                    {
                        Console.WriteLine("Received OPEN cmd from client");
                        session = currentState.HandleOpenCmd();
                        if (session == null)
                        {
                            Console.WriteLine("Unable to open new session");
                        }
                        else
                        {
                            currentState = new ReadyForDocumentCmd(this);
                        }
                    }
                    break;

                    case "resume":
                    {
                        Console.WriteLine("Received RESUME cmd from client");
                        // parse out the sessionId
                        session = currentState.HandleResumeCmd();
                        if (session != null)
                        {
                            // successfully resumed session
                            // change state
                            currentState = new ReadyForDocumentCmd(this);
                        }
                        else
                        {
                            Console.WriteLine("Unable to Resume session");
                        }
                    }
                    break;

                    case "post":
                    {
                        Console.WriteLine("Received POST cmd from client");
                        currentState.HandlePostCmd();
                        currentState = new ReadyForSessionCmd(this);
                    }
                    break;

                    case "get":
                    {
                        Console.WriteLine("Received GET cmd from client");
                        currentState.HandleGetCmd();
                        currentState = new ReadyForSessionCmd(this);
                    }
                    break;

                    case "exit":
                        Console.WriteLine("Received EXIT cmd from client");
                        done = true;
                        break;

                    default:
                        Console.WriteLine("Invalid Command Recieved from Client");
                        break;
                    }
                }

                // disconnect from client and close the socket, it's stream and reader/writer
                Console.WriteLine("Disconnecting from client");
                clientSocket.Disconnect(false);
                socketNetworkStream.Close();
                socketReader.Close();
                socketWriter.Close();
                clientSocket.Close();
                Console.WriteLine("Disconnected from client");
            }
Пример #9
0
 public abstract void HandleGetCmd(SDSession session);
Пример #10
0
            private void Run()
            {
                NetworkStream socketNetworkStream = new NetworkStream(clientSocket);
                StreamReader  socketReader        = new StreamReader(socketNetworkStream);
                StreamWriter  socketWriter        = new StreamWriter(socketNetworkStream);

                currentState = new ReadyForSessionCmd(sessionTable, socketNetworkStream, socketReader, socketWriter);

                bool done = false;

                while (!done && clientSocket.Connected)
                {
                    // read the next command from the client
                    string cmd = socketReader.ReadLine();
                    if (cmd == null)
                    {
                        // client disconnected
                        done = true;
                        break;
                    }
                    Console.WriteLine("Received cmd " + cmd);

                    switch (cmd)
                    {
                    case "open":
                        session      = currentState.HandleOpenCmd();
                        currentState = new ReadyForDocumentCmd(sessionTable, socketNetworkStream, socketReader, socketWriter);
                        break;

                    case "resume":
                    {
                        // parse out the sessionId
                        ulong sessionId = 0;
                        session = currentState.HandleResumeCmd(sessionId);
                        if (session != null)
                        {
                            // successfully resumed session
                            // change state
                            currentState = new ReadyForDocumentCmd(sessionTable, socketNetworkStream, socketReader, socketWriter);
                        }
                        else
                        {
                            //???
                        }
                    }
                    break;

                    case "get":
                    {
                        Console.WriteLine("Received GET cmd from client");
                        currentState.HandleGetCmd(session);
                    }
                    break;

                    case "exit":
                        Console.WriteLine("Received EXIT cmd from client");
                        done = true;
                        break;
                    }
                }

                // disconnect from client and close the socket, it's stream and reader/writer
                Console.WriteLine("Disconnecting from client");
                clientSocket.Disconnect(false);
                socketNetworkStream.Close();
                socketReader.Close();
                socketWriter.Close();
                clientSocket.Close();
                Console.WriteLine("Disconnected from client");
            }
Пример #11
0
 public override void HandleGetCmd(SDSession session)
 {
     SendError("No session open");
 }