Compare commits
4 Commits
0346955015
...
1348a3b6b6
Author | SHA1 | Date |
---|---|---|
pyxu2pz5g | 1348a3b6b6 | 3 years ago |
Xie Changrong | 69e9e55aac | 3 years ago |
Xie Changrong | d1f1936b09 | 3 years ago |
Xie Changrong | 7889a9e970 | 3 years ago |
Binary file not shown.
Binary file not shown.
@ -0,0 +1,35 @@
|
||||
package com.example.logistics.tools;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.java_websocket.client.WebSocketClient;
|
||||
import org.java_websocket.drafts.Draft_6455;
|
||||
import org.java_websocket.handshake.ServerHandshake;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
public class JWebSocketClient extends WebSocketClient {
|
||||
public JWebSocketClient(URI serverUri) {
|
||||
super(serverUri, new Draft_6455());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpen(ServerHandshake handshakedata) {
|
||||
Log.e("JWebSocketClient", "onOpen()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(String message) {
|
||||
Log.e("JWebSocketClient", "onMessage()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose(int code, String reason, boolean remote) {
|
||||
Log.e("JWebSocketClient", "onClose()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception ex) {
|
||||
Log.e("JWebSocketClient", "onError()");
|
||||
}
|
||||
}
|
@ -0,0 +1,200 @@
|
||||
package com.example.logistics.ui;
|
||||
|
||||
import com.example.logistics.R;
|
||||
import com.example.logistics.manager.goodManager;
|
||||
import com.example.logistics.manager.operationManager;
|
||||
import com.example.logistics.manager.userManager;
|
||||
import com.example.logistics.entity.User;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
|
||||
public class LoginActivity extends Activity {
|
||||
|
||||
private int islogin = 0;
|
||||
private Button loginButton;
|
||||
private Button signUpButton;
|
||||
private Button forgetButton;
|
||||
private Button phonenumButton;
|
||||
private AlertDialog alert;
|
||||
private Button changeButton;
|
||||
private EditText fphonenum;
|
||||
private EditText fpassword;
|
||||
int flag = 0; //flag = 0 是用户名登录;=1是手机号
|
||||
|
||||
// 调用Actvity
|
||||
private String TAG = "LoginActivity";
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
loginButton = (Button) this.findViewById(R.id.LoginButton);
|
||||
signUpButton = (Button) this.findViewById(R.id.SignUpButton);
|
||||
forgetButton = (Button) this.findViewById(R.id.forget);
|
||||
phonenumButton = (Button) this.findViewById(R.id.phonenum);
|
||||
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
|
||||
|
||||
View view2 = View.inflate(LoginActivity.this, R.layout.forget, null);
|
||||
|
||||
builder.setTitle("取件").setView(view2);
|
||||
|
||||
alert = builder.create();
|
||||
|
||||
changeButton = (Button) view2.findViewById(R.id.change);
|
||||
fphonenum = (EditText) view2.findViewById(R.id.fphonenum);
|
||||
fpassword = (EditText) view2.findViewById(R.id.fpassword);
|
||||
|
||||
loginButton.setOnClickListener(
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
login(v);
|
||||
Log.d(TAG, "login");
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
signUpButton.setOnClickListener(
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
|
||||
Log.d(TAG, "signup");
|
||||
startActivity(intent);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
forgetButton.setOnClickListener(
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
alert.show();
|
||||
changeButton.setOnClickListener(
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.d(TAG, "change password");
|
||||
change(view);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
EditText userName = (EditText) this.findViewById(R.id.UserNameEdit);
|
||||
phonenumButton.setOnClickListener(
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
userName.setHint("请输入手机号");
|
||||
flag = 1;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
public void change(View view){
|
||||
String phoneunm = fphonenum.getText().toString().trim();
|
||||
String password = fpassword.getText().toString().trim();
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
userManager userManager = new userManager();
|
||||
|
||||
int msg = userManager.change(phoneunm, password);
|
||||
|
||||
Log.d(TAG, "change");
|
||||
if(msg != 1){
|
||||
Log.d(TAG, "pickup,failed");
|
||||
hand2.sendEmptyMessage(msg);
|
||||
return;
|
||||
}
|
||||
Log.d(TAG, "change,success");
|
||||
|
||||
hand2.sendEmptyMessage(msg);
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
public void login(View view){
|
||||
EditText userName = (EditText) this.findViewById(R.id.UserNameEdit);
|
||||
EditText passWord = (EditText) this.findViewById(R.id.PassWordEdit);
|
||||
|
||||
new Thread(){
|
||||
@Override
|
||||
public void run(){
|
||||
userManager userManager = new userManager();
|
||||
int msg = 0;
|
||||
if(flag == 0){
|
||||
msg = userManager.login(userName.getText().toString().trim(), passWord.getText().toString().trim());
|
||||
}else if(flag == 1){
|
||||
msg = userManager.loginByPhone(userName.getText().toString().trim(), passWord.getText().toString().trim());
|
||||
|
||||
}
|
||||
Log.e("MAin", "msg");
|
||||
hand1.sendEmptyMessage(msg);
|
||||
if(msg == 1){
|
||||
User owner = userManager.findUser(userName.getText().toString().trim());
|
||||
Intent intent = new Intent(LoginActivity.this, MenuActivity.class);
|
||||
if(flag == 1){
|
||||
intent.putExtra("user", userName.getText().toString().trim());
|
||||
}else{
|
||||
intent.putExtra("user", owner.getPhoneNum());
|
||||
}
|
||||
startActivity(intent);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
@SuppressLint("HandlerLeak")
|
||||
final Handler hand1 = new Handler(){
|
||||
@Override
|
||||
public void handleMessage(Message msg){
|
||||
if(msg.what == 0){
|
||||
Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_SHORT).show();
|
||||
}else if(msg.what == 1){
|
||||
Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
|
||||
islogin = 1;
|
||||
}else if(msg.what == 2){
|
||||
Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT).show();
|
||||
}else if(msg.what == 3){
|
||||
Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
};
|
||||
@SuppressLint("HandlerLeak")
|
||||
final Handler hand2 = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
if(msg.what == 0){
|
||||
Toast.makeText(LoginActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
|
||||
}else if(msg.what == 1) {
|
||||
Toast.makeText(LoginActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
package com.example.logistics.ui;
|
||||
|
||||
import com.example.logistics.R;
|
||||
import com.example.logistics.dao.userDao;
|
||||
import com.example.logistics.entity.User;
|
||||
import com.example.logistics.tools.MD5Utils;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
public class MainActivity extends Activity {
|
||||
|
||||
private int islogin = 0;
|
||||
// 调用Actvity
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
Button loginButton = (Button) this.findViewById(R.id.LoginButton);
|
||||
Button signUpButton = (Button) this.findViewById(R.id.SignUpButton);
|
||||
|
||||
loginButton.setOnClickListener(
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
login(v);
|
||||
}
|
||||
}
|
||||
);
|
||||
signUpButton.setOnClickListener(
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Intent intent = new Intent(MainActivity.this, SignUpActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
public void login(View view){
|
||||
EditText userName = (EditText) this.findViewById(R.id.UserNameEdit);
|
||||
EditText passWord = (EditText) this.findViewById(R.id.PassWordEdit);
|
||||
|
||||
new Thread(){
|
||||
@Override
|
||||
public void run(){
|
||||
userDao userDao = new userDao();
|
||||
int msg = userDao.login(userName.getText().toString().trim(), passWord.getText().toString().trim());
|
||||
Log.e("MAin", "msg");
|
||||
hand1.sendEmptyMessage(msg);
|
||||
if(msg == 1){
|
||||
User owner = userDao.findUser(userName.getText().toString().trim());
|
||||
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
|
||||
intent.putExtra("user", owner.getPhoneNum());
|
||||
startActivity(intent);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
@SuppressLint("HandlerLeak")
|
||||
final Handler hand1 = new Handler(){
|
||||
@Override
|
||||
public void handleMessage(Message msg){
|
||||
if(msg.what == 0){
|
||||
Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_SHORT).show();
|
||||
}else if(msg.what == 1){
|
||||
Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
|
||||
islogin = 1;
|
||||
}else if(msg.what == 2){
|
||||
Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT).show();
|
||||
}else if(msg.what == 3){
|
||||
Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
@ -1,220 +0,0 @@
|
||||
package com.example.logistics.ui;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.example.logistics.R;
|
||||
import com.example.logistics.dao.goodDao;
|
||||
import com.example.logistics.dao.operationDao;
|
||||
import com.example.logistics.dao.userDao;
|
||||
import com.example.logistics.entity.User;
|
||||
import com.google.zxing.integration.android.IntentIntegrator;
|
||||
import com.google.zxing.integration.android.IntentResult;
|
||||
import com.journeyapps.barcodescanner.CaptureActivity;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class MyFragment2 extends Fragment implements View.OnClickListener{
|
||||
|
||||
private Button mButton;
|
||||
private Context mContext;
|
||||
|
||||
private WebView mWebView;
|
||||
|
||||
public MyFragment2(){
|
||||
|
||||
}
|
||||
@Nullable
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.my_fragment2,container,false);
|
||||
mWebView = (WebView) view.findViewById(R.id.webview);
|
||||
|
||||
WebSettings webSettings = mWebView.getSettings();
|
||||
mButton = (Button)view.findViewById(R.id.Pick_Button);
|
||||
mButton.setOnClickListener(this);
|
||||
mContext = getActivity();
|
||||
|
||||
webSettings.setJavaScriptEnabled(true);
|
||||
mWebView.loadUrl("file:///android_asset/js.html");
|
||||
|
||||
Log.i("set", "listner");
|
||||
|
||||
mWebView.setWebViewClient(new WebViewClient(){
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url){
|
||||
super.onPageFinished(view, url);
|
||||
String jsStr1 = "";
|
||||
try{
|
||||
InputStream in1 = mContext.getAssets().open("em.js");
|
||||
byte buff[] = new byte[1024];
|
||||
ByteArrayOutputStream fromFile = new ByteArrayOutputStream();
|
||||
do {
|
||||
int numRead = in1.read(buff);
|
||||
if (numRead <= 0) {
|
||||
break;
|
||||
}
|
||||
fromFile.write(buff, 0, numRead);
|
||||
} while (true);
|
||||
jsStr1 = fromFile.toString();
|
||||
in1.close();
|
||||
fromFile.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
mWebView.loadUrl("javascript:" + jsStr1);
|
||||
|
||||
String jsStr2 = "";
|
||||
try{
|
||||
InputStream in2 = mContext.getAssets().open("roslib.min.js");
|
||||
byte buff[] = new byte[1024];
|
||||
ByteArrayOutputStream fromFile = new ByteArrayOutputStream();
|
||||
do {
|
||||
int numRead = in2.read(buff);
|
||||
if (numRead <= 0) {
|
||||
break;
|
||||
}
|
||||
fromFile.write(buff, 0, numRead);
|
||||
} while (true);
|
||||
jsStr2 = fromFile.toString();
|
||||
in2.close();
|
||||
fromFile.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
mWebView.loadUrl("javascript:" + jsStr2);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
switch(view.getId()){
|
||||
case R.id.Pick_Button:
|
||||
/*new IntentIntegrator(getActivity())
|
||||
.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE)// 扫码的类型,一维码,二维码,一/二维码,默认为一/二维码
|
||||
.setPrompt("请对准摄像头")
|
||||
.setCameraId(0)
|
||||
.setBeepEnabled(false)
|
||||
.setCaptureActivity(CaptureActivity.class)
|
||||
.initiateScan();*/
|
||||
pickup("abnc");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void test(){
|
||||
mWebView.loadUrl("javascript:callJS()");
|
||||
Log.i("abc", "bcd");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@SuppressLint("MissingSuperCall")
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
|
||||
if(result != null) {
|
||||
if(result.getContents() != null) {
|
||||
//Toast.makeText(mContext, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
|
||||
pickup(result.getContents());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void pickup(String result){
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
|
||||
|
||||
View view2 = View.inflate(mContext, R.layout.pickup, null);
|
||||
|
||||
EditText PickUpCode = (EditText) view2.findViewById(R.id.pk_PickUp);
|
||||
EditText PhoneNum = (EditText) view2.findViewById(R.id.pk_PhoneNum);
|
||||
Button button = (Button) view2.findViewById(R.id.pk_button);
|
||||
|
||||
builder.setTitle("取件").setView(view2);
|
||||
|
||||
builder.create().show();
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
String pickupcode = PickUpCode.getText().toString().trim();
|
||||
String phonenum = PhoneNum.getText().toString().trim();
|
||||
Log.d("onclick", "123");
|
||||
//String QR_pickup = result.split(" ")[0];
|
||||
//String QR_phone = result.split(" ")[1];
|
||||
|
||||
if(true){
|
||||
//if (pickupcode.equals(QR_pickup) && phonenum.equals(QR_phone)) {
|
||||
/*new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
int msg = 0;
|
||||
|
||||
operationDao operationDao = new operationDao();
|
||||
goodDao goodDao = new goodDao();
|
||||
Bundle bundle = getArguments();
|
||||
|
||||
String user = bundle.getString("user");
|
||||
|
||||
boolean flag1 = operationDao.add(pickupcode, user);
|
||||
boolean flag2 = goodDao.delete(pickupcode);
|
||||
|
||||
boolean flag = flag1 & flag2;
|
||||
if (flag) {
|
||||
msg = 1;
|
||||
}
|
||||
int msg = 1;
|
||||
test();
|
||||
hand1.sendEmptyMessage(msg);
|
||||
}
|
||||
}.start();*/
|
||||
Toast.makeText(mContext, "取件成功", Toast.LENGTH_LONG).show();
|
||||
test();
|
||||
|
||||
} else {
|
||||
Toast.makeText(mContext, "取件失败", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressLint("HandlerLeak")
|
||||
final Handler hand1 = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
if (msg.what == 1) {
|
||||
Toast.makeText(mContext, "取件成功", Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
Toast.makeText(mContext, "取件失败", Toast.LENGTH_SHORT);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
<EditText
|
||||
android:layout_width="200dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fphonenum"
|
||||
android:hint="请输入注册时手机号"
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
<EditText
|
||||
android:layout_width="200dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fpassword"
|
||||
android:hint="请输入新密码"
|
||||
/>
|
||||
</LinearLayout>
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="修改"
|
||||
android:id="@+id/change"/>
|
||||
|
||||
</LinearLayout>
|
@ -0,0 +1,33 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
Binary file not shown.
@ -0,0 +1,2 @@
|
||||
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.4/apache-maven-3.8.4-bin.zip
|
||||
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar
|
@ -0,0 +1,316 @@
|
||||
#!/bin/sh
|
||||
# ----------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
# Maven Start Up Batch script
|
||||
#
|
||||
# Required ENV vars:
|
||||
# ------------------
|
||||
# JAVA_HOME - location of a JDK home dir
|
||||
#
|
||||
# Optional ENV vars
|
||||
# -----------------
|
||||
# M2_HOME - location of maven2's installed home dir
|
||||
# MAVEN_OPTS - parameters passed to the Java VM when running Maven
|
||||
# e.g. to debug Maven itself, use
|
||||
# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
|
||||
# MAVEN_SKIP_RC - flag to disable loading of mavenrc files
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
if [ -z "$MAVEN_SKIP_RC" ] ; then
|
||||
|
||||
if [ -f /usr/local/etc/mavenrc ] ; then
|
||||
. /usr/local/etc/mavenrc
|
||||
fi
|
||||
|
||||
if [ -f /etc/mavenrc ] ; then
|
||||
. /etc/mavenrc
|
||||
fi
|
||||
|
||||
if [ -f "$HOME/.mavenrc" ] ; then
|
||||
. "$HOME/.mavenrc"
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
# OS specific support. $var _must_ be set to either true or false.
|
||||
cygwin=false;
|
||||
darwin=false;
|
||||
mingw=false
|
||||
case "`uname`" in
|
||||
CYGWIN*) cygwin=true ;;
|
||||
MINGW*) mingw=true;;
|
||||
Darwin*) darwin=true
|
||||
# Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
|
||||
# See https://developer.apple.com/library/mac/qa/qa1170/_index.html
|
||||
if [ -z "$JAVA_HOME" ]; then
|
||||
if [ -x "/usr/libexec/java_home" ]; then
|
||||
export JAVA_HOME="`/usr/libexec/java_home`"
|
||||
else
|
||||
export JAVA_HOME="/Library/Java/Home"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ -z "$JAVA_HOME" ] ; then
|
||||
if [ -r /etc/gentoo-release ] ; then
|
||||
JAVA_HOME=`java-config --jre-home`
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "$M2_HOME" ] ; then
|
||||
## resolve links - $0 may be a link to maven's home
|
||||
PRG="$0"
|
||||
|
||||
# need this for relative symlinks
|
||||
while [ -h "$PRG" ] ; do
|
||||
ls=`ls -ld "$PRG"`
|
||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||
if expr "$link" : '/.*' > /dev/null; then
|
||||
PRG="$link"
|
||||
else
|
||||
PRG="`dirname "$PRG"`/$link"
|
||||
fi
|
||||
done
|
||||
|
||||
saveddir=`pwd`
|
||||
|
||||
M2_HOME=`dirname "$PRG"`/..
|
||||
|
||||
# make it fully qualified
|
||||
M2_HOME=`cd "$M2_HOME" && pwd`
|
||||
|
||||
cd "$saveddir"
|
||||
# echo Using m2 at $M2_HOME
|
||||
fi
|
||||
|
||||
# For Cygwin, ensure paths are in UNIX format before anything is touched
|
||||
if $cygwin ; then
|
||||
[ -n "$M2_HOME" ] &&
|
||||
M2_HOME=`cygpath --unix "$M2_HOME"`
|
||||
[ -n "$JAVA_HOME" ] &&
|
||||
JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
|
||||
[ -n "$CLASSPATH" ] &&
|
||||
CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
|
||||
fi
|
||||
|
||||
# For Mingw, ensure paths are in UNIX format before anything is touched
|
||||
if $mingw ; then
|
||||
[ -n "$M2_HOME" ] &&
|
||||
M2_HOME="`(cd "$M2_HOME"; pwd)`"
|
||||
[ -n "$JAVA_HOME" ] &&
|
||||
JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
|
||||
fi
|
||||
|
||||
if [ -z "$JAVA_HOME" ]; then
|
||||
javaExecutable="`which javac`"
|
||||
if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
|
||||
# readlink(1) is not available as standard on Solaris 10.
|
||||
readLink=`which readlink`
|
||||
if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
|
||||
if $darwin ; then
|
||||
javaHome="`dirname \"$javaExecutable\"`"
|
||||
javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
|
||||
else
|
||||
javaExecutable="`readlink -f \"$javaExecutable\"`"
|
||||
fi
|
||||
javaHome="`dirname \"$javaExecutable\"`"
|
||||
javaHome=`expr "$javaHome" : '\(.*\)/bin'`
|
||||
JAVA_HOME="$javaHome"
|
||||
export JAVA_HOME
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "$JAVACMD" ] ; then
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||
else
|
||||
JAVACMD="$JAVA_HOME/bin/java"
|
||||
fi
|
||||
else
|
||||
JAVACMD="`\\unset -f command; \\command -v java`"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
echo "Error: JAVA_HOME is not defined correctly." >&2
|
||||
echo " We cannot execute $JAVACMD" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "$JAVA_HOME" ] ; then
|
||||
echo "Warning: JAVA_HOME environment variable is not set."
|
||||
fi
|
||||
|
||||
CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
|
||||
|
||||
# traverses directory structure from process work directory to filesystem root
|
||||
# first directory with .mvn subdirectory is considered project base directory
|
||||
find_maven_basedir() {
|
||||
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
echo "Path not specified to find_maven_basedir"
|
||||
return 1
|
||||
fi
|
||||
|
||||
basedir="$1"
|
||||
wdir="$1"
|
||||
while [ "$wdir" != '/' ] ; do
|
||||
if [ -d "$wdir"/.mvn ] ; then
|
||||
basedir=$wdir
|
||||
break
|
||||
fi
|
||||
# workaround for JBEAP-8937 (on Solaris 10/Sparc)
|
||||
if [ -d "${wdir}" ]; then
|
||||
wdir=`cd "$wdir/.."; pwd`
|
||||
fi
|
||||
# end of workaround
|
||||
done
|
||||
echo "${basedir}"
|
||||
}
|
||||
|
||||
# concatenates all lines of a file
|
||||
concat_lines() {
|
||||
if [ -f "$1" ]; then
|
||||
echo "$(tr -s '\n' ' ' < "$1")"
|
||||
fi
|
||||
}
|
||||
|
||||
BASE_DIR=`find_maven_basedir "$(pwd)"`
|
||||
if [ -z "$BASE_DIR" ]; then
|
||||
exit 1;
|
||||
fi
|
||||
|
||||
##########################################################################################
|
||||
# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
|
||||
# This allows using the maven wrapper in projects that prohibit checking in binary data.
|
||||
##########################################################################################
|
||||
if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Found .mvn/wrapper/maven-wrapper.jar"
|
||||
fi
|
||||
else
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..."
|
||||
fi
|
||||
if [ -n "$MVNW_REPOURL" ]; then
|
||||
jarUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
|
||||
else
|
||||
jarUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
|
||||
fi
|
||||
while IFS="=" read key value; do
|
||||
case "$key" in (wrapperUrl) jarUrl="$value"; break ;;
|
||||
esac
|
||||
done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties"
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Downloading from: $jarUrl"
|
||||
fi
|
||||
wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar"
|
||||
if $cygwin; then
|
||||
wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"`
|
||||
fi
|
||||
|
||||
if command -v wget > /dev/null; then
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Found wget ... using wget"
|
||||
fi
|
||||
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
|
||||
wget "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath"
|
||||
else
|
||||
wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath"
|
||||
fi
|
||||
elif command -v curl > /dev/null; then
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Found curl ... using curl"
|
||||
fi
|
||||
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
|
||||
curl -o "$wrapperJarPath" "$jarUrl" -f
|
||||
else
|
||||
curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f
|
||||
fi
|
||||
|
||||
else
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo "Falling back to using Java to download"
|
||||
fi
|
||||
javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java"
|
||||
# For Cygwin, switch paths to Windows format before running javac
|
||||
if $cygwin; then
|
||||
javaClass=`cygpath --path --windows "$javaClass"`
|
||||
fi
|
||||
if [ -e "$javaClass" ]; then
|
||||
if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo " - Compiling MavenWrapperDownloader.java ..."
|
||||
fi
|
||||
# Compiling the Java class
|
||||
("$JAVA_HOME/bin/javac" "$javaClass")
|
||||
fi
|
||||
if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
|
||||
# Running the downloader
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo " - Running MavenWrapperDownloader.java ..."
|
||||
fi
|
||||
("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
##########################################################################################
|
||||
# End of extension
|
||||
##########################################################################################
|
||||
|
||||
export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo $MAVEN_PROJECTBASEDIR
|
||||
fi
|
||||
MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
|
||||
|
||||
# For Cygwin, switch paths to Windows format before running java
|
||||
if $cygwin; then
|
||||
[ -n "$M2_HOME" ] &&
|
||||
M2_HOME=`cygpath --path --windows "$M2_HOME"`
|
||||
[ -n "$JAVA_HOME" ] &&
|
||||
JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
|
||||
[ -n "$CLASSPATH" ] &&
|
||||
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
|
||||
[ -n "$MAVEN_PROJECTBASEDIR" ] &&
|
||||
MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
|
||||
fi
|
||||
|
||||
# Provide a "standardized" way to retrieve the CLI args that will
|
||||
# work with both Windows and non-Windows executions.
|
||||
MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
|
||||
export MAVEN_CMD_LINE_ARGS
|
||||
|
||||
WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
|
||||
|
||||
exec "$JAVACMD" \
|
||||
$MAVEN_OPTS \
|
||||
$MAVEN_DEBUG_OPTS \
|
||||
-classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
|
||||
"-Dmaven.home=${M2_HOME}" \
|
||||
"-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
|
||||
${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"
|
@ -0,0 +1,188 @@
|
||||
@REM ----------------------------------------------------------------------------
|
||||
@REM Licensed to the Apache Software Foundation (ASF) under one
|
||||
@REM or more contributor license agreements. See the NOTICE file
|
||||
@REM distributed with this work for additional information
|
||||
@REM regarding copyright ownership. The ASF licenses this file
|
||||
@REM to you under the Apache License, Version 2.0 (the
|
||||
@REM "License"); you may not use this file except in compliance
|
||||
@REM with the License. You may obtain a copy of the License at
|
||||
@REM
|
||||
@REM https://www.apache.org/licenses/LICENSE-2.0
|
||||
@REM
|
||||
@REM Unless required by applicable law or agreed to in writing,
|
||||
@REM software distributed under the License is distributed on an
|
||||
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
@REM KIND, either express or implied. See the License for the
|
||||
@REM specific language governing permissions and limitations
|
||||
@REM under the License.
|
||||
@REM ----------------------------------------------------------------------------
|
||||
|
||||
@REM ----------------------------------------------------------------------------
|
||||
@REM Maven Start Up Batch script
|
||||
@REM
|
||||
@REM Required ENV vars:
|
||||
@REM JAVA_HOME - location of a JDK home dir
|
||||
@REM
|
||||
@REM Optional ENV vars
|
||||
@REM M2_HOME - location of maven2's installed home dir
|
||||
@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
|
||||
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
|
||||
@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
|
||||
@REM e.g. to debug Maven itself, use
|
||||
@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
|
||||
@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
|
||||
@REM ----------------------------------------------------------------------------
|
||||
|
||||
@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
|
||||
@echo off
|
||||
@REM set title of command window
|
||||
title %0
|
||||
@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on'
|
||||
@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
|
||||
|
||||
@REM set %HOME% to equivalent of $HOME
|
||||
if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
|
||||
|
||||
@REM Execute a user defined script before this one
|
||||
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
|
||||
@REM check for pre script, once with legacy .bat ending and once with .cmd ending
|
||||
if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %*
|
||||
if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %*
|
||||
:skipRcPre
|
||||
|
||||
@setlocal
|
||||
|
||||
set ERROR_CODE=0
|
||||
|
||||
@REM To isolate internal variables from possible post scripts, we use another setlocal
|
||||
@setlocal
|
||||
|
||||
@REM ==== START VALIDATION ====
|
||||
if not "%JAVA_HOME%" == "" goto OkJHome
|
||||
|
||||
echo.
|
||||
echo Error: JAVA_HOME not found in your environment. >&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the >&2
|
||||
echo location of your Java installation. >&2
|
||||
echo.
|
||||
goto error
|
||||
|
||||
:OkJHome
|
||||
if exist "%JAVA_HOME%\bin\java.exe" goto init
|
||||
|
||||
echo.
|
||||
echo Error: JAVA_HOME is set to an invalid directory. >&2
|
||||
echo JAVA_HOME = "%JAVA_HOME%" >&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the >&2
|
||||
echo location of your Java installation. >&2
|
||||
echo.
|
||||
goto error
|
||||
|
||||
@REM ==== END VALIDATION ====
|
||||
|
||||
:init
|
||||
|
||||
@REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
|
||||
@REM Fallback to current working directory if not found.
|
||||
|
||||
set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
|
||||
IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
|
||||
|
||||
set EXEC_DIR=%CD%
|
||||
set WDIR=%EXEC_DIR%
|
||||
:findBaseDir
|
||||
IF EXIST "%WDIR%"\.mvn goto baseDirFound
|
||||
cd ..
|
||||
IF "%WDIR%"=="%CD%" goto baseDirNotFound
|
||||
set WDIR=%CD%
|
||||
goto findBaseDir
|
||||
|
||||
:baseDirFound
|
||||
set MAVEN_PROJECTBASEDIR=%WDIR%
|
||||
cd "%EXEC_DIR%"
|
||||
goto endDetectBaseDir
|
||||
|
||||
:baseDirNotFound
|
||||
set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
|
||||
cd "%EXEC_DIR%"
|
||||
|
||||
:endDetectBaseDir
|
||||
|
||||
IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
|
||||
|
||||
@setlocal EnableExtensions EnableDelayedExpansion
|
||||
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
|
||||
@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
|
||||
|
||||
:endReadAdditionalConfig
|
||||
|
||||
SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
|
||||
set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
|
||||
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
|
||||
|
||||
set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
|
||||
|
||||
FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
|
||||
IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
|
||||
)
|
||||
|
||||
@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
|
||||
@REM This allows using the maven wrapper in projects that prohibit checking in binary data.
|
||||
if exist %WRAPPER_JAR% (
|
||||
if "%MVNW_VERBOSE%" == "true" (
|
||||
echo Found %WRAPPER_JAR%
|
||||
)
|
||||
) else (
|
||||
if not "%MVNW_REPOURL%" == "" (
|
||||
SET DOWNLOAD_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
|
||||
)
|
||||
if "%MVNW_VERBOSE%" == "true" (
|
||||
echo Couldn't find %WRAPPER_JAR%, downloading it ...
|
||||
echo Downloading from: %DOWNLOAD_URL%
|
||||
)
|
||||
|
||||
powershell -Command "&{"^
|
||||
"$webclient = new-object System.Net.WebClient;"^
|
||||
"if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
|
||||
"$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
|
||||
"}"^
|
||||
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^
|
||||
"}"
|
||||
if "%MVNW_VERBOSE%" == "true" (
|
||||
echo Finished downloading %WRAPPER_JAR%
|
||||
)
|
||||
)
|
||||
@REM End of extension
|
||||
|
||||
@REM Provide a "standardized" way to retrieve the CLI args that will
|
||||
@REM work with both Windows and non-Windows executions.
|
||||
set MAVEN_CMD_LINE_ARGS=%*
|
||||
|
||||
%MAVEN_JAVA_EXE% ^
|
||||
%JVM_CONFIG_MAVEN_PROPS% ^
|
||||
%MAVEN_OPTS% ^
|
||||
%MAVEN_DEBUG_OPTS% ^
|
||||
-classpath %WRAPPER_JAR% ^
|
||||
"-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^
|
||||
%WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
|
||||
if ERRORLEVEL 1 goto error
|
||||
goto end
|
||||
|
||||
:error
|
||||
set ERROR_CODE=1
|
||||
|
||||
:end
|
||||
@endlocal & set ERROR_CODE=%ERROR_CODE%
|
||||
|
||||
if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost
|
||||
@REM check for post script, once with legacy .bat ending and once with .cmd ending
|
||||
if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat"
|
||||
if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd"
|
||||
:skipRcPost
|
||||
|
||||
@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
|
||||
if "%MAVEN_BATCH_PAUSE%"=="on" pause
|
||||
|
||||
if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE%
|
||||
|
||||
cmd /C exit /B %ERROR_CODE%
|
@ -0,0 +1,80 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.5.4</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>testdemo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>testdemo</name>
|
||||
<description>testdemo</description>
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.73</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.seleniumhq.selenium</groupId>
|
||||
<artifactId>selenium-java</artifactId>
|
||||
<version>3.141.59</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>sonar</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<properties>
|
||||
<sonar.host.url>http://127.0.0.1:9000/</sonar.host.url>
|
||||
</properties>
|
||||
</profile>
|
||||
</profiles>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.sonarsource.scanner.maven</groupId>
|
||||
<artifactId>sonar-maven-plugin</artifactId>
|
||||
<version>3.6.0.1398</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>7</source>
|
||||
<target>7</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,42 @@
|
||||
package com.example.testdemo;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.sun.javafx.webkit.EventLoopImpl;
|
||||
import org.openqa.selenium.*;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
import org.openqa.selenium.chrome.ChromeOptions;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.sql.Driver;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class TestdemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
SpringApplication.run(TestdemoApplication.class, args);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,17 @@
|
||||
package com.example.testdemo.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
/**
|
||||
* @Description : websocket配置 //描述
|
||||
*/
|
||||
@Configuration
|
||||
public class WebSocketConfig {
|
||||
|
||||
@Bean
|
||||
public ServerEndpointExporter serverEndpointExporter(){
|
||||
return new ServerEndpointExporter();
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
|
||||
public interface MessageHandler<T extends Message> {
|
||||
public void onMessage(T message);
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
/**
|
||||
* Created by xxhong on 16-11-17.
|
||||
*/
|
||||
@MessageType(string = "std_msgs/Int16MultiArray")
|
||||
public class AudioMsg extends Message {
|
||||
public short[] data;
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
@MessageType(string = "rosgraph_msgs/Clock")
|
||||
public class Clock extends Message {
|
||||
public TimePrimitive clock;
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
@MessageType(string = "std_msgs/Duration")
|
||||
public class Duration extends DurationPrimitive {
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
@MessageType(string = "duration")
|
||||
public class DurationPrimitive extends TimePrimitive {
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
@MessageType(string = "std_srvs/Empty")
|
||||
public class Empty extends Message {
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
@MessageType(string = "std_msgs/Header")
|
||||
public class Header extends Message {
|
||||
public long seq;
|
||||
public TimePrimitive stamp;
|
||||
public String frame_id;
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
@MessageType(string = "rosgraph_msgs/Log")
|
||||
public class Log extends Message {
|
||||
public Header header;
|
||||
public byte level;
|
||||
public String name;
|
||||
public String msg;
|
||||
public String file;
|
||||
public String function;
|
||||
public long line;
|
||||
public String[] topics;
|
||||
}
|
@ -0,0 +1,202 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@MessageType(string = "message")
|
||||
public abstract class Message {
|
||||
|
||||
// Some requirements about message types:
|
||||
// - It must have a MessageType declaration to be recognized on inbound messages
|
||||
// - Every field must be explicitly designated as public
|
||||
// - Every field that is not a primitive or near-primitive must be another Message class
|
||||
// - If there is a non-empty constructor, you must also have an empty constructor
|
||||
// - If it is set up as an inner class, it needs an explicit nullary constructor
|
||||
// (note: I have seen an inner class otherwise fail, I have not tested it with the explicit constructor)
|
||||
|
||||
public static void register(Class c, Map<String, Class> messageClasses) {
|
||||
try {
|
||||
typecheck(c);
|
||||
|
||||
// Must register the class and not have duplicate
|
||||
// This is not recursive because only the top level message class
|
||||
// needs to be determined from the string - others are top-down.
|
||||
String messageString = getMessageType(c);
|
||||
Class existingClass = messageClasses.get(messageString);
|
||||
if (existingClass != null && !existingClass.equals(c))
|
||||
throw new MessageException("Message String \'" + messageString +
|
||||
"\' is assigned to two different classes (" +
|
||||
c.getName() + " and " + existingClass.getName() + ")");
|
||||
messageClasses.put(messageString, c);
|
||||
}
|
||||
catch (MessageException ex) {
|
||||
// should be changed to be a hooked method to give library user control
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static String getMessageType(Class c) {
|
||||
return ((MessageType) c.getAnnotation(MessageType.class)).string();
|
||||
}
|
||||
|
||||
// this has never been used or tested but kind of belongs here
|
||||
// commented out because it uses ReflectiveOperationException which is not available on Android
|
||||
/*
|
||||
public static Message newInstance(String className) {
|
||||
try {
|
||||
Class messageClass = Class.forName(className);
|
||||
if (Message.class.isAssignableFrom(messageClass))
|
||||
return (Message) messageClass.newInstance();
|
||||
else throw new ClassCastException();
|
||||
}
|
||||
catch (ReflectiveOperationException ex) {
|
||||
throw new RuntimeException("Unable to create message of class \'" + className + "\'.", ex);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// Could probably do more checking here, but not sure what right now
|
||||
private static void typecheck(Class c) throws MessageException {
|
||||
|
||||
// Must inherit from Message
|
||||
if (!Message.class.isAssignableFrom(c))
|
||||
throw new MessageException("Class \'" + c.getName() +
|
||||
"\' does not extend Message");
|
||||
|
||||
// Must have the MessageType annotation
|
||||
if (getMessageType(c) == null)
|
||||
throw new MessageException("Class \'" + c.getName() +
|
||||
"\' is missing the MessageType annotation");
|
||||
|
||||
// All fields must also be valid Message classes
|
||||
// Note that this also serves to force-load all the message classes
|
||||
// so that they get registered
|
||||
for (Field f : c.getFields()) {
|
||||
Class fc = f.getType();
|
||||
if (fc.isArray()) {
|
||||
Class ac = fc.getComponentType();
|
||||
if (!isPrimitive(ac))
|
||||
typecheck(ac);
|
||||
}
|
||||
else if (!isPrimitive(fc))
|
||||
typecheck(fc);
|
||||
}
|
||||
}
|
||||
|
||||
public void print() {
|
||||
printMessage(this, "");
|
||||
}
|
||||
|
||||
private static void printMessage(Object o, String indent) {
|
||||
for (Field f : o.getClass().getFields()) {
|
||||
Class c = f.getType();
|
||||
Object fieldObject = getFieldObject(f, o);
|
||||
if (fieldObject != null) {
|
||||
if (isPrimitive(c))
|
||||
System.out.println(indent + f.getName() + ": " + fieldObject);
|
||||
else if (c.isArray()) {
|
||||
System.out.println(indent + f.getName() + ": [");
|
||||
printArray(fieldObject, indent + " ");
|
||||
System.out.println(indent + "]");
|
||||
}
|
||||
else {
|
||||
System.out.println(indent + f.getName() + ":");
|
||||
printMessage(fieldObject, indent + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void printArray(Object array, String indent) {
|
||||
Class arrayClass = array.getClass().getComponentType();
|
||||
for (int i = 0; i < Array.getLength(array); i++) {
|
||||
Object elementObject = Array.get(array, i);
|
||||
if (elementObject != null) {
|
||||
if (isPrimitive(arrayClass))
|
||||
System.out.println(indent + i + ": " + elementObject);
|
||||
else if (arrayClass.isArray()) { // this is not actually allowed in ROS
|
||||
System.out.println(indent + i + ": [");
|
||||
printArray(elementObject, indent + " ");
|
||||
System.out.println(indent + "]");
|
||||
}
|
||||
else {
|
||||
System.out.println(indent + i + ":");
|
||||
printMessage(elementObject, indent + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
// remember to print array indices
|
||||
}
|
||||
|
||||
public static boolean isPrimitive(Class c) {
|
||||
return (c.isPrimitive() ||
|
||||
c.equals(String.class) ||
|
||||
Number.class.isAssignableFrom(c) ||
|
||||
c.equals(Boolean.class));
|
||||
}
|
||||
|
||||
|
||||
// Copied from com.example.testdemo.ros.rosbridge.JSON
|
||||
private static Object getFieldObject(Field f, Object o) {
|
||||
Object fo = null;
|
||||
try {
|
||||
fo = f.get(o);
|
||||
}
|
||||
catch (IllegalAccessException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return fo;
|
||||
}
|
||||
|
||||
public void copyFrom(Message source) {
|
||||
try {
|
||||
if (source.getClass() != getClass())
|
||||
throw new RuntimeException("Attempt to copy non-matching classes");
|
||||
for (Field f : getClass().getFields()) {
|
||||
Class fc = f.getType();
|
||||
if (fc.isArray())
|
||||
throw new RuntimeException("copyFrom - array types not implemented");
|
||||
else if (!isPrimitive(fc))
|
||||
((Message) f.get(this)).copyFrom((Message) f.get(source));
|
||||
else {
|
||||
Object value = f.get(source);
|
||||
f.set(this, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IllegalAccessException ex) {
|
||||
throw new RuntimeException("copyFrom error", ex);
|
||||
}
|
||||
catch (ClassCastException ex) {
|
||||
throw new RuntimeException("copyFrom error", ex);
|
||||
}
|
||||
// ReflectiveOperationException is not available on Android (Java 1.6)
|
||||
/*
|
||||
catch (ReflectiveOperationException ex) {
|
||||
throw new RuntimeException ("copyFrom error", ex);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
public class MessageException extends Exception {
|
||||
|
||||
public MessageException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public MessageException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface MessageType {
|
||||
String string() default "";
|
||||
}
|
||||
|
@ -0,0 +1,13 @@
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
/**
|
||||
* Created by xxhong on 16-11-17.
|
||||
*/
|
||||
@MessageType(string = "std_msgs/String")
|
||||
public class SemanticRequest extends Message {
|
||||
public SemanticRequest(String args) {
|
||||
jsonStr = args;
|
||||
}
|
||||
|
||||
public String jsonStr;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
/**
|
||||
* Created by xxhong on 16-11-17.
|
||||
*/
|
||||
@MessageType(string = "std_msgs/String")
|
||||
public class SemanticResponse extends Message {
|
||||
public String result;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
/**
|
||||
* Created by xxhong on 16-11-17.
|
||||
*/
|
||||
@MessageType(string = "std_msgs/String")
|
||||
public class StdMsg extends Message {
|
||||
public String data;
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
@MessageType(string = "std_msgs/Time")
|
||||
public class Time extends TimePrimitive {
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.message;
|
||||
|
||||
@MessageType(string = "time")
|
||||
public class TimePrimitive extends Message {
|
||||
public int secs; // when requesting this format from ROSbridge, it uses 'sec' (no 's')
|
||||
public int nsecs; // when requesting this format from ROSbridge, it uses 'nsec'
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosapi.message;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "std_msgs/Empty")
|
||||
public class Empty extends Message {
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosapi.message;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
import com.example.testdemo.ros.message.TimePrimitive;
|
||||
|
||||
|
||||
@MessageType(string = "rosapi/GetTimeResponse")
|
||||
public class GetTime extends Message {
|
||||
public TimePrimitive time;
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosapi.message;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "rosapi/MessageDetails")
|
||||
public class MessageDetails extends Message {
|
||||
public TypeDef[] typedefs;
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosapi.message;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "rosapi/Nodes")
|
||||
public class Nodes extends Message {
|
||||
public String[] nodes;
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosapi.message;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "rosapi/Service")
|
||||
public class Service extends Message {
|
||||
public String service;
|
||||
|
||||
public Service() {}
|
||||
|
||||
public Service(String service) {
|
||||
this.service = service;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosapi.message;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "rosapi/Services")
|
||||
public class Services extends Message {
|
||||
public String[] services;
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosapi.message;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "rosapi/Topic")
|
||||
public class Topic extends Message {
|
||||
public String topic;
|
||||
|
||||
public Topic() {}
|
||||
|
||||
public Topic(String topic) {
|
||||
this.topic = topic;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosapi.message;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "rosapi/Topics")
|
||||
public class Topics extends Message {
|
||||
public String[] topics;
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosapi.message;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "rosapi/Type")
|
||||
public class Type extends Message {
|
||||
public String type;
|
||||
|
||||
public Type() {}
|
||||
|
||||
public Type(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosapi.message;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "rosapi/TypeDef")
|
||||
public class TypeDef extends Message {
|
||||
public String type;
|
||||
public String[] fieldnames;
|
||||
public String[] fieldtypes;
|
||||
public int[] fieldarraylen;
|
||||
public String[] examples;
|
||||
|
||||
public static boolean match(String type, Class c) {
|
||||
boolean result = false;
|
||||
if (
|
||||
(type.equals("bool") && ((boolean.class.equals(c)) || (Boolean.class.equals(c)))) ||
|
||||
(type.equals("int8") && ((byte.class.equals(c)) || (Byte.class.equals(c)))) ||
|
||||
(type.equals("byte") && ((byte.class.equals(c)) || (Byte.class.equals(c)))) || // deprecated
|
||||
(type.equals("uint8") && ((short.class.equals(c)) || (Short.class.equals(c)))) ||
|
||||
(type.equals("char") && ((short.class.equals(c)) || (Short.class.equals(c)))) || // deprecated
|
||||
(type.equals("int16") && ((short.class.equals(c)) || (Short.class.equals(c)))) ||
|
||||
(type.equals("uint16") && ((int.class.equals(c)) || (Integer.class.equals(c)))) ||
|
||||
(type.equals("int32") && ((int.class.equals(c)) || (Integer.class.equals(c)))) ||
|
||||
(type.equals("uint32") && ((long.class.equals(c)) || (Long.class.equals(c)))) ||
|
||||
(type.equals("int64") && ((long.class.equals(c)) || (Long.class.equals(c)))) ||
|
||||
(type.equals("float32") && ((float.class.equals(c)) || (Float.class.equals(c)))) ||
|
||||
(type.equals("float64") && ((double.class.equals(c)) || (Double.class.equals(c)))) ||
|
||||
(type.equals("uint64") && (java.math.BigInteger.class.equals(c))) ||
|
||||
(type.equals("string") && (String.class.equals(c)))
|
||||
)
|
||||
result = true;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
|
||||
public interface FullMessageHandler<T extends Message> {
|
||||
public void onMessage(String id, T message);
|
||||
}
|
@ -0,0 +1,575 @@
|
||||
package com.example.testdemo.ros.rosbridge.implementation;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/** A very fast and memory efficient class to encode and decode to and from BASE64 in full accordance
|
||||
* with RFC 2045.<br><br>
|
||||
* On Windows XP sp1 with 1.4.2_04 and later ;), this encoder and decoder is about 10 times faster
|
||||
* on small arrays (10 - 1000 bytes) and 2-3 times as fast on larger arrays (10000 - 1000000 bytes)
|
||||
* compared to <code>sun.misc.Encoder()/Decoder()</code>.<br><br>
|
||||
*
|
||||
* On byte arrays the encoder is about 20% faster than Jakarta Commons Base64 Codec for encode and
|
||||
* about 50% faster for decoding large arrays. This implementation is about twice as fast on very small
|
||||
* arrays (< 30 bytes). If source/destination is a <code>String</code> this
|
||||
* version is about three times as fast due to the fact that the Commons Codec result has to be recoded
|
||||
* to a <code>String</code> from <code>byte[]</code>, which is very expensive.<br><br>
|
||||
*
|
||||
* This encode/decode algorithm doesn't create any temporary arrays as many other codecs do, it only
|
||||
* allocates the resulting array. This produces less garbage and it is possible to handle arrays twice
|
||||
* as large as algorithms that create a temporary array. (E.g. Jakarta Commons Codec). It is unknown
|
||||
* whether Sun's <code>sun.misc.Encoder()/Decoder()</code> produce temporary arrays but since performance
|
||||
* is quite low it probably does.<br><br>
|
||||
*
|
||||
* The encoder produces the same output as the Sun one except that the Sun's encoder appends
|
||||
* a trailing line separator if the last character isn't a pad. Unclear why but it only adds to the
|
||||
* length and is probably a side effect. Both are in conformance with RFC 2045 though.<br>
|
||||
* Commons codec seem to always att a trailing line separator.<br><br>
|
||||
*
|
||||
* <b>Note!</b>
|
||||
* The encode/decode method pairs (types) come in three versions with the <b>exact</b> same algorithm and
|
||||
* thus a lot of code redundancy. This is to not create any temporary arrays for transcoding to/from different
|
||||
* format types. The methods not used can simply be commented out.<br><br>
|
||||
*
|
||||
* There is also a "fast" version of all decode methods that works the same way as the normal ones, but
|
||||
* har a few demands on the decoded input. Normally though, these fast verions should be used if the source if
|
||||
* the input is known and it hasn't bee tampered with.<br><br>
|
||||
*
|
||||
* If you find the code useful or you find a bug, please send me a note at base64 @ miginfocom . com.
|
||||
*
|
||||
* Licence (BSD):
|
||||
* ==============
|
||||
*
|
||||
* Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (base64 @ miginfocom . com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright notice, this list
|
||||
* of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this
|
||||
* list of conditions and the following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
* Neither the name of the MiG InfoCom AB nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without specific
|
||||
* prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* @version 2.2
|
||||
* @author Mikael Grev
|
||||
* Date: 2004-aug-02
|
||||
* Time: 11:31:11
|
||||
*/
|
||||
|
||||
public class Base64
|
||||
{
|
||||
private static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
|
||||
private static final int[] IA = new int[256];
|
||||
static {
|
||||
Arrays.fill(IA, -1);
|
||||
for (int i = 0, iS = CA.length; i < iS; i++)
|
||||
IA[CA[i]] = i;
|
||||
IA['='] = 0;
|
||||
}
|
||||
|
||||
// ****************************************************************************************
|
||||
// * char[] version
|
||||
// ****************************************************************************************
|
||||
|
||||
/** Encodes a raw byte array into a BASE64 <code>char[]</code> representation i accordance with RFC 2045.
|
||||
* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.
|
||||
* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>
|
||||
* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a
|
||||
* little faster.
|
||||
* @return A BASE64 encoded array. Never <code>null</code>.
|
||||
*/
|
||||
public final static char[] encodeToChar(byte[] sArr, boolean lineSep)
|
||||
{
|
||||
// Check special case
|
||||
int sLen = sArr != null ? sArr.length : 0;
|
||||
if (sLen == 0)
|
||||
return new char[0];
|
||||
|
||||
int eLen = (sLen / 3) * 3; // Length of even 24-bits.
|
||||
int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count
|
||||
int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array
|
||||
char[] dArr = new char[dLen];
|
||||
|
||||
// Encode even 24-bits
|
||||
for (int s = 0, d = 0, cc = 0; s < eLen;) {
|
||||
// Copy next three bytes into lower 24 bits of int, paying attension to sign.
|
||||
int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff);
|
||||
|
||||
// Encode the int into four chars
|
||||
dArr[d++] = CA[(i >>> 18) & 0x3f];
|
||||
dArr[d++] = CA[(i >>> 12) & 0x3f];
|
||||
dArr[d++] = CA[(i >>> 6) & 0x3f];
|
||||
dArr[d++] = CA[i & 0x3f];
|
||||
|
||||
// Add optional line separator
|
||||
if (lineSep && ++cc == 19 && d < dLen - 2) {
|
||||
dArr[d++] = '\r';
|
||||
dArr[d++] = '\n';
|
||||
cc = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Pad and encode last bits if source isn't even 24 bits.
|
||||
int left = sLen - eLen; // 0 - 2.
|
||||
if (left > 0) {
|
||||
// Prepare the int
|
||||
int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);
|
||||
|
||||
// Set last four chars
|
||||
dArr[dLen - 4] = CA[i >> 12];
|
||||
dArr[dLen - 3] = CA[(i >>> 6) & 0x3f];
|
||||
dArr[dLen - 2] = left == 2 ? CA[i & 0x3f] : '=';
|
||||
dArr[dLen - 1] = '=';
|
||||
}
|
||||
return dArr;
|
||||
}
|
||||
|
||||
/** Decodes a BASE64 encoded char array. All illegal characters will be ignored and can handle both arrays with
|
||||
* and without line separators.
|
||||
* @param sArr The source array. <code>null</code> or length 0 will return an empty array.
|
||||
* @return The decoded array of bytes. May be of length 0. Will be <code>null</code> if the legal characters
|
||||
* (including '=') isn't divideable by 4. (I.e. definitely corrupted).
|
||||
*/
|
||||
public final static byte[] decode(char[] sArr)
|
||||
{
|
||||
// Check special case
|
||||
int sLen = sArr != null ? sArr.length : 0;
|
||||
if (sLen == 0)
|
||||
return new byte[0];
|
||||
|
||||
// Count illegal characters (including '\r', '\n') to know what size the returned array will be,
|
||||
// so we don't have to reallocate & copy it later.
|
||||
int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
|
||||
for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out.
|
||||
if (IA[sArr[i]] < 0)
|
||||
sepCnt++;
|
||||
|
||||
// Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
|
||||
if ((sLen - sepCnt) % 4 != 0)
|
||||
return null;
|
||||
|
||||
int pad = 0;
|
||||
for (int i = sLen; i > 1 && IA[sArr[--i]] <= 0;)
|
||||
if (sArr[i] == '=')
|
||||
pad++;
|
||||
|
||||
int len = ((sLen - sepCnt) * 6 >> 3) - pad;
|
||||
|
||||
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
|
||||
|
||||
for (int s = 0, d = 0; d < len;) {
|
||||
// Assemble three bytes into an int from four "valid" characters.
|
||||
int i = 0;
|
||||
for (int j = 0; j < 4; j++) { // j only increased if a valid char was found.
|
||||
int c = IA[sArr[s++]];
|
||||
if (c >= 0)
|
||||
i |= c << (18 - j * 6);
|
||||
else
|
||||
j--;
|
||||
}
|
||||
// Add the bytes
|
||||
dArr[d++] = (byte) (i >> 16);
|
||||
if (d < len) {
|
||||
dArr[d++]= (byte) (i >> 8);
|
||||
if (d < len)
|
||||
dArr[d++] = (byte) i;
|
||||
}
|
||||
}
|
||||
return dArr;
|
||||
}
|
||||
|
||||
/** Decodes a BASE64 encoded char array that is known to be resonably well formatted. The method is about twice as
|
||||
* fast as {@link #decode(char[])}. The preconditions are:<br>
|
||||
* + The array must have a line length of 76 chars OR no line separators at all (one line).<br>
|
||||
* + Line separator must be "\r\n", as specified in RFC 2045
|
||||
* + The array must not contain illegal characters within the encoded string<br>
|
||||
* + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br>
|
||||
* @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception.
|
||||
* @return The decoded array of bytes. May be of length 0.
|
||||
*/
|
||||
public final static byte[] decodeFast(char[] sArr)
|
||||
{
|
||||
// Check special case
|
||||
int sLen = sArr.length;
|
||||
if (sLen == 0)
|
||||
return new byte[0];
|
||||
|
||||
int sIx = 0, eIx = sLen - 1; // Start and end index after trimming.
|
||||
|
||||
// Trim illegal chars from start
|
||||
while (sIx < eIx && IA[sArr[sIx]] < 0)
|
||||
sIx++;
|
||||
|
||||
// Trim illegal chars from end
|
||||
while (eIx > 0 && IA[sArr[eIx]] < 0)
|
||||
eIx--;
|
||||
|
||||
// get the padding count (=) (0, 1 or 2)
|
||||
int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0; // Count '=' at end.
|
||||
int cCnt = eIx - sIx + 1; // Content count including possible separators
|
||||
int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0;
|
||||
|
||||
int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes
|
||||
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
|
||||
|
||||
// Decode all but the last 0 - 2 bytes.
|
||||
int d = 0;
|
||||
for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
|
||||
// Assemble three bytes into an int from four "valid" characters.
|
||||
int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]];
|
||||
|
||||
// Add the bytes
|
||||
dArr[d++] = (byte) (i >> 16);
|
||||
dArr[d++] = (byte) (i >> 8);
|
||||
dArr[d++] = (byte) i;
|
||||
|
||||
// If line separator, jump over it.
|
||||
if (sepCnt > 0 && ++cc == 19) {
|
||||
sIx += 2;
|
||||
cc = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (d < len) {
|
||||
// Decode last 1-3 bytes (incl '=') into 1-3 bytes
|
||||
int i = 0;
|
||||
for (int j = 0; sIx <= eIx - pad; j++)
|
||||
i |= IA[sArr[sIx++]] << (18 - j * 6);
|
||||
|
||||
for (int r = 16; d < len; r -= 8)
|
||||
dArr[d++] = (byte) (i >> r);
|
||||
}
|
||||
|
||||
return dArr;
|
||||
}
|
||||
|
||||
// ****************************************************************************************
|
||||
// * byte[] version
|
||||
// ****************************************************************************************
|
||||
|
||||
/** Encodes a raw byte array into a BASE64 <code>byte[]</code> representation i accordance with RFC 2045.
|
||||
* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.
|
||||
* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>
|
||||
* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a
|
||||
* little faster.
|
||||
* @return A BASE64 encoded array. Never <code>null</code>.
|
||||
*/
|
||||
public final static byte[] encodeToByte(byte[] sArr, boolean lineSep)
|
||||
{
|
||||
// Check special case
|
||||
int sLen = sArr != null ? sArr.length : 0;
|
||||
if (sLen == 0)
|
||||
return new byte[0];
|
||||
|
||||
int eLen = (sLen / 3) * 3; // Length of even 24-bits.
|
||||
int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count
|
||||
int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array
|
||||
byte[] dArr = new byte[dLen];
|
||||
|
||||
// Encode even 24-bits
|
||||
for (int s = 0, d = 0, cc = 0; s < eLen;) {
|
||||
// Copy next three bytes into lower 24 bits of int, paying attension to sign.
|
||||
int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff);
|
||||
|
||||
// Encode the int into four chars
|
||||
dArr[d++] = (byte) CA[(i >>> 18) & 0x3f];
|
||||
dArr[d++] = (byte) CA[(i >>> 12) & 0x3f];
|
||||
dArr[d++] = (byte) CA[(i >>> 6) & 0x3f];
|
||||
dArr[d++] = (byte) CA[i & 0x3f];
|
||||
|
||||
// Add optional line separator
|
||||
if (lineSep && ++cc == 19 && d < dLen - 2) {
|
||||
dArr[d++] = '\r';
|
||||
dArr[d++] = '\n';
|
||||
cc = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Pad and encode last bits if source isn't an even 24 bits.
|
||||
int left = sLen - eLen; // 0 - 2.
|
||||
if (left > 0) {
|
||||
// Prepare the int
|
||||
int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);
|
||||
|
||||
// Set last four chars
|
||||
dArr[dLen - 4] = (byte) CA[i >> 12];
|
||||
dArr[dLen - 3] = (byte) CA[(i >>> 6) & 0x3f];
|
||||
dArr[dLen - 2] = left == 2 ? (byte) CA[i & 0x3f] : (byte) '=';
|
||||
dArr[dLen - 1] = '=';
|
||||
}
|
||||
return dArr;
|
||||
}
|
||||
|
||||
/** Decodes a BASE64 encoded byte array. All illegal characters will be ignored and can handle both arrays with
|
||||
* and without line separators.
|
||||
* @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception.
|
||||
* @return The decoded array of bytes. May be of length 0. Will be <code>null</code> if the legal characters
|
||||
* (including '=') isn't divideable by 4. (I.e. definitely corrupted).
|
||||
*/
|
||||
public final static byte[] decode(byte[] sArr)
|
||||
{
|
||||
// Check special case
|
||||
int sLen = sArr.length;
|
||||
|
||||
// Count illegal characters (including '\r', '\n') to know what size the returned array will be,
|
||||
// so we don't have to reallocate & copy it later.
|
||||
int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
|
||||
for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out.
|
||||
if (IA[sArr[i] & 0xff] < 0)
|
||||
sepCnt++;
|
||||
|
||||
// Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
|
||||
if ((sLen - sepCnt) % 4 != 0)
|
||||
return null;
|
||||
|
||||
int pad = 0;
|
||||
for (int i = sLen; i > 1 && IA[sArr[--i] & 0xff] <= 0;)
|
||||
if (sArr[i] == '=')
|
||||
pad++;
|
||||
|
||||
int len = ((sLen - sepCnt) * 6 >> 3) - pad;
|
||||
|
||||
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
|
||||
|
||||
for (int s = 0, d = 0; d < len;) {
|
||||
// Assemble three bytes into an int from four "valid" characters.
|
||||
int i = 0;
|
||||
for (int j = 0; j < 4; j++) { // j only increased if a valid char was found.
|
||||
int c = IA[sArr[s++] & 0xff];
|
||||
if (c >= 0)
|
||||
i |= c << (18 - j * 6);
|
||||
else
|
||||
j--;
|
||||
}
|
||||
|
||||
// Add the bytes
|
||||
dArr[d++] = (byte) (i >> 16);
|
||||
if (d < len) {
|
||||
dArr[d++]= (byte) (i >> 8);
|
||||
if (d < len)
|
||||
dArr[d++] = (byte) i;
|
||||
}
|
||||
}
|
||||
|
||||
return dArr;
|
||||
}
|
||||
|
||||
|
||||
/** Decodes a BASE64 encoded byte array that is known to be resonably well formatted. The method is about twice as
|
||||
* fast as {@link #decode(byte[])}. The preconditions are:<br>
|
||||
* + The array must have a line length of 76 chars OR no line separators at all (one line).<br>
|
||||
* + Line separator must be "\r\n", as specified in RFC 2045
|
||||
* + The array must not contain illegal characters within the encoded string<br>
|
||||
* + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br>
|
||||
* @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception.
|
||||
* @return The decoded array of bytes. May be of length 0.
|
||||
*/
|
||||
public final static byte[] decodeFast(byte[] sArr)
|
||||
{
|
||||
// Check special case
|
||||
int sLen = sArr.length;
|
||||
if (sLen == 0)
|
||||
return new byte[0];
|
||||
|
||||
int sIx = 0, eIx = sLen - 1; // Start and end index after trimming.
|
||||
|
||||
// Trim illegal chars from start
|
||||
while (sIx < eIx && IA[sArr[sIx] & 0xff] < 0)
|
||||
sIx++;
|
||||
|
||||
// Trim illegal chars from end
|
||||
while (eIx > 0 && IA[sArr[eIx] & 0xff] < 0)
|
||||
eIx--;
|
||||
|
||||
// get the padding count (=) (0, 1 or 2)
|
||||
int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0; // Count '=' at end.
|
||||
int cCnt = eIx - sIx + 1; // Content count including possible separators
|
||||
int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0;
|
||||
|
||||
int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes
|
||||
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
|
||||
|
||||
// Decode all but the last 0 - 2 bytes.
|
||||
int d = 0;
|
||||
for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
|
||||
// Assemble three bytes into an int from four "valid" characters.
|
||||
int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]];
|
||||
|
||||
// Add the bytes
|
||||
dArr[d++] = (byte) (i >> 16);
|
||||
dArr[d++] = (byte) (i >> 8);
|
||||
dArr[d++] = (byte) i;
|
||||
|
||||
// If line separator, jump over it.
|
||||
if (sepCnt > 0 && ++cc == 19) {
|
||||
sIx += 2;
|
||||
cc = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (d < len) {
|
||||
// Decode last 1-3 bytes (incl '=') into 1-3 bytes
|
||||
int i = 0;
|
||||
for (int j = 0; sIx <= eIx - pad; j++)
|
||||
i |= IA[sArr[sIx++]] << (18 - j * 6);
|
||||
|
||||
for (int r = 16; d < len; r -= 8)
|
||||
dArr[d++] = (byte) (i >> r);
|
||||
}
|
||||
|
||||
return dArr;
|
||||
}
|
||||
|
||||
// ****************************************************************************************
|
||||
// * String version
|
||||
// ****************************************************************************************
|
||||
|
||||
/** Encodes a raw byte array into a BASE64 <code>String</code> representation i accordance with RFC 2045.
|
||||
* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.
|
||||
* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>
|
||||
* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a
|
||||
* little faster.
|
||||
* @return A BASE64 encoded array. Never <code>null</code>.
|
||||
*/
|
||||
public final static String encodeToString(byte[] sArr, boolean lineSep)
|
||||
{
|
||||
// Reuse char[] since we can't create a String incrementally anyway and StringBuffer/Builder would be slower.
|
||||
return new String(encodeToChar(sArr, lineSep));
|
||||
}
|
||||
|
||||
/** Decodes a BASE64 encoded <code>String</code>. All illegal characters will be ignored and can handle both strings with
|
||||
* and without line separators.<br>
|
||||
* <b>Note!</b> It can be up to about 2x the speed to call <code>decode(str.toCharArray())</code> instead. That
|
||||
* will create a temporary array though. This version will use <code>str.charAt(i)</code> to iterate the string.
|
||||
* @param str The source string. <code>null</code> or length 0 will return an empty array.
|
||||
* @return The decoded array of bytes. May be of length 0. Will be <code>null</code> if the legal characters
|
||||
* (including '=') isn't divideable by 4. (I.e. definitely corrupted).
|
||||
*/
|
||||
public final static byte[] decode(String str)
|
||||
{
|
||||
// Check special case
|
||||
int sLen = str != null ? str.length() : 0;
|
||||
if (sLen == 0)
|
||||
return new byte[0];
|
||||
|
||||
// Count illegal characters (including '\r', '\n') to know what size the returned array will be,
|
||||
// so we don't have to reallocate & copy it later.
|
||||
int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
|
||||
for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out.
|
||||
if (IA[str.charAt(i)] < 0)
|
||||
sepCnt++;
|
||||
|
||||
// Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
|
||||
if ((sLen - sepCnt) % 4 != 0)
|
||||
return null;
|
||||
|
||||
// Count '=' at end
|
||||
int pad = 0;
|
||||
for (int i = sLen; i > 1 && IA[str.charAt(--i)] <= 0;)
|
||||
if (str.charAt(i) == '=')
|
||||
pad++;
|
||||
|
||||
int len = ((sLen - sepCnt) * 6 >> 3) - pad;
|
||||
|
||||
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
|
||||
|
||||
for (int s = 0, d = 0; d < len;) {
|
||||
// Assemble three bytes into an int from four "valid" characters.
|
||||
int i = 0;
|
||||
for (int j = 0; j < 4; j++) { // j only increased if a valid char was found.
|
||||
int c = IA[str.charAt(s++)];
|
||||
if (c >= 0)
|
||||
i |= c << (18 - j * 6);
|
||||
else
|
||||
j--;
|
||||
}
|
||||
// Add the bytes
|
||||
dArr[d++] = (byte) (i >> 16);
|
||||
if (d < len) {
|
||||
dArr[d++]= (byte) (i >> 8);
|
||||
if (d < len)
|
||||
dArr[d++] = (byte) i;
|
||||
}
|
||||
}
|
||||
return dArr;
|
||||
}
|
||||
|
||||
/** Decodes a BASE64 encoded string that is known to be resonably well formatted. The method is about twice as
|
||||
* fast as {@link #decode(String)}. The preconditions are:<br>
|
||||
* + The array must have a line length of 76 chars OR no line separators at all (one line).<br>
|
||||
* + Line separator must be "\r\n", as specified in RFC 2045
|
||||
* + The array must not contain illegal characters within the encoded string<br>
|
||||
* + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br>
|
||||
* @param s The source string. Length 0 will return an empty array. <code>null</code> will throw an exception.
|
||||
* @return The decoded array of bytes. May be of length 0.
|
||||
*/
|
||||
public final static byte[] decodeFast(String s)
|
||||
{
|
||||
// Check special case
|
||||
int sLen = s.length();
|
||||
if (sLen == 0)
|
||||
return new byte[0];
|
||||
|
||||
int sIx = 0, eIx = sLen - 1; // Start and end index after trimming.
|
||||
|
||||
// Trim illegal chars from start
|
||||
while (sIx < eIx && IA[s.charAt(sIx) & 0xff] < 0)
|
||||
sIx++;
|
||||
|
||||
// Trim illegal chars from end
|
||||
while (eIx > 0 && IA[s.charAt(eIx) & 0xff] < 0)
|
||||
eIx--;
|
||||
|
||||
// get the padding count (=) (0, 1 or 2)
|
||||
int pad = s.charAt(eIx) == '=' ? (s.charAt(eIx - 1) == '=' ? 2 : 1) : 0; // Count '=' at end.
|
||||
int cCnt = eIx - sIx + 1; // Content count including possible separators
|
||||
int sepCnt = sLen > 76 ? (s.charAt(76) == '\r' ? cCnt / 78 : 0) << 1 : 0;
|
||||
|
||||
int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes
|
||||
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
|
||||
|
||||
// Decode all but the last 0 - 2 bytes.
|
||||
int d = 0;
|
||||
for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
|
||||
// Assemble three bytes into an int from four "valid" characters.
|
||||
int i = IA[s.charAt(sIx++)] << 18 | IA[s.charAt(sIx++)] << 12 | IA[s.charAt(sIx++)] << 6 | IA[s.charAt(sIx++)];
|
||||
|
||||
// Add the bytes
|
||||
dArr[d++] = (byte) (i >> 16);
|
||||
dArr[d++] = (byte) (i >> 8);
|
||||
dArr[d++] = (byte) i;
|
||||
|
||||
// If line separator, jump over it.
|
||||
if (sepCnt > 0 && ++cc == 19) {
|
||||
sIx += 2;
|
||||
cc = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (d < len) {
|
||||
// Decode last 1-3 bytes (incl '=') into 1-3 bytes
|
||||
int i = 0;
|
||||
for (int j = 0; sIx <= eIx - pad; j++)
|
||||
i |= IA[s.charAt(sIx++)] << (18 - j * 6);
|
||||
|
||||
for (int r = 16; d < len; r -= 8)
|
||||
dArr[d++] = (byte) (i >> r);
|
||||
}
|
||||
|
||||
return dArr;
|
||||
}
|
||||
}
|
@ -0,0 +1,335 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.implementation;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.rosbridge.indication.Indication;
|
||||
import com.example.testdemo.ros.rosbridge.operation.Wrapper;
|
||||
import jdk.nashorn.internal.parser.JSONParser;
|
||||
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
|
||||
// The slightly crazy abstractions here are designed to isolate knowledge of
|
||||
// the JSON library and data types from the Operation details of rosbridge.
|
||||
// Why is this important? A few reasons I can see. First, we might want
|
||||
// to change JSON libraries and this encapsulates all use of JSON-simple.
|
||||
// Second, as much as possible I would like the semantics of the rosbridge
|
||||
// protocol to be encapsulated in the Operation and its subclasses rather
|
||||
// than in a module that is essentially about serialization.
|
||||
//
|
||||
// Unfortunately the hierarchical Message abstraction is a bit broken
|
||||
// at the top level. Beginning at the actual operation (e.g., Publish), the
|
||||
// types of the fields are determined either by the fields themselves or by
|
||||
// an indicator. However, the type of the operation itself is not determined
|
||||
// this way, because the indicator is in the object itself, which means it
|
||||
// would have to be created before its type is known. Rather than build in
|
||||
// exceptions, I elected to create a "Wrapper" operation type that simply
|
||||
// wraps the concrete operation and copies its "op" field.
|
||||
//
|
||||
|
||||
public class JSON {
|
||||
|
||||
/**
|
||||
* Translates a Message recursively into JSON. Normally the Message is also an
|
||||
* Operation, but it does not have to be. The caller constructs a complete
|
||||
* message using @Operation and @Message types. This includes situations
|
||||
* where one or more fields are marked to be turned into arrays, using @AsArray.
|
||||
* @param m the @Message object to be recursively translated.
|
||||
* @return the complete JSON string.
|
||||
*/
|
||||
public static String toJSON(Message m) {
|
||||
JSONObject jo = convertObjectToJSONObject(m); // Object to JSON-Simple
|
||||
return jo.toJSONString(); // JSON-Simple to string
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates JSON into a hierarchical Operation/Message structure.
|
||||
* This includes handling fields that are @Indicated and @AsArray. If the
|
||||
* @Class parameter is a @Wrapper, this is a special case whereby the
|
||||
* object is wrapped to create a consistent hierarchy.
|
||||
* @param json the source JSON string
|
||||
* @param c the top level class of the JSON. Normally @Wrapper
|
||||
* @param r the @Registry containing topic registrations
|
||||
* @return the fully instantiated message hierarchy represented
|
||||
* by the JSON string.
|
||||
*/
|
||||
public static Message toMessage(String json, Class c, Registry<Class> r) {
|
||||
JSONObject joUnwrapped = convertStringToJSONObject(json); // String to JSON-Simple
|
||||
JSONObject jo = joUnwrapped;
|
||||
if (Wrapper.class.isAssignableFrom(c))
|
||||
jo = wrap(joUnwrapped, c); // wrap: a hack to make the hierarchy homogeneous
|
||||
return convertJSONObjectToMessage(jo, c, r); // JSON-Simple to Message
|
||||
}
|
||||
|
||||
// *** Create JSON from Messages *** //
|
||||
|
||||
// Translate the object into a JSON-Simple object, field-by-field,
|
||||
// recursively via convertElementToJSON.
|
||||
// except for the case where AsArray is indicated
|
||||
private static JSONObject convertObjectToJSONObject(Object o) {
|
||||
JSONObject result = new JSONObject();
|
||||
for (Field f : o.getClass().getFields()) {
|
||||
Object fieldObject = getFieldObject(f, o);
|
||||
if (fieldObject != null) {
|
||||
Object resultObject;
|
||||
if (Indication.isBase64Encoded(f))
|
||||
resultObject = convertByteArrayToBase64JSONString(fieldObject);
|
||||
else if (Indication.asArray(f))
|
||||
resultObject = convertObjectToJSONArray(fieldObject);
|
||||
else resultObject = convertElementToJSON(fieldObject);
|
||||
result.put(f.getName(), resultObject);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Convert an array type to a JSON-Simple array, element-by-element,
|
||||
// recursively via convertElementToJSON.
|
||||
private static JSONArray convertArrayToJSONArray(Object array) {
|
||||
JSONArray result = new JSONArray();
|
||||
for (int i = 0; i < Array.getLength(array); i++) {
|
||||
Object elementObject = Array.get(array, i);
|
||||
if (elementObject != null) {
|
||||
Object resultObject = convertElementToJSON(elementObject);
|
||||
result.add(resultObject);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// For AsArray objects, convert the object to a JSON-Simple array
|
||||
// NOTE: This relies on later versions of the JDK providing
|
||||
// the fields in order.
|
||||
private static JSONArray convertObjectToJSONArray(Object o) {
|
||||
JSONArray result = new JSONArray();
|
||||
for (Field f : o.getClass().getFields()) {
|
||||
Object fieldObject = getFieldObject(f, o);
|
||||
if (fieldObject != null) {
|
||||
Object resultObject = convertElementToJSON(fieldObject);
|
||||
result.add(resultObject);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Convert the individual field or array element items recursively
|
||||
private static Object convertElementToJSON(Object elementObject) {
|
||||
Class elementClass = elementObject.getClass();
|
||||
Object resultObject;
|
||||
if (Message.isPrimitive(elementClass))
|
||||
resultObject = elementObject;
|
||||
else if (elementClass.isArray())
|
||||
resultObject = convertArrayToJSONArray(elementObject);
|
||||
else
|
||||
resultObject = convertObjectToJSONObject(elementObject);
|
||||
return resultObject;
|
||||
}
|
||||
|
||||
// Special case for Base 64-encoded fields
|
||||
private static Object convertByteArrayToBase64JSONString(Object fieldObject) {
|
||||
return Base64.encodeToString((byte[]) fieldObject, false);
|
||||
}
|
||||
|
||||
// This is just to buffer the code from the exception. Better error
|
||||
// handling needed here.
|
||||
private static Object getFieldObject(Field f, Object o) {
|
||||
Object fo = null;
|
||||
try {
|
||||
fo = f.get(o);
|
||||
}
|
||||
catch (IllegalAccessException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return fo;
|
||||
}
|
||||
|
||||
// *** Create Messages from JSON *** //
|
||||
|
||||
// Use the JSON-simple parser to create the JSON-Simple object
|
||||
private static JSONObject convertStringToJSONObject(String json) {
|
||||
JSONObject result = null;
|
||||
StringReader r = new StringReader(json);
|
||||
//JSONParser jp = new JSONParser();
|
||||
try {
|
||||
//result = (JSONObject) jp.parse(r);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
System.out.println(t.getMessage());
|
||||
}
|
||||
r.close();
|
||||
return result;
|
||||
}
|
||||
|
||||
// A bit of a hack to create a consistent hierarchy with jsonbridge operations
|
||||
// At least it does not depend on any specific field names, it just copies the
|
||||
// Indicator and Indicated fields.
|
||||
private static JSONObject wrap(JSONObject jo, Class c) {
|
||||
JSONObject result = new JSONObject();
|
||||
String indicatorName = Indication.getIndicatorName(c);
|
||||
String indicatedName = Indication.getIndicatedName(c);
|
||||
result.put(indicatorName, jo.get(indicatorName));
|
||||
result.put(indicatedName, jo);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Convert the JSON-Simple object to the indicated message, field-by-field
|
||||
// recursively via convertElementToField.
|
||||
private static Message convertJSONObjectToMessage(JSONObject jo, Class c, Registry<Class> r) {
|
||||
//System.out.println("JSON.convertJSONObjectToMessage: " + jo.toJSONString());
|
||||
try {
|
||||
Message result = (Message) c.newInstance();
|
||||
for (Field f : c.getFields()) {
|
||||
Class fc = getFieldClass(result, jo, f, r);
|
||||
Object lookup = jo.get(f.getName());
|
||||
if (lookup != null) {
|
||||
Object value = convertElementToField(lookup, fc, f, r);
|
||||
f.set(result, value);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
//ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert the JSON-Simple array to the indicated message, element-by-element
|
||||
// recursively via convertElementToField.
|
||||
private static Object convertJSONArrayToArray(JSONArray ja, Class c, Registry<Class> r) {
|
||||
Object result = Array.newInstance(c, ja.size());
|
||||
for (int i = 0; i < ja.size(); i++) {
|
||||
Object lookup = ja.get(i);
|
||||
Object value = null;
|
||||
if (lookup != null) {
|
||||
if (lookup.getClass().equals(JSONObject.class))
|
||||
value = convertJSONObjectToMessage((JSONObject) lookup, c, r);
|
||||
else if (lookup.getClass().equals(JSONArray.class)) // this is not actually allowed in ROS
|
||||
value = convertJSONArrayToArray((JSONArray) lookup, c.getComponentType(), r);
|
||||
else
|
||||
value = convertJSONPrimitiveToPrimitive(lookup, c);
|
||||
Array.set(result, i, value);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Convert a JSON-Simple array to a Message, field-by-field of the Message,
|
||||
// element-by-element of the array, recursively via convertElementToField.
|
||||
// NOTE: This relies on later versions of the JDK providing
|
||||
// the fields in order.
|
||||
private static Message convertJSONArrayToMessage(JSONArray ja, Class c, Registry<Class> r) {
|
||||
try {
|
||||
Message result = (Message) c.newInstance();
|
||||
int arrayIndex = 0;
|
||||
for (Field f : c.getFields()) {
|
||||
Class fc = getFieldClass(result, null, f, r);
|
||||
Object lookup = ja.get(arrayIndex++); // yes we are assuming that the fields are delivered in order
|
||||
if (lookup != null) {
|
||||
Object value = convertElementToField(lookup, fc, f, r);
|
||||
f.set(result, value);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert an individual array or object element to a field in the Message,
|
||||
// recursively, and applying AsArray if needed.
|
||||
private static Object convertElementToField(Object element, Class fc, Field f, Registry<Class> r) {
|
||||
//System.out.println("JSON.convertElementToField: " + f.getName() + " " + fc.getName());
|
||||
Object value;
|
||||
if (element.getClass().equals(JSONObject.class)) {
|
||||
//System.out.println("JSON.convertElementToField: JSON Object " + ((JSONObject) element).toJSONString());
|
||||
value = convertJSONObjectToMessage((JSONObject) element, fc, r);
|
||||
}
|
||||
else if (element.getClass().equals(JSONArray.class)) {
|
||||
//System.out.println("JSON.convertElementToField: JSON Array " + ((JSONArray) element).toJSONString());
|
||||
if (Indication.asArray(f))
|
||||
value = convertJSONArrayToMessage((JSONArray) element, fc, r);
|
||||
else value = convertJSONArrayToArray((JSONArray) element, fc, r);
|
||||
}
|
||||
else {
|
||||
//System.out.println("JSON.convertElementToField: Primitive " + element);
|
||||
if (Indication.isBase64Encoded(f))
|
||||
value = convertBase64JSONStringToByteArray(element);
|
||||
else value = convertJSONPrimitiveToPrimitive(element, fc);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
// Note that this is not checking ranges
|
||||
public static Object convertJSONPrimitiveToPrimitive(Object o, Class c) {
|
||||
Object result = o;
|
||||
if (c.isPrimitive() || Number.class.isAssignableFrom(c)) {
|
||||
if (c.equals(double.class) || c.equals(Double.class))
|
||||
result = new Double(((Number) o).doubleValue());
|
||||
else if (c.equals(float.class) || c.equals(Float.class))
|
||||
result = new Float(((Number) o).floatValue());
|
||||
else if (c.equals(long.class) || c.equals(Long.class))
|
||||
result = new Long(((Number) o).longValue());
|
||||
else if (int.class.equals(c) || c.equals(Integer.class))
|
||||
result = new Integer(((Number) o).intValue());
|
||||
else if (c.equals(short.class) || c.equals(Short.class))
|
||||
result = new Short(((Number) o).shortValue());
|
||||
else if (c.equals(byte.class) || c.equals(Byte.class))
|
||||
result = new Byte(((Number) o).byteValue());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static byte[] convertBase64JSONStringToByteArray(Object element) {
|
||||
return Base64.decode((String) element);
|
||||
}
|
||||
|
||||
// Determine the target class of a field in the object or array, based
|
||||
// directly on the field's type, or using the Indicator if applicable,
|
||||
// The Indicator field only provides the topic/service, so we have to look
|
||||
// up the Class in the registry.
|
||||
public static Class getFieldClass(Message parent, JSONObject jo, Field f, Registry<Class> r) {
|
||||
Class fc;
|
||||
fc = f.getType();
|
||||
if (fc.isArray())
|
||||
fc = f.getType().getComponentType();
|
||||
if (Indication.isIndicated(f) && (jo != null)) {
|
||||
//fc = Indication.getIndication(parent,
|
||||
// (String) jo.get(Indication.getIndicatorName(parent.getClass())));
|
||||
fc = r.lookup(parent.getClass(),
|
||||
(String) jo.get(Indication.getIndicatorName(parent.getClass())));
|
||||
//System.out.println("JSON.getFieldClass: parent class " + parent.getClass().getName() +
|
||||
// " Indicator: " + Indication.getIndicatorName(parent.getClass()) +
|
||||
// " result: " + fc.getName());
|
||||
}
|
||||
return fc;
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.example.testdemo.ros.rosbridge.implementation;
|
||||
|
||||
import com.example.testdemo.ros.rosbridge.operation.Operation;
|
||||
|
||||
/**EventBus event entity,describe ros server response info
|
||||
* Created by xxhong on 16-11-22.
|
||||
*/
|
||||
|
||||
public class PublishEvent {
|
||||
public String msg;
|
||||
public String id;
|
||||
public String name;
|
||||
public String op;
|
||||
|
||||
|
||||
public PublishEvent(Operation operation, String name, String content) {
|
||||
if(operation != null) {
|
||||
id = operation.id;
|
||||
op = operation.op;
|
||||
}
|
||||
this.name = name;
|
||||
msg = content;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.implementation;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Registry<T> extends HashMap<Class, Map<String, T>> {
|
||||
|
||||
public void register(Class c, String s, T t) {
|
||||
Map<String, T> table = get(c);
|
||||
if (table == null) {
|
||||
table = new HashMap<String, T>();
|
||||
put(c, table);
|
||||
}
|
||||
table.put(s, t);
|
||||
}
|
||||
|
||||
public void unregister(Class c, String s) {
|
||||
Map<String, T> table = get(c);
|
||||
if (table != null)
|
||||
table.remove(s);
|
||||
}
|
||||
|
||||
public T lookup(Class c, String s) {
|
||||
T result = null;
|
||||
Map<String, T> table = get(c);
|
||||
if (table != null)
|
||||
result = table.get(s);
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.indication;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface AsArray {
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.indication;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface Base64Encoded {
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.indication;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface Indicate {
|
||||
// if later we want multiple indicated fields, use an int here
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.indication;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface Indicated {
|
||||
// if later we want multiple indicated fields, use an int here
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.indication;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class Indication {
|
||||
public static boolean isIndicated(Field f) {
|
||||
return (f.getAnnotation(Indicated.class) != null);
|
||||
}
|
||||
|
||||
public static boolean asArray(Field f) {
|
||||
return (f.getAnnotation(AsArray.class) != null);
|
||||
}
|
||||
|
||||
public static boolean isBase64Encoded(Field f) {
|
||||
return ((f.getAnnotation(Base64Encoded.class) != null) &&
|
||||
f.getType().isArray() &&
|
||||
f.getType().getComponentType().equals(byte.class));
|
||||
}
|
||||
|
||||
public static String getIndicatorName(Class c) {
|
||||
return getName(c, Indicator.class);
|
||||
}
|
||||
|
||||
public static String getIndicatedName(Class c) {
|
||||
return getName(c, Indicated.class);
|
||||
}
|
||||
|
||||
private static String getName(Class c, Class annotation) {
|
||||
String result = null;
|
||||
for (Field f : c.getFields()) {
|
||||
if (f.getAnnotation(annotation) != null) {
|
||||
result = f.getName();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
public static Class getIndication(Object o, String s) {
|
||||
Class c = o.getClass();
|
||||
Class result = null;
|
||||
try {
|
||||
Method m = getIndicateMethod(c);
|
||||
result = (Class) (m.invoke(o, s));
|
||||
}
|
||||
catch (ReflectiveOperationException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Method getIndicateMethod(Class c) {
|
||||
Method result = null;
|
||||
for (Method m : c.getMethods()) {
|
||||
if (m.getAnnotation(Indicate.class) != null) {
|
||||
result = m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
*/
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.indication;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface Indicator {
|
||||
// if later we want multiple indicated fields, use an int here
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.operation;
|
||||
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "advertise")
|
||||
public class Advertise extends Operation {
|
||||
public String topic;
|
||||
public String type;
|
||||
|
||||
public Advertise() {}
|
||||
|
||||
public Advertise(String topic, String type) {
|
||||
this.topic = topic;
|
||||
this.type = type;
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.operation;
|
||||
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "auth")
|
||||
public class Authenticate extends Operation {
|
||||
public String mac;
|
||||
public String client;
|
||||
public String dest;
|
||||
public String rand;
|
||||
public int t;
|
||||
public String level;
|
||||
public int end;
|
||||
|
||||
public Authenticate() {}
|
||||
|
||||
public Authenticate(
|
||||
String mac,
|
||||
String client,
|
||||
String dest,
|
||||
String rand,
|
||||
int t,
|
||||
String level,
|
||||
int end)
|
||||
{
|
||||
this.mac = mac;
|
||||
this.client = client;
|
||||
this.dest = dest;
|
||||
this.rand = rand;
|
||||
this.t = t;
|
||||
this.level = level;
|
||||
this.end = end;
|
||||
|
||||
this.id = null; // even though id is on EVERY OTHER operation type
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.operation;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
import com.example.testdemo.ros.rosbridge.indication.AsArray;
|
||||
import com.example.testdemo.ros.rosbridge.indication.Indicated;
|
||||
import com.example.testdemo.ros.rosbridge.indication.Indicator;
|
||||
|
||||
@MessageType(string = "call_service")
|
||||
public class CallService extends Operation {
|
||||
@Indicator public String service;
|
||||
@Indicated @AsArray public Message args;
|
||||
public Integer fragment_size; // use Integer for optional items
|
||||
public String compression;
|
||||
|
||||
public CallService() {}
|
||||
|
||||
public CallService(String service, Message args) {
|
||||
this.service = service;
|
||||
this.args = args;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.operation;
|
||||
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "fragment")
|
||||
public class Fragment extends Operation {
|
||||
public String data;
|
||||
public int num;
|
||||
public int total;
|
||||
|
||||
public Fragment() {}
|
||||
|
||||
public Fragment(String data, int num, int total) {
|
||||
this.data = data;
|
||||
this.num = num;
|
||||
this.total = total;
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.operation;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
import com.example.testdemo.ros.rosbridge.implementation.JSON;
|
||||
import com.example.testdemo.ros.rosbridge.implementation.Registry;
|
||||
|
||||
@MessageType(string = "operation")
|
||||
public class Operation extends Message {
|
||||
private static Long uid = 0L;
|
||||
|
||||
public String op;
|
||||
public String id;
|
||||
|
||||
public Operation() {
|
||||
this.op = getMessageType(getClass());
|
||||
this.id = nextId();
|
||||
}
|
||||
|
||||
private static synchronized String nextId() {
|
||||
String result = uid.toString();
|
||||
uid++;
|
||||
return result;
|
||||
}
|
||||
|
||||
public String toJSON() {
|
||||
return JSON.toJSON(this);
|
||||
}
|
||||
|
||||
public static Operation toOperation(String json, Registry<Class> registry) {
|
||||
return ((Wrapper) JSON.toMessage(json, Wrapper.class, registry)).msg;
|
||||
}
|
||||
|
||||
public static void initialize(Registry<Class> registry) {
|
||||
initClass(registry, Advertise.class);
|
||||
initClass(registry, Authenticate.class);
|
||||
initClass(registry, CallService.class);
|
||||
initClass(registry, Fragment.class);
|
||||
initClass(registry, Operation.class);
|
||||
initClass(registry, PNG.class);
|
||||
initClass(registry, Publish.class);
|
||||
initClass(registry, ServiceResponse.class);
|
||||
initClass(registry, SetStatusLevel.class);
|
||||
initClass(registry, Status.class);
|
||||
initClass(registry, Subscribe.class);
|
||||
initClass(registry, Unadvertise.class);
|
||||
initClass(registry, Unsubscribe.class);
|
||||
initClass(registry, Wrapper.class);
|
||||
|
||||
registry.register(Wrapper.class, Message.getMessageType(Publish.class), Publish.class);
|
||||
registry.register(Wrapper.class, Message.getMessageType(CallService.class), CallService.class);
|
||||
registry.register(Wrapper.class, Message.getMessageType(ServiceResponse.class), ServiceResponse.class);
|
||||
}
|
||||
|
||||
private static void initClass(Registry<Class> registry, Class<? extends Message> c) {
|
||||
registry.register(Message.class, Message.getMessageType(c), c);
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.operation;
|
||||
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "png")
|
||||
public class PNG extends Operation {
|
||||
public String data;
|
||||
public Integer num; // use Integer for optional items
|
||||
public Integer total; // use Integer for optional items
|
||||
|
||||
public PNG() {}
|
||||
|
||||
public PNG(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public PNG(String data, int num, int total) {
|
||||
this.data = data;
|
||||
this.num = num;
|
||||
this.total = total;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.operation;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
import com.example.testdemo.ros.rosbridge.indication.Indicated;
|
||||
import com.example.testdemo.ros.rosbridge.indication.Indicator;
|
||||
|
||||
@MessageType(string = "publish")
|
||||
public class Publish extends Operation {
|
||||
|
||||
@Indicator public String topic;
|
||||
@Indicated
|
||||
public Message msg;
|
||||
|
||||
public Publish() {}
|
||||
|
||||
public Publish(String topic, Message msg) {
|
||||
this.topic = topic;
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.operation;
|
||||
|
||||
import com.example.testdemo.ros.message.Message;
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
import com.example.testdemo.ros.rosbridge.indication.Indicated;
|
||||
import com.example.testdemo.ros.rosbridge.indication.Indicator;
|
||||
|
||||
@MessageType(string = "service_response")
|
||||
public class ServiceResponse extends Operation {
|
||||
@Indicator public String service;
|
||||
public boolean result;
|
||||
@Indicated public Message values;
|
||||
|
||||
public ServiceResponse() {}
|
||||
|
||||
public ServiceResponse(String service) {
|
||||
this.service = service;
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.operation;
|
||||
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "set_level")
|
||||
public class SetStatusLevel extends Operation {
|
||||
public String level;
|
||||
|
||||
public SetStatusLevel() {}
|
||||
|
||||
public SetStatusLevel(String level) {
|
||||
this.level = null;
|
||||
if ("none".equals(level) ||
|
||||
"warning".equals(level) ||
|
||||
"error".equals(level) ||
|
||||
"info".equals(level))
|
||||
this.level = level;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.operation;
|
||||
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "status")
|
||||
public class Status extends Operation {
|
||||
String level;
|
||||
String msg;
|
||||
|
||||
public Status() {}
|
||||
|
||||
public Status(String level, String msg) {
|
||||
this.level = level;
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.operation;
|
||||
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "subscribe")
|
||||
public class Subscribe extends Operation {
|
||||
public String topic;
|
||||
public String type;
|
||||
public Integer throttle_rate; // use Integer for optional items
|
||||
public Integer queue_length; // use Integer for optional items
|
||||
public Integer fragment_size; // use Integer for optional items
|
||||
public String compression;
|
||||
|
||||
public Subscribe() {}
|
||||
|
||||
public Subscribe(String topic, String type) {
|
||||
this.topic = topic;
|
||||
this.type = type;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.operation;
|
||||
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "unadvertise")
|
||||
public class Unadvertise extends Operation {
|
||||
public String topic;
|
||||
|
||||
public Unadvertise() {}
|
||||
|
||||
public Unadvertise(String topic) {
|
||||
this.topic = topic;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.operation;
|
||||
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
|
||||
@MessageType(string = "unsubscribe")
|
||||
public class Unsubscribe extends Operation {
|
||||
public String topic;
|
||||
|
||||
public Unsubscribe() {}
|
||||
|
||||
public Unsubscribe(String topic) {
|
||||
this.topic = topic;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Copyright (c) 2014 example.testdemo Systems, Inc.
|
||||
*
|
||||
* This file is part of the Java ROSBridge Client.
|
||||
*
|
||||
* The Java ROSBridge Client is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The Java ROSBridge Client is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
package com.example.testdemo.ros.rosbridge.operation;
|
||||
|
||||
import com.example.testdemo.ros.message.MessageType;
|
||||
import com.example.testdemo.ros.rosbridge.indication.Indicated;
|
||||
import com.example.testdemo.ros.rosbridge.indication.Indicator;
|
||||
|
||||
@MessageType(string = "wrapper")
|
||||
public class Wrapper extends Operation {
|
||||
@Indicator public String op;
|
||||
@Indicated
|
||||
public Operation msg;
|
||||
|
||||
public Wrapper() {}
|
||||
}
|
@ -0,0 +1 @@
|
||||
|
@ -0,0 +1,18 @@
|
||||
server:
|
||||
port: 8080
|
||||
servlet:
|
||||
contest-path: /
|
||||
spring:
|
||||
application:
|
||||
name: springboot websocket
|
||||
mvc:
|
||||
static-path-pattern: /**
|
||||
thymeleaf:
|
||||
mode: HTML5
|
||||
suffix: .html
|
||||
encoding: UTF-8
|
||||
prefix:
|
||||
classpath: /template
|
||||
cache: false
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,59 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
|
||||
<script type="text/javascript" src="./em.js"></script>
|
||||
<script type="text/javascript" src="./roslib.min.js"></script>
|
||||
|
||||
<script type="text/javascript" type="text/javascript">
|
||||
// Connecting to ROS
|
||||
// -----------------
|
||||
|
||||
var ros = new ROSLIB.Ros({
|
||||
url : 'ws://192.168.43.20:9090'
|
||||
});
|
||||
|
||||
ros.on('connection', function() {
|
||||
console.log('Connected to websocket server.');
|
||||
});
|
||||
|
||||
ros.on('error', function(error) {
|
||||
console.log('Error connecting to websocket server: ', error);
|
||||
});
|
||||
|
||||
ros.on('close', function() {
|
||||
console.log('Connection to websocket server closed.');
|
||||
});
|
||||
|
||||
// Publishing a Topic
|
||||
// ------------------
|
||||
|
||||
var cmdVel = new ROSLIB.Topic({
|
||||
ros : ros,
|
||||
name : '/cmd_vel',
|
||||
messageType : 'geometry_msgs/Twist'
|
||||
});
|
||||
|
||||
var twist = new ROSLIB.Message({
|
||||
linear : {
|
||||
x : 0.1,
|
||||
y : 0.2,
|
||||
z : 0.3
|
||||
},
|
||||
angular : {
|
||||
x : -0.1,
|
||||
y : -0.2,
|
||||
z : -0.3
|
||||
}
|
||||
});
|
||||
cmdVel.publish(twist);
|
||||
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Simple roslib Example</h1>
|
||||
<p>Check your Web Console for output.</p>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,97 @@
|
||||
|
||||
// document.write('<script type="text/javascript" src="http://cdn.robotwebtools.org/EventEmitter2/current/eventemitter2.min.js"></script>\n');
|
||||
// document.write('<script type="text/javascript" src="http://cdn.robotwebtools.org/roslibjs/current/roslib.min.js"></script>\n');
|
||||
|
||||
var ros = new ROSLIB.Ros({
|
||||
url : 'ws://192.168.43.20:9090'
|
||||
});
|
||||
|
||||
ros.on('connection', function() {
|
||||
console.log('Connected to websocket server.');
|
||||
});
|
||||
|
||||
ros.on('error', function(error) {
|
||||
console.log('Error connecting to websocket server: ', error);
|
||||
});
|
||||
|
||||
ros.on('close', function() {
|
||||
console.log('Connection to websocket server closed.');
|
||||
});
|
||||
var move_base= new ROSLIB.ActionClient({
|
||||
ros : ros,
|
||||
serverName : '/move_base',
|
||||
actionName : 'move_base_msgs/MoveBaseAction'
|
||||
});
|
||||
|
||||
var currentTime = new Date();
|
||||
var secs = Math.floor(currentTime.getTime()/1000)
|
||||
var nsecs = Math.round(1000000000*(currentTime.getTime()/1000-secs))
|
||||
|
||||
var goal = new ROSLIB.Goal({
|
||||
actionClient: move_base,
|
||||
goalMessage:{
|
||||
target_pose:{
|
||||
header:{
|
||||
frame_id:'map',
|
||||
stamp:{
|
||||
secs: secs,
|
||||
nsecs:nsecs
|
||||
}
|
||||
},
|
||||
pose:{
|
||||
position:{
|
||||
x:-3.97, //-0.743 -0.0273 取件点
|
||||
y:1.68, //3.00 -0.09
|
||||
z:0.000
|
||||
},
|
||||
orientation:{
|
||||
x:0.000,
|
||||
y:0.000,
|
||||
z:0.000,
|
||||
w:1.000
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
goal.on('feedback', function(feedback) {
|
||||
console.log('Feedback: ' + feedback.sequence);
|
||||
});
|
||||
|
||||
goal.on('result', function(result) {
|
||||
console.log('Final Result: ' + result.sequence);
|
||||
});
|
||||
|
||||
ros.on('connection', function() {
|
||||
console.log('Connected to websocket server.');
|
||||
});
|
||||
|
||||
ros.on('error', function(error) {
|
||||
console.log('Error connecting to websocket server: ', error);
|
||||
});
|
||||
|
||||
ros.on('close', function() {
|
||||
console.log('Connection to websocket server closed.');
|
||||
});
|
||||
|
||||
//goal.send();
|
||||
var cmdVel = new ROSLIB.Topic({
|
||||
ros : ros,
|
||||
name : '/cmd_vel',
|
||||
messageType : 'geometry_msgs/Twist'
|
||||
});
|
||||
|
||||
var twist = new ROSLIB.Message({
|
||||
linear : {
|
||||
x : 0.1,
|
||||
y : 0.2,
|
||||
z : 0.3
|
||||
},
|
||||
angular : {
|
||||
x : -0.1,
|
||||
y : -0.2,
|
||||
z : -0.3
|
||||
}
|
||||
});
|
||||
cmdVel.publish(twist);
|
||||
|
@ -0,0 +1,13 @@
|
||||
package com.example.testdemo;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class TestdemoApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue