-
Notifications
You must be signed in to change notification settings - Fork 0
P2pv2 #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zeyadAbdulghani
wants to merge
3
commits into
master
Choose a base branch
from
p2pv2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
P2pv2 #57
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
JailbreakPong/app/src/main/java/com/kaze/jailbreakpong/Client.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.kaze.jailbreakpong; | ||
|
|
||
| import android.os.Handler; | ||
| import android.os.Message; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.InetAddress; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.Socket; | ||
|
|
||
|
|
||
| public class Client extends Thread { | ||
| Socket socket; | ||
| String hostAdd; | ||
| SendReceive sendReceive; | ||
| byte[] message; | ||
| static final int MESSAGE_READ = 1; | ||
| Handler handler = new Handler(new Handler.Callback() { | ||
| @Override | ||
| public boolean handleMessage(@NonNull Message msg) { | ||
| switch (msg.what){ | ||
| case MESSAGE_READ: | ||
| byte[] readBuff = (byte[]) msg.obj; | ||
| String tempMsg = new String(readBuff,0,msg.arg1); | ||
| int go = 0; | ||
| break; | ||
| } | ||
| return true; | ||
| } | ||
| }); | ||
| public Client(InetAddress hostAddress){ | ||
| hostAdd = hostAddress.getHostAddress(); | ||
| socket = new Socket(); | ||
| } | ||
|
|
||
| public void setMessage(byte[] message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| @Override | ||
| public void run(){ | ||
| try{ | ||
| socket.connect(new InetSocketAddress(hostAdd,8888),500); | ||
| sendReceive = new SendReceive(socket,handler); | ||
| sendReceive.start(); | ||
| while (sendReceive != null){ | ||
| if(message != null){ | ||
| sendReceive.write(message); | ||
| message = null; | ||
| } | ||
| } | ||
|
|
||
| } | ||
| catch (IOException e){ | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
225 changes: 225 additions & 0 deletions
225
JailbreakPong/app/src/main/java/com/kaze/jailbreakpong/NetCore.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| package com.kaze.jailbreakpong; | ||
|
|
||
| import android.app.Activity; | ||
| import android.content.BroadcastReceiver; | ||
| import android.content.Context; | ||
| import android.content.IntentFilter; | ||
| import android.net.wifi.WpsInfo; | ||
| import android.net.wifi.p2p.WifiP2pConfig; | ||
| import android.net.wifi.p2p.WifiP2pDevice; | ||
| import android.net.wifi.p2p.WifiP2pDeviceList; | ||
| import android.net.wifi.p2p.WifiP2pInfo; | ||
| import android.net.wifi.p2p.WifiP2pManager; | ||
| import android.os.Handler; | ||
| import android.os.Message; | ||
| import android.util.Log; | ||
| import android.view.LayoutInflater; | ||
| import android.view.View; | ||
| import android.widget.RelativeLayout; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.InetAddress; | ||
| import java.net.NetworkInterface; | ||
| import java.net.ServerSocket; | ||
| import java.net.SocketException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Enumeration; | ||
| import java.util.HashMap; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
|
|
||
| public class NetCore { | ||
| Context context; | ||
| WifiP2pManager wifiP2pManager; | ||
| IntentFilter intentFilter; | ||
| WifiP2pManager.Channel channel; | ||
| BroadcastReceiver broadcastReceiver; | ||
| HashMap<String, WifiP2pDevice> groupOwnerTable; | ||
| WifiP2pManager.ConnectionInfoListener connectionInfoListener; | ||
| Handler asyncIsOkHandler; | ||
| boolean isInGroup; | ||
| WifiP2pManager.PeerListListener peersListListener; | ||
| private ArrayList<WifiP2pDevice> devicesList; | ||
| private ServerSocket serverSocket; | ||
| Server server; | ||
| Client client; | ||
|
|
||
| public ArrayList<WifiP2pDevice> getDevices(){ | ||
| return devicesList; | ||
| } | ||
| public void setDevicesList(ArrayList devicesListValue){devicesList = devicesListValue;} | ||
|
|
||
| public NetCore(Context context, Handler asyncIsOkHandler) { | ||
| this.context = context; | ||
| this.wifiP2pManager = (WifiP2pManager) this.context.getApplicationContext().getSystemService(Context.WIFI_P2P_SERVICE); | ||
| this.intentFilter = new IntentFilter(); | ||
| this.devicesList = new ArrayList<>(); | ||
| this.intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); | ||
| this.intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); | ||
| this.intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); | ||
| this.intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); | ||
|
|
||
| this.channel = this.wifiP2pManager.initialize(this.context.getApplicationContext(), this.context.getMainLooper(), null); | ||
| this.peersListListener = new WifiP2pManager.PeerListListener() { | ||
| @Override | ||
| public void onPeersAvailable(WifiP2pDeviceList peerList) { | ||
| //groupList.removeAllViews(); | ||
| ArrayList<WifiP2pDevice> devices = getDevices(); | ||
| if(!new ArrayList<>(peerList.getDeviceList()).equals(devices)){ | ||
| groupOwnerTable.clear(); | ||
| ArrayList<WifiP2pDevice> newDevices = new ArrayList<>(peerList.getDeviceList()); | ||
| setDevicesList(newDevices); | ||
| for(int i = 0; i < newDevices.size(); i++) { | ||
| WifiP2pDevice device = newDevices.get(i); | ||
| connectPeer(device); | ||
| if (device.isGroupOwner()) { | ||
| groupOwnerTable.put(device.deviceAddress, device); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| this.connectionInfoListener = new WifiP2pManager.ConnectionInfoListener() { | ||
| @Override | ||
| public void onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo) { | ||
| final InetAddress groupOwnerAddress = wifiP2pInfo.groupOwnerAddress; | ||
| if(wifiP2pInfo.groupFormed && wifiP2pInfo.isGroupOwner){ | ||
| server = new Server(); | ||
| server.start(); | ||
|
|
||
| } | ||
| else if(wifiP2pInfo.groupFormed){ | ||
| client = new Client(groupOwnerAddress); | ||
| client.start(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| this.broadcastReceiver = new WifiDirectBroadcastReceiver(this.wifiP2pManager, this.channel,this.peersListListener,this.connectionInfoListener); | ||
| this.context.registerReceiver(this.broadcastReceiver, this.intentFilter); | ||
| this.groupOwnerTable = new HashMap<String, WifiP2pDevice>(); | ||
| this.asyncIsOkHandler = asyncIsOkHandler; | ||
| this.isInGroup = false; | ||
| } | ||
|
|
||
| public void discoverPeers(final Activity updateActivity) { | ||
| this.wifiP2pManager.discoverPeers(channel, new WifiP2pManager.ActionListener() { | ||
| @Override | ||
| public void onSuccess() { | ||
|
|
||
| int test = 0; | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(int arg0) { | ||
| int test = 0; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public boolean writeServer(byte[] data){ | ||
| if(server.sendReceive.isAlive()){ | ||
| server.setMessage(data); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public boolean writeClient(byte[] data){ | ||
| try{ | ||
| if(client.sendReceive.isAlive()){ | ||
| client.setMessage(data); | ||
| return true; | ||
| } | ||
| } | ||
| catch(Exception e){ | ||
| int go = 0; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public void connectPeer(WifiP2pDevice device) { | ||
| WifiP2pConfig config = new WifiP2pConfig(); | ||
| config.deviceAddress = device.deviceAddress; | ||
| config.wps.setup = WpsInfo.PBC; | ||
|
|
||
| wifiP2pManager.connect(channel, config, new WifiP2pManager.ActionListener() { | ||
| @Override | ||
| public void onSuccess() { | ||
| isInGroup = true; | ||
| Message message = new Message(); | ||
| message.what = 31; | ||
| asyncIsOkHandler.sendMessage(message); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(int arg0) { | ||
| Message message = new Message(); | ||
| message.what = 32; | ||
| asyncIsOkHandler.sendMessage(message); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public void createGroup() { | ||
| this.wifiP2pManager.createGroup(this.channel, new WifiP2pManager.ActionListener() { | ||
| @Override | ||
| public void onSuccess() { | ||
| isInGroup = true; | ||
| Message message = new Message(); | ||
| message.what = 11; | ||
| asyncIsOkHandler.sendMessage(message); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(int reason) { | ||
| Message message = new Message(); | ||
| message.what = 12; | ||
| asyncIsOkHandler.sendMessage(message); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public void dissolveGroup(final Handler notifySuccessOrFailed) { | ||
| this.wifiP2pManager.removeGroup(this.channel, new WifiP2pManager.ActionListener() { | ||
| @Override | ||
| public void onSuccess() { | ||
| isInGroup = false; | ||
| try { | ||
| serverSocket.close(); | ||
| } catch (IOException e) { | ||
| } | ||
| Message message = new Message(); | ||
| message.what = 21; | ||
| notifySuccessOrFailed.sendMessage(message); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(int reason) { | ||
| Message message = new Message(); | ||
| message.what = 22; | ||
| notifySuccessOrFailed.sendMessage(message); | ||
| } | ||
| }); | ||
| } | ||
| public void debug(String content) { | ||
| } | ||
|
|
||
| public String getLocalIpAddress() { | ||
| try { | ||
| for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { | ||
| NetworkInterface intf = en.nextElement(); | ||
| for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { | ||
| InetAddress inetAddress = enumIpAddr.nextElement(); | ||
| if (!inetAddress.isLoopbackAddress()) { | ||
| return inetAddress.getHostAddress().toString(); | ||
| } | ||
| } | ||
| } | ||
| } catch (SocketException ex) { | ||
| // Log.e("WifiPreference IpAddress", ex.toString()); | ||
| } | ||
| return null; | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
JailbreakPong/app/src/main/java/com/kaze/jailbreakpong/PeerConnection.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.kaze.jailbreakpong; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.Socket; | ||
|
|
||
| class PeerConnection { | ||
| private Socket peerSocket; | ||
| private Thread worker; | ||
|
|
||
| public PeerConnection(Socket peer) { | ||
| this.peerSocket = peer; | ||
| this.worker = new Thread(new Runnable() { | ||
| @Override | ||
| public void run() { | ||
| try { | ||
| byte[] buffer = new byte[1024]; | ||
| InputStream peerInputs = peerSocket.getInputStream(); | ||
| while (true) { | ||
| int readlen = peerInputs.read(buffer); | ||
| if (readlen > 0) { | ||
|
|
||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public void startProcessing() { | ||
| this.worker.start(); | ||
| } | ||
|
|
||
| public void stopProcessing() { | ||
| this.worker.stop(); | ||
| } | ||
|
|
||
| public String getPeerIp() { | ||
| return peerSocket.getInetAddress().getHostAddress(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like permissions are added twice