changeset 144:c1d7d4fbcfb9

modify Xml-Rpc.java
author e085711
date Sun, 04 Sep 2011 20:39:14 +0900
parents 1a0c60efc627
children 8bdbed4c4505
files src/myVncProxy/MyRfbProto.java src/myVncProxy/VncProxyService.java src/test/XmlRpc.java
diffstat 3 files changed, 145 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/src/myVncProxy/MyRfbProto.java	Sat Sep 03 15:24:41 2011 +0900
+++ b/src/myVncProxy/MyRfbProto.java	Sun Sep 04 20:39:14 2011 +0900
@@ -54,7 +54,7 @@
 	private boolean clicomp = false;
 
 	private ServerSocket servSock;
-	private int acceptPort;
+	protected int acceptPort;
 	private byte initData[];
 	private LinkedList<Socket> cliListTmp;
 	private LinkedList<Socket> cliList;
@@ -118,6 +118,7 @@
 
 	// 5999を開けるが、開いてないなら+1のポートを開ける。
 	void selectPort(int p) {
+		if(servSock != null ) return ;
 		int port = p;
 		while (true) {
 			try {
--- a/src/myVncProxy/VncProxyService.java	Sat Sep 03 15:24:41 2011 +0900
+++ b/src/myVncProxy/VncProxyService.java	Sun Sep 04 20:39:14 2011 +0900
@@ -53,7 +53,7 @@
 	// Variables read from parameter values.
 	String socketFactory;
 	String host;
-	int port;
+	int port = 5900;
 	String passwordParam;
 	boolean showControls;
 	boolean offerRelogin;
@@ -65,19 +65,51 @@
 	int debugStatsMeasureUpdates;
 
 	int echoPort = 9999;
-	
+
+	String url;
 
+	XmlRpc client;
+	String wpUsername, wpPassword, blogId;
+	String title = "TreeVNC";
 	
 	void checkArgs(String[] argv){
-		if(argv.length > 3){
-			username = argv[3]; 
-		}else if(argv.length < 2){
-			System.out.println("Please enter argv");
-			System.out.println("hostname(IPaddress) port password");
-			System.exit(0);
-		}else{
-			username = argv[0];
+		int len = argv.length;
+
+		for(int i = 0; i < len ; i ++) {
+			String str = argv[i];
+			if (str.equals("-h") || str.equals("--host")){
+				host = argv[++i];
+			} else if (str.equals("-pn") || str.equals("--port")){
+				port = Integer.parseInt(argv[++i]);
+			} else if (str.equals("-pw") || str.equals("--password")){
+				passwordParam = argv[++i];
+			} else if (str.equals("-w") || str.equals("--wpurl")){
+				url = argv[++i];
+			} else if (str.equals("-wu") || str.equals("--wpusername")){
+				wpUsername = argv[++i];
+			} else if (str.equals("-wp") || str.equals("--wppassword")){
+				wpPassword = argv[++i];
+			} else if (str.equals("-wb") || str.equals("--wpblogId")){
+				blogId = argv[++i];
+			} else if (str.equals("--help")) {
+				printHelp();
+				System.exit(0);
+			} else if (len == 3) {
+				host = argv[0];
+				port = Integer.parseInt(argv[1]);
+				passwordParam = argv[2];
+			} else if (len < 3){
+				printHelp();
+				System.exit(0);
+			}
 		}
+		
+	}
+	
+	void printHelp() {
+		System.out.println("usage: java -jar VncProxyService [-h hostname] " +
+				"[-pn portnumber] [-pw password] [-w WordPressURL] [-wu WPUsername] " +
+				"[-wp WPPassword] [-wb blogId]");
 	}
 	
 	//
@@ -87,7 +119,7 @@
 	public void init() {
 
 		readParameters();
-
+		
 		options = new OptionsNoFrame(this);
 		recordingSync = new Object();
 
@@ -107,10 +139,39 @@
 			fatalError("Network error: could not connect to server: " + host
 					+ ":" + port, e);
 		}catch(Exception e){}
-
+		
+		rfb.selectPort(5999);
 		rfbThread = new Thread(this);
 		accThread = new Thread(new AcceptThread(rfb, 5999)); 
 
+		if(url != null) {
+			try {
+				client = new XmlRpc(blogId, wpUsername, wpPassword, url );
+				InetAddress addr = InetAddress.getLocalHost();
+				String add = new String(addr.getHostAddress());
+				setStatusXmlRpc(client, title, add, rfb.acceptPort);
+				client.editPost();
+				System.out.println("URL:\n" + client.getUrl() + "\n");
+			} catch (MalformedURLException e) {
+				System.out.println("Faild create instance of class XmlRpc");
+				e.printStackTrace();
+			} catch (UnknownHostException e) {
+				e.printStackTrace();
+			}
+		}		
+		
+		
+	}
+	
+	void setStatusXmlRpc(XmlRpc client, String title, String hostname, int port) {
+		client.setTitle(title);
+		String description = makeDescription(hostname, Integer.toString(port));
+		client.setDescription(description);
+	}
+	
+	String makeDescription(String hostname, String port) {
+		String description = "<h1>" + hostname + " " + port + "<h1>\n";
+		return description;
 	}
 	
 	public void start_threads(){
@@ -201,7 +262,7 @@
 	//
 
 	void connectAndAuthenticate() throws Exception {
-	    acc = new AcceptClient(mainArgs[0]);
+	    acc = new AcceptClient(host);
 	    geth = new CreateThread(acc , echoPort);
 	    Thread thread = new Thread(geth);
 	    thread.start();
@@ -211,8 +272,6 @@
 
 		showConnectionStatus("Connecting to " + host + ", port " + port + "...");
 
-		// rfb = new RfbProto(host, port, this);
-//		rfb = new MyRfbProto(host, port);
 		rfb = new MyRfbProto(host, port, geth);
 
 		showConnectionStatus("Connected to server");
@@ -538,14 +597,6 @@
 
 	void readParameters() {
 
-		host = mainArgs[0];
-		if(mainArgs.length < 2){
-			port = 5900;
-		}else{
-			port = Integer.parseInt(mainArgs[1]);
-		}
-		
-
 		// Read "ENCPASSWORD" or "PASSWORD" parameter if specified.
 //		readPasswordParameters();
 
--- a/src/test/XmlRpc.java	Sat Sep 03 15:24:41 2011 +0900
+++ b/src/test/XmlRpc.java	Sun Sep 04 20:39:14 2011 +0900
@@ -14,9 +14,11 @@
 public class XmlRpc {
 	static final String META_WEBLOG_NEW_POST = "metaWeblog.newPost";
 	static final String META_WEBLOG_EDIT_POST = "metaWeblog.editPost";
-
+	static final String META_WEBLOG_GET_POST = "metaWeblog.getPost";
+	
 	XmlRpcClient client;
-
+	XmlRpcClientConfigImpl config;
+	
 	String loginId, pw, postUrl;
 	String blogId;
 	String title = ""; 
@@ -27,12 +29,22 @@
 		this.blogId = blogId;
 		this.loginId = loginId;
 		this.pw = pw;
-		postUrl = url;
-		client = createClient(url);
+		postUrl = checkUrl(url);
+		client = createClient(postUrl);
 	}
-	
+	String checkUrl(String url) {
+		String str;
+		if(url.endsWith("xmlrpc.php")){
+			return url;
+		} else if(url.endsWith("/")) {
+			str = url + "xmlrpc.php";
+		} else {
+			str = url + "/xmlrpc.php";
+		}
+		return str;
+	}
 	XmlRpcClient createClient(String url) throws MalformedURLException {
-		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
+		config = new XmlRpcClientConfigImpl();
 		config.setServerURL(new URL(url.trim()));
 
 		XmlRpcClient c = new XmlRpcClient();
@@ -41,6 +53,9 @@
 		return c; 
 	}
 
+	String newPost() {
+		return post(META_WEBLOG_NEW_POST);
+	}
 	String editPost() {
 		return post(META_WEBLOG_EDIT_POST);
 	}
@@ -54,7 +69,7 @@
 
 		Object o = null;
 		try {
-			o = client.execute(META_WEBLOG_EDIT_POST, params);
+			o = client.execute(API, params);
 			result = o.toString();
 		} catch (XmlRpcException e) {
 			e.printStackTrace();
@@ -92,6 +107,7 @@
 		
 		return content;
 	}
+	
 	protected List<Object> buildParam(Map<String, Object> contentParam) {
 		List<Object> params = new ArrayList<Object>();
 
@@ -104,29 +120,63 @@
 
 		return params;
 	}
+	
+	String getUrl() {
+		List<Object> params = new ArrayList<Object>();
+
+		params.add(blogId);
+		params.add(loginId);
+		params.add(pw);
 		
-	public static void main(String[] args) throws MalformedURLException {
+		Object result = null;
+		String str = "false";
+		try {
+			result = weblog(params, META_WEBLOG_GET_POST);
+			Map<String, Object> map = (HashMap) result;
+			str = (String) map.get("link");
+		} catch (XmlRpcException e) {
+			e.printStackTrace();
+		}
+		return str;
+	}
+	
+	Object weblog(List<Object> params, String API) throws XmlRpcException {
+		Object o = null;
+		o = client.execute(API, params);
+		return o;
+	}
+	
+	
+	
+
+	public static void main(String[] args){
 		if (args.length < 2) {
-			System.out.println("usage: XmlRpc username password ");
+			System.out.println("usage: program username password ");
 			System.exit(0);
 		}
 		String username = args[0];
 		String password = args[1];
 		
 		String blogId = "77";
-		String url = "http://single.cr.ie.u-ryukyu.ac.jp/wordpress/xmlrpc.php";
+//		String url = "http://single.cr.ie.u-ryukyu.ac.jp/wordpress/xmlrpc.php";
+//		String url = "http://single.cr.ie.u-ryukyu.ac.jp/wordpress/";
+		String url = "http://single.cr.ie.u-ryukyu.ac.jp/wordpress";
 		
-		XmlRpc rpc = new XmlRpc(blogId, username, password, url );
-		rpc.setTitle("xml-rpcを用いての投稿 @ java");
-		rpc.setDescription("java からの投稿\nテスト");
-		rpc.addCategories("XML-RPC");
+		XmlRpc rpc;
+		try {
+			rpc = new XmlRpc(blogId, username, password, url );
+			rpc.setTitle("xml-rpcを用いての投稿 @ java");
+			rpc.setDescription("java からの投稿\nテスト\n");
+			rpc.addDescription("addDescription()\n");
+			rpc.addCategories("XML-RPC");
 
-		String res = rpc.editPost();
-		System.out.println(res);
+			String res = rpc.getUrl();
+//			String res = rpc.editPost();
+			System.out.println(res);
+		} catch (MalformedURLException e) {
+			e.printStackTrace();
+		}
 
-		
 	}
 	
-	
-	
 }