eclipse删除所有注释

Ctrl+F:  /\*{1,}[\s\S]*?\*/
选择正则表达式,替换全部即可。
发表在 Linux | 留下评论

android Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mtjst.im.ui"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.mtjst.im.ui.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name="com.mtjst.im.service.MqttService" />

        <receiver android:name="com.mtjst.im.service.MqttBootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

 

发表在 android | 留下评论

android 两种消息通知方式

/*******************************************************************************
 * Copyright (c) 1999, 2014 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution. 
 *
 * The Eclipse Public License is available at 
 *    http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 */
package com.mtjst.im.service;

import java.util.Calendar;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat.Builder;
import android.widget.Toast;

import com.mtjst.im.ui.R;

/**
 * Provides static methods for creating and showing notifications to the user.
 * 
 */
public class MqttNotify {

	private static int MessageID = 0;

	public static void notifcation(Context context, String messageString,
			Intent intent, String notificationTitle) {

		// Get the notification manage which we will use to display the
		// notification
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) context
				.getSystemService(ns);

		Calendar.getInstance().getTime().toString();

		long when = System.currentTimeMillis();

		// the message that will be displayed as the ticker
		String ticker = notificationTitle + " " + messageString;

		// build the pending intent that will start the appropriate activity
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0x01,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);

		// build the notification
		Builder notificationCompat = new Builder(context);
		notificationCompat.setAutoCancel(true)
				.setContentTitle(notificationTitle)
				.setContentIntent(pendingIntent).setContentText(messageString)
				.setTicker(ticker).setWhen(when)
				.setSmallIcon(R.drawable.ic_launcher);

		Notification notification = notificationCompat.build();
		notification.defaults = Notification.DEFAULT_SOUND;
		// display the notification
		mNotificationManager.notify(MessageID, notification);
		MessageID++;

	}

	/**
	 * Display a toast notification to the user
	 * 
	 * @param context
	 *            Context from which to create a notification
	 * @param text
	 *            The text the toast should display
	 * @param duration
	 *            The amount of time for the toast to appear to the user
	 */
	public static void toast(Context context, CharSequence text, int duration) {
		Toast toast = Toast.makeText(context, text, duration);
		toast.show();
	}

}

 

发表在 android | 留下评论

nodejs mqtt 定时随机发送糗百帖子

var request = require('request');
var mqtt = require('mqtt');

client = mqtt.createClient(1883, 'localhost', {
    clientId: "Test",
    keepalive: 0,
    clean: false
});

function publishRandomThread() {
	request('http://www.qiushibaike.com/history', function (error, response, body) {
	if (!error && response.statusCode == 200) {
  		var matches = body.match(/<div class="mb-m fs-l">[\s\n]*?(.*?)[\s\n]*?<\/div>/g);
	  		client.publish('$SYS/Musikar', matches[0].replace(/<.*?>/g, ''));
	  	}
	})
}

setInterval(function(){
	publishRandomThread();
}, 60000);

 

发表在 android | 留下评论

android 判断服务有没有启动

public boolean isServiceRunning(Context context) {
	boolean isRunning = false;

	ActivityManager activityManager = (ActivityManager) context
			.getSystemService(Context.ACTIVITY_SERVICE);
	List<ActivityManager.RunningServiceInfo> serviceList = activityManager
			.getRunningServices(Integer.MAX_VALUE);

	if (serviceList.size() < 1) {
		return false;
	}

	for (int i = 0; i < serviceList.size(); i++) {
		if (serviceList.get(i).service.getClassName().equals(
				BootService.class.getName())) {
			isRunning = true;
			break;
		}
	}
	return isRunning;
}

 

发表在 Linux | 留下评论

swoole异步读文件

<?php
	
	swoole_async_readfile(__DIR__ . '/demo.php', function($filename, $content){
		echo $content.PHP_EOL;
		swoole_event_exit();
	});
?>

 

发表在 swoole | 标签为 | 留下评论

strace工具的使用

strace可以跟踪系统调用的执行情况,在程序发生问题后,可以用strace分析和跟踪问题。 使用方法:

FreeBSD/MacOS下可以使用truss

strace -o /tmp/strace.log -f -p $PID
  • -f 表示跟踪多线程和多进程,如果不加-f参数,无法抓取到子进程和子线程的运行情况
  • -o 表示将结果输出到一个文件中
  • -p $PID,指定跟踪的进程ID,通过ps aux可以看到
  • -tt 打印系统调用发生的时间,精确到微妙
发表在 Linux | 留下评论

tcpdump抓包工具的使用

在调试网络通信程序是tcpdump是必备工具。tcpdump很强大,可以看到网络通信的每个细节。如TCP,可以看到3次握手,PUSH/ACT数据推送,close4次挥手,全部细节。包括每一次网络收包的字节数,时间等。

最简单的一个使用示例:

sudo tcpdump -i any tcp port 9501
  • -i 参数制定了网卡,any表示所有网卡
  • tcp 指定仅监听TCP协议
  • port 制定监听的端口

tcpdump需要root权限
需要要看通信的数据内容,可以加 -Xnlps0 参数,其他更多参数请参见网上的文章

运行结果

13:29:07.788802 IP localhost.42333 > localhost.9501: Flags [S], seq 828582357, win 43690, options [mss 65495,sackOK,TS val 2207513 ecr 0,nop,wscale 7], length 0
13:29:07.788815 IP localhost.9501 > localhost.42333: Flags [S.], seq 1242884615, ack 828582358, win 43690, options [mss 65495,sackOK,TS val 2207513 ecr 2207513,nop,wscale 7], length 0
13:29:07.788830 IP localhost.42333 > localhost.9501: Flags [.], ack 1, win 342, options [nop,nop,TS val 2207513 ecr 2207513], length 0
13:29:10.298686 IP localhost.42333 > localhost.9501: Flags [P.], seq 1:5, ack 1, win 342, options [nop,nop,TS val 2208141 ecr 2207513], length 4
13:29:10.298708 IP localhost.9501 > localhost.42333: Flags [.], ack 5, win 342, options [nop,nop,TS val 2208141 ecr 2208141], length 0
13:29:10.298795 IP localhost.9501 > localhost.42333: Flags [P.], seq 1:13, ack 5, win 342, options [nop,nop,TS val 2208141 ecr 2208141], length 12
13:29:10.298803 IP localhost.42333 > localhost.9501: Flags [.], ack 13, win 342, options [nop,nop,TS val 2208141 ecr 2208141], length 0
13:29:11.563361 IP localhost.42333 > localhost.9501: Flags [F.], seq 5, ack 13, win 342, options [nop,nop,TS val 2208457 ecr 2208141], length 0
13:29:11.563450 IP localhost.9501 > localhost.42333: Flags [F.], seq 13, ack 6, win 342, options [nop,nop,TS val 2208457 ecr 2208457], length 0
13:29:11.563473 IP localhost.42333 > localhost.9501: Flags [.], ack 14, win 342, options [nop,nop,TS val 2208457 ecr 2208457], length 0
  • 13:29:11.563473 时间带有精确到微妙
  • localhost.42333 > localhost.9501 表示通信的流向,42333是客户端,9501是服务器端
  • [S] 表示这是一个SYN请求
  • [.] 表示这是一个ACT确认包,(client)SYN->(server)SYN->(client)ACT 就是3次握手过程
  • [P] 表示这个是一个数据推送,可以是从服务器端向客户端推送,也可以从客户端向服务器端推
  • [F] 表示这是一个FIN包,是关闭连接操作,client/server都有可能发起
  • [R] 表示这是一个RST包,与F包作用相同,但RST表示连接关闭时,仍然有数据未被处理。可以理解为是强制切断连接
  • win 342是指滑动窗口大小
  • length 12指数据包的大小
发表在 Linux | 留下评论

TCP连接的状态

 

  • LISTEN:首先服务端需要打开一个socket进行监听,状态为LISTEN. /* The socket is listening for incoming connections. 侦听来自远方TCP端口的连接请求 */
  • SYN_SENT:客户端通过应用程序调用connect进行active open.于是客户端tcp发送一个SYN以请求建立一个连接.之后状态置为SYN_SENT. /*The socket is actively attempting to establish a connection. 在发送连接请求后等待匹配的连接请求 */
  • SYN_RECV:服务端应发出ACK确认客户端的SYN,同时自己向客户端发送一个SYN.之后状态置为SYN_RECV /* A connection request has been received from the network. 在收到和发送一个连接请求后等待对连接请求的确认 */
  • ESTABLISHED: 代表一个打开的连接,双方可以进行或已经在数据交互了。/* The socket has an established connection. 代表一个打开的连接,数据可以传送给用户 */
  • FIN_WAIT1:主动关闭(active close)端应用程序调用close,于是其TCP发出FIN请求主动关闭连接,之后进入FIN_WAIT1状态./* The socket is closed, and the connection is shutting down. 等待远程TCP的连接中断请求,或先前的连接中断请求的确认 */
  • CLOSE_WAIT:被动关闭(passive close)端TCP接到FIN后,就发出ACK以回应FIN请求(它的接收也作为文件结束符传递给上层应用程序),并进入CLOSE_WAIT. /* The remote end has shut down, waiting for the socket to close. 等待从本地用户发来的连接中断请求 */
  • FIN_WAIT2:主动关闭端接到ACK后,就进入了FIN-WAIT-2 ./* Connection is closed, and the socket is waiting for a shutdown from the remote end. 从远程TCP等待连接中断请求 */
  • LAST_ACK:被动关闭端一段时间后,接收到文件结束符的应用程序将调用CLOSE关闭连接。这导致它的TCP也发送一个 FIN,等待对方的ACK.就进入了LAST-ACK . /* The remote end has shut down, and the socket is closed. Waiting for acknowledgement. 等待原来发向远程TCP的连接中断请求的确认 */
  • TIME_WAIT:在主动关闭端接收到FIN后,TCP就发送ACK包,并进入TIME-WAIT状态。/* The socket is waiting after close to handle packets still in the network.等待足够的时间以确保远程TCP接收到连接中断请求的确认 */
  • CLOSING: 比较少见./* Both sockets are shut down but we still don’t have all our data sent. 等待远程TCP对连接中断的确认 */
  • CLOSED: 被动关闭端在接受到ACK包后,就进入了closed的状态。连接结束./* The socket is not being used. 没有任何连接状态 */

 

发表在 Linux | 留下评论

Linux信号列表

 

  • SIGHUP
  • SIGINT
  • SIGQUIT
  • SIGILL
  • SIGTRAP
  • SIGABRT
  • SIGBUS
  • SIGFPE
  • SIGKILL
  • SIGUSR1
  • SIGSEGV
  • SIGUSR2
  • SIGPIPE
  • SIGALRM
  • SIGTERM
  • SIGCHLD
  • SIGCONT
  • SIGSTOP
  • SIGTSTP
  • SIGTTIN
  • SIGTTOU
  • SIGURG
  • SIGXCPU
  • SIGXFSZ
  • SIGVTALRM
  • SIGPROF
  • SIGWINCH
  • SIGIO
  • SIGPWR
  • SIGSYS
  • SIGRTMIN
  • SIGRTMIN+1
  • SIGRTMIN+2
  • SIGRTMIN+3
  • SIGRTMIN+4
  • SIGRTMIN+5
  • SIGRTMIN+6
  • SIGRTMIN+7
  • SIGRTMIN+8
  • SIGRTMIN+9
  • SIGRTMIN+10
  • SIGRTMIN+11
  • SIGRTMIN+12
  • SIGRTMIN+13
  • SIGRTMIN+14
  • SIGRTMIN+15
  • SIGRTMAX-14
  • SIGRTMAX-13
  • SIGRTMAX-12
  • SIGRTMAX-11
  • SIGRTMAX-10
  • SIGRTMAX-9
  • SIGRTMAX-8
  • SIGRTMAX-7
  • SIGRTMAX-6
  • SIGRTMAX-5
  • SIGRTMAX-4
  • SIGRTMAX-3
  • SIGRTMAX-2
  • SIGRTMAX-1
  • SIGRTMAX

 

发表在 Linux | 留下评论