⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vpptransaction.java

📁 这是一个用java和xml编写的流媒体服务器管理软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      if (requestType.equals("PUT"))      {        toSocket.write("Content-Length: " + entityToPut.length() + '\n');        toSocket.flush();        toSocket.write(entityToPut);        toSocket.flush();      }            if (requestType.equals("PUTSUB"))      {        toSocket.write("Content-Length: " + entityToPut.length() + '\n');        toSocket.flush();        toSocket.write(entityToPut);        toSocket.flush();      }      // Read the response      String statusLine = "xxx";      boolean isError = false;      try      {        // if it fails for get, no such file, else perm. denied        if (requestType.equals("GET"))        {          statusLine = getStatusLine(ERROR_MEANS_NO_SUCH_FILE);        }        else        {          statusLine = getStatusLine(ERROR_MEANS_PERM_DENIED);        }        System.out.println("----TRACE---- VPPTransaction:"                            + "sendRequest - statusLine = " + statusLine);      }      catch (VPPException e)      {        System.out.println("----TRACE---- VPPTransaction:"                            + "sendRequest - statusLine = EXCEPTION");        e.printStackTrace();        isError = true;        exToThrow = e;      }      // After a put reqest, the server will respond with the length      // of the received data.      int contentLength = getContentLength();      if (isError == true)      {        throw exToThrow;      }      // If there was no content (for example on a put), return      if (contentLength == 0)      {        return "";      }      // else get and return contents      String contents = getContents(contentLength);      return contents;    }    catch (IOException e)    {      // We catch non-VPP exceptions of I/O type and wrap them      // in low level error      System.out.println("Caught " + e.getMessage() + " -- Reconnecting");      System.out.println("The reqest was:");      System.out.println("    " + requestString);      try      {        reconnect();      }      catch (VPPNoConnException ex)      {        throw new VPPLowLevelException(ex, ex.getMessage());      }    }    return "";  }  /**   * Construct a request and send it across a socket.   * @return the server's response as a string   * @param requestType the type of request   * @param arg1 group for requests that have a group, otherwise name.   * @param arg2 name for requests that hava a group, otherwise empty string   * @param entityToPut the contents of entity that should be attached to   * the request.   */  protected String sendRequest(String requestType, String arg1, String arg2,           String entityToPut) throws VPPException  {    return sendRequest(requestType,                        requestType + ' ' + arg1 + ' ' + arg2 + '\n',                        entityToPut);  }  /**   * Read a line of input and return it, without the newline.   */  protected String readLineFromSocket() throws VPPLowLevelException  {    StringBuffer line = new StringBuffer();    char nextCharacter;    try    {      while ((nextCharacter = (char) fromSocket.read()) != '\n')      {        line.append(nextCharacter);      }    }    catch (IOException e)    {      System.out.println("Caught " + e.getMessage() + " -- Reconnecting");      try      {        reconnect();      }      catch (VPPNoConnException ex)      {        throw new VPPLowLevelException(ex, ex.getMessage());      }    }    return line.toString();  }  /**   * Make a get request, and return the contents of the requested file if the   * transaction was successful.  Otherwise, throw an exception.   * @param group the virtual path of the file to request,   * like a directory path, but with underscores instead of slashes.   * @param file the name of the file   * @return the contents of the file   * @exception VPPException if the server gives a response that is   * illegal according to this protocol   */  public String doGet(String group, String filename) throws VPPException  {    return sendRequest("GET", group, filename, null);  }  /**   * Send authentication message to pserver.   * @param username not used   * @param password send in request string.   * @return true if authentication succeeded   */  protected int doAuth(String username, String password) throws VPPException  {    //String requestString = "AUTH vocal:" + password + " VPP/1.1\n";    String requestString = "AUTH " + username + ":" + password + " VPP/1.1\n";    try    {      // Send the request      toSocket.write(requestString);      toSocket.flush();    }    catch (IOException e)    {      // We catch non-VPP exceptions of I/O type and wrap them      // in low level error      System.out.println("Caught " + e.getMessage() + " -- Reconnecting");      System.out.println("The reqest was:");      System.out.println("    " + requestString);      try      {        reconnect();      }      catch (VPPNoConnException ex)      {        throw new VPPLowLevelException(ex, ex.getMessage());      }    }    // Read the response    String statusLine = getStatusLine(ERROR_MEANS_AUTH_FAILED);        System.out.println("----TRACE---- VPPTransaction:"                            + "send Authorization request: " + statusLine);    if(statusLine.startsWith("200"))    {      authenticated = true;      // throw away the Content-Length line      getContentLength();      //System.out.println("---I get data:"+statusLine+statusLine.indexOf(":3"));            if(-1!=statusLine.indexOf(":1"))      {    	  System.out.println("status:indexof result:"+ statusLine.indexOf(":1"));    	  return 1;      }      if(-1!=statusLine.indexOf(":2"))      {    	  System.out.println("status:indexof result:"+ statusLine.indexOf(":2"));    	  return 2;      }      if(-1!=statusLine.indexOf(":3"))      {    	  System.out.println("status:indexof result:"+ statusLine.indexOf(":3"));    	  return 3;      }      if(-1!=statusLine.indexOf(":4"))      {    	  System.out.println("status:indexof result:"+ statusLine.indexOf(":4"));    	  return 4;      }      return 1;      //return true;    }    authenticated = false;    // throw away the Content-Length line    getContentLength();    //return false;    return 0;  }  public String doDel(String group, String filename) throws VPPException  {    return sendRequest("REMOVE", group, filename, null);  }  public static void doPutToFile(String filePath,           String fileContents) throws VPPException  {    try    {      String path = filePath.substring(0, filePath.lastIndexOf("/") + 1);      String name = filePath.substring(filePath.lastIndexOf("/") + 1);      File outputFile = new File(path, name);      // if can't write file, it may be that the path does not exist      if (!outputFile.canWrite())      {        File pathFile = new File(path);        // create the given directories        boolean response = pathFile.mkdirs();      }      FileWriter out = new FileWriter(outputFile);      out.write(fileContents);      out.flush();      out.close();    }    catch (IOException e)    {      throw new VPPLowLevelException(e, e.getMessage());    }  }  public void doPut(String group, String filename, String entityToPut,                     int destination) throws VPPException  {    if (destination == WRITE_TO_FILE)    {      doPutToFile(group + "/" + filename, entityToPut);    }    else if (destination == WRITE_TO_PSERVER)    {      doPut(group, filename, entityToPut);    }  }  /**   * Format and send a PUT request, uploading a file.   * @param group the virtual path to which to upload   * @param filename name of the file to write   * @param entityToPut the text string to write to file   * @exception VPPException if the server gives a response that is   * illegal according to this protocol   */  public void doPut(String group, String filename,                     String entityToPut) throws VPPException  {    sendRequest("PUT", group, filename, entityToPut);  }  public void doSubPut(String group, String filename,           String entityToPut) throws VPPException  {	  sendRequest("PUTSUB", group, filename, entityToPut);  }    /**   * Delete a user and his associated Contact Lists.   * @param userName the name of the user. Note that this   * does not have any directory path prepended to it.   */  public void doDeleteUser(String userName) throws VPPException  {    sendRequest("DELETE", userName, "", null);  }  /**   * Close the input stream, output stream and socket associated with this   * connection.   */  public void close() throws VPPException  {    try    {      this.isClosed = true;      toSocket.close();      fromSocket.close();      socket.close();    }    catch (IOException e)    {      throw new VPPLowLevelException(e, e.getMessage());    }  }  /**   * Sends a request of the given type (may be GET or PUT, or whatever) to   * the server and returns the response as a BufferedReader. <p>   * @param requestType and identifier of the type of request being made. Eg:   * "GET" or "PUT"   * @param arg1 see sendRequest arg1   * @param arg2 see sendRequest arg2   * @return the server's response as a BufferedReader   */  public BufferedReader request(String requestType, String arg1,           String arg2) throws VPPException  {    String response = sendRequest(requestType, arg1, arg2, null);    return new BufferedReader(new StringReader(response));  }  public String requestString(String requestType, String arg1,           String arg2) throws VPPException  {    return sendRequest(requestType, arg1, arg2, null);  }  public void reconnect() throws VPPNoConnException  {    System.out.println("---ERROR--- reconnct started "                        + Thread.currentThread().toString());    connected = false;    authenticated = false;    reconnect = new Reconnect(this, host, port);    Thread thread = new Thread(reconnect, "Reconnect");    thread.start();    throw new VPPNoConnException("Connection to server lost.");  }  /**   * Callback used by the Reconnect to notify the transaction that the   * reconnect was successful.   */  public void reconnected()  {    fromSocket = reconnect.getInputStream();    toSocket = reconnect.getOutputStream();    socket = reconnect.getSocket();    connected = true;  }  /**   * Callback used by the Reconnect to notify this class that the reconnect   * failed (because the max number of retries was exceeded).   */  public void reconnectFailed()  {    // what should this do?  }  /**   * Callback used by the Reconnect before each reconnect attempt is   * started.   */  public boolean okToReconnect()  {    if (isClosed)    {      return false;    }    return true;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -