Skip to content

Commit

Permalink
Finished uPnP portmaping, need real testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhou Chang committed Nov 2, 2012
1 parent 5c92de0 commit de72e9f
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 10 deletions.
82 changes: 82 additions & 0 deletions jni/natpmpclient.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,92 @@
#include <string>
#include <sstream>
#include <sys/select.h>
#include "beginvision.h"

#include "libnatpmp/natpmp.h"

#define JNIDEFINE(fname) Java_teaonly_droideye_MainActivity_##fname

extern "C" {
JNIEXPORT jstring JNICALL JNIDEFINE(nativeQueryInternet)(JNIEnv* env, jclass clz);
};


jstring JNICALL JNIDEFINE(nativeQueryInternet)(JNIEnv* env, jclass clz) {
natpmp_t natpmp;
natpmpresp_t response;
std::string ipaddr;
uint16_t privatePort = 8080;
uint16_t publicPort = 7910;
uint32_t lifetime = 36000; // 10 hours
int protocol = NATPMP_PROTOCOL_TCP;
in_addr_t gateway = 0;
fd_set fds;
struct timeval timeout;
int ret;
std::string result = "";

// 1. init internal object
ret = initnatpmp(&natpmp, 0, gateway);
if (ret < 0) {
result = "error:gateway";
goto _done;
}

// 2. get internet public ip address
ret = sendpublicaddressrequest(&natpmp);
if ( ret != 2) {
result = "error:ipaddr";
goto _done;
}
FD_ZERO(&fds);
FD_SET(natpmp.s, &fds);
getnatpmprequesttimeout(&natpmp, &timeout);
ret = select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
if(ret < 0) {
result = "error:select";
goto _done;
}
ret = readnatpmpresponseorretry(&natpmp, &response);
if( ret == NATPMP_TRYAGAIN ) {
result = "error:timeout";
goto _done;
} else if ( ret < 0) {
result = "error:failed";
goto _done;
}
ipaddr = inet_ntoa(response.pnu.publicaddress.addr);

// 3. set extern port mapping
ret = sendnewportmappingrequest(&natpmp, protocol,
privatePort, publicPort,
lifetime);
FD_ZERO(&fds);
FD_SET(natpmp.s, &fds);
getnatpmprequesttimeout(&natpmp, &timeout);
ret = select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
if(ret < 0) {
result = "error:select";
goto _done;
}
ret = readnatpmpresponseorretry(&natpmp, &response);
if( ret == NATPMP_TRYAGAIN ) {
result = "error:timeout";
goto _done;
} else if ( ret < 0) {
result = "error:failed";
goto _done;
}
privatePort = response.pnu.newportmapping.privateport;
publicPort = response.pnu.newportmapping.mappedpublicport;

{
std::stringstream stream;
stream << "http://" << ipaddr << ":" << publicPort;
result = stream.str();
}

_done:
return env->NewStringUTF(result.c_str());
}

14 changes: 12 additions & 2 deletions res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,19 @@
android:layout_width="fill_parent"
android:gravity="center"
android:background="#88333333"
android:orientation="horizontal">
android:orientation="vertical">
<TextView
android:id="@+id/tv_message1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft = "5dip"
android:layout_marginRight = "5dip"
android:gravity="center"
android:textColor="#FFFFFFFF"
android:textSize="24dip"/>
<TextView
android:id="@+id/tv_message"
android:id="@+id/tv_message2"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft = "5dip"
Expand Down
8 changes: 5 additions & 3 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
<resources>
<string name="app_name">Wifi Camera</string>

<string name="action_setup">Setup</string>
<string name="action_exit">Close</string>
<string name="action_exit">Exit</string>

<string name="msg_access">Open URL in PC </string>
<string name="msg_access_local">Open at local: </string>
<string name="msg_access_internet">Open at internet: </string>
<string name="msg_access_query">Detecting internet ... </string>
<string name="msg_access_query_error">Can not be opened at internet</string>
<string name="msg_error">Error: Please enable wifi</string>
</resources>
41 changes: 36 additions & 5 deletions src/teaonly/droideye/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public class MainActivity extends Activity
private CameraView cameraView_;
private OverlayView overlayView_;
private Button btnExit;
private TextView tvMessage;
private TextView tvMessage1;
private TextView tvMessage2;

private AudioRecord audioCapture = null;
private StreamingLoop audioLoop = null;
Expand All @@ -96,7 +97,8 @@ public void onCreate(Bundle savedInstanceState) {

btnExit = (Button)findViewById(R.id.btn_exit);
btnExit.setOnClickListener(exitAction);
tvMessage = (TextView)findViewById(R.id.tv_message);
tvMessage1 = (TextView)findViewById(R.id.tv_message1);
tvMessage2 = (TextView)findViewById(R.id.tv_message2);

for(int i = 0; i < maxVideoNumber; i++) {
videoFrames[i] = new VideoFrame(1024*1024*2);
Expand Down Expand Up @@ -224,12 +226,16 @@ private boolean initWebServer() {
}
}
if ( webServer != null) {
tvMessage.setText( getString(R.string.msg_access) + " http://" + ipAddr + ":8080" );
tvMessage1.setText( getString(R.string.msg_access_local) + " http://" + ipAddr + ":8080" );
tvMessage2.setText( getString(R.string.msg_access_query));
tvMessage2.setVisibility(View.VISIBLE);
return true;
} else {
tvMessage.setText( getString(R.string.msg_error) );
tvMessage1.setText( getString(R.string.msg_error) );
tvMessage2.setVisibility(View.GONE);
return false;
}

}

private OnClickListener exitAction = new OnClickListener() {
Expand Down Expand Up @@ -414,5 +420,30 @@ public void run() {
nativeCloseEncoder();
}
}
}


static private native String nativeQueryInternet();
private class NatPMPClinet extends Thread {
String ret = nativeQueryInternet();
@Override
public void run(){
Handler handleQueryResult = new Handler(getMainLooper());
if ( ret.startsWith("error:") ) {
handleQueryResult.post( new Runnable() {
@Override
public void run() {
tvMessage2.setText( getString(R.string.msg_access_query_error));
}
});
} else {
handleQueryResult.post( new Runnable() {
@Override
public void run() {
tvMessage2.setText( getString(R.string.msg_access_internet) + " " + ret );
}
});
}
}
}
}

0 comments on commit de72e9f

Please sign in to comment.